Skip to content

Commit

Permalink
v2.0.0 upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanatosDi committed Jul 22, 2019
1 parent 81af233 commit bfb7595
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 181 deletions.
44 changes: 0 additions & 44 deletions allow_setting.json

This file was deleted.

146 changes: 102 additions & 44 deletions app2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import mimetypes
import os
import re
import shutil
import sys
import time
import zipfile
import chardet
import logging
from configparser import ConfigParser

from modules.console import Console
from modules.utils.error import FileTypeError, FileUnzipError, ConfigError
from modules.logger import Logger
from modules.opencc import OpenCC
from modules.utils.tools import get_key, resource_path
from modules.utils.error import (ConfigError, FileTypeError, FileUnzipError,
ZhConvertError)
from modules.utils.tools import encoding, get_key, resource_path
from modules.zhconvert import ZhConvert


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

Expand All @@ -45,21 +48,23 @@ def _read_config(self, config):
"""
if os.path.exists(config):
self.logger.info('_read_config', 'read config')
with open(config, 'r', encoding='utf-8') as r_c:
config = json.loads(r_c.read())
cfg = ConfigParser()
cfg_encoding = encoding(config)['encoding']
self.logger.info('_read_config encoding',encoding(config)['encoding'])
cfg.read(config, encoding=cfg_encoding)
self.logger.info(
'_read_config', f"Aleady read config\nengine: {config['engine']}\nconverter: {config['converter']}\nformat: {config['format']}")
return config
'_read_config', f"already read config\nengine: {cfg['setting']['engine']}\nconverter: {cfg['setting']['converter']}\nformat: {cfg['setting']['format']}")
return cfg
else:
print('error')
self.logger.info(f'_read_config', f'can\'t find "config.ini", please check config file.')

""" def _read_allow_setting(self, config):
'''讀取允許設定
def _read_allow_setting(self, config):
"""讀取允許設定
Arguments:
config {str} -- allow_setting.json path
"""
print(resource_path('allow_setting.json'))
'''
print(resource_path('allow_setting.json')) """

@property
def _zip(self):
Expand Down Expand Up @@ -103,6 +108,7 @@ def convert(self, epub_file_path):
"""
try:
self.file_path = epub_file_path
self.logger.info('convert', f'file path: {self.file_path}')
self._check(epub_file_path)
self._unzip(epub_file_path)
if self.convert_file_list:
Expand All @@ -111,10 +117,10 @@ def convert(self, epub_file_path):
self._convert_content(self.convert_file_list)
self._rename(self.convert_file_list)
self._zip
# self._clean
self._clean
self.logger.info('convert', f'success convert {os.path.basename(epub_file_path)}')
except Exception as e:
self.logger.error('convert', f'{str(e)}')
os.system('pause')

def _rename(self, convert_file_list):
"""重新命名已轉換的檔案
Expand All @@ -132,7 +138,7 @@ def _filename(self):
"s2t": ["s2t", "s2tw", "Traditional", "Taiwan", "WikiTraditional"],
"t2s": ["t2s", "tw2s", "Simplified", "China", "WikiSimplified"]
}
converter = get_key(converter_dict, self.config['converter'])
converter = get_key(converter_dict, self.cfg['setting']['converter'])
openCC = OpenCC(converter)
new_filename = openCC.convert(os.path.basename(self.file_path))
return os.path.join(os.path.dirname(self.file_path), new_filename)
Expand All @@ -159,42 +165,65 @@ def _convert_content(self, convert_file_list):
"format": ["Straight", "Horizontal"]
}
# 檢查設定檔是否有無錯誤
if self.config['engine'] not in setting['engine']:
raise ConfigError('Engine is not a right engine in "config.json"')
if self.config['converter'] not in setting['converter'][self.config['engine']]:
if self.cfg['setting']['engine'] not in setting['engine']:
raise ConfigError('Engine is not a right engine in "config.ini"')
if self.cfg['setting']['converter'] not in setting['converter'][self.cfg['setting']['engine']]:
raise ConfigError(
'Converter is not a right converter in "config.json"')
if self.config['format'] not in setting['format']:
raise ConfigError('Format is not a right format in "config.json"')
'Converter is not a right converter in "config.ini"')
if self.cfg['setting']['format'] not in setting['format']:
raise ConfigError('Format is not a right format in "config.ini"')
# 判斷轉換引擎並轉換
if self.config['engine'].lower() == 'opencc':
if self.cfg['setting']['engine'].lower() == 'opencc':
self.logger.debug('convert_text', 'engine: opencc')
for f in convert_file_list:
self.logger.debug(
'convert_text', f'now convert "{os.path.basename(f)}"')
self._content_opt_lang(f)
self._opencc(self.config['converter'], f)
if self.config['engine'].lower() == 'zhconvert':
self._opencc(self.cfg['setting']['converter'], f)
if self.cfg['setting']['engine'].lower() == 'zhconvert':
self.logger.debug('convert_text', 'engine: zhconvert 繁化姬')
for f in convert_file_list:
self.logger.debug(
'convert_text', f'now convert "{os.path.basename(f)}"')
self._content_opt_lang(f)
self._zhconvert(self.cfg['setting']['converter'], f)

def _opencc(self, converter, file):
"""opencc 轉換作業
"""opencc
Arguments:
converter {str} -- config.json 中 converter 設定,轉換模式
converter {str} -- config.ini 中 converter 設定,轉換模式
file {str} -- 欲進行文字轉換的內文文檔的絕對路徑
"""
openCC = OpenCC(converter)
f_r = open(file, 'r', encoding='utf-8').readlines()
f_encoding = encoding(file)['encoding']
start_time = time.time()
f_r = open(file, 'r', encoding=f_encoding).readlines()
with open(file + '.new', 'w', encoding='utf-8') as f_w:
for line in f_r:
converted = openCC.convert(line)
f_w.write(converted)
end_time = time.time()
self.logger.info('_opencc', f'convert file: {os.path.basename(file)} cost {"{:.2f}".format(end_time-start_time)}s')

def _zhconvert(self, converter):
""" """
def _zhconvert(self, converter, file):
"""zhconvert 繁化姬
Arguments:
converter {str} -- config.ini 中 converter 設定,轉換模式
file {str} -- 欲進行文字轉換的內文文檔的絕對路徑
"""
zhconvert = ZhConvert()
f_encoding = encoding(file)['encoding']
start_time = time.time()
with open(file, 'r', encoding=f_encoding) as f_r:
zhconvert.convert(text=f_r.read(), converter=converter)
with open(file + '.new', 'w', encoding='utf-8') as f_w:
if zhconvert.text is None:
raise ZhConvertError()
f_w.write(zhconvert.text)
end_time = time.time()
self.logger.info('_zhconvert', f'convert file: {os.path.basename(file)} cost {"{:.2f}".format(end_time-start_time)}s')

def _content_opt_lang(self, content_file_path):
"""修改 content.opf 中語言標籤的值
Expand All @@ -210,21 +239,51 @@ def _content_opt_lang(self, content_file_path):
regex = re.compile(
r"<dc:language>[\S]*</dc:language>", re.IGNORECASE)
fileline = open(content_file_path, encoding='utf-8').read()
if self.config['converter'] in converter["zh-TW"]:
if self.cfg['setting']['converter'] in converter["zh-TW"]:
self.logger.info('_content_lang', 'convert language to zh-TW')
modify = re.sub(
regex, f'<dc:language>zh-TW</dc:language>', fileline)
if self.config['converter'] in converter["zh-CN"]:
if self.cfg['setting']['converter'] in converter["zh-CN"]:
self.logger.info('_content_lang', 'convert language to zh-CN')
modify = re.sub(
regex, f'<dc:language>zh-CN</dc:language>', fileline)
open(content_file_path, 'w', encoding='utf-8').write(modify)

def _format(self):
""" """
# def _format(self, file_path):
# """ """
# modify_files = {}
# opf_tmp = []
# css_tmp = []
# content_tmp = []
# for root, _dirs, files in os.walk(f'{file_path}_files/'):
# for filename in files:
# if filename.endswith('opf'):
# opf_tmp.append(filename)
# if filename.endswith('css'):
# css_tmp.append(filename)
# if filename.endswith(('xhtml', 'html', 'htm')):
# content_tmp.append(filename)
# modify_files['opf'] = opf_tmp
# modify_files['css'] = css_tmp
# modify_files['content'] = content_tmp
# #橫式
# if self.cfg['setting']['format'].lower() == 'horizontal':
# self.logger.info('_format', 'set content to horizontal')
# if not any(modify_files['css']):
# print('css file not found')
# #直式
# if self.cfg['setting']['format'].lower() == 'straight':
# self.logger.info('_format', 'set content to straight')
# print('直式')

@property
def _clean(self):
""" """
""" 清除解壓縮後的檔案 """
if os.path.isdir( f'{self.file_path}_files'):
self.logger.info('_clean', f'delete tmp files: {self.file_path}_files')
shutil.rmtree(f'{self.file_path}_files')
else:
self.logger.error('_clean', f'path: {self.file_path}_files not found.')

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


if __name__ == "__main__":
#epub = EPubConv()
# epub.convert('H:/VSCode/Python/epubconv/1.epub')
""" zh = ZhConvert()
zh.convert() """
#epub._read_allow_setting('allow_setting.json')
EPubConvert = EPubConv()
for epub in sys.argv[1:]:
EPubConvert.convert(epub)
os.system("pause")
pass
4 changes: 4 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[setting]
engine=zhconvert
converter=Traditional
format=Horizontal
35 changes: 0 additions & 35 deletions config.json

This file was deleted.

43 changes: 43 additions & 0 deletions file_version_info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# UTF-8
#
# For more details about fixed file info 'ffi' see:
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
VSVersionInfo(
ffi=FixedFileInfo(
# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
# Set not needed items to zero 0.
filevers=(2, 0, 0, 0),
prodvers=(2, 0, 0, 0),
# Contains a bitmask that specifies the valid bits 'flags'r
mask=0x3f,
# Contains a bitmask that specifies the Boolean attributes of the file.
flags=0x0,
# The operating system for which this file was designed.
# 0x4 - NT and there is no need to change it.
OS=0x40004,
# The general type of file.
# 0x1 - the file is an application.
fileType=0x1,
# The function of the file.
# 0x0 - the function is not defined for this fileType
subtype=0x0,
# Creation date and time stamp.
date=(0, 0)
),
kids=[
StringFileInfo(
[
StringTable(
u'040904B0',
[StringStruct(u'CompanyName', u''),
StringStruct(u'FileDescription', u'EpubConv'),
StringStruct(u'FileVersion', u'2.0.0'),
StringStruct(u'InternalName', u'Epubconv'),
StringStruct(u'LegalCopyright', u''),
StringStruct(u'OriginalFilename', u'Epubconv.Exe'),
StringStruct(u'ProductName', u'Epubconv'),
StringStruct(u'ProductVersion', u'2.0.0')])
]),
VarFileInfo([VarStruct(u'Translation', [1033, 1200])])
]
)
Loading

0 comments on commit bfb7595

Please sign in to comment.