aboutsummaryrefslogtreecommitdiffstats
path: root/parse/dir_map.py
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-02-08 16:20:01 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-02-08 16:20:01 -0500
commit7c647198fc40e72ef6ca23c2484bf49eba2079ee (patch)
tree270de30a1cd896382a0954b2d8f7994ca84f88b7 /parse/dir_map.py
parentd312e270ed5c2926c8651291a4026062213876f8 (diff)
Added translation from DirMap to ReducedTupleTable.
Diffstat (limited to 'parse/dir_map.py')
-rw-r--r--parse/dir_map.py43
1 files changed, 29 insertions, 14 deletions
diff --git a/parse/dir_map.py b/parse/dir_map.py
index 51a1390..e4d83e6 100644
--- a/parse/dir_map.py
+++ b/parse/dir_map.py
@@ -3,18 +3,16 @@ import numpy as np
3 3
4from collections import defaultdict 4from collections import defaultdict
5 5
6class TreeNode(object):
7 def __init__(self, parent = None):
8 self.parent = parent
9 self.children = defaultdict(lambda : TreeNode(self))
10 self.values = []
11
12class DirMap(object): 6class DirMap(object):
13 def __init__(self, in_dir = None): 7 class Node(object):
14 self.root = TreeNode(None) 8 def __init__(self, parent = None):
9 self.parent = parent
10 self.children = defaultdict(lambda : DirMap.Node(self))
11 self.values = []
12
13 def __init__(self):
14 self.root = DirMap.Node(None)
15 self.values = [] 15 self.values = []
16 if in_dir:
17 self.__read(in_dir)
18 16
19 def add_values(self, path, values): 17 def add_values(self, path, values):
20 node = self.root 18 node = self.root
@@ -22,18 +20,18 @@ class DirMap(object):
22 node = node.children[p] 20 node = node.children[p]
23 node.values += values 21 node.values += values
24 22
25 def reduce(self): 23 def remove_childless(self):
26 def reduce2(node): 24 def remove_childless2(node):
27 for key in node.children.keys(): 25 for key in node.children.keys():
28 child = node.children[key] 26 child = node.children[key]
29 reduce2(child) 27 remove_childless2(child)
30 if not (child.children or child.values): 28 if not (child.children or child.values):
31 node.children.pop(key) 29 node.children.pop(key)
32 30
33 if len(node.values) == 1: 31 if len(node.values) == 1:
34 node.values = [] 32 node.values = []
35 33
36 reduce2(self.root) 34 remove_childless2(self.root)
37 35
38 def write(self, out_dir): 36 def write(self, out_dir):
39 def write2(path, node): 37 def write2(path, node):
@@ -42,6 +40,7 @@ class DirMap(object):
42 # Leaf 40 # Leaf
43 with open("/".join(path), "w") as f: 41 with open("/".join(path), "w") as f:
44 arr = [",".join([str(b) for b in n]) for n in node.values] 42 arr = [",".join([str(b) for b in n]) for n in node.values]
43 arr = sorted(arr, key=lambda x: x[0])
45 f.write("\n".join(arr) + "\n") 44 f.write("\n".join(arr) + "\n")
46 elif not os.path.isdir(out_path): 45 elif not os.path.isdir(out_path):
47 os.mkdir(out_path) 46 os.mkdir(out_path)
@@ -53,6 +52,21 @@ class DirMap(object):
53 52
54 write2([out_dir], self.root) 53 write2([out_dir], self.root)
55 54
55
56 def leafs(self):
57 def leafs2(path, node):
58 if node.children:
59 for child_name, child_node in node.children.iteritems():
60 path += [child_name]
61 for leaf in leafs2(path, child_node):
62 yield leaf
63 path.pop()
64 elif path:
65 yield (path, node.values)
66
67 for leaf in leafs2([], self.root):
68 yield leaf
69
56 @staticmethod 70 @staticmethod
57 def read(in_dir): 71 def read(in_dir):
58 dir_map = DirMap() 72 dir_map = DirMap()
@@ -72,6 +86,7 @@ class DirMap(object):
72 86
73 stripped = path if path.find(in_dir) else path[len(in_dir):] 87 stripped = path if path.find(in_dir) else path[len(in_dir):]
74 path_arr = stripped.split("/") 88 path_arr = stripped.split("/")
89 path_arr = filter(lambda x: x != '', path_arr)
75 90
76 dir_map.add_values(path_arr, values) 91 dir_map.add_values(path_arr, values)
77 92