-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookmarks_jstree.py
executable file
·151 lines (126 loc) · 3.84 KB
/
bookmarks_jstree.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
146
147
148
149
150
151
#!/usr/bin/python
#
# Bookmark Tools - JsTree
# written by Nick Shin - [email protected]
# created Feb 9 2011
#
# ==============================================================================
# Changelist:
#
# updated Mar 4 2017:
# + changed license to Unlicense
# + upgraded to jstree 3.3.3 (which is single page load friendly)
import getopt # getopt GetoptError
import sys # exit stdin argv
import json # loads
# ================================================================================
# JSON_BKMKS_NODES {{{
# ================================================================================
def jstree_nodes( jsonobj, jstree, _depth = "node_0" ):
if not jsonobj.has_key('children') or len( jsonobj['children'] ) <= 0:
return ""
# ----------------------------------------
i = 0;
for child in jsonobj['children']:
nodeid = _depth + '_' + str(i)
i += 1
title = child['title'] if child.has_key('title') and len( child['title'] ) > 0 else "UN-NAMED"
if title.startswith('***'):
title = '<b>' + title.replace( '***', '' ) + '</b>'
parentMarker = "#" if _depth == "node_0" else _depth # special case
if child['type'] == "text/x-moz-place-container":
jstree.append({
"id" : nodeid,
"parent" : parentMarker,
"text" : title,
"type" : "folder" })
elif child['type'] == "text/x-moz-place":
jstree.append({
"id" : nodeid,
"parent" : parentMarker,
"text" : title,
"a_attr" : { "href" : child['uri'] },
"type" : "file" })
else:
jstree.append({
"id" : nodeid,
"parent" : parentMarker,
"text" : "----------------------------------------------------------------------------------------------------",
"type" : "line" })
# ----------------------------------------
# drill down children array
j = 0;
for child in jsonobj['children']:
nodeid = _depth + "_" + str(j)
j += 1
if child['type'] == "text/x-moz-place-container":
jstree_nodes( child, jstree, nodeid )
# ================================================================================
# JSON_BKMKS_NODES }}}
# MAIN {{{
# ================================================================================
def usage():
print "Usage: " + sys.argv[0] + " [options]"
print """
Options are:
-h, --help
This usage message.
-i filename, --input=filename
Read JSON data from [ filename ] or else, read data from [ stdin ].
-o filename, --output=filename
Output results to [ filename ] or else, output to [ stdout ].
Special Note: Titles that start with *** (triple stars) will be displayed in BOLD.
"""
def main():
# args ----------------------------------------
try:
opts, args = getopt.getopt( sys.argv[1:],
"ho:i:",
[ "help",
"output=", "input="
] )
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(2)
datain = None
dataout = None
for o, a in opts:
if o in ( "-h", "--help" ):
usage()
sys.exit()
elif o in ( "-o", "--output" ):
dataout = a
elif o in ( "-i", "--input" ):
datain = a
else:
assert False, "unhandled option"
# read and prep json text ----------------------------------------
# json.loads() doesn't like newlines...
data = ""
if datain == None:
for _ in sys.stdin:
data += _.strip()
else:
with open( datain, 'r' ) as f:
for _ in f:
data += _.strip()
f.close()
# WORK ----------------------------------------
objs = json.loads( data )
jstree = []
jstree_nodes( objs, jstree )
# write out result ----------------------------------------
dump = json.dumps(jstree)
if dataout == None:
print dump
else:
with open( dataout, 'w' ) as f:
f.write( dump )
f.close()
if __name__ == "__main__":
main()
# ================================================================================
# MAIN }}}
# ================================================================================
# vim:ts=4:noexpandtab