-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastructure.py
executable file
·78 lines (63 loc) · 2.59 KB
/
datastructure.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
#!/usr/bin/python
import sys
import re
import pprint
import json
# Datastructure Hashes
hierarchy_pc = dict()
hierarchy = dict()
# Read data from STDIN
# STDIN must be comma separated data with the following columns:
# userid,parentid,childid,childname,idirectory
#-------------------------------------------------------------------------------
# Step 1 of 3
#
# Read lines from STDIN
# STDIN must be comma separated data with the following columns:
# userid,parentid,childid,childname,idirectory
#
#-------------------------------------------------------------------------------
for line in sys.stdin:
if re.match(r'^\s*$|^\s*#', line): continue
line_split = line.split(',')
userid = line_split[0]
parentId = line_split[1]
childId = line_split[2]
childName = line_split[3]
isDirectory = line_split[4]
# Determine if datastructure element is a folder or file
# this needs improvement
if re.match(r'1', isDirectory): xtype = 'folder'
if re.match(r'0', isDirectory): xtype = 'file'
hierarchy_pc[parentId] = {}
hierarchy_pc[parentId]['children'] = {}
hierarchy_pc[parentId]['children'][childId] = {}
# Add name & type of datastructure element to hash for $parentId & $childId
hierarchy_pc[parentId]['children'][childId]['name'] = childName
hierarchy_pc[parentId]['children'][childId]['type'] = xtype
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Step 2 of 3
#
# Determine if a child is also a parent; if so, add it to $hierarchy
#
#-------------------------------------------------------------------------------
for parentId in hierarchy_pc:
hierarchy[parentId] = {}
hierarchy[parentId]['children'] = {}
for childId in hierarchy_pc[parentId]['children']:
hierarchy[parentId]['children'] = hierarchy_pc[parentId]['children']
if childId in hierarchy_pc:
hierarchy[parentId]['children'] = {}
hierarchy[parentId]['children'][childId] = {}
hierarchy[parentId]['children'][childId]['children'] = {}
hierarchy[parentId]['children'][childId]['children'] = hierarchy_pc[childId]['children']
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Step 3 of 3
#
# Delete keys whose parent is not <HOME>. They are aliens.
#
#-------------------------------------------------------------------------------
# Not ready yet
pprint.pprint(hierarchy)