This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
usernamer.py
executable file
·420 lines (325 loc) · 10.9 KB
/
usernamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env python
"""
$Id: $
Copyright (c) 2012-2013 Jan Seidl <[email protected]> (http://wroot.org/)
LICENSE:
This software is distributed under the GNU General Public License version 3 (GPLv3)
LEGAL NOTICE:
THIS SOFTWARE IS PROVIDED FOR EDUCATIONAL USE ONLY!
IF YOU ENGAGE IN ANY ILLEGAL ACTIVITY
THE AUTHOR DOES NOT TAKE ANY RESPONSIBILITY FOR IT.
BY USING THIS SOFTWARE YOU AGREE WITH THESE TERMS.
"""
import getopt, sys
import string
####
# Program info
####
USERNAMER_VERSION="1.0-rc3"
BUILD_DATE="2018-11-11"
AVAILABLE_PLUGINS=[ 'normal', 'two_terms', 'one_term', 'normal_abbreviated', 'dotted_two_terms', 'starts_with', 'under_score']
AVAILABLE_FILTERS=[ 'sort', 'unique' ]
####
# Program Functions
####
def parse_file(filePath, plugins = [], filters = []):
try:
with open(filePath, 'r') as fileObject:
for line in fileObject:
parse_name(line, plugins, filters)
except IOError:
e = "Could not open the file: " + filePath
error(e)
def parse_name(name, plugins = [], filters = []):
name = name.strip() # Trim whitespaces
nameTokens = name.split(' ') # Tokenize name and each surname
numTokens = len(nameTokens)
if numTokens < 2:
error('Name and at least one Surname must be supplied')
# Split First Name and Surnames
firstName = nameTokens[0]
nameTokens.pop(0)
surnames = nameTokens
results = []
# Run Plugins
run_plugins(firstName, surnames, results, plugins)
# Run Filters
run_filters(results, filters)
for result in results:
print result
def run_plugins(firstName, surnames, resultList, plugins = []):
defaultPlugins = AVAILABLE_PLUGINS
if len(plugins) == 0:
plugins = defaultPlugins
for pluginName in plugins:
internalPluginName = "plugin_"+pluginName
# Validate if plugin exists
if not internalPluginName in globals():
error("Invalid plugin: "+pluginName)
pluginObject = globals()[internalPluginName]
pluginObject(firstName, surnames, resultList)
def run_filters(resultList, filters = []):
defaultFilters = AVAILABLE_FILTERS
if len(filters) == 0:
filters = defaultFilters
for filterName in filters:
internalFilterName = "filter_"+filterName
# Validate if filter exists
if not internalFilterName in globals():
error("Invalid plugin: "+filterName)
filterObject = globals()[internalFilterName]
filterObject(resultList)
####
# Result Filters
####
# Unique Filter
#
# Removes duplicated entries
def filter_unique(resultList):
uniqueResults = set(resultList)
del resultList[:]
for result in uniqueResults:
resultList.append(result)
# Sort Filter
#
# Filter entries alphabetically
def filter_sort(resultList):
resultList.sort()
# Lowercase Filter
#
# Transforms entries to lowercase
def filter_lowercase(resultList):
for key, result in enumerate(resultList):
resultList[key] = result.lower()
####
# Parsing Plugins
####
# Normal Plugin
#
# Generates usernames based on concatenation
# of first name with surnames in permutation
#
# Ex: JohnPaulJones, JohnJonesPaul
def plugin_normal(firstName, surnames, resultList):
surnamePermutations = permutate_all(surnames)
for permutations in surnamePermutations:
resultList.append(firstName+string.join(permutations, ''))
resultList.append(string.join(permutations, '')+firstName)
# Two Terms Plugin
#
# Generates usernames based on concatenation
# of first name with surnames in permutation
#
# Ex: JohnPaul, JohnJones, PaulJones
def plugin_two_terms(firstName, surnames, resultList):
# Try each surname with
# first name and reversed
for surname in surnames:
resultList.append(firstName+surname)
resultList.append(surname+firstName)
# If more than one surname,
# combine'em too
if len(surnames) > 1:
tokens = list(surnames)
for surname in surnames:
firstToken = tokens.pop(0)
for token in tokens:
resultList.append(firstToken+token)
# One Term Plugin
#
# Generates usernames based on permutation
# of first name and surnames generating one-word
# usernames
#
# Ex: John, Paul, Jones
def plugin_one_term(firstName, surnames, resultList):
tokens = [ firstName ]
tokens += surnames
for name in tokens:
resultList.append(name)
# Dotted Two Terms Plugin
#
# Generates usernames based on concatenation
# of first name with surnames in permutation
# with a dot in the middle
#
# Ex: John.Paul, John.Jones, Paul.Jones
def plugin_dotted_two_terms(firstName, surnames, resultList):
# Try each surname with
# first name and reversed
for surname in surnames:
resultList.append(firstName+'.'+surname)
resultList.append(surname+'.'+firstName)
# Normal Abbreviated Plugin
#
# Generates usernames based on concatenation
# of first name with surnames in permutation
# in abbreviated forms
#
# Ex: JohnPJones, JohnPaulJ, JohnJonesP JohnJPaul
def plugin_normal_abbreviated(firstName, surnames, resultList):
permutatedSurnames = permutate_all(surnames)
firstNameArr = [ firstName ]
# All Terms
for entry in permutatedSurnames:
nameFirst = list(firstNameArr+entry)
nameLast = list(entry+firstNameArr)
for name in abbreviate(nameFirst):
resultList.append(name)
for name in abbreviate(nameLast):
resultList.append(name)
# Two Words
for surname in surnames:
for name in abbreviate([ firstName, surname ]):
resultList.append(name)
for name in abbreviate([ surname, firstName]):
resultList.append(name)
# Starts With Plugin
# contributed by Derek Callaway [decal (AT) sdf {D0T} org] https://decal.github.io
#
# X Letters First Name Starts With and/or
# Y Letters Last Name Starts With
#
# Generates usernames based on concatenation of
# first X letters in first name and/or
# first Y letters in last name
#
# Such naming schemes are often found on legacy
# systems like AS/400 mainframes, DEC VMS/VAX, etc.
#
# Ex. dc dca dcal dcall dec deca decal decall derc derca dercal dercall
def plugin_starts_with(firstName, surnames, resultList):
for x in range(0, 1 + len(firstName) / 2):
for surname in surnames:
resultList.append(firstName[0:x] + surname)
for y in range(0, 1 + len(surname) / 2):
resultList.append(firstName[0:x] + surname[0:y])
resultList.append(firstName + surname[0:y])
def plugin_under_score(firstName, surnames, resultList):
for x in range(0, 1 + len(firstName)):
for surname in surnames:
resultList.append(firstName[0:x] + "_" + surname)
resultList.append(surname + "_" + firstName[0:x])
for y in range(0, 1 + len(surname)):
resultList.append(firstName[0:x] + "_" + surname[0:y])
resultList.append(surname[0:y] + "_" + firstName[0:x])
resultList.append(firstName + "_" + surname[0:y])
resultList.append(surname[0:y] + "_" + firstName)
####
# Util functions
####
def permutate_all(tokens):
if len(tokens) <=1:
yield tokens
else:
for perm in permutate_all(tokens[1:]):
for i in range(len(perm)+1):
yield perm[:i] + tokens[0:1] + perm[i:]
def abbreviate(tokens):
resultList = []
tokenCount = len(tokens)
# One abbreviated word
for i in range(tokenCount):
output = ''
position = 0
for j in tokens:
if i == position:
output += j[0]
else:
output += j
position += 1;
resultList.append(output)
# Two abbreviated words
for i in range(tokenCount):
output = ''
position = 0
for j in tokens:
if i == position or i == position+1:
output += j[0]
else:
output += j
position += 1;
resultList.append(output)
# All-but-one abbreviated words
if tokenCount > 3:
for i in range(tokenCount):
output = ''
position = 0
for j in tokens:
if i == position:
output += j
else:
output += j[0]
position += 1;
resultList.append(output)
return resultList
####
# Main
####
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "hlp:f:n:", ["help", "lowercase", "plugins", "file=","name="])
inputFile = None
inputName = None
defaultPlugins = AVAILABLE_PLUGINS
defaultFilters = AVAILABLE_FILTERS
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-f", "--file"):
inputFile = a
elif o in ("-p", "--plugins"):
pluginList = str(a).split(',')
validPlugins = []
for plugin in pluginList:
try:
pluginIndex = AVAILABLE_PLUGINS.index(plugin) # check plugin existance
validPlugins.append(plugin)
except ValueError:
error('Invalid plugin: "'+plugin+'"')
defaultPlugins = validPlugins
elif o in ("-n", "--name"):
inputName = a
elif o in ("-l", "--lowercase"):
defaultFilters.append('lowercase')
else:
error("option '"+o+"' doesn't exists")
if inputFile == None and inputName == None:
error('Please specify an input file or name')
if inputFile != None and inputName != None:
error('Please specify only an input file or name, not both')
# If name was supplied,
# process single entry and exit
if inputName:
parse_name(inputName, plugins = defaultPlugins, filters = defaultFilters)
sys.exit(0)
# If file was supplied,
# process each line
if inputFile:
parse_file(inputFile, plugins = defaultPlugins, filters = defaultFilters)
sys.exit(0)
except getopt.GetoptError, err:
# print help information and exit:
sys.stderr.write(str(err))
usage()
sys.exit(2)
def usage():
print
print "usage: " + sys.argv[0] + " [ -f <file> ] [ -n <full name> ] [ -l ]";
print
print "flags:"
print "\t-n\tsupplies a single name"
print "\t-f\tsupplies name entries from text file"
print "\t-l\tconverts result to lowercase"
print "\t-p\tmanually specify plugins (comma-separated) [default: all]"
print "\t\t"+str(AVAILABLE_PLUGINS)
print ""
def error(errorMsg, fatal=True, showUsage=True):
sys.stderr.write(errorMsg+"\n")
if showUsage:
usage()
if fatal:
sys.exit(2)
if __name__ == "__main__":
main()