1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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()
|