Skip to content

Commit bd00d68

Browse files
committed
Update editor
1 parent 5fd1195 commit bd00d68

18 files changed

+460
-192
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@ Thumbs.db
8585
/tools/*.txt
8686
/installer/python/*
8787
/test/*/*
88+
/test/°*.txt

CentralBlockTable.py

Lines changed: 101 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,78 @@
11
import os, sys, traceback, json, subprocess
2+
import concurrent.futures
3+
import PyQt5.QtCore
4+
import PyQt5.QtGui
5+
import PyQt5.QtWidgets
26
from common.files import *
37
from common.common import *
48
import SortingBlockTree
59
import settings
610
import InfoPanel
711

12+
executor = concurrent.futures.ThreadPoolExecutor(max_workers=50)
13+
executor_dir = ''
14+
executor_file = ''
15+
16+
17+
def wait_on_editor():
18+
global executor_dir, executor_file
19+
args = list()
20+
exe = executor_dir + '/editor.exe'.replace('/', os.sep)
21+
if os.path.isfile(exe):
22+
if os.name == 'nt':
23+
args.append('start')
24+
args.append(executor_dir + '/editor.exe'.replace('/', os.sep))
25+
args.append(executor_file.replace('/', os.sep))
26+
# args.append('debug')
27+
else:
28+
if os.name == 'nt':
29+
args.append('start')
30+
args.append('python')
31+
args.append(executor_dir + '/editor/editor.py'.replace('/', os.sep))
32+
args.append(executor_file.replace('/', os.sep))
33+
args.append('debug')
34+
35+
try:
36+
return_code = subprocess.call(args, shell=True)
37+
except Exception:
38+
traceback.print_exc()
39+
40+
41+
def wait_on_reader():
42+
global executor_dir, executor_file
43+
args = list()
44+
exe = executor_dir + '/reader.exe'.replace('/', os.sep)
45+
if os.path.isfile(exe):
46+
if os.name == 'nt':
47+
args.append('start')
48+
args.append(executor_dir + '/reader.exe'.replace('/', os.sep))
49+
args.append(executor_file.replace('/', os.sep))
50+
# args.append('debug')
51+
else:
52+
# if os.name == 'nt':
53+
# args.append('start')
54+
args.append('python')
55+
args.append(executor_dir + '/reader/reader.py'.replace('/', os.sep))
56+
args.append(executor_file.replace('/', os.sep))
57+
args.append('debug')
58+
59+
try:
60+
return_code = subprocess.call(args, shell=True)
61+
except Exception:
62+
traceback.print_exc()
63+
64+
65+
def wait_on_open_ext():
66+
global executor_file
67+
args = list()
68+
args.append('start')
69+
args.append(executor_file.replace('/', os.sep))
70+
71+
try:
72+
return_code = subprocess.call(args, shell=True)
73+
except Exception:
74+
traceback.print_exc()
75+
876

977
class HomeWindowCentralBlock(InfoPanel.HomeWindowInfoPanel):
1078
central_block_table_cases_uid_list = []
@@ -24,6 +92,8 @@ def central_block_table_define_slots(self):
2492
:return:
2593
"""
2694
self.central_block_table.currentCellChanged.connect(self.central_block_table_new_selection)
95+
self.central_block_table.setContextMenuPolicy(PyQt5.QtCore.Qt.CustomContextMenu)
96+
self.central_block_table.customContextMenuRequested.connect(self.central_block_table_context_menu)
2797
self.central_block_table.itemChanged.connect(self.central_block_table_item_changed)
2898
self.central_block_table.cellDoubleClicked.connect(self.central_block_table_cell_double_clicked)
2999
self.central_block_table.horizontalHeader().sortIndicatorChanged.connect(self.central_block_table_sort_indicator_changed)
@@ -144,32 +214,22 @@ def central_block_table_cell_double_clicked(self, current_row, current_column):
144214
:param current_column:
145215
:return: void
146216
"""
217+
global executor_dir, executor_file
147218
# print("--------------------------------")
148219
# print("central_block_table_cell_double_clicked")
149220
item = self.central_block_table.item(current_row, current_column)
150221
guid_book = item.data(99)
151222
# print("Book GUID : {}".format(guid_book))
152223
if self.currentBook != guid_book:
153224
self.currentBook = guid_book
154-
args = list()
155-
if self.BDD.get_books(guid_book)[0]['files'][0]['format'] in ['CBZ', 'CBR', 'EPUB']:
156-
exe = self.app_directory + '/reader.exe'.replace('/', os.sep)
157-
if os.path.isfile(exe):
158-
args.append(self.app_directory + '/reader.exe'.replace('/', os.sep))
159-
args.append(self.BDD.get_books(guid_book)[0]['files'][0]['link'].replace('/', os.sep))
160-
args.append('debug')
161-
else:
162-
args.append('python')
163-
args.append(self.app_directory + '/reader/reader.py'.replace('/', os.sep))
164-
args.append(self.BDD.get_books(guid_book)[0]['files'][0]['link'].replace('/', os.sep))
165-
args.append('debug')
225+
files = self.BDD.get_books(guid_book)[0]['files']
226+
if files[0]['format'] in ['CBZ', 'CBR', 'EPUB']:
227+
executor_dir = self.app_directory
228+
executor_file = files[0]['link']
229+
executor.submit(wait_on_reader)
166230
else:
167-
args.append(self.BDD.get_books(guid_book)[0]['files'][0]['link'])
168-
print(args)
169-
try:
170-
return_code = subprocess.call(args, shell=True)
171-
except Exception:
172-
traceback.print_exc()
231+
executor_file = files[0]['link']
232+
executor.submit(wait_on_open_ext)
173233

174234
def central_block_table_item_changed(self, new_item):
175235
"""
@@ -218,6 +278,29 @@ def central_block_table_item_changed(self, new_item):
218278
except Exception:
219279
traceback.print_exc()
220280

281+
def central_block_table_context_menu(self, point: PyQt5.QtCore.QPoint):
282+
global executor_dir, executor_file
283+
try:
284+
current_row = self.central_block_table.currentRow()
285+
current_column = self.central_block_table.currentColumn()
286+
guid_book = self.central_block_table.item(current_row, current_column).data(99)
287+
book_infos = self.BDD.get_books(guid_book)[0]
288+
menu = PyQt5.QtWidgets.QMenu()
289+
action0 = PyQt5.QtWidgets.QAction(self.lang['Library/CentralBlockTableContextMenu/EditMetadata'], None)
290+
action0.triggered.connect(lambda: print("Edit Metadata!"))
291+
menu.addAction(action0)
292+
for file in book_infos['files']:
293+
if file['format'] == 'EPUB':
294+
executor_dir = self.app_directory
295+
executor_file = file['link']
296+
action1 = PyQt5.QtWidgets.QAction(self.lang['Library/CentralBlockTableContextMenu/EditBook'], None)
297+
action1.triggered.connect(lambda: executor.submit(wait_on_editor))
298+
menu.addAction(action1)
299+
menu.exec(PyQt5.QtGui.QCursor.pos())
300+
301+
except Exception:
302+
traceback.print_exc()
303+
221304
@staticmethod
222305
def new_book_table_item(guid: str, book_type: str, value: str, editable: bool = True, alt: any = None,
223306
alt_type: str = None, locked: bool = False):

common/archive.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import os, sys
22
import subprocess
3-
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
4-
from common.vars import *
3+
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
4+
import vars
55

66

77
def inflate(src: str, dest: str):
88
list_args = list() # create list argument for external command execution
9-
list_args.append(env_vars['tools']['archiver']['path'] + os.sep + env_vars['tools']['archiver'][os.name]['exe']) # insert executable path
10-
temp_args = env_vars['tools']['archiver'][os.name]['params_inflate'].split(' ') # create table of raw command arguments
9+
list_args.append(vars.load_path_archiver() + os.sep + vars.env_vars['tools']['archiver'][os.name]['exe']) # insert executable path
10+
temp_args = vars.env_vars['tools']['archiver'][os.name]['params_inflate'].split(' ') # create table of raw command arguments
1111
for var in temp_args: # parse table of raw command arguments
1212
# insert parsed param
1313
list_args.append(var.replace('%input%', src).replace('%output%', dest))
@@ -17,8 +17,8 @@ def inflate(src: str, dest: str):
1717

1818
def deflate(src: str, dest: str):
1919
list_args = list() # create list argument for external command execution
20-
list_args.append(env_vars['tools']['archiver']['path'] + os.sep + env_vars['tools']['archiver'][os.name]['exe']) # insert executable path
21-
temp_args = env_vars['tools']['archiver'][os.name]['params_deflate'].split(' ') # create table of raw command arguments
20+
list_args.append(vars.load_path_archiver() + os.sep + vars.env_vars['tools']['archiver'][os.name]['exe']) # insert executable path
21+
temp_args = vars.env_vars['tools']['archiver'][os.name]['params_deflate'].split(' ') # create table of raw command arguments
2222
for var in temp_args: # parse table of raw command arguments
2323
# insert parsed param
2424
list_args.append(var.replace('%input%', src).replace('%output%', dest))

common/books.py

Lines changed: 91 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ def get_epub_info(path: str):
323323
i+=1
324324
base += '/'
325325
myfile = myzip.open(file2)
326-
mydoc = minidom.parseString(myfile.read())
326+
metadata_file_content = myfile.read()
327+
mydoc = minidom.parseString(metadata_file_content)
327328

328329
try: ret['guid'] = mydoc.getElementsByTagName('dc:identifier')[0].firstChild.data
329330
except Exception: {}
@@ -346,39 +347,40 @@ def get_epub_info(path: str):
346347
if meta.attributes['name'].value == 'dc:series': ret['series'] = meta.attributes['content'].value
347348

348349
items = mydoc.getElementsByTagName('item')
349-
if mydoc.getElementsByTagName('spine')[0].hasAttribute('toc'):
350-
spine = mydoc.getElementsByTagName('spine')[0].attributes['toc'].value
351-
for itm in items:
352-
if itm.attributes['id'].value == spine:
353-
ret['toc'] = itm.attributes['href'].value
354-
myfile = myzip.open(base + ret['toc'])
355-
mydoc = minidom.parseString(myfile.read())
356-
itemrefs = mydoc.getElementsByTagName('navPoint')
357-
for ref in itemrefs:
358-
id = ref.attributes['id'].value
359-
ret['chapters'].append({
360-
'id': ref.attributes['id'].value,
361-
'name': ref.getElementsByTagName('text')[0].firstChild.data,
362-
'src': base + ref.getElementsByTagName('content')[0].attributes['src'].value
363-
})
364-
myfile.close()
365-
else:
366-
refs_list = mydoc.getElementsByTagName('spine')[0].getElementsByTagName('itemref')
367-
for itemref in refs_list:
368-
idref = itemref.attributes['idref'].value
369-
for itm in items:
370-
if itm.attributes['id'].value == idref:
371-
try:
372-
chapter_file = myzip.open(base + itm.attributes['href'].value, "r")
373-
content = chapter_file.read().decode("utf8")
374-
title = re.search("<title>(.*)</title>", content)[1]
375-
ret['chapters'].append({
376-
'id': idref,
377-
'name': title,
378-
'src': base + itm.attributes['href'].value
379-
})
380-
except Exception:
381-
traceback.print_exc()
350+
ret['chapters'] = parse_content_table(metadata_file_content, base, myzip)
351+
# if mydoc.getElementsByTagName('spine')[0].hasAttribute('toc'):
352+
# spine = mydoc.getElementsByTagName('spine')[0].attributes['toc'].value
353+
# for itm in items:
354+
# if itm.attributes['id'].value == spine:
355+
# ret['toc'] = itm.attributes['href'].value
356+
# myfile = myzip.open(base + ret['toc'])
357+
# mydoc = minidom.parseString(myfile.read())
358+
# itemrefs = mydoc.getElementsByTagName('navPoint')
359+
# for ref in itemrefs:
360+
# id = ref.attributes['id'].value
361+
# ret['chapters'].append({
362+
# 'id': ref.attributes['id'].value,
363+
# 'name': ref.getElementsByTagName('text')[0].firstChild.data,
364+
# 'src': base + ref.getElementsByTagName('content')[0].attributes['src'].value
365+
# })
366+
# myfile.close()
367+
# else:
368+
# refs_list = mydoc.getElementsByTagName('spine')[0].getElementsByTagName('itemref')
369+
# for itemref in refs_list:
370+
# idref = itemref.attributes['idref'].value
371+
# for itm in items:
372+
# if itm.attributes['id'].value == idref:
373+
# try:
374+
# chapter_file = myzip.open(base + itm.attributes['href'].value, "r")
375+
# content = chapter_file.read().decode("utf8")
376+
# title = re.search("<title>(.*)</title>", content)[1]
377+
# ret['chapters'].append({
378+
# 'id': idref,
379+
# 'name': title,
380+
# 'src': base + itm.attributes['href'].value
381+
# })
382+
# except Exception:
383+
# traceback.print_exc()
382384

383385
for itm in items:
384386
if cov_id != '':
@@ -409,6 +411,61 @@ def get_epub_info(path: str):
409411
return None
410412

411413

414+
def parse_content_table(metadata_file_content: str, base: str, folder: str or zipfile.ZipFile) -> list:
415+
mydoc = minidom.parseString(metadata_file_content)
416+
items = mydoc.getElementsByTagName('item')
417+
toc_file = ''
418+
ret_list = []
419+
420+
if mydoc.getElementsByTagName('spine')[0].hasAttribute('toc'):
421+
spine = mydoc.getElementsByTagName('spine')[0].attributes['toc'].value
422+
for itm in items:
423+
if itm.attributes['id'].value == spine:
424+
toc_file = itm.attributes['href'].value
425+
myfile = None
426+
if isinstance(folder, zipfile.ZipFile):
427+
myfile = folder.open(base + toc_file)
428+
elif isinstance(folder, str):
429+
myfile = open(folder + os.sep + base + toc_file)
430+
mydoc = minidom.parseString(myfile.read())
431+
myfile.close()
432+
itemrefs = mydoc.getElementsByTagName('navPoint')
433+
for ref in itemrefs:
434+
id = ref.attributes['id'].value
435+
ret_list.append({
436+
'id': ref.attributes['id'].value,
437+
'name': ref.getElementsByTagName('text')[0].firstChild.data,
438+
'src': base + ref.getElementsByTagName('content')[0].attributes['src'].value
439+
})
440+
else:
441+
refs_list = mydoc.getElementsByTagName('spine')[0].getElementsByTagName('itemref')
442+
for itemref in refs_list:
443+
idref = itemref.attributes['idref'].value
444+
for itm in items:
445+
if itm.attributes['id'].value == idref:
446+
try:
447+
chapter_file = None
448+
content = ''
449+
if isinstance(folder, zipfile.ZipFile):
450+
chapter_file = folder.open(base + itm.attributes['href'].value, "r")
451+
content = chapter_file.read().decode("utf8")
452+
elif isinstance(folder, str):
453+
chapter_file = open(
454+
folder + base + itm.attributes['href'].value.replace('/', os.sep),
455+
encoding="utf8"
456+
)
457+
content = chapter_file.read()
458+
title = re.search("<title>(.*)</title>", content)[1]
459+
ret_list.append({
460+
'id': idref,
461+
'name': title,
462+
'src': base + itm.attributes['href'].value
463+
})
464+
except Exception:
465+
traceback.print_exc()
466+
return ret_list
467+
468+
412469
def insert_book(database: bdd.BDD, file_name_template: str, file_name_separator: str, file: str):
413470
file = file.replace('/', os.sep)
414471
if os.path.isfile(file) is True:

0 commit comments

Comments
 (0)