Fix version output when missing.
[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 # $Id$
8 # $URL$
9 #
10
11 from PLC.Faults import *
12 from PLC.Parameter import Parameter
13 from PLC.Table import Row, Table
14 from PLC.Filter import Filter
15
16 class PCUProtocolType(Row):
17     """
18     Representation of a row in the pcu_protocol_type table. To use,
19     instantiate with a dict of values.
20     """
21
22     table_name = 'pcu_protocol_type'
23     primary_key = 'pcu_protocol_type_id'
24     join_tables = []
25     fields = {
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")
31         }
32
33     def validate_port(self, port):
34         # make sure port is not blank
35
36         if not port:
37             raise PLCInvalidArgument, "Port must be specified"
38
39         return port
40
41     def validate_protocol(self, protocol):
42         # make sure port is not blank
43         if not len(protocol):
44             raise PLCInvalidArgument, "protocol must be specified"
45
46         return protocol
47
48 class PCUProtocolTypes(Table):
49     """
50     Representation of the pcu_protocol_types table in the database.
51     """
52
53     def __init__(self, api, protocol_type_filter = None, columns = None):
54         Table.__init__(self, api, PCUProtocolType, columns)
55
56         sql = "SELECT %s FROM pcu_protocol_type WHERE True" % \
57               ", ".join(self.columns)
58
59         if protocol_type_filter is not None:
60             if isinstance(protocol_type_filter, (list, tuple, set, int, long)):
61                 protocol_type_filter = Filter(PCUProtocolType.fields, {'pcu_protocol_type_id': protocol_type_filter})
62                 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "OR")
63             elif isinstance(protocol_type_filter, dict):
64                 protocol_type_filter = Filter(PCUProtocolType.fields, protocol_type_filter)
65                 sql += " AND (%s) %s" % protocol_type_filter.sql(api, "AND")
66             else:
67                 raise PLCInvalidArgument, "Wrong pcu_protocol_type filter %r"%protocol_type_filter
68
69
70         self.selectall(sql)