def load_from_dynetml(self, mn_text, properties_to_include=None, properties_to_ignore=None, nodeclasses_to_include=None, nodeclasses_to_ignore=None, networks_to_include=None, networks_to_ignore=None): """ Parses XML containing a Meta-Network and loads the contents :param mn_text: XML containing a Meta-Network :param properties_to_include: a list of nodeclass properties that should be included :param properties_to_ignore: a list of nodeclass properties that should be ignored :param nodeclasses_to_include: a list of nodeclasses that should be included :param nodeclasses_to_ignore: a list of nodeclasses that should be ignored :param networks_to_include: a list of networks that should be included :param networks_to_ignore: a list of networks that should be ignored :param mn_text: str or unicode :param properties_to_include: lists :param properties_to_ignore: lists :param nodeclasses_to_include: lists :param nodeclasses_to_ignore: lists :param networks_to_include: lists :param networks_to_ignore: lists :raise TypeError: if mn_text isn't a str or unicode """ dmlpu.check_type(mn_text, 'mn_text', (unicode, str)) mn_tag = etree.XML(mn_text) if mn_tag.tag != 'MetaNetwork': return for attrib_key in mn_tag.attrib: self.attributes[attrib_key] = dmlpu.format_prop(mn_tag.attrib[attrib_key]) self.load_from_tag(mn_tag, properties_to_include, properties_to_ignore, nodeclasses_to_include, nodeclasses_to_ignore, networks_to_include, networks_to_ignore)
def create_nodeset_property(self, nodeclass_name, nodeset_name, property_name, type_str, singlevalued_bool): """ Create a new nodeset property :param nodeclass_name: the name of the parent of nodeset_name :param nodeset_name: the name of the nodeset that should get the new property :param property_name: the name of the property to be added :param type_str: the type of the parameter :param singlevalued_bool: whether or not the parameter should be single-valued :type nodeclass_name: str or unicode :type nodeset_name: str or unicode :type property_name: str or unicode :type type_str: str or unicode :type singlevalued_bool: bool :raise KeyError: If a property named property_name already exists. :raise TypeError: If type_str isn't a str or unicode :raise ValueError: If type_str isn't "number", "date", "text", "categoryText", or "URI" :raise TypeError: If singlevalued_bool isn't a bool """ self.__validate_tree_branch(nodeclass_name, nodeset_name) dmlpu.check_key(property_name, self.__node_tree[nodeclass_name][nodeset_name], 'property {0} already exists for nodeset {1}'.format(property_name, nodeset_name), False) dmlpu.check_type(type_str, 'type_str', (str, unicode)) if type_str not in ['number', 'date', 'text', 'categoryText', 'URI']: raise ValueError('type_str must be "number", "date" "text", "categoryText", or "URI"; got {0}'. format(type_str)) dmlpu.check_type(singlevalued_bool, 'singlevalued_bool', bool) self.__node_tree[nodeclass_name][nodeset_name][0][property_name] = type_str, singlevalued_bool
def union_nodesets(self, nodeclass_name, *args): """ Takes two nodesets and combines them in a new nodeset. Properties and entries from the first nodeset override those from the second nodeset. :param nodeclass_name: The name of the parent nodeclass of nodeset_one_name and nodeset_two_name :param nodeset_one_name: The name of the first nodeset in the union. :param nodset_two_name: The name of the second ndeset in the union. :param other_nodesets: union_nodesets takes a *args argument, so any number of nodesets is acceptable :param new_nodeset_name: The name of the new nodeset to be created :type nodeset_one_name: str or unicode :type nodeset_two_name: str or unicode :type new_nodeset_name: str or unicode """ if len(args) < 3: raise ValueError('Need at least 3 argumets; got {0}'.format(len(args))) for nodeset_name in args[:-1]: self.__validate_tree_branch(nodeclass_name, nodeset_name) dmlpu.check_type(args[-1], 'new_nodeset_name', (str, unicode)) dmlpu.check_key(args[-1], 'union_nodeset', self.__node_tree[nodeclass_name], nodeclass_name, False) merge_nodeset = self.__node_tree[nodeclass_name][args[-2]] for i in range(len(args)-3, -1, -1): for entry in self.__node_tree[nodeclass_name][args[i]][1]: merge_nodeset[1][entry] = self.__node_tree[nodeclass_name][args[i]][1][entry] for entry in self.__node_tree[nodeclass_name][args[i]][0]: merge_nodeset[0][entry] = self.__node_tree[nodeclass_name][args[i]][0][entry] self.__node_tree[nodeclass_name][args[-1]] = merge_nodeset
def write_dynetml(self, out_file_path): """:param str|unicode out_file_path: Write the meta-network to this path.""" dmlpu.check_type(out_file_path, 'out_file_path', (str, unicode)) if os.path.exists(out_file_path) and os.path.isdir(out_file_path): raise IOError('out_file_path cannot be a directory') # bs = self.convert_to_dynetml(True) # # with codecs.open(out_file_path, 'w', 'utf8') as outfile: # outfile.write(bs.prettify()) xml_root = self.convert_to_dynetml() with codecs.open(out_file_path, 'w', 'utf8') as outfile: outfile.write('<?xml version="1.0" standalone="yes"?>\n\n') outfile.write(etree.tostring(xml_root, pretty_print=True))
def set_node_property(self, nodeclass_name, nodeset_name, node_name, property_name, value): """ Set the value of a node property :param str|unicode nodeclass_name: the name of the parent of nodeset_name :param str|unicode nodeset_name: the name of the parent of node_name :param str|unicode node_name: the name of the node whose property is being set :param str|unicode property_name: the name of the property to be set :param str|unicode|bool|float|datetime.datetime value: the value of the parameter """ self.__validate_tree_branch(nodeclass_name, nodeset_name, node_name) dmlpu.check_type(property_name, 'property_name', (str, unicode)) dmlpu.check_key(property_name, 'property_name', self.__node_tree[nodeclass_name][nodeset_name][0], nodeset_name) self.__node_tree[nodeclass_name][nodeset_name][1][node_name][property_name] = \ dmlpu.format_prop(value, self.__node_tree[nodeclass_name][nodeset_name][0][property_name][0])
def convert_to_dynetml(self, is_entire_file=False): """ Converts the graph to dynetml and returns a BeautifulSoup tag :param is_entire_file: if True, wraps value as a soup. If False, returns the top tag :type is_entire_file: bool :return: bs4.element.Tag :raise TypeError: if is_entire_file isn't a bool """ dmlpu.check_type(is_entire_file, 'is_entire_file', bool) bs = BeautifulSoup(features='xml') bs.append(bs.new_tag('MetaNetwork')) for attr in self.attributes: bs.MetaNetwork[attr] = dmlpu.unformat_prop(self.attributes[attr]) bs.MetaNetwork.append(dmlpu.get_property_identities_tag(self.propertyIdentities)) bs.MetaNetwork.append(bs.new_tag('properties')) for key in self.properties: prop_tag = bs.new_tag('property') prop_tag['id'] = key prop_tag['value'] = dmlpu.unformat_prop(self.properties[key]) bs.MetaNetwork.properties.append(prop_tag) bs.MetaNetwork.append(bs.new_tag('nodes')) for class_type in self.__node_tree: for class_id in self.__node_tree[class_type]: nodeclass_tag = bs.new_tag('nodeclass', type=class_type, id=class_id) nodeclass_tag.append(dmlpu.get_property_identities_tag(self.__node_tree[class_type][class_id][0])) for key in self.__node_tree[class_type][class_id][1]: node_tag = bs.new_tag('node', id=key) for attr in self.__node_tree[class_type][class_id][1][key][0]: node_tag[attr] = dmlpu.unformat_prop(self.__node_tree[class_type][class_id][1][key][0][attr]) node_tag.append(dmlpu.get_properties_tag(self.__node_tree[class_type][class_id][1][key][1])) nodeclass_tag.append(node_tag) bs.MetaNetwork.nodes.append(nodeclass_tag) networks_tag = self._get_networks_tag() bs.MetaNetwork.networks.append(networks_tag) if not is_entire_file: bs = bs.MetaNetwork return bs
def create_node(self, nodeclass_name, nodeset_name, node_name, property_dict=None): """ Create a node in a give nodeset in a give nodeclass. If a set of properties are specified, add the properties. :param str|unicode nodeclass_name: name of the parent of nodeset_name :param str|unicode nodeset_name: name of the parent of node_name :param str|unicode node_name: name of the node to be created :param dict|None property_dict: A dictionary of properties to assign the new node. """ self.__validate_tree_branch(nodeclass_name, nodeset_name) dmlpu.check_type(node_name, 'node_name', (str, unicode)) dmlpu.check_key(node_name, 'node_name', self.__node_tree[nodeclass_name][nodeset_name][1], nodeset_name, False) dmlpu.check_type(property_dict, 'property_dict', (dict, None)) for property_name in property_dict.keys(): dmlpu.check_key( property_name, 'property_name', self.__node_tree[nodeclass_name][nodeset_name][0], '{0} properties'.format(nodeset_name)) self.__node_tree[nodeclass_name][nodeset_name][1][node_name] = property_dict
def create_nodeset_property(self, nodeclass_name, nodeset_name, property_name, type_str, singlevalued_bool): """ Create a new nodeset property :param str|unicode nodeclass_name: the name of the parent of nodeset_name :param str|unicode nodeset_name: the name of the nodeset that should get the new property :param str|unicode property_name: the name of the property to be added :param str|unicode type_str: the type of the parameter :param bool singlevalued_bool: whether or not the parameter should be single-valued """ self.__validate_tree_branch(nodeclass_name, nodeset_name) dmlpu.check_key(property_name, 'property_name', self.__node_tree[nodeclass_name][nodeset_name][0], nodeset_name, False) dmlpu.check_type(type_str, 'type_str', (str, unicode)) if type_str not in ['number', 'date', 'text', 'categoryText', 'URI']: raise ValueError('type_str must be "number", "date" "text", "categoryText", or "URI"; got {0}'. format(type_str)) dmlpu.check_type(singlevalued_bool, 'singlevalued_bool', bool) self.__node_tree[nodeclass_name][nodeset_name][0][property_name] = type_str, singlevalued_bool
def add_node(self, nodeclass_name, nodeset_name, node_name, property_dict=None): """ Add a node to a give nodeset in a give nodeclass. If a set of properties are specified, add the properties. :param nodeclass_name: name of the parent of nodeset_name :param nodeset_name: name of the parent of node_name :param node_name: name of the node to be created :param property_dict: A dictionary of properties to assign the new node. :type nodeclass_name: str or unicode :type nodeset_name: str or unicode :type node_name: str or unicode :type property_dict: dict or None """ self.__validate_tree_branch(nodeclass_name, nodeset_name) dmlpu.check_type(node_name, 'node_name', (str, unicode)) dmlpu.check_key(node_name, 'node_name', self.__node_tree[nodeclass_name][nodeset_name][1], nodeset_name, False) dmlpu.check_type(property_dict, 'property_dict', (dict, None)) for property_name in property_dict.keys(): dmlpu.check_key(property_name, 'property_name', self.__node_tree[nodeclass_name][nodeset_name][0], '{0} properties'.format(nodeset_name)) self.__node_tree[nodeclass_name][nodeset_name][1][node_name] = property_dict
def set_node_property(self, nodeclass_name, nodeset_name, node_name, property_name, value): """ Set the value of a node property :param nodeclass_name: the name of the parent of nodeset_name :param nodeset_name: the name of the parent of node_name :param node_name: the name of the node whose property is being set :param property_name: the name of the property to be set :param value: the value of the parameter :type nodeclass_name: str or unicode :type nodeset_name: str or unicode :type node_name: str or unicode :type property_name: str or unicode :type value: str, unicode, or the appropriate type specified in dmlpu.format_prop :raise TypeError: if property_name isn't a str or unicode """ self.__validate_tree_branch(nodeclass_name, nodeset_name, node_name) dmlpu.check_type(property_name, 'property_name', (str, unicode)) dmlpu.check_key(property_name, 'property_name', self.__node_tree[nodeclass_name][nodeset_name][0], nodeset_name) self.__node_tree[nodeclass_name][nodeset_name][1][node_name][property_name] = \ dmlpu.format_prop(value, self.__node_tree[nodeclass_name][nodeset_name][0][property_name][0])
def rename_node(self, nodeclass_name, nodeset_name, node_name, new_node_name): """ Rename a particular node, both in the node tree and in the networks containing it. :param str|unicode nodeclass_name: name of the parent of nodeset_name :param str|unicode nodeset_name: name of the parent of node_name :param str|unicode node_name: current node name :param str|unicode new_node_name: new name for the node """ self.__validate_tree_branch(nodeclass_name, nodeset_name, node_name) dmlpu.check_type(new_node_name, 'new_node_name', (str, unicode)) dmlpu.check_key(new_node_name, 'new_node_name', self.__node_tree[nodeclass_name][nodeset_name][1], nodeset_name, False) self.__node_tree[nodeclass_name][nodeset_name][1][new_node_name] = \ self.__node_tree[nodeclass_name][nodeset_name][1][node_name] del self.__node_tree[nodeclass_name][nodeset_name][1][node_name] # We assume 'id' exists. If it doesn't, the data has bigger problems. self.__node_tree[nodeclass_name][nodeset_name][new_node_name]['id'] = new_node_name self._rename_network_nodes(nodeclass_name, nodeset_name, node_name, new_node_name)
def rename_node(self, nodeclass_name, nodeset_name, node_name, new_node_name): """ Rename a particular node, both in the node tree and in the networks containing it. :param nodeclass_name: name of the parent of nodeset_name :param nodeset_name: name of the parent of node_name :param node_name: current node name :param new_node_name: new name for the node :type nodeclass_name: str or unicode :type nodeset_name: str or unicode :type node_name: str or unicode :type new_node_name: str or unicode :raise TypeError: if new_node_name is not a str or unicode :raise KeyError: if new_node_name is already a node in the node class """ self.__validate_tree_branch(nodeclass_name, nodeset_name, node_name) dmlpu.check_type(new_node_name, 'new_node_name', (str, unicode)) dmlpu.check_key(new_node_name, self.__node_tree[nodeclass_name][nodeset_name], '{0} is already a key in nodeset {1}'.format(new_node_name, nodeset_name), False) self.__node_tree[nodeclass_name][nodeset_name][new_node_name] = \ self.__node_tree[nodeclass_name][nodeset_name][node_name] del self.__node_tree[nodeclass_name][nodeset_name][node_name] self._rename_network_nodes(nodeclass_name, nodeset_name, node_name, new_node_name)
def __validate_tree_branch(self, nodeclass_name, nodeset_name=None, node_name=None): """ Verify that a particular nodeclass, nodeset, or node exists in __node_tree :param unicde|str nodeclass_name: The name of a nodeclass :param unicode|str|None nodeset_name: The name of a nodeset :param unicode|str|None node_name: The name of a node """ dmlpu.check_type(nodeclass_name, 'nodeclass_name', (str, unicode)) dmlpu.check_type(nodeset_name, 'nodeset_name', (str, unicode, None)) dmlpu.check_type(node_name, 'node_name', (str, unicode, None)) dmlpu.check_key(nodeclass_name, 'nodeclass_name', self.__node_tree, 'self.__node_tree') if nodeset_name is not None: dmlpu.check_key(nodeset_name, 'nodeset_name', self.__node_tree[nodeclass_name], nodeclass_name) if node_name is not None: dmlpu.check_key(node_name, 'node_name', self.__node_tree[nodeclass_name][nodeset_name][1], 'nodeset_name')
def __validate_tree_branch(self, nodeclass_name, nodeset_name=None, node_name=None): """ Verify that a particular nodeclass, nodeset, or node exists in the node tree :param nodeclass_name: The name of a nodeclass :param nodeset_name: The name of a nodeset :param node_name: The name of a node :type nodeclass_name: unicode or str :type nodeset_name: unicode, str, or None :type node_name: unicode, str, or None :raise TypeError: if nodeclass_name, nodeset_name, or node_name is not a str, unicode or None :raise KeyError: if nodeclass_name, nodeset_name, or node_name cannot be found in self.__node_tree """ dmlpu.check_type(nodeclass_name, 'nodeclass_name', (str, unicode)) dmlpu.check_type(nodeset_name, 'nodeset_name', (str, unicode, None)) dmlpu.check_type(node_name, 'node_name', (str, unicode, None)) dmlpu.check_key(nodeclass_name, 'nodeclass_name', self.__node_tree, 'self.__node_tree') if nodeset_name is not None: dmlpu.check_key(nodeset_name, 'nodeset_name', self.__node_tree[nodeclass_name], nodeclass_name) if node_name is not None: dmlpu.check_key(node_name, 'node_name', self.__node_tree[nodeclass_name][nodeset_name][1], 'nodeset_name')