-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds.py
133 lines (96 loc) · 2.68 KB
/
ds.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
# -*- coding: utf-8 -*-
"""
ds
----------------
data structure of regular.express
:author: Prev(prevdev@gmail.com)
"""
import ast
class EnvInfo :
def __init__(self, model) :
result = model.get('/repos/rexpress/environments/git/trees/master?recursive=1', baseurl='https://api.github.com', headers={})
self.data = {}
# init env info data
for info in result['tree'] :
p = info['path'].split('/')
if len(p) != 2 : continue
env_group = p[0]
env_child = p[1].split('.json')[0]
if env_group not in self.data :
self.data[ env_group ] = EnvGroup()
if env_child == '_info' :
self.data[ env_group ].name = env_group
self.data[ env_group ].info = model.macro('#git-env', env_group + '/_info.json#' + info['sha'])
else :
self.data[ env_group ].append_child( EnvChild(
name = env_child,
info = model.macro('#git-env', env_group + '/' + env_child + '.json#' + info['sha']),
))
@property
def groups(self) :
return self.data
def getdict(self, group, child=None, loose_mode=False) :
if group not in self.data :
if loose_mode : {'name': child or group}
else : return None
eg = self.data[ group ]
if child is None :
eg = ast.literal_eval( str(eg) )
return eg
else :
c = eg.get_child(child)
if c is not None :
c = ast.literal_eval( str(c) )
return c
else :
if loose_mode : return {'name': child}
else : return None
def json(self) :
import ast, json
d = ast.literal_eval( str(self.groups) )
return json.dumps(d)
class EnvBase :
def __init__(self, name, info) :
self.name = name
self.info = info
def __repr__(self):
return str({
'name': self.name,
'info': self.info,
})
class EnvGroup(EnvBase) :
def __init__(self, name=None, info=None) :
EnvBase.__init__(self, name, info)
self.children = {}
def append_child(self, env_child) :
self.children[ env_child.name ] = env_child
env_child.parent = self
def get_child(self, child_name) :
if child_name in self.children :
return self.children[ child_name ]
else :
return None
def __repr__(self):
if self.children is None :
return EnvBase.__repr__(self)
else :
return str({
'name': self.name,
'info': self.info,
'children': self.children,
})
#dict( map( lambda k,v : (k, str(v)), self.children.keys(), self.children.values() ) )
class EnvChild(EnvBase) :
def __init__(self, name, info, parent=None) :
EnvBase.__init__(self, name, info)
self.parent = parent
def __repr__(self):
if self.parent is None : nparent = None
else :
nparent = EnvGroup(self.parent.name, self.parent.info)
nparent.children = None
return str({
'name': self.name,
'info': self.info,
'parent': nparent,
})