added create_network(), delete_network(), create_subnet(), delete_subnet(), process_t...
[plcapi.git] / PLC / AddressTypes.py
1 #
2 # Functions for interacting with the address_types 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 types import StringTypes
9 from PLC.Faults import *
10 from PLC.Parameter import Parameter
11 from PLC.Filter import Filter
12 from PLC.Storage.AlchemyObject import AlchemyObj
13 from PLC.Table import Row, Table
14
15 class AddressType(AlchemyObj):
16     """
17     Representation of a row in the address_types table. To use,
18     instantiate with a dict of values.
19     """
20
21     tablename = 'address_types'
22     join_tables = ['address_address_type']
23     fields = {
24         'address_type_id': Parameter(int, "Address type identifier", primary_key=True),
25         'name': Parameter(str, "Address type", max = 20),
26         'description': Parameter(str, "Address type description", max = 254),
27         }
28
29     def validate_name(self, name):
30         # Make sure name is not blank
31         if not len(name):
32             raise PLCInvalidArgument, "Address type must be specified"
33
34         # Make sure address type does not already exist
35         conflicts = AddressTypes(self.api, [name])
36         for address_type_id in conflicts:
37             if 'address_type_id' not in self or self['address_type_id'] != address_type_id:
38                 raise PLCInvalidArgument, "Address type name already in use"
39
40         return name
41
42     def sync(self, insert=False, validate=True):
43         AlchemyObj.sync(self, insert, validate)
44         if insert == True or 'address_type_id'  not in self:
45             AlchemyObj.insert(self, dict(self))
46         else:
47             AlchemyObj.update(self, dict(self))
48
49
50     def delete(self, commit=True):
51         assert 'address_type_id' in self
52         AlchemyObj.delete(self, dict(self))
53
54 class AddressTypes(list):
55     """
56     Representation of the address_types table in the database.
57     """
58
59     def __init__(self, api, address_type_filter = None, columns = None):
60         if not address_type_filter:
61             address_types = AddressType().select()
62         elif isinstance(address_type_filter, (list, tuple, set)):
63             # Separate the list into integers and strings
64             ints = filter(lambda x: isinstance(x, (int, long)), address_type_filter)
65             strs = filter(lambda x: isinstance(x, StringTypes), address_type_filter)
66             address_types = AddressType().select(filter={'address_type_id': ints, 'name': strs})
67         elif isinstance(address_type_filter, dict):
68             address_types = AddressType().select(filter=address_type_filter)
69         elif isinstance(address_type_filter, (int, long)):
70             address_types = AddressType().select(filter={'address_type_id': address_type_filter})
71         elif isinstance(address_type_filter, StringTypes):
72             address_types = AddressType().select(filter={'name': address_type_filter})
73         else:
74             raise PLCInvalidArgument, "Wrong address type filter %r"%address_type_filter
75
76         for address_type in address_types:
77             self.append(address_type)