# # Functions for interacting with the pcu_type_port table in the database # # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # from PLC.Faults import * from PLC.Parameter import Parameter from PLC.Storage.AlchemyObject import AlchemyObj class PCUProtocolType(AlchemyObj): """ Representation of a row in the pcu_protocol_type table. To use, instantiate with a dict of values. """ tablename = 'pcu_protocol_type' join_tables = [] fields = { 'pcu_protocol_type_id': Parameter(int, "PCU protocol type identifier", primary_key=True), 'pcu_type_id': Parameter(int, "PCU type identifier"), 'port': Parameter(int, "PCU port"), 'protocol': Parameter(str, "Protocol"), 'supported': Parameter(bool, "Is the port/protocol supported by PLC") } def validate_port(self, port): # make sure port is not blank if not port: raise PLCInvalidArgument, "Port must be specified" return port def validate_protocol(self, protocol): # make sure port is not blank if not len(protocol): raise PLCInvalidArgument, "protocol must be specified" return protocol def sync(self, commit=True, validate=True): AlchemyObj.sync(self, commit=commit, validate=validate) if 'pcu_protocol_type_id' not in self: AlchemyObj.insert(self, dict(self)) else: filter = {'pcu_protocol_type_id', self['pcu_protocol_type_id']} AlchemyObj.update(self, filter, dict(self)) def delete(self, commit=True): assert 'pcu_protocol_type_id' in self AlchemyObj.delete(self, dict(self)) class PCUProtocolTypes(list): """ Representation of the pcu_protocol_types table in the database. """ def __init__(self, api, protocol_type_filter = None, columns = None): if not protocol_type_filter: protocol_types = PCUProtocolType().select() elif isinstance(protocol_type_filter, (list, tuple, set, int, long)): protocol_types = PCUProtocolType.select(filter={'pcu_protocol_type_id': protocol_type_filter}) elif isinstance(protocol_type_filter, dict): protocol_types = PCUProtocolType.select(filter= protocol_type_filter) else: raise PLCInvalidArgument, "Wrong pcu_protocol_type filter %r"%protocol_type_filter self.extend(protocol_types)