def _open_arg_file(sender, arg_file): if os.path.isfile(arg_file): log.info(f"The application was invoked with a file: {arg_file}") uri = DocumentUri.from_filename(arg_file) wx.GetApp().mainFrame.open_uri(uri) elif DocumentUri.is_bookworm_uri(arg_file): uri = DocumentUri.from_uri_string(arg_file) log.info(f"The application was invoked with a uri: {uri}") wx.GetApp().mainFrame.open_uri(uri) else: try: uri = DocumentUri.from_base64_encoded_string(arg_file) except: log.exception(f"Invalid or unknown document uri: {arg_file}") else: wx.GetApp().mainFrame.open_uri(uri)
def test_epub_document_section_at_text_position(asset): uri = DocumentUri.from_filename(asset("epub30-spec.epub")) epub = create_document(uri) position_to_section_title = { 247743: "1.1. Purpose and Scope", 370161: "3.1.1. HTML5", 127838: "4.3.2. Metadata ", 242323: "B.4.1.2. Description", 17556: "Terminology", 34564: "2.6. Rendering and CSS", 349355: "Acknowledgements and Contributors", 363566: "EPUB 3 Changes from EPUB 2.0.1", 371108: "3.1.5. Content Switching", 135534: "4.3.2. Metadata ", 130440: "4.3.2. Metadata ", 60425: "2.2. Reading System Conformance", 49786: "4.6. Scripting", 278229: "3.5.2. Media Overlays Metadata Vocabulary", 63656: "3.4.1. The ", 380720: "4.1.4. Filesystem Container", 173840: "2.1.3.1.3. Vocabulary Association", 25363: "1.2. Roadmap", 114545: "4.2.2. Default Vocabulary", 9227: "EPUB 3 Specifications - Table of Contents", } for (text_position, section_title) in position_to_section_title.items(): section = epub.get_section_at_position(text_position) assert section.title == section_title
def read(self): mobi_file_path = self.get_file_system_path() unpacked_file_path = self.unpack_mobi(mobi_file_path) raise ChangeDocument( old_uri=self.uri, new_uri=DocumentUri.from_filename(unpacked_file_path), reason="Unpacked the mobi file to epub", )
def read(self): odf_file_path = self.get_file_system_path() parser = OdfParser(odf_file_path) converted_file = parser.get_converted_filename() raise ChangeDocument( old_uri=self.uri, new_uri=DocumentUri.from_filename(converted_file), reason="Docx converted to html", )
def read(self): docx_file_path = self.get_file_system_path() converted_file = process_worker.submit(self.get_converted_filename, docx_file_path).result() raise ChangeDocument( old_uri=self.uri, new_uri=DocumentUri.from_filename(converted_file), reason="Docx converted to html", )
def test_uri_from_filename(asset): filename = Path(asset("The Diary of a Nobody.epub")).resolve() uri = DocumentUri.from_filename(str(filename)) assert uri.format == 'epub' assert Path(uri.path).resolve() == filename assert uri.openner_args == {} as_str = uri.to_uri_string() parsed_uri = uri.from_uri_string(as_str) assert uri.path == parsed_uri.path assert uri == parsed_uri
def test_serde_toc_tree(asset): uri = DocumentUri.from_filename(asset("epub30-spec.epub")) epub_document = create_document(uri) constructed = load_toc_tree(dump_toc_tree(epub_document.toc_tree)) assert len(epub_document.toc_tree) == len(constructed) compare_pairs = zip(constructed.iter_children(), epub_document.toc_tree.iter_children()) assert all(t.title == s.title for (t, s) in compare_pairs)
def _do_add_files_to_bookshelf(self, filenames, category_name, tags_names, should_add_to_fts): for filename in filenames: add_document_to_bookshelf( DocumentUri.from_filename(filename), category_name=category_name, tags_names=tags_names, should_add_to_fts=should_add_to_fts, database_file=DEFAULT_BOOKSHELF_DATABASE_FILE, )
def test_wire_serde(asset, library): uri = DocumentUri.from_filename(asset("epub30-spec.epub")) epub_document = create_document(uri) serialized = library.dumps(dump_toc_tree(epub_document.toc_tree)) deserialized = library.loads(serialized) constructed = load_toc_tree(deserialized) compare_pairs = zip(constructed.iter_children(), epub_document.toc_tree.iter_children()) assert all(t.title == s.title for (t, s) in compare_pairs)
def read(self): self.filename = self.get_file_system_path() rendered_md_path = home_data_path("rendered_markdown") rendered_md_path.mkdir(parents=True, exist_ok=True) filehash = generate_file_md5(self.filename) target_file = rendered_md_path / f"{filehash}.html" html_content = markdown(self.filename.read_text(), escape=False) target_file.write_text(html_content) raise ChangeDocument( old_uri=self.uri, new_uri=DocumentUri.from_filename(target_file), reason="Unpacked the mobi file to epub", )
def _import_document(category_name, should_add_to_fts, filename): try: uri = DocumentUri.from_filename(filename) with contextlib.closing(create_document(uri)) as document: add_document_to_bookshelf( document, category_name, tags_names=(), should_add_to_fts=should_add_to_fts, database_file=DEFAULT_BOOKSHELF_DATABASE_FILE, ) except: return
def __init__(self, uri): if isinstance(uri, str): try: self.uri = DocumentUri.from_uri_string(uri) except ValueError as e: raise ReaderError( f"Failed to parse document uri {self.uri}") from e else: self.uri = uri doc_format_info = get_document_format_info() if (doc_format := self.uri.format) not in doc_format_info: raise UnsupportedDocumentError( f"Could not open document from uri {self.uri}. The format is not supported." )
class UrlOpenService(BookwormService): name = "url_open" config_spec = {} has_gui = True def __post_init__(self): self._cancel_query = threading.Event() def process_menubar(self, menubar): webservices_menu = (wx.GetApp().service_handler.get_service( "webservices").web_sservices_menu) open_url = webservices_menu.Insert(0, -1, _("&Open URL\tCtrl+U")) self.open_url_from_clipboard = webservices_menu.Insert( 1, -1, _("&Open URL From Clipboard\tCtrl+Shift+U")) self.view.Bind(wx.EVT_MENU, self.onOpenUrl, open_url) self.view.Bind(wx.EVT_MENU, self.onOpenUrlFromClipboard, self.open_url_from_clipboard) def get_keyboard_shortcuts(self): return {self.open_url_from_clipboard.GetId(): "Ctrl-Shift-U"} def onOpenUrl(self, event): url = self.view.get_text_from_user( # Translators: title of a dialog for entering a URL _("Enter URL"), # Translators: label of a textbox in a dialog for entering a URL _("URL"), ) self.open_url_in_bookworm(url) def onOpenUrlFromClipboard(self, event): try: url = get_clipboard_text() return self.open_url_in_bookworm(url) except: log.exception("Failed to get url from clipboard.", exc_info=True) def open_url_in_bookworm(self, url: str): if not (url := url.strip()): wx.Bell() return canonical_url = url_normalize(url) uri = DocumentUri(format="webpage", path=canonical_url, openner_args={}) self.view.open_uri(uri)
def add_to_bookshelf_view(): data = request.json doc_uri = data["document_uri"] try: document = create_document(DocumentUri.from_uri_string(doc_uri)) except: log.exception(f"Failed to open document: {doc_uri}", exc_info=True) abort(400, f"Failed to open document: {doc_uri}") else: if document.__internal__: abort(400, f"Document is an internal document: {doc_uri}") else: local_bookshelf_process_executor.submit( add_document_to_bookshelf, document, data["category"], data["tags"], data["should_add_to_fts"], data["database_file"], ) return {"status": "OK", "document_uri": doc_uri}
def onOpenEBook(self, event): last_folder = config.conf["history"]["last_folder"] if not os.path.isdir(last_folder): last_folder = str(Path.home()) openFileDlg = wx.FileDialog( self.view, # Translators: the title of a file dialog to browse to a document message=_("Select a document"), defaultDir=last_folder, wildcard=self.view._get_ebooks_wildcards(), style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST, ) if openFileDlg.ShowModal() == wx.ID_OK: self.view.unloadCurrentEbook() filename = openFileDlg.GetPath().strip() openFileDlg.Destroy() if filename: config.conf["history"]["last_folder"] = os.path.split( filename)[0] config.save() self.view.open_uri(DocumentUri.from_filename(filename))
def test_epub_metadata(asset): uri = DocumentUri.from_filename(asset("The Diary of a Nobody.epub")) epub = create_document(uri) assert epub.metadata.title == "The Diary of a Nobody" assert epub.metadata.author == "George Grossmith"
def process_result_value(self, value, dialect): return DocumentUri.from_uri_string(value)
def python_value(self, value): return DocumentUri.from_uri_string(value)