-
Notifications
You must be signed in to change notification settings - Fork 0
/
libsea.py
338 lines (275 loc) · 11.2 KB
/
libsea.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
# Copyright 2019 Aldous Rice-Leech
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import collections
import re
from collections import *
import itertools
def _indent(string, amount):
if amount == 0: return string
lines = string.split('\n')
for i, line in enumerate(lines):
# This is a check for new line case. Kinda hacky: FIXME
if line:
lines[i] = amount * " " + line
return '\n'.join(lines)
def indentable(func):
def wrapper(*args, **kwargs):
indent = kwargs.pop('indent', 0)
result = func(*args, **kwargs)
return _indent(result, indent)
return wrapper
class Element:
"""
A base class for describing LibSea types.
"""
TERM = ";"
def _label(self, name, labels=False):
if labels:
return "@"+name+"="
else:
return ""
def __str__(self):
return self.serialize(labels=True)
@indentable
def serialize(self, labels=False):
"""
The following standards are defined for any classes implementing serialize
1. The method must accept a "labels" keyword argument
2. The method must accept an "indent" keyword argument, or
be wrapped by the @indentable decorator
3. The method should respect the indent value passed in and apply it to all
output lines, or be wrapped by the @indentable decorator
4. The method should indent internally where appropriate
5. The return value should not end in a ';' separator, or in a newline
"""
return ""
def ser(obj, *args, **kwargs):
if type(obj) == str:
obj = String(obj)
elif type(obj) == float:
obj = Double(obj)
elif type(obj) == int:
obj = Integer(obj)
elif type(obj) == list:
obj = List(obj)
elif isinstance(obj, collections.Mapping):
obj = Object(obj)
elif isinstance(obj, tuple):
try:
fields = obj._fields
except AttributeError:
fields = ()
obj = Tuple(fields, obj)
elif obj == None:
obj = ""
try:
string = obj.serialize(*args, **kwargs)
except AttributeError:
string = str(obj)
return string
def to_libsea_string(obj):
t = type(obj)
if t == str:
obj = '"' + str(obj) + '"'
else:
obj = str(obj)
class Value(Element):
def __init__(self, value=None):
self.value = value
@indentable
def serialize(self, labels=False):
"""
Write out the python string representation of this object
"""
return str(self.value)
class String(Value):
def __init__(self, value):
self.value = value.replace('\r', r'\r').replace('\n', r'\n')
@indentable
def serialize(self, labels=False):
return '"' + re.sub(r'([\\"\n\r\t\f\b|])', r'\\\1', str(self.value)) + '"'
class Bool(Value):
@indentable
def serialize(self, labels=False):
if self.value:
return 'T'
else:
return 'F'
class Identifier(Value):
@indentable
def serialize(self, labels=False):
return '$'+str(self.value)
class Double(Value):
pass
class Integer(Value):
pass
class Tuple(Element):
def __init__(self, names, values):
self.names = names
self.values = values
@indentable
def serialize(self, labels=True):
if self.names:
return "{" + ' '.join(self._label(name, labels=labels)+ser(value, labels=labels)+self.TERM for name,value in zip(self.names, self.values)) + "}"
else:
return "{" + ' '.join(ser(value, labels=labels)+self.TERM for value in self.values) + "}"
Link = namedtuple('Link', ('source','destination'))
attrValue = namedtuple('attrValue', ('id', 'value'))
class List(Element):
def __init__(self, items):
self.items = items
@indentable
def serialize(self, labels=False):
output = "[\n"
output += ",\n".join(ser(item, indent=4, labels=labels) for item in self.items)
output += "\n]"
return output
class Object(Element):
def __init__(self, dict):
self.dict = dict
@indentable
def serialize(self, labels=True):
output = "{\n"
for key, value in self.dict.items():
output += _indent(self._label(key, labels=labels)+ser(value, labels=labels)+self.TERM+"\n", 4)
output += "}"
return output
def attributeDefinition():
return OrderedDict(
name=None,
type=None,
default=None,
nodeValues=[],
linkValues=[],
pathValues=[],
)
def enumeration():
return OrderedDict(
name=None,
enumerators=[],
)
def qualifier():
return OrderedDict(
type=None,
name=None,
description=None,
attributes=[],
)
class Graph(Element):
def __init__(self, name="Graph"):
self.name = name
self.nodes = []
self.links = []
self.paths = []
self.enums = []
self.attributes = []
self.attribute_order = {}
self.qualifiers = []
@indentable
def print_var(self, name, value, labels=True):
return self._label(name, labels=labels)+ser(value, labels=labels)+self.TERM+"\n"
@indentable
def serialize(self, labels=True, comments=True):
output = "Graph\n"
output += "{\n"
if comments: output += _indent("# Metadata\n", 4)
output += self.print_var('name', self.name, indent=4, labels=labels)
output += self.print_var('description', self.description, indent=4, labels=labels)
if comments: output += "\n"
if comments: output += _indent("# Lengths\n", 4)
output += self.print_var('numNodes', len(self.nodes), indent=4, labels=labels)
output += self.print_var('numLinks', len(self.links), indent=4, labels=labels)
output += self.print_var('numPaths', len(self.paths), indent=4, labels=labels)
output += self.print_var('numPathLinks', 0, indent=4, labels=labels) #FIXME
if comments: output += "\n"
if comments: output += _indent("# Structural information\n", 4)
output += self.print_var('links', self.links, indent=4, labels=labels)
output += self.print_var('paths', self.paths, indent=4, labels=labels)
if comments: output += "\n"
if comments: output += _indent("# Attributes\n", 4)
output += self.print_var('enumerations', self.enums, indent=4, labels=labels)
output += self.print_var('attributeDefinitions', self.attributes, indent=4, labels=labels)
output += self.print_var('qualifiers', self.qualifiers, indent=4, labels=labels)
if comments: output += "\n"
if comments: output += _indent("# Visualization\n", 4)
output += self.print_var('filters', None, indent=4, labels=labels)
output += self.print_var('selectors', None, indent=4, labels=labels)
output += self.print_var('displays', None, indent=4, labels=labels)
output += self.print_var('presentations', None, indent=4, labels=labels)
if comments: output += "\n"
if comments: output += _indent("# Interface\n", 4)
output += self.print_var('presentationMenus', None, indent=4, labels=labels)
output += self.print_var('displayMenus', None, indent=4, labels=labels)
output += self.print_var('selectorMenus', None, indent=4, labels=labels)
output += self.print_var('filterMenus', None, indent=4, labels=labels)
output += self.print_var('attributeMenus', None, indent=4, labels=labels)
output += "}"
return output
def from_graph(graph):
LibSea = Graph()
LibSea.name = graph.graph_attr.get('name', 'Graph')
LibSea.description = graph.graph_attr.get('description', 'Generated by libsea.py')
attributes = defaultdict(lambda: defaultdict(dict))
# Lookup table to get libsea node id from graphviz node
node_order = {}
for i, node in enumerate(graph.nodes()):
node_order[node] = i
# For every attribute that this node has
for attribute_name in node.attr:
value = node.attr[attribute_name]
if value == "True": value = Value('T')
elif value == "False": value = Value('F')
# Add it to the appropriate atrtibute definition as a key-value store of id : value
attributes[attribute_name]['nodeValues'][i] = value
# This will be converted to {@id=i; @value=node.attr[attribute_name]}
if len(node_order) != len(graph.nodes()):
raise Exception("Node collision")
# replace with links = List()
links = []
for i, edge in enumerate(graph.edges()):
id1 = node_order[edge[0]]
id2 = node_order[edge[1]]
links.append(Link(id1, id2))
for attribute_name in edge.attr:
value = edge.attr[attribute_name]
if value == "True": value = Value('T')
elif value == "False": value = Value('F')
attributes[attribute_name]['linkValues'][i] = value
LibSea.nodes = node_order
LibSea.links = links
for i, name in enumerate(attributes):
data = attributes[name]
attr = attributeDefinition()
attr['name'] = Identifier(name)
# FIXME: root and tree_link need to be bool, not string
attr['type'] = Value('string')
attr['nodeValues'] = [attrValue(id, value) for id, value in data['nodeValues'].items()]
attr['linkValues'] = [attrValue(id, value) for id, value in data['linkValues'].items()]
LibSea.attributes.append(attr)
LibSea.attribute_order[name] = i
LibSea.attributes[LibSea.attribute_order['root']]['type'] = Value('bool')
LibSea.attributes[LibSea.attribute_order['tree_link']]['type'] = Value('bool')
LibSea.qualifiers.append(OrderedDict(
type=Identifier("spanning_tree"),
name=Identifier("default_spanning_tree"),
description="Spanning tree for walrus",
attributes=[
(LibSea.attribute_order['root'], Identifier('root')),
(LibSea.attribute_order['tree_link'], Identifier('tree_link')),
]
))
return LibSea
def add_tree(self, tree):
pass