summaryrefslogtreecommitdiffstats
path: root/.bin/qb
diff options
context:
space:
mode:
Diffstat (limited to '.bin/qb')
-rwxr-xr-x.bin/qb130
1 files changed, 130 insertions, 0 deletions
diff --git a/.bin/qb b/.bin/qb
new file mode 100755
index 0000000..3caf57e
--- /dev/null
+++ b/.bin/qb
@@ -0,0 +1,130 @@
+#!/usr/bin/env python3
+
+import os
+import sys
+import yaml
+
+
+def bypass_file_exists(func, *args):
+ """
+ Exceptions for existing files are ignored.
+ """
+ try:
+ func(*args)
+ except FileExistsError as e:
+ print(e)
+
+
+def start_qutebrowser(args):
+ """
+ Replace the current process with qutebrowser.
+ """
+ os.execvp('qutebrowser', args)
+
+
+def generate_tab(url):
+ full_url = f'https://{url}'
+
+ return {
+ 'active': True,
+ 'history': [{
+ 'active': True,
+ 'pinned': True,
+ 'last_visited': '2021-09-16T12:22:57',
+ 'scroll-pos': {'x': 0, 'y': 0},
+ 'zoom': 1.0,
+ 'title': url,
+ 'original-url': full_url,
+ 'url': full_url
+ }]
+ }
+
+
+def restore_default_tabs(profile, path=None):
+ """
+ Declarative way to enforce opened/favorite tabs.
+ """
+ mapping = {
+ 'default': [
+ 'mailbox.org',
+ 'news.ycombinator.com',
+ 'miniflux.rgoncalves.se',
+ 'status.rgoncalves.se'
+ ],
+ 'work': [
+ 'mail.zoho.eu',
+ 'cliq.zoho.eu',
+ 'gitlab.viperdev.io',
+ 'projects.zoho.eu',
+ 'kb.viperdev.io',
+ ]
+ }
+
+ assert mapping[profile]
+
+ # Open file an cleanup
+ with open(path, 'r') as file:
+ data = yaml.safe_load(file)
+
+ # Scrap existing tabs,
+ # then filter pinned tabs
+ original_tabs = data['windows'][0]['tabs']
+ tabs = [tab['history'][-1] for tab in original_tabs
+ if len(tab['history']) != 0]
+ tabs = [tab for tab in tabs if tab['pinned']]
+
+ missing_urls = [url for url in mapping[profile]
+ if not any(url in tab['url'] for tab in tabs)]
+
+ for url in missing_urls:
+ original_tabs.append(generate_tab(url))
+
+ original_tabs.sort(reverse=True, key=lambda k: k['history'][-1]['pinned'])
+
+ # Persist tabs in yaml file
+ with open(path, 'w') as file:
+ yaml.dump(data, file)
+
+
+def main():
+ try:
+ QB_PROFILE = f'{sys.argv[1]}'
+ except IndexError:
+ QB_PROFILE = 'default'
+
+ """
+ restore_default_tabs(
+ QB_PROFILE,
+ path=f'{os.environ["HOME"]}/' +
+ '.local/share/qutebrowser/sessions/default.yml'
+ )
+ """
+ start_qutebrowser([' '])
+ sys.exit(0)
+
+ QB_DIR = f'{os.environ["HOME"]}/.config/qutebrowser'
+ QB_PROFILES_DIR = f'{QB_DIR}/_profiles'
+ QB_PROFILE_DIR = f'{QB_PROFILES_DIR}/{QB_PROFILE}'
+ QB_PROFILE_DIR_CONFIG = f'{QB_PROFILE_DIR}/config'
+ QB_PROFILE_TAG = QB_PROFILE[0].lower()
+
+ # Ensure directory existence
+ for dir in [QB_PROFILES_DIR, QB_PROFILE_DIR, QB_PROFILE_DIR_CONFIG]:
+ bypass_file_exists(os.mkdir, dir)
+
+ # Ensure common files are shared with other profiles
+ for file in ['config.py', 'bookmarks', 'greasemonkey']:
+ bypass_file_exists(os.symlink,
+ f'{QB_DIR}/{file}',
+ f'{QB_PROFILE_DIR_CONFIG}/{file}')
+
+ start_qutebrowser([
+ ' ', '--basedir', QB_PROFILE_DIR,
+ '--set', 'window.title_format', f'[{QB_PROFILE_TAG}]',
+ '--set', 'tabs.title.format',
+ f'[{QB_PROFILE_TAG}] {{audio}}{{index}}: {{current_title}}'
+ ])
+
+
+if __name__ == '__main__':
+ main()
remember that computers suck.