2 # Functions for interacting with the pcu_types table in the database
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
8 from types import StringTypes
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Storage.AlchemyObject import AlchemyObj
14 class PCUType(AlchemyObj):
16 Representation of a row in the pcu_types table. To use,
17 instantiate with a dict of values.
20 tablename = 'pcu_types'
21 join_tables = ['pcu_protocol_type']
23 'pcu_type_id': Parameter(int, "PCU Type Identifier", primary_key=True),
24 'model': Parameter(str, "PCU model", max = 254),
25 'name': Parameter(str, "PCU full name", max = 254),
26 'pcu_protocol_type_ids': Parameter([int], "PCU Protocol Type Identifiers", joined=True),
27 'pcu_protocol_types': Parameter([dict], "PCU Protocol Type List", joined=True)
30 def validate_model(self, model):
31 # Make sure name is not blank
33 raise PLCInvalidArgument, "Model must be specified"
35 # Make sure boot state does not alredy exist
36 conflicts = PCUTypes(self.api, model)
37 for pcu_type in conflicts:
38 if 'pcu_type_id' not in self or self['pcu_type_id'] != pcu_type['pcu_type_id']:
39 raise PLCInvalidArgument, "Model already in use"
45 Representation of the pcu_types table in the database.
48 def __init__(self, api, pcu_type_filter = None, columns = None):
50 # Remove pcu_protocol_types from query since its not really a field
51 # in the db. We will add it later
52 if not pcu_type_filter:
53 pcu_types = PCUType().select()
54 elif isinstance(pcu_type_filter, (list, tuple, set)):
55 # Separate the list into integers and strings
56 ints = filter(lambda x: isinstance(x, (int, long)), pcu_type_filter)
57 strs = filter(lambda x: isinstance(x, StringTypes), pcu_type_filter)
58 pcu_types = PCUType().select(filter = {'pcu_type_id': ints, 'model': strs})
59 elif isinstance(pcu_type_filter, dict):
60 pcu_types = PCUType().select(filter = pcu_type_filter)
61 elif isinstance (pcu_type_filter, StringTypes):
62 pcu_types = PCUType().select(filter = {'model': pcu_type_filter})
63 elif isinstance (pcu_type_filter, int):
64 pcu_types = PCUType().select(filter = {'pcu_type_id': pcu_filter})
66 raise PLCInvalidArgument, "Wrong pcu_type filter %r"%pcu_type_filter
69 self.extend(pcu_types)
71 # return a list of protocol type objects for each port type
72 if 'pcu_protocol_types' in removed_fields:
73 from PLC.PCUProtocolTypes import PCUProtocolTypes
74 protocol_type_ids = set()
76 protocol_type_ids.update(pcu_type['pcu_protocol_type_ids'])
78 protocol_return_fields = ['pcu_protocol_type_id', 'port', 'protocol', 'supported']
79 all_protocol_types = PCUProtocolTypes(self.api, list(protocol_type_ids), \
80 protocol_return_fields).dict('pcu_protocol_type_id')
83 pcu_type['pcu_protocol_types'] = []
84 for protocol_type_id in pcu_type['pcu_protocol_type_ids']:
85 pcu_type['pcu_protocol_types'].append(all_protocol_types[protocol_type_id])