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 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 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 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')