-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
86 lines (72 loc) · 2.79 KB
/
common.py
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
from brotab import main as btm
from brotab import api as brapi
default_browser = "firefox"
def get_client(requested_browser = None):
if requested_browser is None:
requested_browser = default_browser
clients = btm.create_clients()
if not clients:
raise Exception("No clients found!")
browser_matches = [cl for cl in clients if cl._get_browser() == requested_browser]
if not browser_matches:
raise Exception("No clients found!")
if len(browser_matches) > 1:
raise Exception("Too many clients found, don't know what to do!")
return browser_matches[0]
class Tab():
def __init__(self, prefix, window, id, name, url):
self.prefix = prefix
self.window = window
self.id = id
self.name = name
self.url = url
def get_full_id(self):
return ".".join([self.prefix, self.window, self.id])
def to_string(self):
return "\t".join([self.get_full_id(), self.name, self.url])
def serialize_tabs(tabs):
"""
converts a list of Tab objects into strings, as they were before parsing
this can then be used in update_tabs
"""
return [tab.to_string() for tab in tabs]
def update_tabs(client, old_tabs, new_tabs):
"""
after moving tabs between windows, pass old and new serialized tabs here so that the changes can actually be applied
tab indices need to be recalculated, that's done by the brotab API call
"""
return brapi.MultipleMediatorsAPI(None)._move_tabs_if_changed(client, old_tabs, new_tabs)
def get_window_ids(tabs):
"""
gets tab objects and returns a list of window IDs
"""
window_ids = [tab.window for tab in tabs]
window_ids = list(set(window_ids))
return window_ids
def parse_tabs(client):
"""
returns a list of Tab objects
"""
tabs = client.list_tabs_safe(None)
for i, tab in enumerate(tabs):
tabs[i] = tab.split('\t')
# sanity-check tab lengths after parsing - result should be {3}
tab_list_lengths = set([len(tab) for tab in tabs])
try:
assert ( tab_list_lengths == {3})
except AssertionError:
faulty_tab_strs = [repr(tab) for tab in tabs if len(tab) != 3]
faulty_tabs_str = ", ".join(faulty_tab_strs)
raise ValueError("Tab info parsing problem! Tab list lengths after parsing: {}, faulty tabs: {}".format(repr(tab_list_lengths), faulty_tabs_str))
for i, tab in enumerate(tabs):
tab[1:1] = tab[0].split('.')
tab.pop(0)
tabs[i] = Tab(*tabs[i])
return tabs
def is_empty_tab(tab):
return tab.url == "about:newtab" or tab.url == "about:home" or tab.url == "about:blank"
def filter_tabs_by_window(tabs, window):
return [tab for tab in tabs if tab.window == window]
if __name__ == "__main__":
client = get_client()
tabs = parse_tabs(client)