Setting tag plcapi-5.4-2
[plcapi.git] / PLC / PCUProtocolTypes.py
1 #
2 # Functions for interacting with the pcu_type_port table in the database
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7
8 from PLC.Faults import *
9 from PLC.Parameter import Parameter
10 from PLC.Table import Row, Table
11 from PLC.Filter import Filter
12
13 class PCUProtocolType(Row):
14     """
15     Representation of a row in the pcu_protocol_type table. To use,
16     instantiate with a dict of values.
17     """
18
19     table_name = 'pcu_protocol_type'
20     primary_key = 'pcu_protocol_type_id'
21     join_tables = []
22     fields = {
23         'pcu_protocol_type_id': Parameter(int, "PCU protocol type identifier"),
24         'pcu_type_id': Parameter(int, "PCU type identifier"),
25         'port': Parameter(int, "PCU port"),
26         'protocol': Parameter(str, "Protocol"),
27         'supported': Parameter(bool, "Is the port/protocol supported by PLC")
28         }
29
30     def validate_port(self, port):
31         # make sure port is not blank
32
33         if not port:
34             raise PLCInvalidArgument, "Port must be specified"
35
36         return port
37
38     def validate_protocol(self, protocol):
39         # make sure port is not blank
40         if not len(protocol):
41             raise PLCInvalidArgument, "protocol must be specified"
42
43         return protocol
44
45 class PCUProtocolTypes(Table):
46     """
47     Representation of the pcu_protocol_types table in the database.
48     """
49
50     def __init__(self, api, protocol_type_filter = None, columns = None):
51         Table.__init__(self, api, PCUProtocolType, columns)
52
53         sql = "SELECT %s FROM pcu_protocol_type WHERE True" % \
54               ", ".join(self.columns)
55
56         if protocol_type_filter is not None:
57             if isinstance(protocol_type_filter, (list, tuple, set, int, long)):
58                 protocol_type_filter = Filter(PCUProtocolType.fields, {'pcu_protocol_type_id': protocol_type_filter})
59                 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "OR")
60             elif isinstance(protocol_type_filter, dict):
61                 protocol_type_filter = Filter(PCUProtocolType.fields, protocol_type_filter)
62                 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "AND")
63             else:
64                 raise PLCInvalidArgument, "Wrong pcu_protocol_type filter %r"%protocol_type_filter
65
66
67         self.selectall(sql)