Пример #1
0
    def new_book(self, doc_stream, good_stream, bad_stream):
        self.book_name = doc_stream.filename()
        self.editv.book_renamed(self.book_name)
        self.book_folder = doc_stream.folderpath()
        self.book_full_path = doc_stream.fullpath()
        self.editm.setPlainText(doc_stream.readAll())
        self.editm.setModified(True)
        # If there are good_words and bad_words streams, call the worddata
        # metadata reader functions directly to accept them.
        if good_stream:
            self.wordm.good_file(good_stream)
        if bad_stream:
            self.wordm.bad_file(bad_stream)
        self.pagem.scan_pages()  # develop page metadata if possible
        self.hook_images()  # set up display of scan images if possible
        self.editv.set_cursor(self.editv.make_cursor(0, 0))  # cursor to top
        self._speller = dictionaries.Speller(self.dict_tag, self.book_folder)
        # Check the loaded text for \ufffd "replacement" chars indicating
        # mis-decoding of the file.
        findtc = self.editm.find(C.UNICODE_REPL, position=0)
        if not findtc.isNull():
            m1 = _TR(
                'File:Open finds bad encoding',
                'This document contains at least one Unicode Replacement Character!'
            )
            m2 = _TR(
                'File: Open finds bad encoding',
                '''This indicates it was read with the wrong encoding!
See the Help file under "File Encodings and File Names"
The first bad character is at ''', 'integer follows this') + str(
                    findtc.position())
            utilities.warning_msg(m1, m2, self.mainwindow)
Пример #2
0
 def _read_dict(self, sentinel, value, version):
     tag_dict = dictionaries.get_tag_list(self.book_folder)
     try:
         dict_path = tag_dict[value]  # index error if value not a known tag
         speller = dictionaries.Speller(value, dict_path)
         if not speller.is_valid():
             raise ValueError
         # we have a valid dictionary tag and spellcheck object
         self.dict_tag = value
         self._speller = speller
     except:
         self.logger.error(
             'Unable to open default dictionary {0}, using default {1}'.
             format(value, self.dict_tag))
Пример #3
0
 def ask_dictionary(self):
     tag_list = dictionaries.get_tag_list(self.book_folder)
     if 0 < len(tag_list):
         # dictionaries knows about at least one tag, display it/them
         item_list = sorted(list(tag_list.keys()))
         current = 0
         if self.dict_tag in item_list:
             current = item_list.index(self.dict_tag)
         title = _TR("EditViewWidget", "Primary dictionary for this book",
                     "Dictionary pop-up list")
         explanation = _TR(
             "EditViewWidget",
             "Select the best dictionary for spell-checking this book",
             "Dictionary pop-up list")
         new_tag = utilities.choose_from_list(title,
                                              explanation,
                                              item_list,
                                              parent=self.editv,
                                              current=current)
         if (new_tag is not None) and (new_tag != self.dict_tag):
             # a choice was made and it's different from before
             self.dict_tag = new_tag
             self._speller = dictionaries.Speller(new_tag,
                                                  tag_list[new_tag])
             self.wordm.recheck_spelling(self._speller)
             return True
     else:
         # no known dictionaries, probably the Extras have not been
         # configured -- tell user.
         utilities.warning_msg(
             text=_TR("EditViewWidget", "No dictionaries found",
                      "Dictionary request warning"),
             info=_TR("EditViewWidget",
                      "Perhaps the 'extras' folder is not defined?'",
                      "Dictionary request info"))
     return False
Пример #4
0
assert T.check_log(".dic but not", logging.ERROR)
os.remove(dic_path)  # clean up Files
# cause T.path_to_Files to have mismatched foobar.aff
aff_path = os.path.join(T.path_to_Files, 'foobar.aff')
f = open(aff_path, 'w')
f.close()
tag_list = dictionaries.get_tag_list(T.path_to_Files)
assert T.check_log(".aff but not", logging.ERROR)
os.remove(aff_path)  # clean up Files

# "skipping" should appear if we check a path twice
tag_list = dictionaries.get_tag_list(test_dicts)
assert T.check_log("Skipping", logging.INFO)

# Check the spellcheck object: bad input makes not is_valid
# and all words are ok
SP = dictionaries.Speller("xx_YY", paths.get_dicts_path())
assert T.check_log('bad dictionary path', logging.ERROR)
assert not SP.is_valid()
assert SP.check('neenerneener', None)
SP = None  # get rid of that object

SP = dictionaries.Speller("en_US", paths.get_dicts_path())
assert SP.is_valid()
assert SP.check('raspberry', None)
assert not SP.check('bazongas', None)
assert SP.check('framboise', 'fr_FR')
assert not SP.check('bazongas', 'fr_FR')
# nonexistent alt tag produces True
assert SP.check('bazongas', 'en_AU')
Пример #5
0
 def __init__(self, sequence, the_main):
     super().__init__(None)
     # Save our mainwindow ref and our unique sequence number
     self.mainwindow = the_main
     self.sequence = sequence
     # Create our dict of activity panel objects, used by the main
     # window when giving us the focus.
     self.panel_dict = mainwindow.PANEL_DICT.copy()
     # Set up a book-unique logging channel
     self.logger = logging.getLogger(name='book_' + str(self.sequence))
     # Create the spellchecker using the global default dictionary. It may
     # be recreated when we have a book path and/or read a preferred dict
     # tag from metadata.
     self.dict_tag = dictionaries.get_default_tag()
     self._speller = dictionaries.Speller(self.dict_tag,
                                          paths.get_dicts_path())
     # Connect to find out about a change in the dicts path.
     paths.notify_me(self.path_change_slot)
     # Initialize slots for info about our book, which get filled in when
     # the mainwindow calls new_empty, old_book, or new_book below.
     self.edit_point_size = C.DEFAULT_FONT_SIZE
     self.edit_cursor = (0, 0)
     self.book_name = ''
     self.book_folder = ''
     self.book_full_path = ''
     self.last_find_button_path = ''
     # Initialize the metadata-modified flag, see metadata_modified()
     self.md_modified = 0
     # Initialize bookmarks, loaded from metadata later. The bookmarks
     # are indexed 1-9 (from control-1 to control-9 keys) but the list
     # has ten entries, entry 0 not being used.
     self.bookmarks = [
         None, None, None, None, None, None, None, None, None, None
     ]
     # Initialize the book-facts dict in case none is read from metadata.
     self.book_facts = {
         "Title": "?",
         "Author": "?",
         "Publication date": "1???",
         "Language": "English"
     }
     #
     # Create the metadata manager, then register to read and write the
     # metadata sections that are stored at this level: the last-set main
     # dictionary, the cursor position at save, the edit point size at
     # save, the bookmarks, the hash value of the saved document, and the
     # book_facts dictionary.
     #
     self.metamgr = metadata.MetaMgr()
     self.metamgr.register(C.MD_MD, self._read_dict, self._save_dict)
     self.metamgr.register(C.MD_CU, self._read_cursor, self._save_cursor)
     self.metamgr.register(C.MD_ES, self._read_size, self._save_size)
     self.metamgr.register(C.MD_BM, self._read_bookmarks,
                           self._save_bookmarks)
     self.metamgr.register(C.MD_DH, self._read_hash, self._save_hash)
     self.metamgr.register(C.MD_BI, self._read_facts, self._save_facts)
     #
     # Create the data model objects. These are private to the book.
     self.editm = editdata.Document(
         self)  # document, to be initialized later
     self.pagem = pagedata.PageData(self)  # page data
     self.charm = chardata.CharData(self)  # character data
     self.wordm = worddata.WordData(self)  # vocabulary data
     self.fnotm = fnotdata.FnoteData(self)  # footnote data
     #
     # Create the view objects that display and interact with the data
     # models. These need to be accessible to the main window for the
     # focus_me operation so are stored in the panel_dict.
     #
     self.editv = editview.EditView(
         self, lambda: self.mainwindow.focus_me(self.sequence))
     self.imagev = imageview.ImageDisplay(self)  # keep a short reference
     self.panel_dict['Images'] = self.imagev
     self.panel_dict['Notes'] = noteview.NotesPanel(self)
     self.panel_dict['Find'] = findview.FindPanel(self)
     self.panel_dict['Chars'] = charview.CharView(self)
     self.panel_dict['Words'] = wordview.WordPanel(
         self)  # wordview.WordPanel(self)
     self.panel_dict['Pages'] = pageview.PagePanel(
         self)  # pageview.PagePanel(self)
     self.panel_dict['Fnote'] = fnotview.FnotePanel(
         self)  # fnotview.FnotePanel(self)
     self.panel_dict['Loupe'] = loupeview.LoupeView(self)
     self.panel_dict['tab_list'] = [
         (tabname, self.panel_dict[tabname])
         for tabname in self.panel_dict['default']
     ]
Пример #6
0
 def path_change_slot(self, what_path):
     if what_path == 'dicts':
         self._speller = dictionaries.Speller(self.dict_tag,
                                              self.book_folder)