-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
145 lines (117 loc) · 4.59 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
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
from __future__ import print_function
import sys
import os
import codecs
import code
import code.config_parser as config_parser
import code.generator as generator
import code.parser as parser
import code.tester as tester
import code.codec as codec
def print_err(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
if __name__ == '__main__':
import sys
args = sys.argv
show_trigger = False
encoding = 'UTF-8'
operation = None
config_file = os.path.join('resources', 'default.ini')
i = 0
while i < len(args):
arg = args[i]
if arg == '-h' or arg == '--help':
print('Circle code generator')
print(' -c, --config path : Set config file')
print(' -e, --encoding name : Encoding for files.', encoding, 'by default')
print(' -g, --gen-inline msg : Generate image from message specified')
print(' -G, --gen-file path : Generate image from file specified')
print(' -h, --help : Help')
print(' -p, --parse path : Parse code from image')
print(' -P, --force-parse path : Parse code from distorted image')
print(' -t, --test : Run tests')
print(' -T, --test-draw : Draw test image')
print(' -s, --show : Show generated result on screen')
exit()
if arg == '-c' or arg == '--config':
if i + 1 < len(args):
i += 1
config_file = args[i]
else:
print_err('Not enough arguments: path to config is not specified. Using default.')
if arg == '-g' or arg == '--gen-inline':
if i + 1 < len(args):
operation = ('gen-inline', args[i+1])
i += 1
else:
print_err('Not enough arguments: message is not specified. Aborting.')
exit()
if arg == '-G' or arg == '--gen-file':
if i + 1 < len(args):
operation = ('gen-file', args[i+1])
i += 1
else:
print_err('Not enough arguments: file not specified. Aborting.')
exit()
if arg == '-e' or arg == '--encoding':
if i + 1 < len(args):
i += 1
encoding = args[i]
else:
print_err('Not enough arguments: encoding is not specified. Using default.')
if arg == '-p' or arg == '--parse':
if i + 1 < len(args):
operation = ('parse', args[i+1])
i += 1
else:
print_err('Not enough arguments: message is not specified. Aborting.')
exit()
if arg == '-P' or arg == '--force-parse':
if i + 1 < len(args):
operation = ('Fparse', args[i+1])
i += 1
else:
print_err('Not enough arguments: message is not specified. Aborting.')
exit()
if arg == '-t' or arg == '--test':
print('Running tests')
tester.test('tests', code)
print('Completed.')
exit()
if arg == '-T' or arg == '--test-draw':
config = config_parser.Config(config_file, encoding)
gen = generator.Generator(config)
gen.generate(None)
exit()
if arg == '-s' or arg == '--show':
show_trigger = True
i += 1
if operation == None:
print_err('No operation specified. Please, use flags -g, -G or -p')
exit()
config = config_parser.Config(config_file, encoding)
coder = codec.Codec(config)
if operation[0] == 'gen-inline':
msg = operation[1]
msg = coder.encode(msg)
gen = generator.Generator(config)
gen.generate(msg)
if show_trigger:
gen.show()
elif operation[0] == 'gen-file':
f = codecs.open(operation[1], 'r', encoding)
msg = f.read()
msg = coder.encode(msg)
gen = generator.Generator(config)
gen.generate(msg)
if show_trigger:
gen.show()
elif operation[0] == 'parse':
prs = parser.Parser(config)
msg = prs.parse(operation[1])
print(msg)
print(coder.decode(msg))
elif operation[0] == 'Fparse':
prs = parser.Parser(config)
msg = prs.force_parse(operation[1])
print(coder.decode(msg))