forked from ilius/pyglossary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyglossary.pyw
executable file
·380 lines (341 loc) · 8.03 KB
/
pyglossary.pyw
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ui_main.py
#
# Copyright © 2008-2010 Saeed Rasooli <saeed.gnu@gmail.com> (ilius)
# This file is part of PyGlossary project, https://github.com/ilius/pyglossary
#
# This program is a free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. Or on Debian systems, from /usr/share/common-licenses/GPL
# If not, see <http://www.gnu.org/licenses/gpl.txt>.
import os
import sys
import argparse
import builtins
from os.path import dirname, join, realpath
from pprint import pformat
import logging
from pyglossary import core # essential
from pyglossary import VERSION
from pyglossary.text_utils import startRed, endFormat
# the first thing to do is to set up logger.
# other modules also using logger 'root', so it's essential to set it up prior
# to importing anything else; with exception to pyglossary.core which sets up
# logger class, and so should be done before actually initializing logger.
# verbosity level may be given on command line, so we have to parse arguments
# before setting up logger.
# once more:
# - import system modules like os, sys, argparse etc and pyglossary.core
# - parse args
# - set up logger
# - import submodules
# - other code
# no-progress-bar only for command line UI
# FIXME: load ui-dependent available options from ui modules
# (for example ui_cmd.available_options)
# the only problem is that it has to "import gtk" before it get the
# "ui_gtk.available_options"
# FIXME
# -v (verbose or version?)
# -r (reverse or read-options)
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument(
'-v',
'--verbosity',
action='store',
dest='verbosity',
type=int,
choices=(0, 1, 2, 3, 4),
required=False,
default=3, # FIXME
)
parser.add_argument(
'--version',
action='version',
version='PyGlossary %s' % VERSION,
)
parser.add_argument(
'-h',
'--help',
dest='help',
action='store_true',
)
parser.add_argument(
'-u',
'--ui',
dest='ui_type',
default='auto',
choices=(
'cmd',
'gtk',
'tk',
# 'qt',
'auto',
'none',
),
)
parser.add_argument(
'-r',
'--read-options',
dest='readOptions',
default='',
)
parser.add_argument(
'-w',
'--write-options',
dest='writeOptions',
default='',
)
parser.add_argument(
'--read-format',
dest='inputFormat',
)
parser.add_argument(
'--write-format',
dest='outputFormat',
action='store',
)
parser.add_argument(
'--direct',
dest='direct',
action='store_true',
default=None,
help='if possible, convert directly without loading into memory',
)
parser.add_argument(
'--indirect',
dest='direct',
action='store_false',
default=None,
help='disable `direct` mode, load full data into memory before writing'
', this is default',
)
parser.add_argument(
'--reverse',
dest='reverse',
action='store_true',
)
parser.add_argument(
'--no-progress-bar',
dest='progressbar',
action='store_false',
default=None,
)
parser.add_argument(
'--sort',
dest='sort',
action='store_true',
default=None,
)
parser.add_argument(
'--no-sort',
dest='sort',
action='store_false',
default=None,
)
parser.add_argument(
'--sort-cache-size',
dest='sortCacheSize',
type=int,
default=None,
)
parser.add_argument(
'--utf8-check',
dest='utf8Check',
action='store_true',
default=None,
)
parser.add_argument(
'--no-utf8-check',
dest='utf8Check',
action='store_false',
default=None,
)
parser.add_argument(
'--lower',
dest='lower',
action='store_true',
default=None,
help='lowercase words before writing',
)
parser.add_argument(
'--no-lower',
dest='lower',
action='store_false',
default=None,
help='don\'t lowercase words before writing',
)
parser.add_argument(
'--skip-resources',
dest='skipResources',
action='store_true',
default=None,
help='skip resources (images, audio, etc)',
)
parser.add_argument(
'--no-color',
dest='noColor',
action='store_true',
)
parser.add_argument(
'inputFilename',
action='store',
default='',
nargs='?',
)
parser.add_argument(
'outputFilename',
action='store',
default='',
nargs='?',
)
args = parser.parse_args()
log = logging.getLogger('root')
log.setVerbosity(args.verbosity)
log.addHandler(
core.StdLogHandler(noColor=args.noColor),
)
# with the logger setted up, we can import other pyglossary modules, so they
# can do some loggging in right way.
core.checkCreateConfDir()
##############################
from pyglossary.glossary import Glossary
from ui.ui_cmd import COMMAND, help, parseFormatOptionsStr
##############################
def dashToCamelCase(text): # converts "hello-PYTHON-user" to "helloPythonUser"
parts = text.split('-')
parts[0] = parts[0].lower()
for i in range(1, len(parts)):
parts[i] = parts[i].capitalize()
return ''.join(parts)
ui_list = (
'gtk',
'tk',
'qt',
)
# log.info('PyGlossary %s'%VERSION)
if args.help:
help()
sys.exit(0)
if os.sep != '/':
args.noColor = True
# only used in ui_cmd for now
readOptions = parseFormatOptionsStr(args.readOptions)
writeOptions = parseFormatOptionsStr(args.writeOptions)
"""
examples for read and write options:
--read-options testOption=stringValue
--read-options enableFoo=True
--read-options fooList=[1,2,3]
--read-options 'fooList=[1, 2, 3]'
--read-options 'testOption=stringValue; enableFoo=True; fooList=[1, 2, 3]'
--read-options 'testOption=stringValue;enableFoo=True;fooList=[1,2,3]'
"""
# FIXME
prefOptionsKeys = (
# 'verbosity',
'utf8Check',
'lower',
'skipResources',
)
convertOptionsKeys = (
'direct',
'progressbar',
'sort',
'sortCacheSize',
# 'sortKey',# or sortAlg FIXME
)
prefOptions = {}
for param in prefOptionsKeys:
value = getattr(args, param, None)
if value is not None:
prefOptions[param] = value
convertOptions = {}
for param in convertOptionsKeys:
value = getattr(args, param, None)
if value is not None:
convertOptions[param] = value
log.pretty(prefOptions, 'prefOptions = ')
log.pretty(readOptions, 'readOptions = ')
log.pretty(writeOptions, 'writeOptions = ')
log.pretty(convertOptions, 'convertOptions = ')
"""
ui_type: User interface type
Possible values:
cmd - Command line interface, this ui will automatically selected
if you give both input and output file
gtk - GTK interface
tk - Tkinter interface
qt - Qt interface
auto - Use the first available UI
"""
ui_type = args.ui_type
if args.inputFilename:
if args.outputFilename and ui_type != 'none':
ui_type = 'cmd' # silently? FIXME
else:
if ui_type == 'cmd':
log.error('no input file given, try --help')
exit(1)
if ui_type == 'none':
if args.reverse:
log.error('--reverse does not work with --ui=none')
sys.exit(1)
glos = Glossary()
glos.convert(
args.inputFilename,
inputFormat=args.inputFormat,
outputFilename=args.outputFilename,
outputFormat=args.outputFormat,
readOptions=readOptions,
writeOptions=writeOptions,
**convertOptions
)
sys.exit(0)
elif ui_type == 'cmd':
from ui import ui_cmd
sys.exit(0 if ui_cmd.UI().run(
args.inputFilename,
outputFilename=args.outputFilename,
inputFormat=args.inputFormat,
outputFormat=args.outputFormat,
reverse=args.reverse,
prefOptions=prefOptions,
readOptions=readOptions,
writeOptions=writeOptions,
convertOptions=convertOptions,
) else 1)
if ui_type == 'auto':
ui_module = None
for ui_type2 in ui_list:
try:
ui_module = getattr(
__import__('ui.ui_%s' % ui_type2),
'ui_%s' % ui_type2,
)
except ImportError:
log.exception('error while importing UI module:') # FIXME
else:
break
if ui_module is None:
log.error('no user interface module found!')
sys.exit(1)
else:
ui_module = getattr(
__import__('ui.ui_%s' % ui_type),
'ui_%s' % ui_type,
)
sys.exit(0 if ui_module.UI(**prefOptions).run(
editPath=args.inputFilename,
readOptions=readOptions,
) else 1)