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()
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()
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
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
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)
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
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()
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}
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}
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)
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)))
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
def boss(self): ' The :class:`calibre.gui2.tweak_book.boss.Boss` object. Used to control the user interface. ' return get_boss()