-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tree.py
executable file
·458 lines (384 loc) · 17.2 KB
/
Tree.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/usr/bin/env python3
# external modules
import openpyxl
from openpyxl.styles import Font, Alignment
from openpyxl.styles.colors import BLUE
# my modules
import DCC
import Config as CF
import FileSys
import MyUtil
# Set Excel Styles
# Excel hyperlink style is calibri 11, underline blue
font_url_style = Font(color = BLUE, underline = 'single')
bold_style = Font(bold = True)
align_hv_cen_style = Alignment(horizontal = 'center', vertical = 'center')
align_ver_cen_style = Alignment(vertical = 'center')
align_hv_cen_wrap_style = Alignment(horizontal = 'center', vertical = 'center', wrap_text = True)
#function uses kwarg argument to check a load flag is given. If a flag is given then the function will load the existing file. It gives the option to load from the disk or recreate the json file
def return_tree(s, target, rootfilename, **kwargs):
load_flag = kwargs.get('Load')
if FileSys.file_check_json(s, rootfilename) == True:
if load_flag == None:
print('File [',rootfilename,'] already exists - use existing file? ', sep = '', end = '')
load_flag = MyUtil.get_yn('[Load from disk = Y, re-create = N] (Y/N)? ')
if load_flag == True:
tr = FileSys.file_read_json(rootfilename)
FileSys.file_write_json(tr, rootfilename, path = CF.dccfilepath)
return(tr)
tr = get_tree(s,target, **kwargs)
FileSys.file_write_json(tr, rootfilename, path = CF.dccfilepath)
# print_tree(s, tr)
return(tr)
def flat_tree(tree, key, list):
branch = tree[key]
for doc in branch['documents']:
list.append(doc)
for other in branch['others']:
list.append(other)
for col in branch['collections']:
list.append(col)
flat_tree(tree, col, list)
return(list)
#function that iterates through the tree and prints document information
def iter_print_tree(s, tree, key, indent):
branch = tree[key]
for doc in branch['documents']:
nameData = DCC.prop_get(s, doc, InfoSet = 'DocBasic')
print(indent + doc)
print(indent+' DCC Title: ',nameData['title'],sep='')
print(indent+' TMT Doc. Num.: ',nameData['tmtnum'],sep='')
print(indent+' Owner: ',nameData['owner-name'])
print(indent+' Filename: ',nameData['filename'],sep='')
print(indent+' Date Modified: ',nameData['date'],sep='')
print(indent+' URL: ','https://docushare.tmt.org/docushare/dsweb/ServicesLib/',doc,'/View',sep='')
for other in branch['others']:
nameData = DCC.prop_get(s, other, InfoSet = 'Title')
print(indent+other, ':', nameData['title'])
for col in branch['collections']:
nameData = DCC.prop_get(s, col, InfoSet = 'Title')
print(indent+col, ':', nameData['title'])
print(indent+' URL: ','https://docushare.tmt.org/docushare/dsweb/ServicesLib/',col,sep='')
iter_print_tree(s, tree, col, indent+' ')
# This function calls the iterative version of print tree
def print_tree(s,tree):
iter_print_tree(s, tree, 'root', '')
# creates a hyperlink using the url given in arguments
def href_str(name, url):
return('<a href="'+url+'">'+name+'</a>')
def url_view(handle):
return('https://docushare.tmt.org/docushare/dsweb/ServicesLib/' + handle + '/View')
def url_access(handle):
return('https://docushare.tmt.org/docushare/dsweb/View/' + handle)
def url_perm(handle):
return('https://docushare.tmt.org/docushare/dsweb/ServicesLib/' + handle + '/Permissions')
def url_ver(handle):
return('https://docushare.tmt.org/docushare/dsweb/ServicesLib/' + handle + '/History')
def url_loc(handle):
return('https://docushare.tmt.org/docushare/dsweb/ServicesLib/' + handle + '/Location')
# function that iterates through html tree
def iter_html_tree(s, htmlfile, tree, key, indent, **kwargs):
branch = tree[key]
keyword = kwargs.get('Keyword', '')
for doc in branch['documents']:
nameData = DCC.prop_get(s, doc, InfoSet = 'DocBasic')
if keyword in nameData['keywords']:
print(doc)
print('<div style="text-indent: ',str(indent),'em;">',file=htmlfile,sep='')
print('<p>',file=htmlfile,sep='')
print(href_str(nameData['title'],url_access(doc)),file=htmlfile,sep='')
print('[',file=htmlfile,sep='')
print(href_str(doc,url_view(doc)),file=htmlfile,sep='')
print(', ',file=htmlfile,sep='')
print(href_str('Perm',url_perm(doc)),file=htmlfile,sep='')
print(', ',file=htmlfile,sep='')
print(href_str('Ver',url_ver(doc)),file=htmlfile,sep='')
print(', ',file=htmlfile,sep='')
print(href_str('Loc',url_loc(doc)),file=htmlfile,sep='')
print(']',file=htmlfile,sep='')
print('</p>',file=htmlfile,sep='')
print('</div>',file=htmlfile,sep='')
print('<div style="text-indent: ',str(indent+2),'em;">',file=htmlfile,sep='')
print('<p>TMT Doc. Num.: ',nameData['tmtnum'],'</p>',file=htmlfile,sep='')
print('<p>Owner: ',nameData['owner-name'],file=htmlfile,sep='')
print('<p>Filename: ',nameData['filename'],file=htmlfile,sep='')
print('<p>Date Modified: ',nameData['date'],file=htmlfile,sep='')
print('</div>',file=htmlfile,sep='')
for other in branch['others']:
print(other)
nameData = DCC.prop_get(s, other, InfoSet = 'Title')
print('<div style="text-indent: ',str(indent),'em;">',file=htmlfile,sep='')
print('<p>',file=htmlfile,sep='')
print('[',file=htmlfile,sep='')
print(href_str(other,url_access(other)),file=htmlfile,sep='')
print(']',file=htmlfile,sep='')
print(href_str(nameData['title'],url_view(other)),file=htmlfile,sep='')
print('[',file=htmlfile,sep='')
print(href_str('Perm',url_perm(other)),file=htmlfile,sep='')
print(']',file=htmlfile,sep='')
print('</p>',file=htmlfile,sep='')
print('</div>',file=htmlfile,sep='')
for col in branch['collections']:
print(col)
nameData = DCC.prop_get(s, col, InfoSet = 'Title')
print('<div style="text-indent: ',str(indent),'em;">',file=htmlfile,sep='')
print('<p></p>',file=htmlfile,sep='')
print('<p>',file=htmlfile,sep='')
print(href_str(nameData['title'],url_access(col)),file=htmlfile,sep='')
print('[',file=htmlfile,sep='')
print(href_str(col,url_view(col)),file=htmlfile,sep='')
print(', ',file=htmlfile,sep='')
print(href_str('Perm',url_perm(col)),file=htmlfile,sep='')
print(', ',file=htmlfile,sep='')
print(href_str('Ver',url_ver(col)),file=htmlfile,sep='')
print(', ',file=htmlfile,sep='')
print(href_str('Loc',url_loc(col)),file=htmlfile,sep='')
print(']',file=htmlfile,sep='')
print('</p>',file=htmlfile,sep='')
print('</div>',file=htmlfile,sep='')
iter_html_tree(s, htmlfile, tree, col, indent+2, **kwargs)
def html_tree(s,tree,froot,**kwargs):
htmlfile = open(CF.reportfilepath + froot+'.html','w+')
print('<!DOCTYPE html><html><body>',file = htmlfile)
print('<style> p {line-height: 0.35} </style>', file = htmlfile)
print('<h1>',froot,'</h1>', file = htmlfile)
iter_html_tree(s, htmlfile, tree, 'root', 0, **kwargs)
print('</body></html>',file = htmlfile)
htmlfile.close
# function sets excel workbook headings for html tree
def xls_tree_headings(ws):
col = 1
ws.cell(row = 1, column = col).value = "ID"
col += 1
ws.cell(row = 1, column = col).value = "Collection Handle"
col += 1
ws.cell(row = 1, column = col).value = "Collection Title"
col += 1
ws.cell(row = 1, column = col).value = "Document Title"
col += 1
ws.cell(row = 1, column = col).value = "TMT Doc Number"
col += 1
ws.cell(row = 1, column = col).value = "Document Handle"
col += 1
ws.cell(row = 1, column = col).value = "Version Handle"
col += 1
ws.cell(row = 1, column = col).value = "Owner"
col += 1
ws.cell(row = 1, column = col).value = "File Name"
col += 1
ws.cell(row = 1, column = col).value = "Date Modified"
colcnt = col+1
# sets alignment and font style for headings
for col in range(1, colcnt):
ws.cell(row = 1, column = col).alignment = align_hv_cen_wrap_style
ws.cell(row = 1, column = col).font = bold_style
# Set column widths
ws.column_dimensions["A"].width = 5.0
ws.column_dimensions["B"].width = 15.0
ws.column_dimensions["C"].width = 25.0
ws.column_dimensions["D"].width = 60.0
ws.column_dimensions["E"].width = 25.0
ws.column_dimensions["F"].width = 15.0
ws.column_dimensions["G"].width = 15.0
ws.column_dimensions["H"].width = 20.0
ws.column_dimensions["I"].width = 60.0
ws.column_dimensions["J"].width = 35.0
# Prints data to xls document
def xls_print_ssrow(ws, collData, docData, ssrow):
col = 1
# Column 1: ID
ws.cell(row = ssrow, column = col).value = ssrow-1
ws.cell(row = ssrow, column = col).alignment = align_hv_cen_style
col += 1
# Column 2: Coll Handle
ws.cell(row = ssrow, column = col).value = collData['handle']
ws.cell(row = ssrow, column = col).font = font_url_style
ws.cell(row = ssrow, column = col).hyperlink = url_view(collData['handle'])
ws.cell(row = ssrow, column = col).alignment = Alignment(vertical = 'center')
col += 1
# Column 3: Coll Name
ws.cell(row = ssrow, column = col).value = collData['title']
ws.cell(row = ssrow, column = col).font = font_url_style
ws.cell(row = ssrow, column = col).hyperlink = url_access(collData['handle'])
ws.cell(row = ssrow, column = col).alignment = Alignment(vertical = 'center')
col += 1
# Column 4: Doc Title
ws.cell(row = ssrow, column = col).value = docData['title']
ws.cell(row = ssrow, column = col).font = font_url_style
ws.cell(row = ssrow, column = col).hyperlink = url_access(docData['handle'])
ws.cell(row = ssrow, column = col).alignment = Alignment(wrap_text = True, vertical = 'center')
col += 1
# Column 5: TMT Document Number
ws.cell(row = ssrow, column = col).value = docData['tmtnum']
ws.cell(row = ssrow, column = col).alignment = Alignment(wrap_text = True, vertical = 'center')
col += 1
# Column 6: Doc Handle
ws.cell(row = ssrow, column = col).value = docData['handle']
ws.cell(row = ssrow, column = col).font = font_url_style
ws.cell(row = ssrow, column = col).hyperlink = url_view(docData['handle'])
ws.cell(row = ssrow, column = col).alignment = Alignment(vertical = 'center')
col += 1
# Column 7: Ver Handle
ws.cell(row = ssrow, column = col).value = docData['Versions']['prefver']
ws.cell(row = ssrow, column = col).font = font_url_style
ws.cell(row = ssrow, column = col).hyperlink = url_view(docData['Versions']['prefver'])
ws.cell(row = ssrow, column = col).alignment = Alignment(vertical = 'center')
col += 1
# Column 8: Owner
ws.cell(row = ssrow, column = col).value = docData['owner-name']
ws.cell(row = ssrow, column = col).alignment = align_hv_cen_style
col += 1
# Column 9: File Name
ws.cell(row = ssrow, column = col).value = docData['filename']
ws.cell(row = ssrow, column = col).alignment = Alignment(wrap_text = True, vertical = 'center')
col += 1
# Column 10: Date Modified
ws.cell(row = ssrow, column = col).value = docData['date']
ws.cell(row = ssrow, column = col).alignment = align_hv_cen_style
# Global Variable ssrow
ssrow = 2
#iterates through xls version of html tree
def xls_tree_iter(s,ws,tree,col, **kwargs):
global ssrow
# Write and format the headings in Excel
xls_tree_headings(ws)
collData = DCC.prop_get(s, col, InfoSet = 'Title')
branch = tree[col]
keyword = kwargs.get('Keyword', '')
for doc in branch['documents']:
print(col,doc)
docData = DCC.prop_get(s, doc, InfoSet = 'DocBasic')
docData['Versions'] = DCC.prop_get(s,doc,InfoSet = 'Versions',WriteProp = True)
if keyword in docData['keywords']:
xls_print_ssrow(ws, collData, docData, ssrow)
ssrow += 1
for newcol in branch['collections']:
xls_tree_iter(s,ws,tree,newcol,**kwargs)
def xls_tree(s,tree,col,fname, **kwargs):
# Open the spreadsheet
wb = openpyxl.Workbook()
ws = wb.worksheets[0]
xls_tree_iter(s,ws,tree,col,**kwargs)
wb.save(CF.reportfilepath + fname + '.xls')
# Funciton builds html tree and uses prop_get to get information of the target collection
# Uses kwarg argument to recognize exclude list
def build_tree(s, keyname, target, tree, **kwargs):
# kwargs options:
# Exclude - List of handles to not be included in the tree
excludeList = kwargs.get('Exclude',[])
documents = []
collections = []
others = []
dict = {}
fd = DCC.prop_get(s, target, InfoSet = 'CollCont', Depth = '1')
for idx,d in enumerate(fd):
handle = d['name'][1]
print(handle)
if not handle in excludeList:
if idx == 0:
dict['parent'] = handle
else:
if 'Document' in handle:
documents.append(handle)
elif 'Collection' in handle:
collections.append(handle)
else:
others.append(handle)
dict['collections'] = collections
dict['documents'] = documents
dict['others'] = others
tree[keyname] = dict
for col in collections:
if not col in excludeList:
tree = build_tree(s, col, col, tree, **kwargs)
return(tree)
def get_tree(s, collhandle, **kwargs):
return(build_tree(s, collhandle, collhandle, build_root(collhandle), **kwargs))
def get_flat_tree(tree):
fl = flat_tree(tree, 'root', [])
return(fl)
def build_root(collhandle):
tree = {}
tree['root'] = {'collections' : [collhandle], 'documents' : [], 'others' : []}
return(tree)
def test_tree():
collhandle = 'Collection-286'
exclude = ['Collection-7337','Document-21244', 'Document-26018']
# Login to DCC
s = DCC.login(CF.dcc_url + CF.dcc_login)
print('excluding:',exclude)
tree = get_tree(s, collhandle, Exclude = exclude)
print_tree(s,tree)
print('\n\n')
for branch in tree:
print(branch+': ',tree[branch])
fl = flat_tree(tree, 'root', [])
print(fl)
if __name__ == '__main__':
print("Running module test code for",__file__)
# test_tree()
# Login to DCC
s = DCC.login(CF.dcc_url + CF.dcc_login)
# froot = 'Listing of Scott Collection'
# coll = 'Collection-286'
# froot = 'Listing M1CS Preliminary Design Review Collections'
# coll = 'Collection-10725'
#
# tr = return_tree(s, coll, froot)
# # print_tree(s,tr)
# html_tree(s,tr,froot)
# load_flag = True
# froot = 'Listing of WFOS for Suijian'
# coll = 'Collection-7798'
# tr = return_tree(s, coll, froot)
# html_tree(s,tr,froot)
froot = 'Test of HTML bug'
coll = 'Collection-2656'
tr = return_tree(s, coll, froot)
html_tree(s,tr,froot)
# froot = 'Listing of IRIS'
# coll = 'Collection-2463'
# froot = 'Listing of STR CID'
# coll = 'Collection-10669'
# coll = 'Collection-11377'
# tr = return_tree(s, coll, froot)
# xls_tree(s,tr,coll,froot)
# html_tree(s,tr,froot)
# froot = 'Listing of STR CID'
# coll = 'Collection-10668'
# tr = return_tree(s, coll, froot)
# html_tree(s,tr,froot)
# froot = 'Listing of 2013 [Nov 12-14] Telescope Structure System Preliminary Design Review'
# coll = 'Collection-7095'
# tr = return_tree(s, coll, froot, Load = load_flag)
# html_tree(s,tr,froot)
#
# froot = 'Listing of 2014 [April 15 -16] Telescope Structure System Controls-Software Preliminary Design Review'
# coll = 'Collection-7857'
# tr = return_tree(s, coll, froot, Load = load_flag)
# html_tree(s,tr,froot)
#
# froot = 'Listing of 2014 [April 15 -16] Telescope Structure System Controls-Software Preliminary Design Review'
# coll = 'Collection-7857'
# tr = return_tree(s, coll, froot, Load = load_flag)
# html_tree(s,tr,froot)
#
# froot = 'Listing of 2014 [Nov 18-20] Segment Handling System Preliminary Design Review'
# coll = 'Collection-8674'
# tr = return_tree(s, coll, froot, Load = load_flag)
# html_tree(s,tr,froot)
#
# froot = 'Listing of 2015 [Feb 17-20] Telescope Structure (STR) Final Design Review Presentation (FDRP1)'
# coll = 'Collection-9067'
# tr = return_tree(s, coll, froot, Load = load_flag)
# html_tree(s,tr,froot)
#
# froot = 'Listing of 2015 [Jul 27-29] Telescope Structure (STR) Final Design Review Presentation (FDRP2)'
# coll = 'Collection-10071'
# tr = return_tree(s, coll, froot, Load = load_flag)
# html_tree(s,tr,froot)
#
# froot = 'Listing of 2015 [Oct 08-09] Telescope Structure (STR) Long Lead Procurement Review (LPR)'
# coll = 'Collection-11045'
# tr = return_tree(s, coll, froot, Load = load_flag)
# html_tree(s,tr,froot)