-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunown.py
executable file
·77 lines (63 loc) · 2.62 KB
/
unown.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
#!/usr/bin/env python3
#
# Unown is an EPUB generation utility conforming to EPUB 3.0.
# Copyright (C) 2018 Kiyoshi Aman
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, 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
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program. If not, see
# <https://www.gnu.org/licenses/>.
import sys
import uuid
import os.path as op
import unown
def generate_epub(cfg, uuid, files, filename, mode='all'):
if mode not in ['white', 'black', 'all']:
raise ValueError('unknown mode')
path = op.join('.', cfg['directory'])
print('Generating {}...'.format(filename))
if 'single_file' in cfg.keys() and cfg['single_file']:
print(' Building EPUB Package Document...')
unown.build_package_singleton(cfg, path, mode)
print(' Building EPUB Container...')
unown.make_zip_singleton(filename, path, cfg['source'], filename, mode)
else:
print(' Building EPUB Package Document...')
unown.build_package(cfg, path, files, mode, uuid)
print(' Building EPUB Container...')
unown.make_zip(filename, path, files, mode)
def process_list(cfg, mode='white'):
if mode not in ['white', 'black']:
raise ValueError('unknown mode')
listtype = mode + 'list'
sets = [item for item in cfg[listtype].keys() if not item.endswith('_uuid')]
count = len(sets)
print('Processing {}{}...'.format(listtype, 's' if count > 1 else ''))
for name in sets:
files = cfg[listtype][name]
filename = '{} {}.epub'.format(cfg['title'], name)
uuid_field = '{}_uuid'.format(name)
if uuid_field not in cfg[listtype]:
cfg[listtype][uuid_field] = str(uuid.uuid1())
generate_epub(cfg, cfg[listtype][uuid_field], files, filename, mode)
print('Loading configuration...')
cfg = unown.load_config(sys.argv[1])
if 'whitelist' in cfg.keys():
filtered = True
process_list(cfg, 'white')
if 'blacklist' in cfg.keys():
filtered = True
process_list(cfg, 'black')
if cfg['generate_all'] or not filtered:
generate_epub(cfg, cfg['uuid'], None, '{}.epub'.format(cfg['title']), 'all')
print('Saving configuration...')
unown.save_config(sys.argv[1], cfg)