Skip to content

Commit 1170349

Browse files
committed
feat: no-server
1 parent 8b45c7e commit 1170349

3 files changed

Lines changed: 30 additions & 28 deletions

File tree

epub_browser/library.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(self, output_dir=None):
1313
self.books = {} # 存储所有书籍信息,使用哈希作为键
1414
self.output_dir = output_dir
1515

16-
# 创建基础目录用于服务器
16+
# 创建基础目录
1717
if output_dir is not None:
1818
if os.path.exists(output_dir):
1919
try:
@@ -811,3 +811,20 @@ def create_library_home(self):
811811
library_html = minify_html.minify(library_html, minify_css=True, minify_js=True)
812812
with open(os.path.join(self.base_directory, 'index.html'), 'w', encoding='utf-8') as f:
813813
f.write(library_html)
814+
815+
def cleanup(self):
816+
"""清理所有文件"""
817+
if self.output_dir is not None:
818+
# 用户自己的目录,不要一个全删
819+
for book_hash, book_info in self.books.items():
820+
temp_dir = book_info['temp_dir']
821+
if os.path.exists(temp_dir):
822+
shutil.rmtree(temp_dir)
823+
print(f"Cleaned up book: {book_info['title']}, path: {temp_dir}")
824+
os.remove(os.path.join(self.output_dir, "index.html"))
825+
return
826+
else:
827+
# 清理基础目录
828+
if os.path.exists(self.base_directory):
829+
shutil.rmtree(self.base_directory)
830+
print(f"Cleaned up library base directory: {self.base_directory}")

epub_browser/main.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def main():
2121
parser.add_argument('--output-dir', '-o', help='Output directory for converted books')
2222
parser.add_argument('--keep-files', action='store_true', help='Keep converted files after server stops')
2323
parser.add_argument('--log', action='store_true', help='Enable log messages')
24+
parser.add_argument('--no-server', action='store_true', help='Do not start a server, just generate files')
2425

2526
args = parser.parse_args()
2627

@@ -32,15 +33,14 @@ def main():
3233

3334
# 创建图书馆
3435
library = EPUBLibrary(args.output_dir)
35-
server_instance = EPUBServer(library, args.log)
3636

3737
# 添加所有书籍
3838
success_count = 0
39-
# 收集真实的 epub file
39+
# 收集所有的 epub file,可能传递了路径需要下钻
4040
real_epub_files = []
4141
for filename in args.filename:
42-
files = library.epub_file_discover(filename)
43-
real_epub_files.extend(files)
42+
cur_files = library.epub_file_discover(filename)
43+
real_epub_files.extend(cur_files)
4444
# 构建图书馆
4545
for filename in tqdm(real_epub_files):
4646
if library.add_book(filename):
@@ -50,7 +50,12 @@ def main():
5050
sys.exit(1)
5151

5252
library.create_library_home()
53-
53+
54+
# 创建服务器
55+
if args.no_server:
56+
print(f"Files generated in: {library.base_directory}")
57+
return
58+
server_instance = EPUBServer(library, args.log)
5459
try:
5560
server_instance.start_server(
5661
port=args.port,
@@ -63,7 +68,7 @@ def main():
6368
finally:
6469
server_instance.stop_server()
6570
if not args.keep_files:
66-
server_instance.cleanup()
71+
library.cleanup()
6772

6873

6974
if __name__ == '__main__':

epub_browser/server.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -314,24 +314,4 @@ def stop_server(self):
314314

315315
def is_running(self):
316316
"""检查服务器是否正在运行"""
317-
return self._is_running
318-
319-
def cleanup(self):
320-
"""清理所有临时文件"""
321-
# 先停止服务器
322-
if self.is_running():
323-
self.stop_server()
324-
if self.library.output_dir is not None:
325-
# 用户自己的目录,不要一个全删
326-
for book_hash, book_info in self.library.books.items():
327-
temp_dir = book_info['temp_dir']
328-
if os.path.exists(temp_dir):
329-
shutil.rmtree(temp_dir)
330-
print(f"Cleaned up book: {book_info['title']}, path: {temp_dir}")
331-
os.remove(os.path.join(self.library.output_dir, "index.html"))
332-
return
333-
else:
334-
# 清理基础目录
335-
if os.path.exists(self.library.base_directory):
336-
shutil.rmtree(self.library.base_directory)
337-
print(f"Cleaned up library base directory: {self.library.base_directory}")
317+
return self._is_running

0 commit comments

Comments
 (0)