Skip to content
This repository has been archived by the owner on Mar 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #30 from rupor-github/tageditor
Browse files Browse the repository at this point in the history
Tag editor - write fb2.zip algorithm changed
  • Loading branch information
dnkorpushov authored Sep 21, 2017
2 parents 058d099 + f269ba3 commit cf56a0e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
8 changes: 4 additions & 4 deletions fb2mobi-gui.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
<GooglePassword/>
<KindleMail/>
<bookInfoVisible>True</bookInfoVisible>
<bookInfoSplitterState>516, 413</bookInfoSplitterState>
<bookInfoSplitterState>630, 299</bookInfoSplitterState>
<columns>
<c0>100</c0>
<c1>100</c1>
<c2>314</c2>
<c2>428</c2>
</columns>
<geometry>
<x>314</x>
<y>257</y>
<x>-1286</x>
<y>68</y>
<width>952</width>
<height>503</height>
</geometry>
Expand Down
38 changes: 29 additions & 9 deletions ui/fb2meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import os
import zipfile
import codecs
import shutil
import tempfile


from io import StringIO, BytesIO
from lxml import etree
Expand Down Expand Up @@ -62,6 +65,7 @@ def __init__(self, file):
self.coverdata = None
self.coverpage_href = ''
self.is_zip = False
self.encoding = ''

if os.path.splitext(self.file)[1].lower() == '.zip':
self.is_zip = True
Expand All @@ -76,11 +80,13 @@ def __init__(self, file):
f.close()

self.tree = etree.parse(BytesIO(content), parser=etree.XMLParser(recover=True))
self.encoding = self.tree.docinfo.encoding
else:
# TODO: архиве несколько файлов. Ошибка
pass
else:
self.tree = etree.parse(self.file, parser=etree.XMLParser(recover=True))
self.encoding = self.tree.docinfo.encoding

def get_first_series(self):
series_name = ''
Expand Down Expand Up @@ -258,16 +264,30 @@ def write(self):
indent(self.tree.getroot())

if self.is_zip:
zip_file = zipfile.ZipFile(self.file, 'w', zipfile.ZIP_DEFLATED)
zipped_file_name = os.path.splitext(os.path.split(self.file)[1])[0]
if not zipped_file_name.lower().endswith('.fb2'):
zipped_file_name += '.fb2'

zip_file.writestr(zipped_file_name, etree.tostring(self.tree, encoding='utf-8',
method='xml', xml_declaration=True))
zip_file.close()
zipped_file_name = ''
temp_dir = tempfile.mkdtemp()
try:
zip_file = zipfile.ZipFile(self.file, 'r', zipfile.ZIP_DEFLATED)
zip_file.extractall(temp_dir)
zip_file.close()

file_list = os.listdir(temp_dir)
if len(file_list) == 1:
for file in file_list:
if file.lower().endswith('.fb2'):
zipped_file_name = file
break
shutil.rmtree(temp_dir)
except:
shutil.rmtree(temp_dir)

if zipped_file_name:
zip_file = zipfile.ZipFile(self.file, 'w', zipfile.ZIP_DEFLATED)
zip_file.writestr(zipped_file_name, etree.tostring(self.tree, encoding=self.encoding,
method='xml', xml_declaration=True))
zip_file.close()
else:
self.tree.write(self.file, encoding='utf-8', method='xml', xml_declaration=True, pretty_print=False)
self.tree.write(self.file, encoding=self.encoding, method='xml', xml_declaration=True, pretty_print=False)

if __name__ == '__main__':
# meta = Fb2Meta('Судья Ди 01. Золото Будды.fb2.zip')
Expand Down
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

WINDOWS = platform.system().lower() == "windows"

VERSION = u'3.6.36'
VERSION = u'3.6.37'

0 comments on commit cf56a0e

Please sign in to comment.