Epub - Port to Python 3
James Cameron
4 years ago
4 | 4 | |
5 | 5 | # import speech |
6 | 6 | |
7 | from cStringIO import StringIO | |
7 | from io import StringIO | |
8 | 8 | |
9 | 9 | _logger = logging.getLogger('read-activity') |
10 | 10 | |
50 | 50 | |
51 | 51 | def update_metadata(self, activity): |
52 | 52 | self.metadata = activity.metadata |
53 | self.metadata['Read_zoom'] = self.get_zoom() | |
53 | self.metadata['Read_zoom'] = str(self.get_zoom()) | |
54 | 54 | |
55 | 55 | def zoom_to_width(self): |
56 | 56 | pass |
14 | 14 | # along with this program; if not, write to the Free Software |
15 | 15 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
16 | 16 | |
17 | from epub import _Epub as Epub | |
18 | from epubview import _View as EpubView | |
19 | from jobs import _JobFind as JobFind | |
17 | from .epub import _Epub as Epub | |
18 | from .epubview import _View as EpubView | |
19 | from .jobs import _JobFind as JobFind |
21 | 21 | import shutil |
22 | 22 | import logging |
23 | 23 | |
24 | import navmap | |
25 | import epubinfo | |
24 | from . import navmap | |
25 | from . import epubinfo | |
26 | 26 | |
27 | 27 | |
28 | 28 | class _Epub(object): |
39 | 39 | self._tempdir = tempfile.mkdtemp() |
40 | 40 | |
41 | 41 | if not self._verify(): |
42 | print 'Warning: This does not seem to be a valid epub file' | |
42 | print('Warning: This does not seem to be a valid epub file') | |
43 | 43 | |
44 | 44 | self._get_opf() |
45 | 45 | self._get_ncx() |
110 | 110 | Method to crudely check to verify that what we |
111 | 111 | are dealing with is a epub file or not |
112 | 112 | ''' |
113 | if isinstance(self._file, basestring): | |
113 | if isinstance(self._file, str): | |
114 | 114 | if not os.path.exists(self._file): |
115 | 115 | return False |
116 | 116 | |
123 | 123 | mimetype = mtypefile.readline() |
124 | 124 | |
125 | 125 | # Some files seem to have trailing characters |
126 | if not mimetype.startswith('application/epub+zip'): | |
126 | if not mimetype.startswith(b'application/epub+zip'): | |
127 | 127 | return False |
128 | 128 | |
129 | 129 | return True |
22 | 22 | from gi.repository import GObject |
23 | 23 | from gi.repository import Gdk |
24 | 24 | from gi.repository import WebKit2 |
25 | import widgets | |
25 | from . import widgets | |
26 | 26 | |
27 | 27 | import logging |
28 | 28 | import os.path |
29 | 29 | import math |
30 | 30 | import shutil |
31 | 31 | |
32 | from jobs import _JobPaginator as _Paginator | |
32 | from .jobs import _JobPaginator as _Paginator | |
33 | 33 | |
34 | 34 | LOADING_HTML = ''' |
35 | 35 | <html style="height: 100%; margin: 0; padding: 0; width: 100%;"> |
483 | 483 | j = 0 |
484 | 484 | word_begin = 0 |
485 | 485 | word_end = 0 |
486 | ignore_chars = [' ', '\n', u'\r', '_', '[', '{', ']', '}', '|', | |
486 | ignore_chars = [' ', '\n', '\r', '_', '[', '{', ']', '}', '|', | |
487 | 487 | '<', '>', '*', '+', '/', '\\'] |
488 | 488 | ignore_set = set(ignore_chars) |
489 | 489 | self.word_tuples = [] |
498 | 498 | i = j |
499 | 499 | word_tuple = (word_begin, word_end, |
500 | 500 | page_text[word_begin: word_end]) |
501 | if word_tuple[2] != u'\r': | |
501 | if word_tuple[2] != '\r': | |
502 | 502 | self.word_tuples.append(word_tuple) |
503 | 503 | i = i + 1 |
504 | 504 | |
624 | 624 | else: |
625 | 625 | self._load_page(int(value)) |
626 | 626 | else: |
627 | print 'Warning: unknown scrolltype %s with value %f' \ | |
628 | % (str(scrolltype), value) | |
627 | print('Warning: unknown scrolltype %s with value %f' \ | |
628 | % (str(scrolltype), value)) | |
629 | 629 | |
630 | 630 | # FIXME: This should not be needed here |
631 | 631 | self._scrollbar.set_value(self._loaded_page) |
22 | 22 | from gi.repository import Gtk |
23 | 23 | from gi.repository import Gdk |
24 | 24 | from gi.repository import WebKit2 |
25 | import widgets | |
25 | from . import widgets | |
26 | 26 | import math |
27 | 27 | import os.path |
28 | 28 | import xml.etree.ElementTree as etree |
29 | import htmlentitydefs as html_entities | |
29 | import html.entities as html_entities | |
30 | 30 | |
31 | 31 | import threading |
32 | 32 | |
68 | 68 | |
69 | 69 | def _searchfile(self, fileobj): |
70 | 70 | parser = etree.XMLParser(html=1) |
71 | for name, codepoint in html_entities.name2codepoint.iteritems(): | |
72 | parser.entity[name] = unichr(codepoint) | |
71 | for name, codepoint in html_entities.name2codepoint.items(): | |
72 | parser.entity[name] = chr(codepoint) | |
73 | 73 | tree = etree.parse(fileobj, parser=parser) |
74 | 74 | root = tree.getroot() |
75 | 75 | |
229 | 229 | ''' |
230 | 230 | Returns the pageno which begins in filename |
231 | 231 | ''' |
232 | for key in self._pagemap.keys(): | |
232 | for key in list(self._pagemap.keys()): | |
233 | 233 | if self._pagemap[key][0].replace('file://', '') == filename: |
234 | 234 | return key |
235 | 235 |