Example #1
0
def set_data(name, val):
    if name in editors:
        editors[name].replace_data(val, only_if_different=False)
    else:
        with current_container().open(name, 'wb') as f:
            f.write(val)
    get_boss().set_modified()
Example #2
0
def set_data(name, val):
    if name in editors:
        editors[name].replace_data(val, only_if_different=False)
    else:
        with current_container().open(name, 'wb') as f:
            if isinstance(val, str):
                val = val.encode('utf-8')
            f.write(val)
    get_boss().set_modified()
Example #3
0
 def double_clicked(self, index):
     from calibre.gui2.tweak_book.boss import get_boss
     boss = get_boss()
     if boss is None:
         return
     index = self.proxy.mapToSource(index)
     entry = self.model.index_to_entry(index)
     if entry is None:
         return
     if isinstance(entry, CSSEntry):
         loc = entry.rule.location
         name, sourceline, col = loc
     elif isinstance(entry, CSSFileMatch):
         name, sourceline = entry.file_name, 0
     else:
         name = self.model.index_to_entry(index.parent()).file_name
         sourceline = entry.sourceline
     editor = boss.edit_file_requested(name)
     if editor is None:
         return
     editor = editor.editor
     block = editor.document().findBlockByNumber(max(0, sourceline - 1))  # blockNumber() is zero based
     c = editor.textCursor()
     c.setPosition(block.position() if block.isValid() else 0)
     editor.setTextCursor(c)
     boss.show_editor(name)
    def anchor_clicked(self, qurl):
        url = qurl.toString()
        if url.startswith('err:'):
            errnum = int(url[4:])
            err = self.errors[errnum]
            newurl, ok = QInputDialog.getText(self,
                                              _('Fix URL'),
                                              _('Enter the corrected URL:') +
                                              '\xa0' * 40,
                                              text=err[2])
            if not ok:
                return
            nmap = defaultdict(set)
            for name, href in {(l[0], l[1]) for l in err[0]}:
                nmap[name].add(href)

            for name, hrefs in iteritems(nmap):
                raw = oraw = get_data(name)
                for href in hrefs:
                    raw = raw.replace(href, newurl)
                if raw != oraw:
                    set_data(name, raw)
            self.fixed_errors.add(errnum)
            self.populate_results()
        elif url.startswith('loc:'):
            lnum, name = url[4:].partition(',')[::2]
            lnum = int(lnum or 1)
            editor = get_boss().edit_file(name)
            if lnum and editor is not None and editor.has_line_numbers:
                editor.current_line = lnum
Example #5
0
    def anchor_clicked(self, qurl):
        url = qurl.toString()
        if url.startswith('err:'):
            errnum = int(url[4:])
            err = self.errors[errnum]
            newurl, ok = QInputDialog.getText(self, _('Fix URL'), _('Enter the corrected URL:') + '\xa0'*40, text=err[2])
            if not ok:
                return
            nmap = defaultdict(set)
            for name, href in {(l[0], l[1]) for l in err[0]}:
                nmap[name].add(href)

            for name, hrefs in nmap.iteritems():
                raw = oraw = get_data(name)
                for href in hrefs:
                    raw = raw.replace(href, newurl)
                if raw != oraw:
                    set_data(name, raw)
            self.fixed_errors.add(errnum)
            self.populate_results()
        elif url.startswith('loc:'):
            lnum, name = url[4:].partition(',')[::2]
            lnum = int(lnum or 1)
            editor = get_boss().edit_file(name)
            if lnum and editor is not None and editor.has_line_numbers:
                editor.current_line = lnum
Example #6
0
 def double_clicked(self, index):
     entry = index.data(Qt.UserRole)
     if entry is not None:
         from calibre.gui2.tweak_book.boss import get_boss
         boss = get_boss()
         if boss is not None:
             boss.find_word((entry.word, entry.locale), entry.usage)
Example #7
0
 def double_clicked(self, index):
     entry = index.data(Qt.UserRole)
     if entry is not None:
         from calibre.gui2.tweak_book.boss import get_boss
         boss = get_boss()
         if boss is not None:
             boss.find_word((entry.word, entry.locale), entry.usage)
Example #8
0
    def find_next_location(self, entry):
        from calibre.gui2.tweak_book.boss import get_boss
        boss = get_boss()
        if boss is None:
            return
        files = entry.usage
        current_editor_name = boss.currently_editing
        if current_editor_name not in files:
            current_editor_name = None
        else:
            idx = files.index(current_editor_name)
            before, after = files[:idx], files[idx+1:]
            files = [current_editor_name] + after + before + [current_editor_name]

        pat = regex.compile(regex.escape(entry.char))
        for file_name in files:
            from_cursor = False
            if file_name == current_editor_name:
                from_cursor = True
                current_editor_name = None
            ed = boss.edit_file_requested(file_name)
            if ed is None:
                return
            if ed.editor.find(pat, complete=not from_cursor):
                boss.show_editor(file_name)
                return True
        return False
Example #9
0
    def find_next_location(self, entry):
        from calibre.gui2.tweak_book.boss import get_boss
        boss = get_boss()
        if boss is None:
            return
        files = entry.usage
        current_editor_name = boss.currently_editing
        if current_editor_name not in files:
            current_editor_name = None
        else:
            idx = files.index(current_editor_name)
            before, after = files[:idx], files[idx+1:]
            files = [current_editor_name] + after + before + [current_editor_name]

        pat = regex.compile(regex.escape(entry.char))
        for file_name in files:
            from_cursor = False
            if file_name == current_editor_name:
                from_cursor = True
                current_editor_name = None
            ed = boss.edit_file_requested(file_name)
            if ed is None:
                return
            if ed.editor.find(pat, complete=not from_cursor):
                boss.show_editor(file_name)
                return True
        return False
Example #10
0
 def refresh(self):
     from calibre.gui2.tweak_book.boss import get_boss
     boss = get_boss()
     boss.commit_all_editors_to_container()
     self.beginResetModel()
     self.build()
     self.endResetModel()
Example #11
0
 def init_env(self, name=''):
     from calibre.gui2.tweak_book.boss import get_boss
     self.context_name = name or ''
     self.match_index = 0
     self.boss = get_boss()
     self.data = {}
     self.debug_buf = StringIO()
     self.functions = {name:func.mod for name, func in functions().iteritems() if func.mod is not None}
Example #12
0
 def init_env(self, name=''):
     from calibre.gui2.tweak_book.boss import get_boss
     self.context_name = name or ''
     self.match_index = 0
     self.boss = get_boss()
     self.data = {}
     self.debug_buf = PolyglotStringIO()
     self.functions = {name:func.mod for name, func in iteritems(functions()) if func.mod is not None}
Example #13
0
 def double_clicked(self, index):
     from calibre.gui2.tweak_book.boss import get_boss
     boss = get_boss()
     if boss is None:
         return
     index = self.proxy.mapToSource(index)
     entry = self.model.index_to_entry(index)
     if entry is None:
         return
     self.handle_double_click(entry, index, boss)
Example #14
0
 def double_clicked(self, index):
     from calibre.gui2.tweak_book.boss import get_boss
     boss = get_boss()
     if boss is None:
         return
     index = self.proxy.mapToSource(index)
     entry = self.model.index_to_entry(index)
     if entry is None:
         return
     self.handle_double_click(entry, index, boss)
Example #15
0
def jump_to_location(loc):
    from calibre.gui2.tweak_book.boss import get_boss
    boss = get_boss()
    if boss is None:
        return
    name = loc.name
    editor = boss.edit_file_requested(name)
    if editor is None:
        return
    editor = editor.editor
    if loc.line_number is not None:
        block = editor.document().findBlockByNumber(loc.line_number - 1)  # blockNumber() is zero based
        if not block.isValid():
            return
        c = editor.textCursor()
        c.setPosition(block.position(), c.MoveAnchor)
        editor.setTextCursor(c)
        if loc.text_on_line is not None:
            editor.find(regex.compile(regex.escape(loc.text_on_line)))
Example #16
0
def jump_to_location(loc):
    from calibre.gui2.tweak_book.boss import get_boss
    boss = get_boss()
    if boss is None:
        return
    name = loc.name
    editor = boss.edit_file_requested(name)
    if editor is None:
        return
    editor = editor.editor
    if loc.line_number is not None:
        block = editor.document().findBlockByNumber(loc.line_number - 1)  # blockNumber() is zero based
        if not block.isValid():
            return
        c = editor.textCursor()
        c.setPosition(block.position(), c.MoveAnchor)
        editor.setTextCursor(c)
        if loc.text_on_line is not None:
            editor.find(regex.compile(regex.escape(loc.text_on_line)))
Example #17
0
 def add_file(name, data, mt=None):
     from calibre.gui2.tweak_book.boss import get_boss
     name = current_container().add_file(name, data, media_type=mt, modify_name_if_needed=True)
     get_boss().refresh_file_list()
     return name
Example #18
0
 def add_file(name, data, mt=None):
     from calibre.gui2.tweak_book.boss import get_boss
     name = current_container().add_file(name, data, media_type=mt, modify_name_if_needed=True)
     get_boss().refresh_file_list()
     return name
Example #19
0
 def boss(self):
     ' The :class:`calibre.gui2.tweak_book.boss.Boss` object. Used to control the user interface. '
     return get_boss()
Example #20
0
 def boss(self):
     ' The :class:`calibre.gui2.tweak_book.boss.Boss` object. Used to control the user interface. '
     return get_boss()