-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
94 lines (83 loc) · 2.82 KB
/
__main__.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
"""Main"""
import argparse
import basepart
import ko
def main():
"""Main procedure of application"""
# Parse arguments
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument( \
'language', \
choices=('ko', ), \
help='set the valid language' \
)
arg_parser.add_argument( \
'repeat', \
type=int, \
nargs='?', \
default=1, \
help='set the number of repetition' \
)
arg_parser.add_argument( \
'length', \
type=int, \
nargs='?', \
help='set the length of name' \
)
arg_parser.add_argument( \
'-f', '--fresh', \
action='store_true', \
help='make the application return fresh result' \
)
arg_parser.add_argument( \
'-p', '--print', \
action='store_true', \
help='print the result' \
)
arg_parser.add_argument( \
'-s', '--storage', \
default=basepart.DEFAULT_STORAGE, \
help='set the path of result file' \
)
arg = arg_parser.parse_args()
# Language-specified modification
if arg.language == 'ko':
if arg.length is None:
arg.length = 2
num_total_characters = ko.ComposerElementKorean.num_character_recommend ** 2
# General modification
if arg.length <= 0:
raise basepart.NamingLibException('Wrong name length', arg.length)
if arg.repeat <= 0:
raise basepart.NamingLibException('Wrong repeat number', arg.repeat)
# Open file
file_stream = open(arg.storage, 'a+')
file_stream.seek(0)
list_name_original = list(set(file_stream.readlines()))
list_name_added = list()
# Generate and store
idx_generated = 0
while idx_generated < arg.repeat:
result = list()
if arg.language == 'ko':
for _ in range(arg.length):
result.append(ko.ComposerElementKorean( \
initial=basepart.IncludeList(ko.ComposerElementKorean.character_recommend[0]), \
medial=basepart.IncludeList(ko.ComposerElementKorean.character_recommend[1]), \
final=basepart.IncludeList(ko.ComposerElementKorean.character_recommend[2]) \
).compose() \
)
result = ''.join(result)
if arg.fresh and (result + '\n') in list_name_original + list_name_added:
if len(list_name_original + list_name_added) >= num_total_characters:
raise basepart.NamingLibException('generated all of the eligible names')
else:
list_name_added.append(result + '\n')
if arg.print:
print(result)
idx_generated += 1
# Write to file and close file
file_stream.writelines(list_name_added)
file_stream.close()
if __name__ == '__main__':
main()