def read_entries_from_db(regex: str) -> dict[Path, DbEntry]: """ :param regex: :return: dict: path -> (key, frequency) """ query = f""" SELECT {TABLE_ENTRY}.{ENTRY_PATH}, {TABLE_ENTRY}.{ENTRY_KEY}, {TABLE_ACCESS}.{ACCESS_TIMESTAMP} FROM {TABLE_ENTRY} LEFT JOIN {TABLE_ACCESS} ON {TABLE_ENTRY}.{ENTRY_PATH}={TABLE_ACCESS}.{ACCESS_PATH} WHERE {TABLE_ENTRY}.{ENTRY_PATH} REGEXP ? """ conn = open_connection() try: cursor = conn.cursor().execute(query, (regex,)) result: dict[Path, DbEntry] = {} for row in cursor: path = Path(row[0]) access = row[2] if access is not None and path in result: result[path].access_history.append(access) else: key = Key(row[1]) access_history = [] if access is not None: access_history.append(access) entry = DbEntry(path, key=key, access_history=access_history) result[path] = entry return result finally: conn.close()
def handle_key_individually(self, key: str): if hook.is_hook(key, self.model.current_entry): hook.trigger_hook(key, self.model.current_entry) return True elif self.navigator.is_navigation_key(key): self.go_to_key(Key(key)) return True
def __init__(self, path: Path, access_history: list[int] = None, parent: 'Entry' = None, key: Key = Key('')): """ :param path: the absolute, canonical path of this entry """ self.path = Path(os.path.normpath(str(path))) self.unexplored = False self.access_history: list[int] = access_history or [] self._rating: Optional[float] = None self.parent = parent self.dir: Path = self.path.parent self.name = path.name self._key = key self._children: list['Entry'] = [] self.path_to_child: dict[Path, Entry] = {} self.key_to_child: dict[Key, Entry] = {} self.hooks: list[Hook] = [] self.stat = os.lstat(path) self.size = self.stat.st_size self.permissions = oct(self.stat.st_mode)[-3:]
def __init__(self, path: Path, key: Key = None, access_history: list[int] = None): if not key: key = Key('') if not access_history: access_history = [] self.path: Path = path self.key: Key = key self.access_history: list[int] = access_history
def choose_entry(self, key: str) -> None: if key in key_module.get_all_keys(): entry = self.navigator.current_entry.get_child_for_key(Key(key)) assert entry self.list.select(entry) viewmodel.global_mode.mode = Mode.ASSIGN_CHOOSE_KEY elif key == 'enter': viewmodel.global_mode.mode = Mode.ASSIGN_CHOOSE_KEY
def delete(self, key=None): if key: if self.navigator.is_navigation_key(key): target = self.navigator.current_entry.get_child_for_key( Key(key)) buffer.registers[0].move_to(target) self.navigator.reload_current_entry() self.active_action = None self.enter_navigate_mode() notify.show(f"deleted {target.path.name}") return True else: notify.show("delete what?", duration=0) self.active_action = self.delete
def yank(self, key=None): if key: if self.navigator.is_navigation_key(key): target = self.navigator.current_entry.get_child_for_key( Key(key)) buffer.registers[0].copy_to(target) self.enter_navigate_mode() self.active_action = None notify.show(f"yanked {target.path.name}") else: self.enter_navigate_mode() else: notify.show("yank what?", duration=0) self.active_action = self.yank
def rename(self, key=None): if key: if self.list_entry_for_renaming: exit_action_key = self.actions[ActionName.exit].keybinding navigate_mode_action_key = self.actions[ ActionName.navigate_mode].keybinding if key in (exit_action_key, navigate_mode_action_key): if key == exit_action_key: new_name = self.list_entry_for_renaming.edit_text operate.rename(self.list_entry_for_renaming.entry, new_name) self.list_entry_for_renaming.editing = False self.list.render(self.list_size, focus=True) self.list_entry_for_renaming = None self.active_action = None self.enter_navigate_mode() notify.show("renamed file") else: self.list_entry_for_renaming.keypress( self.filter_size, key) return True else: if self.navigator.is_navigation_key(key): target = self.navigator.current_entry.get_child_for_key( Key(key)) notify.show(f"renaming {target.name}", duration=0) list_entry = self.list.get_list_entry(target) if list_entry: list_entry.editing = True self.list.selection = list_entry self.list.render(self.list_size, focus=True) self.list_entry_for_renaming = list_entry return True else: notify.show("rename what?", duration=0) self.active_action = self.rename
def is_navigation_key(self, key: str) -> bool: if key_module.is_navigation_key(key): return self.entry_access.is_possible(Key(key)) return False
def assign_key(self, key: str): if key_module.is_navigation_key(key): selected_entry = self.list.selection.entry self.navigator.assign_key(Key(key), Path(selected_entry.path)) self.list.on_entries_changed(self.model.entries) self.enter_navigate_mode()