def test_get_existing_dir__caches(self, mocker): path = Path('/usr/bin/foo') assert path.get_existing_dir() == '/usr/bin' os_path_exitst = mocker.patch('ulauncher.utils.Path.os.path.exists') assert path.get_existing_dir() == '/usr/bin' assert path.get_existing_dir() == '/usr/bin' assert not os_path_exitst.called
def test_on_alt_enter_file(self, result_item, path, OpenFolderItem, Path, RenderAction, CopyPathToClipboardItem): path.is_dir.return_value = False assert result_item.on_alt_enter('query') == RenderAction.return_value Path.assert_called_with(path.get_dirname.return_value) OpenFolderItem.assert_called_with(Path.return_value) RenderAction.assert_called_with([ OpenFolderItem.return_value, CopyPathToClipboardItem.return_value ])
def _get_file_alt_menu(self): """ :rtype: list of ResultItems """ open_folder = OpenFolderItem(Path(self.path.get_dirname())) open_folder.set_name('Open Containing Folder') return [open_folder, CopyPathToClipboardItem(self.path)]
def handle_key_press_event(self, widget, event, query): keyval = event.get_keyval() keyname = Gdk.keyval_name(keyval[1]) ctrl = event.state & Gdk.ModifierType.CONTROL_MASK path = Path(query) if keyname == 'BackSpace' and not ctrl and widget.get_position() == len(query) and path.is_dir() and \ not widget.get_selection_bounds(): # stop key press event if: # it's a BackSpace key and # Ctrl modifier is not pressed and # cursor is at the last position and # path exists and it's a directory and # input text is not selected widget.emit_stop_by_name('key-press-event') return SetUserQueryAction(os.path.join(path.get_dirname(), '')) return DoNothingAction()
def handle_query(self, query: str) -> BaseAction: if query == '~': return SetUserQueryAction('~/') path = Path(query) # type: Path result_items = [] # type: Union[List, SortedList] try: existing_dir = path.get_existing_dir() if existing_dir == path.get_abs_path(): file_names = self.list_files(path.get_abs_path(), sort_by_usage=True) for name in self.filter_dot_files(file_names)[:self.RESULT_LIMIT]: file = os.path.join(existing_dir, name) result_items.append(self.create_result_item(file)) else: file_names = self.list_files(existing_dir) search_for = path.get_search_part() if not search_for.startswith('.'): file_names = self.filter_dot_files(file_names) files = [os.path.join(existing_dir, name) for name in file_names] result_items = SortedList(search_for, min_score=40, limit=self.RESULT_LIMIT) result_items.extend([self.create_result_item(name) for name in reversed(files)]) except (InvalidPathError, OSError): result_items = [] return RenderResultListAction(result_items)
def test_get_existing_dir(self): assert Path('/usr/bin').get_existing_dir() == '/usr/bin' assert Path('/usr/bin/foo').get_existing_dir() == '/usr/bin' assert Path('/usr/bin/foo/bar').get_existing_dir() == '/usr/bin' assert Path('/usr/bin/.').get_existing_dir() == '/usr/bin' assert Path('/usr/bin/sh').get_existing_dir() == '/usr/bin' assert Path('~/foo/bar').get_existing_dir() == os.path.expanduser('~')
def create_result_item(self, path_srt: str) -> FileBrowserResultItem: return FileBrowserResultItem(Path(path_srt))
def test_get_dirname(self): assert Path('~/Pictures/').get_dirname() == '~' assert Path('~/').get_dirname() == '' assert Path('~').get_dirname() == '' assert Path('/').get_dirname() == '' assert Path('/usr/bin/foo').get_dirname() == '/usr/bin'
def test_get_basename(self): assert Path('/bin').get_basename() == 'bin' assert Path('/usr/bin/My Videos').get_basename() == 'My Videos' assert Path('~/foo/bar//').get_basename() == 'bar'
def test_get_user_path(self): assert Path('/bin').get_user_path() == '/bin' assert Path('/usr/bin/foo').get_user_path() == '/usr/bin/foo' assert Path('~/foo/bar//').get_user_path() == '~/foo/bar'
def test_get_search_part(self): assert Path('/usr/bin').get_search_part() == '' assert Path('/usr/bin/foo').get_search_part() == 'foo' assert Path('/usr/bin/foo/bar').get_search_part() == 'foo/bar' assert Path('~/foo/bar/').get_search_part() == 'foo/bar'
def test_get_existing_dir__raises(self): with pytest.raises(InvalidPathError): assert Path('~~').get_existing_dir()