From 6d8e7767e541ff6c8706d6e8153145cbd5f9252a Mon Sep 17 00:00:00 2001
From: Dnomd343
Date: Thu, 16 Mar 2023 10:56:15 +0800
Subject: [PATCH] refactor: release module structure
---
src/release/calibre_html.py | 30 -----------------------
src/release/common.py | 45 ----------------------------------
src/release/demo.py | 23 ++++++++++++++++++
src/release/json_dumps.py | 23 ------------------
src/release/txt_release.py | 30 -----------------------
src/release/utils/__init__.py | 10 ++++++++
src/release/utils/common.py | 46 +++++++++++++++++++++++++++++++++++
src/release/utils/sequence.py | 43 ++++++++++++++++++++++++++++++++
8 files changed, 122 insertions(+), 128 deletions(-)
delete mode 100755 src/release/calibre_html.py
delete mode 100644 src/release/common.py
create mode 100644 src/release/demo.py
delete mode 100755 src/release/json_dumps.py
delete mode 100755 src/release/txt_release.py
create mode 100644 src/release/utils/__init__.py
create mode 100644 src/release/utils/common.py
create mode 100644 src/release/utils/sequence.py
diff --git a/src/release/calibre_html.py b/src/release/calibre_html.py
deleted file mode 100755
index 04b4b5b..0000000
--- a/src/release/calibre_html.py
+++ /dev/null
@@ -1,30 +0,0 @@
-#!/usr/bin/env python3
-# -*- coding: utf-8 -*-
-
-import sys
-from common import loadData
-from common import saveFile
-from common import releaseInfo
-
-
-def formatMetadata(metadata: dict) -> str:
- return '%s
\n' % metadata['name'] + '\n'.join(
- ['
%s
' % x for x in metadata['desc']] - ) - - -def htmlRelease(metadata: dict, content: dict) -> str: - result = [formatMetadata(metadata)] - for (title, chapter) in content.items(): - result.append( - '%s
' % x for x in chapter]) - ) - return '\n\n'.join(result) + '\n' - - -if __name__ == '__main__': - data = loadData(sys.argv[1]) - saveFile( - releaseInfo['htmlFile'], - htmlRelease(data['metadata'], data['content']) - ) diff --git a/src/release/common.py b/src/release/common.py deleted file mode 100644 index e126593..0000000 --- a/src/release/common.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -import os -import sys -import json - -rootPath = os.path.join( - os.path.dirname(os.path.realpath(__file__)), '../../' -) -dataPath = os.path.join(rootPath, './release/') -releasePath = os.path.join(dataPath, './output/') -metadataFile = os.path.join(rootPath, './assets/metadata.json') - -releaseInfo = { - 'htmlFile': 'xxrs.html', - 'jsonFile': 'XXRS.json', - 'txtFile': '栩栩若生.txt', - 'gitbookDir': 'xxrs-online', -} -releaseInfo = {x: os.path.join(releasePath, y) for (x, y) in releaseInfo.items()} - - -def loadData(jsonName: str) -> dict: # load book data from json file - content = json.loads(open( - os.path.join(dataPath, '%s.json' % jsonName) - ).read()) - metadata = json.loads(open(metadataFile).read()) - return { - 'metadata': metadata, - 'content': content, - } - - -def saveFile(fileName: str, content: str) -> None: # save into file - with open(fileName, 'w') as fileObj: - fileObj.write(content) - - -def createFolder(folderName: str) -> None: # create folder - if not os.path.exists(folderName): - os.mkdir(folderName) - - -createFolder(releasePath) diff --git a/src/release/demo.py b/src/release/demo.py new file mode 100644 index 0000000..d62388a --- /dev/null +++ b/src/release/demo.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from utils import loadBook +from utils import saveFile +from utils import txtRelease +from utils import jsonRelease +from utils import htmlRelease +from utils import releaseInfo + +metadata, content = loadBook('rc-5') + +saveFile( + releaseInfo['txt'], txtRelease(metadata, content) +) + +saveFile( + releaseInfo['json'], jsonRelease(metadata, content) +) + +saveFile( + releaseInfo['calibre'], htmlRelease(metadata, content) +) diff --git a/src/release/json_dumps.py b/src/release/json_dumps.py deleted file mode 100755 index c5fec65..0000000 --- a/src/release/json_dumps.py +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -import sys -import json -from common import loadData -from common import saveFile -from common import releaseInfo - - -def jsonRelease(metadata: dict, content: dict) -> str: - return json.dumps({ - 'metadata': metadata, - 'content': content, - }, separators = (',', ':')) # without space - - -if __name__ == '__main__': - data = loadData(sys.argv[1]) - saveFile( - releaseInfo['jsonFile'], - jsonRelease(data['metadata'], data['content']) - ) diff --git a/src/release/txt_release.py b/src/release/txt_release.py deleted file mode 100755 index 0b1c67c..0000000 --- a/src/release/txt_release.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -import sys -from common import loadData -from common import saveFile -from common import releaseInfo - - -def formatMetadata(metadata: dict) -> str: - return '%s\n\n作者:%s\n\n\n%s' % ( - metadata['name'], - metadata['author'], - '\n\n'.join(metadata['desc']), - ) - - -def txtRelease(metadata: dict, content: dict) -> str: - result = [formatMetadata(metadata)] - for (title, chapter) in content.items(): - result.append('\n\n'.join([title] + chapter)) - return '\n\n\n'.join(result) + '\n' - - -if __name__ == '__main__': - data = loadData(sys.argv[1]) - saveFile( - releaseInfo['txtFile'], - txtRelease(data['metadata'], data['content']) - ) diff --git a/src/release/utils/__init__.py b/src/release/utils/__init__.py new file mode 100644 index 0000000..c3016ba --- /dev/null +++ b/src/release/utils/__init__.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from .common import loadBook +from .common import saveFile +from .common import releaseInfo + +from .sequence import txtRelease +from .sequence import jsonRelease +from .sequence import htmlRelease diff --git a/src/release/utils/common.py b/src/release/utils/common.py new file mode 100644 index 0000000..a8aafbe --- /dev/null +++ b/src/release/utils/common.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import os +import json + +rootPath = os.path.join( # project root directory + os.path.dirname(os.path.realpath(__file__)), '../../../' +) +dataPath = os.path.join(rootPath, './release/') +releasePath = os.path.join(dataPath, './output/') +metadataFile = os.path.join(rootPath, './assets/metadata.json') + +releaseInfo = { # release file name + 'json': 'XXRS.json', + 'txt': '栩栩若生.txt', + 'epub': '栩栩若生.epub', + 'mobi': '栩栩若生.mobi', + 'static': 'XXRS.tar.xz', + 'calibre': 'xxrs.html', + 'gitbook': 'xxrs-online/', +} +releaseInfo = { # convert to absolute path + key: os.path.join(releasePath, file) for (key, file) in releaseInfo.items() +} + + +def createFolder(folderName: str) -> None: # create folder + if not os.path.exists(folderName): + os.mkdir(folderName) + + +def saveFile(fileName: str, content: str) -> None: # save into file + with open(fileName, 'w') as fileObj: + fileObj.write(content) + + +def loadBook(jsonName: str) -> tuple[dict, dict]: # load book data from json file + content = json.loads(open( + os.path.join(dataPath, '%s.json' % jsonName) # book content + ).read()) + metadata = json.loads(open(metadataFile).read()) # book metadata + return metadata, content + + +createFolder(releasePath) # create output folder diff --git a/src/release/utils/sequence.py b/src/release/utils/sequence.py new file mode 100644 index 0000000..087ddea --- /dev/null +++ b/src/release/utils/sequence.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import json + + +def jsonRelease(metadata: dict, content: dict) -> str: + return json.dumps({ + 'metadata': metadata, + 'content': content, + }, separators = (',', ':')) # without space + + +def txtMetadata(metadata: dict) -> str: # txt metadata + return '%s\n\n作者:%s\n\n\n%s' % ( + metadata['name'], + metadata['author'], + '\n\n'.join(metadata['desc']), + ) + + +def txtRelease(metadata: dict, content: dict) -> str: + result = [txtMetadata(metadata)] + for (title, chapter) in content.items(): + result.append('\n\n'.join([title] + chapter)) # combine txt content + return '\n\n\n'.join(result) + '\n' + + +def htmlMetadata(metadata: dict) -> str: # html metadata + return '%s
' % x for x in metadata['desc']] + ) + + +def htmlRelease(metadata: dict, content: dict) -> str: + result = [htmlMetadata(metadata)] + for (title, chapter) in content.items(): + result.append( + '%s
' % x for x in chapter] + ) + ) + return '\n\n'.join(result) + '\n'