def set_plugin(self, name, attrs): try: kwargs = dict([str(unquote(attr.name)), unquote(attr.value)] for attr in attrs) plugins.load([name], diagram=self, **kwargs) except: msg = "WARNING: fail to load plugin: %s\n" % name raise AttributeError(msg)
def instantiate(self, rack, tree): for stmt in tree.stmts: if isinstance(stmt, parser.Attr): try: rack.set_attribute(stmt) except AttributeError: self.diagram.set_attribute(stmt) elif isinstance(stmt, parser.RackItem): if stmt.number is None: if len(rack.nodes) > 0: item = rack.nodes[-1] number = item.number + item.colheight else: number = 1 else: number = stmt.number item = RackItem(number, unquote(stmt.label)) item.set_attributes(stmt.attrs) rack.nodes.append(item) elif isinstance(stmt, parser.Rack): _rack = Rack() self.diagram.racks.append(_rack) self.instantiate(_rack, stmt) elif isinstance(stmt, parser.AttrPlugin): self.diagram.set_plugin(stmt.name, stmt.attrs)
def instantiate(self, group, block, tree): for stmt in tree.stmts: if isinstance(stmt, parser.Node): node = DiagramNode.get(stmt.id) node.set_attributes(stmt.attrs) self.append_node(node, group) elif isinstance(stmt, parser.Edge): self.instantiate_edge(group, block, stmt) elif isinstance(stmt, parser.Group): node = NodeGroup.get(None) self.instantiate(node, block, stmt) self.diagram.groups.append(node) elif isinstance(stmt, parser.Attr): if block: block.set_attribute(stmt) else: group.set_attribute(stmt) elif isinstance(stmt, parser.Separator): sep = EdgeSeparator(stmt.type, unquote(stmt.value)) sep.group = group self.diagram.separators.append(sep) group.edges.append(sep) elif isinstance(stmt, parser.Fragment): subblock = AltBlock(stmt.type, stmt.id) if block: subblock.xlevel = block.xlevel + 1 self.diagram.altblocks.append(subblock) self.instantiate(group, subblock, stmt) if block: for edge in subblock.edges: block.edges.append(edge) elif isinstance(stmt, parser.Extension): if stmt.type == 'class': name = unquote(stmt.name) Diagram.classes[name] = stmt elif stmt.type == 'plugin': self.diagram.set_plugin(stmt.name, stmt.attrs) return group
def get(cls, elemid): if not elemid: elemid = uuid.generate() unquote_id = unquote(elemid) if unquote_id not in cls.namespace: obj = cls(elemid) cls.namespace[unquote_id] = obj return cls.namespace[unquote_id]
def set_attributes(self, network, attrs=None): if attrs is None: attrs, network = network, None for attr in attrs: if attr.name == 'address': address = re.sub('\s*,\s*', '\n', unquote(attr.value)) self.address[network] = address else: self.set_attribute(attr)
def on_attr_changing(self, node, attr): value = unquote(attr.value) if attr.name == 'background': return self.on_background_changing(node, value) elif attr.name == 'resizable': return self.on_resizable_changing(node, value) elif attr.name == 'label': return self.on_label_changing(node, value) else: return True
def instantiate(self, group, tree): for stmt in tree.stmts: # Translate Node having group attribute to Group if isinstance(stmt, parser.Node): group_attr = [a for a in stmt.attrs if a.name == 'group'] if group_attr: group_id = group_attr[-1] stmt.attrs.remove(group_id) if group_id.value != group.id: stmt = parser.Group(group_id.value, [stmt]) # Instantiate statements if isinstance(stmt, parser.Node): node = DiagramNode.get(stmt.id) node.set_attributes(stmt.attrs) self.belong_to(node, group) elif isinstance(stmt, parser.Edge): from_nodes = [DiagramNode.get(n) for n in stmt.from_nodes] to_nodes = [DiagramNode.get(n) for n in stmt.to_nodes] for node in from_nodes + to_nodes: self.belong_to(node, group) for node1 in from_nodes: for node2 in to_nodes: edge = DiagramEdge.get(node1, node2) edge.set_dir(stmt.edge_type) edge.set_attributes(stmt.attrs) elif isinstance(stmt, parser.Group): subgroup = NodeGroup.get(stmt.id) subgroup.level = group.level + 1 self.belong_to(subgroup, group) self.instantiate(subgroup, stmt) elif isinstance(stmt, parser.Attr): group.set_attribute(stmt) elif isinstance(stmt, parser.Extension): if stmt.type == 'class': name = unquote(stmt.name) Diagram.classes[name] = stmt elif stmt.type == 'plugin': self.diagram.set_plugin(stmt.name, stmt.attrs, config=self.config) elif isinstance(stmt, parser.Statements): self.instantiate(group, stmt) group.update_order() return group
def __init__(self, elemid): self.id = unquote(elemid) self.label = '' self.xy = XY(0, 0) self.group = None self.drawable = False self.order = 0 self.color = self.basecolor self.width = None self.height = None self.colwidth = 1 self.colheight = 1 self.stacked = False
def __init__(self, elemid): super(DiagramNode, self).__init__(elemid) self.label = unquote(elemid) or '' self.numbered = None self.icon = None self.background = None self.description = None self.rotate = 0 self.drawable = True self.href = None plugins.fire_node_event(self, 'created')
def on_attr_changing(self, node, attr): value = unquote(attr.value) if attr.name == 'background' and value.startswith('math://'): image = self.create_formula_image(value.replace('math://', '')) if image: formula_images.append(image) node.background = image else: node.background = None return False else: return True
def instantiate(self, group, tree, lane=None): for stmt in tree.stmts: if isinstance(stmt, parser.Node): node = DiagramNode.get(stmt.id) node.set_attributes(stmt.attrs) self.belong_to(node, lane) elif isinstance(stmt, parser.Edge): from_nodes = [DiagramNode.get(n) for n in stmt.from_nodes] to_nodes = [DiagramNode.get(n) for n in stmt.to_nodes] for node in from_nodes + to_nodes: self.belong_to(node, lane) for node1 in from_nodes: for node2 in to_nodes: edge = DiagramEdge.get(node1, node2) edge.set_dir(stmt.edge_type) edge.set_attributes(stmt.attrs) elif isinstance(stmt, parser.Lane): _lane = NodeGroup.get(stmt.id) if _lane not in self.diagram.lanes: self.diagram.lanes.append(_lane) self.instantiate(group, stmt, _lane) elif isinstance(stmt, parser.Attr): if lane: lane.set_attribute(stmt) else: self.diagram.set_attribute(stmt) elif isinstance(stmt, parser.Extension): if stmt.type == 'class': name = unquote(stmt.name) Diagram.classes[name] = stmt if stmt.type == 'plugin': self.diagram.set_plugin(stmt.name, stmt.attrs) elif isinstance(stmt, parser.Statements): self.instantiate(group, stmt, lane) group.update_order() return group
def instantiate(self, tree): for stmt in tree.stmts: if isinstance(stmt, parser.Attr): self.diagram.set_attribute(stmt) elif isinstance(stmt, parser.FieldItem): item = FieldItem(stmt.begin, stmt.end, unquote(stmt.label)) item.set_attributes(stmt.attrs) if item.number is None: if len(self.diagram.fields) == 0: item.number = 0 else: last_item = self.diagram.fields[-1] item.number = last_item.number + last_item.colwidth self.diagram.fields.append(item) elif isinstance(stmt, parser.AttrPlugin): self.diagram.set_plugin(stmt.name, stmt.attrs)
def set_attribute(self, attr): name = attr.name value = unquote(attr.value) if name == 'class': if value in Diagram.classes: klass = Diagram.classes[value] self.set_attributes(klass.attrs) else: msg = "Unknown class: %s" % value raise AttributeError(msg) elif hasattr(self, "set_%s" % name): getattr(self, "set_%s" % name)(value) elif name in self.int_attrs: setattr(self, name, int(value)) elif hasattr(self, name) and not callable(getattr(self, name)): setattr(self, name, value) else: class_name = self.__class__.__name__ msg = "Unknown attribute: %s.%s" % (class_name, attr.name) raise AttributeError(msg)
def on_attr_changing(self, node, attr): if attr.name not in ('background', 'icon'): return True value = unquote(attr.value) matched = prefix.match(value) if not matched: return True code = icons.get(matched.group(1)) if code is None: warning('unknown octicon: %s', value) setattr(node, attr.name, None) return False if value not in icon_images: options = to_option(matched.group(2)) image = self.create_octicon_image(code, options) icon_images[value] = image setattr(node, attr.name, icon_images[value]) return False
def set_plugin(self, name, attrs, **kwargs): kwargs.update(dict([str(unquote(attr.name)), unquote(attr.value)] for attr in attrs)) plugins.load([name], diagram=self, **kwargs)
def test_unquote(self): self.assertEqual('test', unquote('"test"')) self.assertEqual('test', unquote("'test'")) self.assertEqual("'half quoted", unquote("'half quoted")) self.assertEqual('"half quoted', unquote('"half quoted'))
def instantiate(self, network, group, tree): for stmt in tree.stmts: if isinstance(stmt, parser.Node): node = DiagramNode.get(stmt.id) node.set_attributes(network, stmt.attrs) if group: if (node.group and node.group != self.diagram and (group != node.group)): msg = "DiagramNode could not belong to two groups" raise RuntimeError(msg) node.group = group group.nodes.append(node) elif node.group is None: node.group = self.diagram if network is not None: if network not in node.networks: node.networks.append(network) if node not in network.nodes: network.nodes.append(node) if node not in self.diagram.nodes: self.diagram.nodes.append(node) elif isinstance(stmt, parser.Network): subnetwork = Network.get(stmt.id) subnetwork.label = stmt.id if subnetwork not in self.diagram.networks: self.diagram.networks.append(subnetwork) substmt = namedtuple('Statements', 'stmts')([]) for s in stmt.stmts: if isinstance(s, parser.Attr): subnetwork.set_attribute(s) else: substmt.stmts.append(s) self.instantiate(subnetwork, group, substmt) elif isinstance(stmt, parser.Group): subgroup = NodeGroup.get(stmt.id) if subgroup not in self.diagram.groups: self.diagram.groups.append(subgroup) substmt = namedtuple('Statements', 'stmts')([]) for s in stmt.stmts: if isinstance(s, parser.Attr): subgroup.set_attribute(s) else: substmt.stmts.append(s) self.instantiate(network, subgroup, substmt) elif isinstance(stmt, parser.Peer): nodes = [] for edge in stmt.edges: nodes.append(DiagramNode.get(edge.from_node)) nodes.append(DiagramNode.get(edge.to_node)) for node in nodes: if node.group is None: node.group = self.diagram if node not in self.diagram.nodes: self.diagram.nodes.append(node) if len(nodes[0].networks) == 0: nw = Network.create_anonymous([nodes[0]]) if nw: self.diagram.networks.append(nw) for edge in stmt.edges: from_node = DiagramNode.get(edge.from_node) to_node = DiagramNode.get(edge.to_node) _nodes = [from_node, to_node] nw = Network.create_anonymous(_nodes, edge.attrs) if nw: self.diagram.networks.append(nw) elif isinstance(stmt, parser.Route): nodes = [DiagramNode.get(n) for n in stmt.nodes] for node1, node2 in zip(nodes[:-1], nodes[1:]): route = Route(node1, node2) route.set_attributes(stmt.attrs) self.diagram.routes.append(route) elif isinstance(stmt, parser.Attr): self.diagram.set_attribute(stmt) elif isinstance(stmt, parser.Extension): if stmt.type == 'class': name = unquote(stmt.name) Diagram.classes[name] = stmt elif stmt.type == 'plugin': self.diagram.set_plugin(stmt.name, stmt.attrs) elif isinstance(stmt, parser.Statements): self.instantiate(network, group, stmt) return network
def set_plugin(self, name, attrs): kwargs = dict([str(unquote(attr.name)), unquote(attr.value)] for attr in attrs) plugins.load([name], diagram=self, **kwargs)