This repository has been archived by the owner on Mar 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfb2mobi.py
executable file
·698 lines (581 loc) · 33.9 KB
/
fb2mobi.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import logging
import tempfile
import subprocess
import argparse
import zipfile
import time
import shutil
import uuid
from slugify import slugify
import version
from modules.utils import get_executable_name, get_executable_path, format_pattern, clean_file_name
from modules.fb2html import Fb2XHTML
from modules.epub import EpubProc
from modules.config import ConverterConfig
from modules.sendtokindle import SendToKindle
from modules.mobi_split import mobi_split, mobi_read
from modules.mobi_pagemap import PageMapProcessor
def create_epub(rootdir, epubname):
epub = zipfile.ZipFile(epubname, "w")
epub.write(os.path.join(rootdir, 'mimetype'), 'mimetype', zipfile.ZIP_STORED)
for root, _, files in os.walk(rootdir):
relpath = os.path.relpath(root, rootdir)
if relpath != '.':
epub.write(root, relpath)
for filename in files:
if filename != 'mimetype' and filename != '_unzipped.fb2':
epub.write(os.path.join(root, filename), os.path.join(relpath, filename), zipfile.ZIP_DEFLATED)
epub.close()
def get_mobi_filename(filename, translit=False):
out_file, _ = os.path.splitext(filename)
if out_file.lower().endswith('.fb2'):
out_file, _ = os.path.splitext(out_file)
if translit:
out_file = slugify(out_file)
return '{0}.mobi'.format(out_file)
def unzip(filename, tempdir):
unzipped_file = None
zfile = zipfile.ZipFile(filename)
zname = zfile.namelist()[0]
_, zfilename = os.path.split(zname)
if zfilename:
unzipped_file = os.path.join(tempdir, '_unzipped.fb2')
with open(unzipped_file, 'wb') as f:
f.write(zfile.read(zname))
zfile.close()
return unzipped_file
def unzip_epub(filename, tempdir):
unzipped_file = None
zfile = zipfile.ZipFile(filename)
zname = next((x for x in zfile.namelist() if x.lower().endswith('.opf')), None)
if zname:
zfile.extractall(tempdir)
unzipped_file = os.path.normpath(os.path.join(tempdir, zname))
zfile.close()
return unzipped_file
def rm_tmp_files(dest, deleteroot=True):
for root, dirs, files in os.walk(dest, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
if deleteroot:
os.rmdir(dest)
def process_file(config, infile, outfile=None):
start_time = time.clock()
temp_dir = tempfile.mkdtemp()
if not os.path.exists(infile):
config.log.critical('File {0} not found'.format(infile))
return
fbname = os.path.basename(infile)
config.log.info('Converting "{0}" in "{1}"...'.format(fbname, temp_dir))
config.log.info('Using profile "{0}".'.format(config.current_profile['name']))
# Проверка корректности параметров
if infile:
if not infile.lower().endswith(('.fb2', '.fb2.zip', '.zip', '.epub')):
config.log.critical('"{0}" not *.fb2, *.fb2.zip, *.zip or *.epub'.format(infile))
return
if not config.current_profile['css'] and not infile.lower().endswith(('.epub')):
config.log.warning('Profile does not have link to css file.')
if 'xslt' in config.current_profile and not os.path.exists(config.current_profile['xslt']):
config.log.critical('Transformation file {0} not found'.format(config.current_profile['xslt']))
return
if config.kindle_compression_level < 0 or config.kindle_compression_level > 2:
config.log.warning('Parameter kindleCompressionLevel should be between 0 and 2, using default value (1).')
config.kindle_compression_level = 1
# Если не задано имя выходного файла - вычислим
if not outfile:
outdir, outputfile = os.path.split(infile)
outputfile = get_mobi_filename(outputfile, config.transliterate)
if config.output_dir:
if not os.path.exists(config.output_dir):
os.makedirs(config.output_dir)
if config.input_dir and config.save_structure:
rel_path = os.path.join(config.output_dir, os.path.dirname(os.path.relpath(infile, config.input_dir)))
if not os.path.exists(rel_path):
os.makedirs(rel_path)
outputfile = os.path.join(rel_path, outputfile)
else:
outputfile = os.path.join(config.output_dir, outputfile)
else:
outputfile = os.path.join(outdir, outputfile)
else:
_output_file, _output_format = os.path.splitext(outfile)
if _output_format not in ('.mobi', '.azw3', '.epub'):
config.log.critical('Output format "{0}" is not supported'.format(_output_format))
return
else:
_output_format = _output_format.lower()[1:]
if not config.mhl:
config.output_format = _output_format
outputfile = '{0}.{1}'.format(_output_file, config.output_format)
if config.output_format.lower() == 'epub':
# Для epub всегда разбиваем по главам
config.current_profile['chapterOnNewPage'] = True
input_ext = os.path.splitext(infile)[1].lower()
if input_ext == '.epub':
config.log.info('Unpacking epub...')
tmp_infile = infile
try:
infile = unzip_epub(infile, temp_dir)
except:
config.log.critical('Error unpacking file "{0}".'.format(tmp_infile))
return
if not infile:
config.log.critical('Error unpacking file "{0}".'.format(tmp_infile))
return
# Let's see what we could do
config.log.info('Processing epub...')
epubparser = EpubProc(infile, config)
epubparser.process()
document_id = epubparser.book_uuid
else:
if input_ext == '.zip':
config.log.info('Unpacking...')
tmp_infile = infile
try:
infile = unzip(infile, temp_dir)
except:
config.log.critical('Error unpacking file "{0}".'.format(tmp_infile))
return
if not infile:
config.log.critical('Error unpacking file "{0}".'.format(tmp_infile))
return
# Конвертируем в html
config.log.info('Converting fb2 to html...')
interrupted = False
try:
fb2parser = Fb2XHTML(infile, temp_dir, config)
fb2parser.generate()
document_id = fb2parser.book_uuid
infile = os.path.join(temp_dir, 'OEBPS', 'content.opf')
if not outfile and config.output_pattern:
# pylint: disable=C0330
# yapf: disable
fname = format_pattern(config.output_pattern,
[
('#title', '' if not fb2parser.book_title else fb2parser.book_title.strip()),
('#series', '' if not fb2parser.book_series else fb2parser.book_series.strip()),
('#abbrseries', ''.join(word[0] for word in fb2parser.book_series.split()).lower() if fb2parser.book_series else ''),
('#number', '' if not fb2parser.book_series_num else fb2parser.book_series_num.strip()),
('#padnumber', '' if not fb2parser.book_series_num else fb2parser.book_series_num.strip().zfill(fb2parser.seriespositions)),
('#authors', fb2parser.get_book_authors()),
('#author', fb2parser.get_book_authors(True)),
('#bookid', document_id),
])
# yapf: enable
head, tail = os.path.splitext(outputfile)
fname = clean_file_name(fname)
config.log.info('Re-setting output file name to "{0}"...'.format(fname))
outputfile = os.path.join(os.path.dirname(head), fname + tail)
except:
interrupted = True
config.log.critical('Error while converting file "{0}"'.format(infile))
config.log.debug('Getting details', exc_info=True)
finally:
if config.debug:
# to avoid problems with file name length we'll base debug directory name on input file, but will place it in output file directory
# part of the name will be number of seconds from epoc start - so name will be unique and easily sorted
debug_dir = '{0}_debug_{1}'.format(os.path.join(os.path.dirname(outputfile), os.path.splitext(fbname)[0]), time.strftime("%Y%m%d%H%M%S"))
# for debugging
config.log.info('Copying intermediate files to "{0}"...'.format(debug_dir))
if os.path.exists(debug_dir):
rm_tmp_files(debug_dir)
shutil.copytree(temp_dir, debug_dir)
if not input_ext == '.epub':
# Store fb2 after xslt transformation for debugging
fb2parser.write_debug(debug_dir)
if interrupted:
# pylint: disable=W0150
return
config.log.info('Processing took {0} sec.'.format(round(time.clock() - start_time, 2)))
if config.output_format.lower() in ('mobi', 'azw3'):
# Запускаем kindlegen
application_path = get_executable_path()
kindlegen_cmd = 'kindlegen.exe' if version.WINDOWS else 'kindlegen'
if os.path.exists(os.path.join(application_path, kindlegen_cmd)):
kindlegen_cmd = os.path.join(application_path, kindlegen_cmd)
interrupted = False
try:
config.log.info('Running kindlegen...')
kindlegen_cmd_pars = '-c{0}'.format(config.kindle_compression_level)
startupinfo = None
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
with subprocess.Popen([kindlegen_cmd, infile, kindlegen_cmd_pars, '-locale', 'en'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, startupinfo=startupinfo) as result:
config.log.debug(str(result.stdout.read(), 'utf-8', errors='replace'))
except OSError as e:
interrupted = True
if e.errno == os.errno.ENOENT:
config.log.critical('{0} not found'.format(kindlegen_cmd))
else:
if version.WINDOWS:
config.log.critical(e.winerror)
config.log.critical(e.strerror)
config.log.debug('Getting details', exc_info=True, stack_info=True)
finally:
if interrupted:
rm_tmp_files(temp_dir)
sys.exit(-1)
elif config.output_format.lower() == 'epub':
# Собираем epub
config.log.info('Creating epub...')
outputfile = os.path.splitext(outputfile)[0] + '.epub'
create_epub(temp_dir, outputfile)
ext = config.output_format.lower()
if ext in ('mobi', 'azw3'):
# Копируем mobi(azw3) из временного в выходной каталог
result_book = infile.replace('.opf', '.mobi')
if not os.path.isfile(result_book):
config.log.critical('kindlegen error, conversion interrupted.')
rm_tmp_files(temp_dir)
return
else:
try:
remove_personal = config.current_profile['kindleRemovePersonalLabel']
if ext in 'mobi' and config.noMOBIoptimization:
config.log.info('Copying resulting file...')
shutil.copyfile(result_book, outputfile)
else:
config.log.info('Optimizing resulting file...')
splitter = mobi_split(result_book, document_id, remove_personal, ext)
open(os.path.splitext(outputfile)[0] + '.' + ext, 'wb').write(splitter.getResult() if ext == 'mobi' else splitter.getResult8())
except:
config.log.critical('Error optimizing file, conversion interrupted.')
config.log.debug('Getting details', exc_info=True, stack_info=True)
rm_tmp_files(temp_dir)
return
if config.apnx:
try:
base, _ = os.path.splitext(outputfile)
reader = mobi_read(base + '.' + ext)
pagedata = reader.getPageData()
if pagedata:
config.log.info('Generating page index (APNX)...')
pages = PageMapProcessor(pagedata, config.log)
asin = reader.getCdeContentKey()
if not asin:
asin = reader.getASIN()
apnx = pages.generateAPNX({
'contentGuid': str(uuid.uuid4()).replace('-', '')[:8],
'asin': asin,
'cdeType': reader.getCdeType(),
'format': 'MOBI_8' if ext in 'azw3' else 'MOBI_7',
'pageMap': pages.getPageMap(),
'acr': reader.getACR()
})
if config.apnx == 'eink':
basename = os.path.basename(base)
sdr = base + '.sdr'
if not os.path.exists(sdr):
os.makedirs(sdr)
apnxfile = os.path.join(sdr, basename + '.apnx')
else:
apnxfile = base + '.apnx'
open(apnxfile, 'wb').write(apnx)
else:
config.log.warning('No information to generate page index')
except:
config.log.warning('Unable to generate page index (APNX)')
config.log.debug('Getting details', exc_info=True, stack_info=True)
config.log.info('Book conversion completed in {0} sec.\n'.format(round(time.clock() - start_time, 2)))
if config.send_to_kindle['send']:
if config.output_format.lower() != 'mobi':
config.log.warning('Kindle Personal Documents Service only accepts personal mobi files')
else:
config.log.info('Sending book...')
try:
kindle = SendToKindle()
kindle.smtp_server = config.send_to_kindle['smtpServer']
kindle.smtp_port = config.send_to_kindle['smtpPort']
kindle.smtp_login = config.send_to_kindle['smtpLogin']
kindle.smtp_password = config.send_to_kindle['smtpPassword']
kindle.user_email = config.send_to_kindle['fromUserEmail']
kindle.kindle_email = config.send_to_kindle['toKindleEmail']
kindle.convert = False
kindle.send_mail([outputfile])
config.log.info('Book has been sent to "{0}"'.format(config.send_to_kindle['toKindleEmail']))
if config.send_to_kindle['deleteSendedBook']:
try:
os.remove(outputfile)
except:
config.log.error('Unable to remove file "{0}".'.format(outputfile))
return
except KeyboardInterrupt:
print('User interrupt. Exiting...')
rm_tmp_files(temp_dir)
sys.exit(-1)
except:
config.log.error('Error sending file')
config.log.debug('Getting details', exc_info=True, stack_info=True)
# Чистим временные файлы
rm_tmp_files(temp_dir)
return os.path.splitext(outputfile)[0] + '.' + ext
def process_folder(config, inputdir, outputdir=None):
if outputdir:
if not os.path.exists(outputdir):
os.makedirs(outputdir)
if os.path.isdir(inputdir):
for root, _, files in os.walk(inputdir):
for file in files:
try:
if file.lower().endswith(('.fb2', '.fb2.zip', '.zip', '.epub')):
inputfile = os.path.join(root, file)
# Обработка каталога. Смотрим признак рекурсии по подкаталогам
if (not config.recursive and inputdir == root) or config.recursive:
process_file(config, inputfile, None)
if config.delete_source_file:
try:
os.remove(inputfile)
except:
config.log.error('Unable to remove file "{0}"'.format(inputfile))
continue
except KeyboardInterrupt as e:
print('User interrupt. Exiting...')
sys.exit(-1)
except IOError as e:
config.log.error('(I/O error {0}) {1} - {2}'.format(e.errno, e.strerror, e.filename))
except:
config.log.error('Error processing folder')
config.log.debug('Getting details', exc_info=True, stack_info=True)
else:
config.log.critical('Unable to find directory "{0}"'.format(inputdir))
sys.exit(-1)
def get_log_level(log_level):
if log_level.lower() == 'info':
return logging.INFO
elif log_level.lower() == 'error':
return logging.ERROR
elif log_level.lower() == 'critical':
return logging.CRITICAL
elif log_level.lower() == 'debug':
return logging.DEBUG
else:
return logging.INFO
def process(myargs):
infile = myargs.infile
outfile = myargs.outfile
application_path = get_executable_path()
config_file_name = "{0}.config".format(get_executable_name())
if myargs.config_file:
if os.path.exists(myargs.config_file):
# full path is given
config_file = myargs.config_file
elif os.path.exists(os.path.join(application_path, myargs.config_file)):
# found relative to program directory
config_file = os.path.join(application_path, myargs.config_file)
else:
print('Unable to locate configuration file "{0}"'.format(myargs.config_file))
sys.exit(-1)
else:
config_file = os.path.join(os.path.expanduser('~'), 'fb2mobi' if version.WINDOWS else '.fb2mobi', config_file_name)
if not os.path.exists(config_file):
# last resort - see if we have default config in program directory
if os.path.exists(os.path.join(application_path, config_file_name)):
config_file = os.path.join(application_path, config_file_name)
# if configuration does not exist - it will be created in user HOME directory
config = ConverterConfig(config_file)
if myargs.profilelist:
print('Profile list in {0}:'.format(config.config_file))
for p in config.profiles:
print('\t{0}: {1}'.format(p, config.profiles[p]['description']))
sys.exit(0)
# Если указаны параметры в командной строке, переопределяем дефолтные параметры 1
if myargs:
if myargs.debug:
config.debug = myargs.debug
if myargs.log:
config.log_file = myargs.log
if myargs.loglevel:
config.log_level = myargs.loglevel
if myargs.consolelevel:
config.console_level = myargs.consolelevel
if myargs.recursive:
config.recursive = True
if myargs.nc:
config.mhl = True
log = logging.getLogger('fb2mobi')
log.setLevel("DEBUG")
log_stream_handler = logging.StreamHandler()
log_stream_handler.setLevel(get_log_level(config.console_level))
log_stream_handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
log.addHandler(log_stream_handler)
if config.log_file:
log_file_handler = logging.FileHandler(filename=config.log_file, mode='a', encoding='utf-8')
log_file_handler.setLevel(get_log_level(config.log_level))
log_file_handler.setFormatter(logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s'))
log.addHandler(log_file_handler)
config.log = log
if myargs.profile:
config.setCurrentProfile(myargs.profile)
else:
config.setCurrentProfile(config.default_profile)
# Если указаны параметры в командной строке, переопределяем дефолтные параметры 2
if myargs:
if myargs.apnx:
config.apnx = myargs.apnx.lower()
if myargs.outputformat:
config.output_format = myargs.outputformat
if myargs.hyphenate is not None:
config.current_profile['hyphens'] = myargs.hyphenate
if myargs.transliterate is not None:
config.transliterate = myargs.transliterate
if myargs.screen_width is not None:
config.screen_width = myargs.screen_width
if myargs.screen_height is not None:
config.screen_height = myargs.screen_height
if myargs.kindlecompressionlevel:
config.kindle_compression_level = myargs.kindlecompressionlevel
if myargs.css:
config.current_profile['css'] = myargs.css
if myargs.xslt:
config.current_profile['xslt'] = myargs.xslt
if myargs.dropcaps is not None:
config.current_profile['dropcaps'] = myargs.dropcaps
if myargs.toctype:
config.current_profile['tocType'] = myargs.toctype.lower()
if myargs.tocmaxlevel:
config.current_profile['tocMaxLevel'] = myargs.tocmaxlevel
if myargs.tockindlelevel:
config.current_profile['tocKindleLevel'] = myargs.tockindlelevel
if myargs.tocbeforebody is not None:
config.current_profile['tocBeforeBody'] = myargs.tocbeforebody
if myargs.notesmode:
config.current_profile['notesMode'] = myargs.notesmode
if myargs.notesbodies:
config.current_profile['notesBodies'] = myargs.notesbodies
if myargs.annotationtitle:
config.current_profile['annotationTitle'] = myargs.annotationtitle
if myargs.toctitle:
config.current_profile['tocTitle'] = myargs.toctitle
if myargs.chapteronnewpage is not None:
config.current_profile['chapterOnNewPage'] = myargs.chapteronnewpage
if myargs.chapterlevel is not None:
config.current_profile['chapterLevel'] = myargs.chapterlevel
if myargs.seriespositions is not None:
config.current_profile['seriesPositions'] = myargs.seriespositions
if myargs.removepngtransparency is not None:
config.current_profile['removePngTransparency'] = myargs.removepngtransparency
if myargs.noMOBIoptimization:
config.noMOBIoptimization = myargs.noMOBIoptimization
if myargs.sendtokindle is not None:
config.send_to_kindle['send'] = myargs.sendtokindle
if myargs.inputdir:
config.input_dir = myargs.inputdir
if myargs.outputdir:
config.output_dir = myargs.outputdir
if myargs.deletesourcefile:
config.delete_source_file = myargs.deletesourcefile
if myargs.savestructure:
config.save_structure = myargs.savestructure
if myargs.openbookfromcover is not None:
config.current_profile['openBookFromCover'] = myargs.openbookfromcover
if myargs.coverStamp is not None:
config.current_profile['coverStamp'] = myargs.coverStamp
if myargs.imageScale is not None:
config.current_profile['scaleImages'] = myargs.imageScale
if myargs.transliterateauthorandtitle is not None:
config.transliterate_author_and_title = myargs.transliterateauthorandtitle
if myargs.inputdir:
config.log.info(' ')
config.log.info('**********************************************')
config.log.info('Using configuration "{0}".'.format(config_file))
config.log.info(' ')
process_folder(config, myargs.inputdir, myargs.outputdir)
if myargs.deleteinputdir:
try:
rm_tmp_files(myargs.inputdir, False)
except:
config.log.error('Unable to remove directory "%s"', myargs.inputdir)
elif infile:
config.log.info(' ')
config.log.info('**********************************************')
config.log.info('Using configuration "{0}".'.format(config_file))
config.log.info(' ')
process_file(config, infile, outfile)
if myargs.deletesourcefile:
try:
os.remove(infile)
except:
config.log.error('Unable to remove file "%s"', infile)
else:
print(argparser.description)
argparser.print_usage()
if __name__ == '__main__':
# Настройка парсера аргументов
# yapf: disable
argparser = argparse.ArgumentParser(description='Converter of fb2 and epub ebooks to mobi, azw3 and epub formats. Version {0}'.format(version.VERSION))
argparser.add_argument('-c', '--config', dest='config_file', type=str, default=None, help='Configuration file name (full path or relative of the program directory). if not present {0}.config in program directory or home directory is assumed'.format(get_executable_name()))
input_args_group = argparser.add_mutually_exclusive_group()
input_args_group.add_argument('infile', type=str, nargs='?', default=None, help='Source file name (fb2, fb2.zip, zip or epub)')
input_args_group.add_argument('-i', '--input-dir', dest='inputdir', type=str, default=None, help='Source directory for batch conversion')
output_args_group = argparser.add_mutually_exclusive_group()
output_args_group.add_argument('outfile', type=str, nargs='?', default=None, help='Destination file name (mobi, azw3 or epub)')
output_args_group.add_argument('-o', '--output-dir', dest='outputdir', type=str, default=None, help='Destination directory name for batch conversion')
argparser.add_argument('-f', '--output-format', dest='outputformat', type=str, default=None, help='Output format: mobi, azw3 or epub')
argparser.add_argument('-r', dest='recursive', action='store_true', default=False, help='Perform recursive processing of files in source directory')
argparser.add_argument('-s', '--save-structure', dest='savestructure', action='store_true', default=False, help='Keep directory structure during batch processing')
argparser.add_argument('--delete-source-file', dest='deletesourcefile', action='store_true', default=False, help='In case of success remove source file')
argparser.add_argument('--delete-input-dir', dest='deleteinputdir', action='store_true', default=False, help='Remove source directory')
argparser.add_argument('-d', '--debug', action='store_true', default=None, help='Keep imtermediate files in desctination directory')
argparser.add_argument('--log', type=str, default=None, help='Log file name')
argparser.add_argument('--log-level', dest='loglevel', type=str, default=None, help='Log level: INFO, ERROR, CRITICAL, DEBUG')
argparser.add_argument('--console-level', dest='consolelevel', type=str, default=None, help='Log level: INFO, ERROR, CRITICAL, DEBUG')
argparser.add_argument('--apnx', dest='apnx', type=str, default=None, choices=['eInk', 'PC'], help='Genrate page index file (APNX): eInk, PC')
hyphenate_group = argparser.add_mutually_exclusive_group()
hyphenate_group.add_argument('--hyphenate', dest='hyphenate', action='store_true', default=None, help='Turn on hyphenation')
hyphenate_group.add_argument('--no-hyphenate', dest='hyphenate', action='store_false', default=None, help='Turn off hyphenation')
transliterate_group = argparser.add_mutually_exclusive_group()
transliterate_group.add_argument('--transliterate', dest='transliterate', action='store_true', default=None, help='Transliterate destination file name')
transliterate_group.add_argument('--no-transliterate', dest='transliterate', action='store_false', default=None, help='Do not transliterate destination file name')
transliterate_author_group = argparser.add_mutually_exclusive_group()
transliterate_author_group.add_argument('--transliterate-author-and-title', dest='transliterateauthorandtitle', action='store_true', default=None, help='Transliterate book title and author(s)')
transliterate_author_group.add_argument('--no-transliterate-author-and-title', dest='transliterateauthorandtitle', action='store_false', default=None, help='Do not transliterate book title and author(s)')
screen_group = argparser.add_argument_group('Target device screen dimensions for calculating proper cover size', 'default 800x573')
screen_group.add_argument('--screen-width', dest='screen_width', type=int, default=None, help='Target screen width')
screen_group.add_argument('--screen-height', dest='screen_height', type=int, default=None, help='Target screen height')
argparser.add_argument('--kindle-compression-level', dest='kindlecompressionlevel', type=int, default=None, help='Kindlegen compression level - 0, 1, 2')
argparser.add_argument('-p', '--profile', type=str, default=None, help='Profile name from configuration')
argparser.add_argument('--no-MOBI-optimization', dest='noMOBIoptimization', action='store_true', default=False, help='Do not do anything with resulting mobi file (Old behavior)')
argparser.add_argument('--css', type=str, default=None, help='css file name')
argparser.add_argument('--xslt', type=str, default=None, help='xslt file name')
argparser.add_argument('--dropcaps', dest='dropcaps', type=str, default=None, choices=['Simple', 'Smart', 'None'], help='Control dropcaps processing (Simple, Smart, None)')
argparser.add_argument('-l', '--profile-list', dest='profilelist', action='store_true', default=False, help='Show list of available profiles')
argparser.add_argument('--notes-mode', dest='notesmode', type=str, default=None, choices=['default', 'inline', 'block', 'float'], help='How to show footnotes: default, inline, block or float')
argparser.add_argument('--notes-bodies', dest='notesbodies', type=str, default=None, help='List of fb2 part names (body) with footnotes (comma separated)')
argparser.add_argument('--annotation-title', dest='annotationtitle', type=str, default=None, help='Annotations title')
argparser.add_argument('--chapter-level', dest='chapterlevel', type=int, default=None, help='Sections above this level are not chapters')
argparser.add_argument('--open-book-from-cover', dest='openbookfromcover', action='store_true', default=None, help='Open book on cover page')
chapter_group = argparser.add_mutually_exclusive_group()
chapter_group.add_argument('--chapter-on-new-page', dest='chapteronnewpage', action='store_true', default=None, help='Start chapter from the new page')
chapter_group.add_argument('--no-chapter-on-new-page', dest='chapteronnewpage', action='store_false', default=None, help='Do not start chapter from the new page')
sendtokindle_group = argparser.add_mutually_exclusive_group()
sendtokindle_group.add_argument('--send-to-kindle', dest='sendtokindle', action='store_true', default=None, help='Use Kindle Personal Documents Service to send book to device (<sendToKindle> in configuration file must have proper values!)')
sendtokindle_group.add_argument('--no-send-to-kindle', dest='sendtokindle', action='store_false', default=None, help='Do not use Kindle Personal Documents Service to send book to device')
argparser.add_argument('--series-positions', dest='seriespositions', type=int, default=None, help='How much zero padding to use for #padnumber')
tocplace_group = argparser.add_mutually_exclusive_group()
tocplace_group.add_argument('--toc-before-body', dest='tocbeforebody', action='store_true', default=None, help='Put TOC at the book beginning')
tocplace_group.add_argument('--toc-after-body', dest='tocbeforebody', action='store_false', default=None, help='Put TOC at the book end')
argparser.add_argument('--toc-title', dest='toctitle', type=str, default=None, help='TOC title')
argparser.add_argument('--toc-type', dest='toctype', type=str, default=None, choices=['Flat', 'Kindle', 'Normal'], help='TOC (NCX) type for target device')
argparser.add_argument('--toc-max-level', dest='tocmaxlevel', type=int, default=None, help='Maximum level of titles in the TOC')
argparser.add_argument('--toc-kindle-level', dest='tockindlelevel', type=int, default=None, help='Where to put second level of TOC (NCX) for Kindle')
pngtransparency_group = argparser.add_mutually_exclusive_group()
pngtransparency_group.add_argument('--remove-png-transparency', dest='removepngtransparency', action='store_true', default=None, help='Remove transparency in PNG images')
pngtransparency_group.add_argument('--no-remove-png-transparency', dest='removepngtransparency', action='store_false', default=None, help='Do not remove transparency in PNG images')
argparser.add_argument('--stamp-cover', dest='coverStamp', type=str, default=None, choices=['Top', 'Center', 'Bottom', 'None'], help='Place stamp on cover image (Top, Center, Bottom, None)')
argparser.add_argument('--scale-images', dest='imageScale', type=float, default=None, help='Resample all embedded images but cover (Positive ratio, 0 - no scaling)')
# Для совместимости с MyHomeLib добавляем аргументы, которые передает MHL в fb2mobi.exe
argparser.add_argument('-nc', action='store_true', default=False, help='For MyHomeLib compatibility')
argparser.add_argument('-cl', action='store_true', help='For MyHomeLib compatibility')
argparser.add_argument('-us', action='store_true', help='For MyHomeLib compatibility')
argparser.add_argument('-nt', action='store_true', help='For MyHomeLib compatibility')
# yapf: enable
# Парсим аргументы командной строки
args = argparser.parse_args()
process(args)