-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwilliam.py
134 lines (75 loc) · 3.99 KB
/
william.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
from argparse import ArgumentParser
from logging import basicConfig, DEBUG, debug, disable, CRITICAL
from itertools import combinations as itercombinations
from sys import exit as sysexit
# Doing the basic configuration for the debugging feature
basicConfig(level=DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
# Disabling the debugging feature. Comment out the line to enable debugging.
disable(CRITICAL)
class William:
def __init__(self, min, max, modification, wordlist, output):
self.version = "William 1.0.1"
self.min = int(min)
self.max = int(max)
self.modification = modification
self.wordlist = wordlist
self.output = output
def generate_wordlist(self):
"""A function which generates a word list from an another wordlist"""
print("Generating a Word List")
print("-" * 30)
# Read words from the source file
with open(self.wordlist, "r") as source_file:
words = source_file.read().splitlines()
# Generate combinations of words
combinations = []
for r in range(self.min, self.max + 1):
for combo in itercombinations(words, r):
if self.modification == "Capitalize":
combo = [word.capitalize() for word in combo]
elif self.modification == "First-Word":
combo = [combo[0].capitalize()] + list(combo[1:])
combinations.append("".join(combo))
# Write the generated wordlist to the output file
with open(self.output, "w") as output_file:
output_file.write("\n".join(combinations))
debug(combinations)
print(f'Generated {len(combinations)} combinations')
print(f'Output saved to {self.output}')
def display_version(self):
"""A function which displays the application version"""
print(self.version)
def main():
"""The main function which runs the entire application"""
# Create an argument parser
parser = ArgumentParser(description="A command line tool to create a word list from combination of words.")
# Add arguments
parser.add_argument('-v', '--version', action="store_true", help="Display the application's version information",)
parser.add_argument("-min", "--minimum", help="Minimum length of a word combination",)
parser.add_argument("-max", "--maximum", help="Maximum length of a word combination",)
parser.add_argument("-c", "--capitalize", action="store_true", help="Capitalize each sub word's first letter")
parser.add_argument("-f", "--firstword", action="store_true", help="Capitalize each word's first letter")
parser.add_argument("source_path", nargs="?", help="Source path of your wordlist")
parser.add_argument("destination_path", nargs="?", help="Destination path of your wordlist")
# Parse the arguments
args = parser.parse_args()
if args.version:
app = William(0, 0, None, None, None) # Pass None since wordlist is not used here
app.display_version()
elif args.minimum and args.maximum and args.source_path and args.destination_path:
if args.capitalize and not args.firstword:
modification = "Capitalize"
elif args.firstword and not args.capitalize:
modification = "First-Word"
elif args.capitalize and args.firstword:
print("You can't use capitalize and firstword arguments at the same time.")
sysexit()
else:
modification = None
app = William(min=args.minimum, max=args.maximum, modification=modification, wordlist=args.source_path, output=args.destination_path)
app.generate_wordlist()
else:
print("Invalid usage. Use -h or --help to get more information.")
# Evaluate if the source is being run on its own or being imported somewhere else. With this conditional in place, your code can not be imported somewhere else.
if __name__ == '__main__':
main()