-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperabookmarks.py
52 lines (43 loc) · 1.5 KB
/
operabookmarks.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
import sys
def tree(filename):
root = {"type": "folder", "ID": "(root)", "NAME": "(root)", "children": []}
current_folder = root
flat = [current_folder]
stack = [] # does not include current
current = current_folder
for line in open(filename):
line = line.rstrip()
if line.startswith("#URL"):
current = {"type": "url"}
current_folder["children"].append(current)
flat.append(current)
elif line.startswith("#FOLDER"):
stack.append(current_folder)
current = {"type": "folder", "children": []}
current_folder["children"].append(current)
flat.append(current)
current_folder = current
elif line.startswith("\t"):
optname, optval = line[1:].split("=", 1)
current[optname] = optval
elif line == "-":
current_folder = stack.pop()
return root, flat
def dumptree(foldernode, level=0):
#import pprint
#pprint.pprint(dippa)
indent = "\t" * level
print indent + foldernode["NAME"]
for node in foldernode["children"]:
if node["type"] == "url":
print indent + "\t" + node["NAME"] + " " + node["URL"]
for node in foldernode["children"]:
if node["type"] == "folder":
dumptree(node, level + 1)
def main():
root, flat = tree(sys.argv[1])
flatdict = dict((obj["NAME"], obj) for obj in flat)
dippa = flatdict["dippa"]
dumptree(dippa)
if __name__ == "__main__":
main()