-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbkm_search.py
145 lines (117 loc) · 4.94 KB
/
bkm_search.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import argparse
import getpass
import utilities.url_utils as ul
import os
def report_on_data(bmk_links):
'''
Outputs info on the Chrome bookmarks status, how many, how many duplicates
:param bmk_links: dfs_chrome_bookmarks object
:return: None
'''
# Todo: should this be a method of dfs_chrome_bookmarks obj?
print("...data about bookmarks obtained.")
print(f"Number of links found: {len(bmk_links.link_list)}")
# test for uniquess of links in the Bookmarks
n_unique_links = len(set(i[0] for i in bmk_links.link_list))
n_links = len(bmk_links.link_list)
if n_links != n_unique_links:
print(f"{n_links - n_unique_links} Duplicate links exists in your bookmarks!")
else:
print("There are no duplicate links! OK!")
ask = input("\nDo you want to print the obtained links out? Y/N/How many? ")
if str(ask).strip(' ') == 'Y':
ul.myprint(bmk_links.link_list, N=n_links)
elif (int(ask) > 0) and (int(ask) <= n_links):
ul.myprint(bmk_links.link_list, N=int(ask))
# Todo: do this well taking care of the N case....
return None
def system_and_env_helper():
'''
Collects info about the system, platform and check/set env variables
:return: list of proxy related env variables name
'''
# check and set env variables, get environment
env_keys = os.environ.keys()
# collect env variables related to proxy settings
print("\n\n\nCurrent environment status relevant to proxy configuration is the following:\n")
proxy_env_keys = [k for k in
env_keys if ("PROXY" in k.upper()
and "NO_PROXY" not in k.upper())]
for k in proxy_env_keys:
print(f"proxy related env variable {k} = {os.environ[k]}")
# let the user choose about what proxy settings to use
proxy_choice = input("\n\nDo you want to use these proxy settings (Y) or you want to skip the proxy (S)?")
# Todo: check input for correctness
if proxy_choice.upper() == 'S':
for k in proxy_env_keys:
del (os.environ[k])
print("\nOK. Status of proxy environment variables has changed:\n")
for k in proxy_env_keys:
print(f'{k}={os.environ.get(k)}')
return proxy_env_keys
def print_env():
'''
Helper function to print env variables
:return: None
'''
for k, v in os.environ.items():
print(f'{k}={v}')
return None
def myprint(stack, N=1000, offset=3):
'''
Helper to print out a list of tuples with the second element being a list
:param stack: list of tuples
:param N: default max elements to print
:param offset: exclude offset initial elements of the second component
:return: None
'''
i = 0
while stack and i < N:
k, v = stack.pop()
print(i, ' ', k, v[offset:]) # exclude ('na', 'na', 'na',)
i += 1
def get_Chrome_bookmarks_data(bmk_file):
'''
Open the Chrome bookmarks file of the user and
return an object that contains all the links in a list of tuples
:param bmk_file:
:return: class dfs_chrome_bookmarks object with the list of tuples (folder, links) filled
'''
# Todo: make it return a dict with keys the location in the bookmark tree and values
# Todo: the list of urls for each folder
with open(bmk_file, 'rt') as data_file:
bookmark_data = json.load(data_file)
exx = dfs_chrome_bookmarks(bookmark_data)
return exx
def main(username, lpattern):
'''
Search for pattern in the list of Chrome bookmarks
:param username: useful to find the Chrome Bookmarks
:param lpattern: pattern to match in the bookmarked page content
'''
# print some info about the system and set environment according to user's preferences
proxy_env_keys = system_and_env_helper()
# identify bookmarks file
# Todo: check for data in input to be ok and deal with non-Mac cases
# Todo: also the user should be able to specify the full path
bookmarks_file = username.join(['/Users/', '/Library/Application\ Support/Google/Chrome/Default/Bookmarks'])
bookmarks_file = bookmarks_file.replace("\\", '')
print(f"\nLooking into Bookmarks file: {bookmarks_file}")
# Get bookmarks into a manageable dict
bmk_links = ul.get_Chrome_bookmarks_data(bookmarks_file)
# and report on the bookmarks status
report_on_data(bmk_links)
# search for chosen pattern into a specified subset of bookmarked links
# Todo: user should input the pattern here and not only on cli comand,
# Todo: and also the set of bookmarks folders to look into
ul.do_search(bmk_links, pattern_sought=lpattern, folder_list=None)
if __name__ == '__main__':
# get username
uname = getpass.getuser()
# get script params
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--pattern', help='string to match in the bookmarked pages')
args = parser.parse_args()
pattern = args.pattern
# start
main(uname, pattern)