# # Functions for interacting with the node_types table in the database # # from PLC.Faults import * from PLC.Parameter import Parameter from PLC.Storage.AlchemyObject import AlchemyObj class NodeType(AlchemyObj): """ Representation of a row in the node_types table. To use, instantiate with a dict of values. """ tablename = 'node_types' join_tables = ['nodes'] fields = { 'node_type': Parameter(str, "Node type", max = 20, primary_key=True), } def validate_node_type(self, name): # Make sure name is not blank if not len(name): raise PLCInvalidArgument, "Node type must be specified" # Make sure node type does not alredy exist conflicts = NodeTypes(self.api, [name]) if conflicts: raise PLCInvalidArgument, "Node type name already in use" return name def sync(self, commit=True, validate=True): AlchemyObj.sync(self, commit=commit, validate=validate) AlchemyObj.insert(self, dict(self)) def delete(self, commit=True): assert 'node_type' in self AlcemyObj.delete(self, dict(self)) class NodeTypes(list): """ Representation of the node_types table in the database. """ def __init__(self, api, node_types = None): if not node_types: result = NodeType().select() elif isinstance(node_types, (StringTypes, list, tuple, set)): result = NodeType().select(filter={'node_type': node_types}) else: raise PLCInvalidArgument, "Wrong node type filter %r" % node_types self.extend(result)