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
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Table import Row, Table
13 from PLC.Filter import Filter
15 class PCUProtocolType(Row):
17 Representation of a row in the pcu_protocol_type table. To use,
18 instantiate with a dict of values.
21 table_name = 'pcu_protocol_type'
22 primary_key = 'pcu_protocol_type_id'
25 'pcu_protocol_type_id': Parameter(int, "PCU protocol type identifier"),
26 'pcu_type_id': Parameter(int, "PCU type identifier"),
27 'port': Parameter(int, "PCU port"),
28 'protocol': Parameter(str, "Protocol"),
29 'supported': Parameter(bool, "Is the port/protocol supported by PLC")
32 def validate_port(self, port):
33 # make sure port is not blank
36 raise PLCInvalidArgument, "Port must be specified"
40 def validate_protocol(self, protocol):
41 # make sure port is not blank
43 raise PLCInvalidArgument, "protocol must be specified"
47 class PCUProtocolTypes(Table):
49 Representation of the pcu_protocol_types table in the database.
52 def __init__(self, api, protocol_type_filter = None, columns = None):
53 Table.__init__(self, api, PCUProtocolType, columns)
55 sql = "SELECT %s FROM pcu_protocol_type WHERE True" % \
56 ", ".join(self.columns)
58 if protocol_type_filter is not None:
59 if isinstance(protocol_type_filter, (list, tuple, set)):
60 # Separate the list into integers and strings
61 ints = filter(lambda x: isinstance(x, (int, long)), protocol_type_filter)
62 protocol_type_filter = Filter(PCUProtocolType.fields, {'pcu_protocol_type_id': ints})
63 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "OR")
64 elif isinstance(protocol_type_filter, dict):
65 protocol_type_filter = Filter(PCUProtocolType.fields, protocol_type_filter)
66 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "AND")
67 elif isinstance (protocol_type_filter, int):
68 protocol_type_filter = Filter(PCUProtocolType.fields, {'pcu_protocol_type_id':[protocol_type_filter]})
70 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "AND")
72 raise PLCInvalidArgument, "Wrong pcu_protocol_type filter %r"%protocol_type_filter