A web browser-independent bookmark manager that uses rofi as an interface to save, open or type out URL bookmarks.
git clone https://mcol.xyz/code/bkmkfi
Log | Files | Refs | README | LICENSE

commit 3c52f87f9963f91cd9a08635d65d641002ee4cea
parent ab7feef93371aa092fff2f3d36f8f4bb4a549234
Author: mcol <mcol@posteo.net>
Date:   Tue, 31 Dec 2019 17:59:35 +0000

Move functionality into class for scriptability

Diffstat:
Mbkmkfi/__init__.py | 79++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 74 insertions(+), 5 deletions(-)

diff --git a/bkmkfi/__init__.py b/bkmkfi/__init__.py @@ -88,6 +88,11 @@ def parse_args(): print(f"Actions: {' '.join(ACTIONS)}") sys.exit(1) + if args.action[0] == 'open' and len(args.action) == 1: + print(f"Action 'open' requires a subsequent argument specifying the command") + print(f"used to open the URL e.g. bkmkfi open 'tor-browser %s'") + sys.exit(1) + return args @@ -110,7 +115,7 @@ def rofi(stdin='', prompt=None, theme=None): ) if result.returncode or not result.stdout: - sys.exit(1) + return None # strip newline return result.stdout[0:-1] @@ -167,8 +172,8 @@ def open_url(url, open_cmd): Open URL in web browser new tab. %s in second positional argument replaced with the URL. """ - if os.fork() == 0: - os.system(open_cmd % url) + full_cmd = open_cmd % url + subprocess.Popen(full_cmd.split(' ')) def copy(url): @@ -176,7 +181,7 @@ def copy(url): Copy bookmark URL to clipboard. This copies by piping the URL to xclip. """ - os.system(f'printf {url} | xclip -selection clipboard') + subprocess.Popen(f'printf {url} | xclip -selection clipboard') def type_out(url, hit_return=None): @@ -216,7 +221,7 @@ def print_actions(): def main(): """ - Main program + Main program used for cli script """ opts = parse_args() action = opts.action[0] @@ -231,6 +236,8 @@ def main(): prompt=PROMPTS[action], theme=opts.theme, ) + if selected is None: + sys.exit(1) # Save if new if selected not in bookmarks: @@ -258,3 +265,65 @@ def main(): elif action == 'type_cr': type_out(url, hit_return=True) + + +class Bkmkfi: + """ + Object that can be loaded and interacted with e.g. for keybinds in Qtile. + + All actions specified above are exposed by this as methods of the same name. + Bookmarks are loaded from the file when the object is instantiated, and will only + touch the file when deleting a bookmark or saving a new bookmark. + """ + def __init__(self, file=None, theme=None, sort=False): + self.file = file if file else f'{HOME}/.config/bkmks' + self.sort = sort + self.theme = theme + self.bookmarks = load_bookmarks(self.file) + + def _select(self, action): + selected = rofi( + stdin='\n'.join(self.bookmarks), + prompt=PROMPTS[action], + theme=self.theme, + ) + if selected is not None and selected not in self.bookmarks: + self.bookmarks.append(selected) + if self.sort: + self.bookmarks.sort() + save(self.bookmarks, self.file) + return None + return selected + + def open(self, open_cmd): + selected = self._select('open') + if selected is not None: + url = get_url(selected) + open_url(url, open_cmd) + + def copy(self): + selected = self._select('copy') + if selected is not None: + url = get_url(selected) + copy(url) + + def type(self): + selected = self._select('type') + if selected is not None: + url = get_url(selected) + type_out(url) + + def type_cr(self): + selected = self._select('type_cr') + if selected is not None: + url = get_url(selected) + type_out(url, hit_return=True) + + def delete(self): + selected = self._select('delete') + if selected is not None: + self.bookmarks.remove(selected) + save(self.bookmarks, self.file) + + def save(self): + self._select('save')