Skip to content

Commit bfb7595

Browse files
committed
v2.0.0 upload
1 parent 81af233 commit bfb7595

File tree

11 files changed

+269
-181
lines changed

11 files changed

+269
-181
lines changed

allow_setting.json

Lines changed: 0 additions & 44 deletions
This file was deleted.

app2.py

Lines changed: 102 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
import mimetypes
44
import os
55
import re
6+
import shutil
67
import sys
8+
import time
79
import zipfile
8-
import chardet
9-
import logging
10+
from configparser import ConfigParser
1011

1112
from modules.console import Console
12-
from modules.utils.error import FileTypeError, FileUnzipError, ConfigError
1313
from modules.logger import Logger
1414
from modules.opencc import OpenCC
15-
from modules.utils.tools import get_key, resource_path
15+
from modules.utils.error import (ConfigError, FileTypeError, FileUnzipError,
16+
ZhConvertError)
17+
from modules.utils.tools import encoding, get_key, resource_path
1618
from modules.zhconvert import ZhConvert
1719

1820

@@ -26,14 +28,15 @@ def __init__(self):
2628
Objects:
2729
logger -- log記錄檔物件
2830
workpath -- 本程式所在的絕對路徑
29-
config -- 讀取本程式路徑底下的 config.json 設定檔內容
31+
cfg -- 讀取本程式路徑底下的 config.ini 設定檔內容
3032
convert_file_list -- 執行 unzip 方法後取得 EPub 中需要轉換的檔案之絕對路徑清單(list)
3133
new_filename -- 轉換後的 EPub 檔案的檔案名稱
3234
"""
33-
self.logger = Logger(name='EPUB')
3435
self.workpath = os.path.abspath(
3536
os.path.join(sys.argv[0], os.path.pardir))
36-
self.config = self._read_config(f'{self.workpath}/config.json')
37+
self.logger = Logger(
38+
name='EPUB', workpath=self.workpath)
39+
self.cfg = self._read_config(f'{self.workpath}/config.ini')
3740
self.convert_file_list = None
3841
self.file_path = None
3942

@@ -45,21 +48,23 @@ def _read_config(self, config):
4548
"""
4649
if os.path.exists(config):
4750
self.logger.info('_read_config', 'read config')
48-
with open(config, 'r', encoding='utf-8') as r_c:
49-
config = json.loads(r_c.read())
51+
cfg = ConfigParser()
52+
cfg_encoding = encoding(config)['encoding']
53+
self.logger.info('_read_config encoding',encoding(config)['encoding'])
54+
cfg.read(config, encoding=cfg_encoding)
5055
self.logger.info(
51-
'_read_config', f"Aleady read config\nengine: {config['engine']}\nconverter: {config['converter']}\nformat: {config['format']}")
52-
return config
56+
'_read_config', f"already read config\nengine: {cfg['setting']['engine']}\nconverter: {cfg['setting']['converter']}\nformat: {cfg['setting']['format']}")
57+
return cfg
5358
else:
54-
print('error')
59+
self.logger.info(f'_read_config', f'can\'t find "config.ini", please check config file.')
60+
61+
""" def _read_allow_setting(self, config):
62+
'''讀取允許設定
5563
56-
def _read_allow_setting(self, config):
57-
"""讀取允許設定
58-
5964
Arguments:
6065
config {str} -- allow_setting.json path
61-
"""
62-
print(resource_path('allow_setting.json'))
66+
'''
67+
print(resource_path('allow_setting.json')) """
6368

6469
@property
6570
def _zip(self):
@@ -103,6 +108,7 @@ def convert(self, epub_file_path):
103108
"""
104109
try:
105110
self.file_path = epub_file_path
111+
self.logger.info('convert', f'file path: {self.file_path}')
106112
self._check(epub_file_path)
107113
self._unzip(epub_file_path)
108114
if self.convert_file_list:
@@ -111,10 +117,10 @@ def convert(self, epub_file_path):
111117
self._convert_content(self.convert_file_list)
112118
self._rename(self.convert_file_list)
113119
self._zip
114-
# self._clean
120+
self._clean
121+
self.logger.info('convert', f'success convert {os.path.basename(epub_file_path)}')
115122
except Exception as e:
116123
self.logger.error('convert', f'{str(e)}')
117-
os.system('pause')
118124

119125
def _rename(self, convert_file_list):
120126
"""重新命名已轉換的檔案
@@ -132,7 +138,7 @@ def _filename(self):
132138
"s2t": ["s2t", "s2tw", "Traditional", "Taiwan", "WikiTraditional"],
133139
"t2s": ["t2s", "tw2s", "Simplified", "China", "WikiSimplified"]
134140
}
135-
converter = get_key(converter_dict, self.config['converter'])
141+
converter = get_key(converter_dict, self.cfg['setting']['converter'])
136142
openCC = OpenCC(converter)
137143
new_filename = openCC.convert(os.path.basename(self.file_path))
138144
return os.path.join(os.path.dirname(self.file_path), new_filename)
@@ -159,42 +165,65 @@ def _convert_content(self, convert_file_list):
159165
"format": ["Straight", "Horizontal"]
160166
}
161167
# 檢查設定檔是否有無錯誤
162-
if self.config['engine'] not in setting['engine']:
163-
raise ConfigError('Engine is not a right engine in "config.json"')
164-
if self.config['converter'] not in setting['converter'][self.config['engine']]:
168+
if self.cfg['setting']['engine'] not in setting['engine']:
169+
raise ConfigError('Engine is not a right engine in "config.ini"')
170+
if self.cfg['setting']['converter'] not in setting['converter'][self.cfg['setting']['engine']]:
165171
raise ConfigError(
166-
'Converter is not a right converter in "config.json"')
167-
if self.config['format'] not in setting['format']:
168-
raise ConfigError('Format is not a right format in "config.json"')
172+
'Converter is not a right converter in "config.ini"')
173+
if self.cfg['setting']['format'] not in setting['format']:
174+
raise ConfigError('Format is not a right format in "config.ini"')
169175
# 判斷轉換引擎並轉換
170-
if self.config['engine'].lower() == 'opencc':
176+
if self.cfg['setting']['engine'].lower() == 'opencc':
171177
self.logger.debug('convert_text', 'engine: opencc')
172178
for f in convert_file_list:
173179
self.logger.debug(
174180
'convert_text', f'now convert "{os.path.basename(f)}"')
175181
self._content_opt_lang(f)
176-
self._opencc(self.config['converter'], f)
177-
if self.config['engine'].lower() == 'zhconvert':
182+
self._opencc(self.cfg['setting']['converter'], f)
183+
if self.cfg['setting']['engine'].lower() == 'zhconvert':
178184
self.logger.debug('convert_text', 'engine: zhconvert 繁化姬')
179185
for f in convert_file_list:
186+
self.logger.debug(
187+
'convert_text', f'now convert "{os.path.basename(f)}"')
180188
self._content_opt_lang(f)
189+
self._zhconvert(self.cfg['setting']['converter'], f)
181190

182191
def _opencc(self, converter, file):
183-
"""opencc 轉換作業
192+
"""opencc
184193
185194
Arguments:
186-
converter {str} -- config.json 中 converter 設定,轉換模式
195+
converter {str} -- config.ini 中 converter 設定,轉換模式
187196
file {str} -- 欲進行文字轉換的內文文檔的絕對路徑
188197
"""
189198
openCC = OpenCC(converter)
190-
f_r = open(file, 'r', encoding='utf-8').readlines()
199+
f_encoding = encoding(file)['encoding']
200+
start_time = time.time()
201+
f_r = open(file, 'r', encoding=f_encoding).readlines()
191202
with open(file + '.new', 'w', encoding='utf-8') as f_w:
192203
for line in f_r:
193204
converted = openCC.convert(line)
194205
f_w.write(converted)
206+
end_time = time.time()
207+
self.logger.info('_opencc', f'convert file: {os.path.basename(file)} cost {"{:.2f}".format(end_time-start_time)}s')
195208

196-
def _zhconvert(self, converter):
197-
""" """
209+
def _zhconvert(self, converter, file):
210+
"""zhconvert 繁化姬
211+
212+
Arguments:
213+
converter {str} -- config.ini 中 converter 設定,轉換模式
214+
file {str} -- 欲進行文字轉換的內文文檔的絕對路徑
215+
"""
216+
zhconvert = ZhConvert()
217+
f_encoding = encoding(file)['encoding']
218+
start_time = time.time()
219+
with open(file, 'r', encoding=f_encoding) as f_r:
220+
zhconvert.convert(text=f_r.read(), converter=converter)
221+
with open(file + '.new', 'w', encoding='utf-8') as f_w:
222+
if zhconvert.text is None:
223+
raise ZhConvertError()
224+
f_w.write(zhconvert.text)
225+
end_time = time.time()
226+
self.logger.info('_zhconvert', f'convert file: {os.path.basename(file)} cost {"{:.2f}".format(end_time-start_time)}s')
198227

199228
def _content_opt_lang(self, content_file_path):
200229
"""修改 content.opf 中語言標籤的值
@@ -210,21 +239,51 @@ def _content_opt_lang(self, content_file_path):
210239
regex = re.compile(
211240
r"<dc:language>[\S]*</dc:language>", re.IGNORECASE)
212241
fileline = open(content_file_path, encoding='utf-8').read()
213-
if self.config['converter'] in converter["zh-TW"]:
242+
if self.cfg['setting']['converter'] in converter["zh-TW"]:
214243
self.logger.info('_content_lang', 'convert language to zh-TW')
215244
modify = re.sub(
216245
regex, f'<dc:language>zh-TW</dc:language>', fileline)
217-
if self.config['converter'] in converter["zh-CN"]:
246+
if self.cfg['setting']['converter'] in converter["zh-CN"]:
218247
self.logger.info('_content_lang', 'convert language to zh-CN')
219248
modify = re.sub(
220249
regex, f'<dc:language>zh-CN</dc:language>', fileline)
221250
open(content_file_path, 'w', encoding='utf-8').write(modify)
222251

223-
def _format(self):
224-
""" """
252+
# def _format(self, file_path):
253+
# """ """
254+
# modify_files = {}
255+
# opf_tmp = []
256+
# css_tmp = []
257+
# content_tmp = []
258+
# for root, _dirs, files in os.walk(f'{file_path}_files/'):
259+
# for filename in files:
260+
# if filename.endswith('opf'):
261+
# opf_tmp.append(filename)
262+
# if filename.endswith('css'):
263+
# css_tmp.append(filename)
264+
# if filename.endswith(('xhtml', 'html', 'htm')):
265+
# content_tmp.append(filename)
266+
# modify_files['opf'] = opf_tmp
267+
# modify_files['css'] = css_tmp
268+
# modify_files['content'] = content_tmp
269+
# #橫式
270+
# if self.cfg['setting']['format'].lower() == 'horizontal':
271+
# self.logger.info('_format', 'set content to horizontal')
272+
# if not any(modify_files['css']):
273+
# print('css file not found')
274+
# #直式
275+
# if self.cfg['setting']['format'].lower() == 'straight':
276+
# self.logger.info('_format', 'set content to straight')
277+
# print('直式')
225278

279+
@property
226280
def _clean(self):
227-
""" """
281+
""" 清除解壓縮後的檔案 """
282+
if os.path.isdir( f'{self.file_path}_files'):
283+
self.logger.info('_clean', f'delete tmp files: {self.file_path}_files')
284+
shutil.rmtree(f'{self.file_path}_files')
285+
else:
286+
self.logger.error('_clean', f'path: {self.file_path}_files not found.')
228287

229288
def _check(self, file_path):
230289
"""檢查檔案 MIME 格式
@@ -242,9 +301,8 @@ def _check(self, file_path):
242301

243302

244303
if __name__ == "__main__":
245-
#epub = EPubConv()
246-
# epub.convert('H:/VSCode/Python/epubconv/1.epub')
247-
""" zh = ZhConvert()
248-
zh.convert() """
249-
#epub._read_allow_setting('allow_setting.json')
304+
EPubConvert = EPubConv()
305+
for epub in sys.argv[1:]:
306+
EPubConvert.convert(epub)
307+
os.system("pause")
250308
pass

config.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[setting]
2+
engine=zhconvert
3+
converter=Traditional
4+
format=Horizontal

config.json

Lines changed: 0 additions & 35 deletions
This file was deleted.

file_version_info.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# UTF-8
2+
#
3+
# For more details about fixed file info 'ffi' see:
4+
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
5+
VSVersionInfo(
6+
ffi=FixedFileInfo(
7+
# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
8+
# Set not needed items to zero 0.
9+
filevers=(2, 0, 0, 0),
10+
prodvers=(2, 0, 0, 0),
11+
# Contains a bitmask that specifies the valid bits 'flags'r
12+
mask=0x3f,
13+
# Contains a bitmask that specifies the Boolean attributes of the file.
14+
flags=0x0,
15+
# The operating system for which this file was designed.
16+
# 0x4 - NT and there is no need to change it.
17+
OS=0x40004,
18+
# The general type of file.
19+
# 0x1 - the file is an application.
20+
fileType=0x1,
21+
# The function of the file.
22+
# 0x0 - the function is not defined for this fileType
23+
subtype=0x0,
24+
# Creation date and time stamp.
25+
date=(0, 0)
26+
),
27+
kids=[
28+
StringFileInfo(
29+
[
30+
StringTable(
31+
u'040904B0',
32+
[StringStruct(u'CompanyName', u''),
33+
StringStruct(u'FileDescription', u'EpubConv'),
34+
StringStruct(u'FileVersion', u'2.0.0'),
35+
StringStruct(u'InternalName', u'Epubconv'),
36+
StringStruct(u'LegalCopyright', u''),
37+
StringStruct(u'OriginalFilename', u'Epubconv.Exe'),
38+
StringStruct(u'ProductName', u'Epubconv'),
39+
StringStruct(u'ProductVersion', u'2.0.0')])
40+
]),
41+
VarFileInfo([VarStruct(u'Translation', [1033, 1200])])
42+
]
43+
)

0 commit comments

Comments
 (0)