-
Notifications
You must be signed in to change notification settings - Fork 1
/
x2html-tree-alt.py
83 lines (65 loc) · 3.06 KB
/
x2html-tree-alt.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
# Convert XML to an interactive tree
# More expensive HTML version
from pathlib import Path
from lxml import etree as xet
listroot = xet.Element('div', attrib = {'class' : 'tree'})
listtree = xet.ElementTree()
listtree._setroot(listroot)
driveroot = xet.SubElement(xet.SubElement(listroot, 'ul'), 'li')
is_gdrive_tree_root = True
filetree = xet.parse(str(Path('out/drive-tree.xml')))
fileroot = filetree.getroot()
def sizePretty(sizeString):
sizeDigits = len(sizeString)
cSize = int(sizeString)
sizeFactor = 0
sizePrefix = ''
if (sizeDigits > 3 and sizeDigits <= 6):
sizeFactor = 1
sizePrefix = 'K'
elif (sizeDigits > 6 and sizeDigits <= 9):
sizeFactor = 2
sizePrefix = 'M'
elif (sizeDigits > 9):
sizeFactor = 3
sizePrefix = 'G'
cSize /= 1024.0**sizeFactor
return cSize, sizePrefix
def extractNodeLevel(file_current_parent,list_current_parent):
global is_gdrive_tree_root
divelem = xet.SubElement(list_current_parent, 'div')
# The name element
nameelem = xet.SubElement(divelem, 'span', attrib = {'class' : 'name'})
xet.SubElement(nameelem, 'i', attrib = {'class' : 'fa fa-folder-open'})
# nameelem.text = child.attrib['name']
if (is_gdrive_tree_root):
xet.SubElement(nameelem, 'span').text = ' gdrive-tree-root'
else:
xet.SubElement(nameelem, 'span').text = ' ' + file_current_parent.attrib['name']
# The size element
sizeelem = xet.SubElement(divelem, 'span', attrib = {'class' : 'size'})
cSize, sizePrefix = sizePretty(file_current_parent.attrib['cSize'])
sizeelem.text = str(f"{cSize:.2f} {sizePrefix}B")
is_gdrive_tree_root = False
list_current_parent = xet.SubElement(list_current_parent, 'ul', attrib = {'class' : 'nested'})
for child in file_current_parent:
listelem = xet.SubElement(list_current_parent,'li')
if(len(list(child)) > 0):
extractNodeLevel(child, listelem)
else:
divelem = xet.SubElement(listelem, 'div')
# The name element
nameelem = xet.SubElement(divelem, 'span', attrib = {'class' : 'name'})
xet.SubElement(nameelem, 'i', attrib = {'class' : 'fa fa-file'})
# nameelem.text = child.attrib['name']
xet.SubElement(nameelem, 'span').text = ' ' + child.attrib['name']
extractNodeLevel(fileroot,driveroot)
# SOLVED: Encoding works improperly, Bengali letters remain as escape sequences!
# Apparently works with decode(encoding='utf-8') now, after adding the encoding to `with open(...)`?
# Write the generated tree to an HTML file;
# Setting 'pretty-print' to True automatically adds newlines and indents
with open(Path('out/drive-tree-alt.html'), 'w+', encoding = 'utf-8') as f:
for line in open(Path('res/tree-template-2.html'), 'rt', encoding = 'utf-8').readlines():
f.write(line)
if line.find('<div id="collapseDVR3" class="panel-collapse collapse in">') > -1:
f.write('\n' + xet.tostring(listroot, pretty_print = True, encoding = 'utf-8', method = 'html').decode(encoding='utf-8') + '\n')