2 # Functions for interacting with the pcu_type_port table in the database
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
11 from PLC.Faults import *
12 from PLC.Parameter import Parameter
13 from PLC.Table import Row, Table
14 from PLC.Filter import Filter
16 class PCUProtocolType(Row):
18 Representation of a row in the pcu_protocol_type table. To use,
19 instantiate with a dict of values.
22 table_name = 'pcu_protocol_type'
23 primary_key = 'pcu_protocol_type_id'
26 'pcu_protocol_type_id': Parameter(int, "PCU protocol type identifier"),
27 'pcu_type_id': Parameter(int, "PCU type identifier"),
28 'port': Parameter(int, "PCU port"),
29 'protocol': Parameter(str, "Protocol"),
30 'supported': Parameter(bool, "Is the port/protocol supported by PLC")
33 def validate_port(self, port):
34 # make sure port is not blank
37 raise PLCInvalidArgument, "Port must be specified"
41 def validate_protocol(self, protocol):
42 # make sure port is not blank
44 raise PLCInvalidArgument, "protocol must be specified"
48 class PCUProtocolTypes(Table):
50 Representation of the pcu_protocol_types table in the database.
53 def __init__(self, api, protocol_type_filter = None, columns = None):
54 Table.__init__(self, api, PCUProtocolType, columns)
56 sql = "SELECT %s FROM pcu_protocol_type WHERE True" % \
57 ", ".join(self.columns)
59 if protocol_type_filter is not None:
60 if isinstance(protocol_type_filter, (list, tuple, set)):
61 # Separate the list into integers and strings
62 ints = filter(lambda x: isinstance(x, (int, long)), protocol_type_filter)
63 protocol_type_filter = Filter(PCUProtocolType.fields, {'pcu_protocol_type_id': ints})
64 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "OR")
65 elif isinstance(protocol_type_filter, dict):
66 protocol_type_filter = Filter(PCUProtocolType.fields, protocol_type_filter)
67 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "AND")
68 elif isinstance (protocol_type_filter, int):
69 protocol_type_filter = Filter(PCUProtocolType.fields, {'pcu_protocol_type_id':[protocol_type_filter]})
71 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "AND")
73 raise PLCInvalidArgument, "Wrong pcu_protocol_type filter %r"%protocol_type_filter