added create_network(), delete_network(), create_subnet(), delete_subnet(), process_t...
[plcapi.git] / PLC / KeyTypes.py
1 #
2 # Functions for interacting with the key_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 PLC.Faults import *
9 from PLC.Parameter import Parameter
10 from PLC.Storage.AlchemyObject import AlchemyObj
11
12 class KeyType(AlchemyObj):
13     """
14     Representation of a row in the key_types table. To use,
15     instantiate with a dict of values.
16     """
17
18     tablename = 'key_types'
19     fields = {
20         'key_type': Parameter(str, "Key type", max = 20, primary_key=True),
21         }
22
23     def validate_key_type(self, name):
24         # Make sure name is not blank
25         if not len(name):
26             raise PLCInvalidArgument, "Key type must be specified"
27
28         # Make sure key type does not alredy exist
29         conflicts = KeyType().select(filter={'key_type': name})
30         if conflicts:
31             raise PLCInvalidArgument, "Key type name already in use"
32
33         return name
34
35     def sync(self, commit = True, validate=True):
36         AlchemyObj.sync(self, commit=commit, validate=validate)
37         AlchemyObj.insert(self, dict(self))
38
39     def delete(self, commit=True):
40         """
41         Delete existing key type
42         """
43         assert 'key_type' in self
44         AlchemyObj.delete(self, filter={'key_type': self['key_type']})  
45
46 class KeyTypes(list):
47     """
48     Representation of the key_types table in the database.
49     """
50
51     def __init__(self, api, key_types = None):
52        if key_types:
53             key_types = KeyType().select(filter={'key_type': key_types})
54         else:
55             key_types = KeyType().select()
56
57         for key_type in key_types:
58             key_type = KeyType(api, object=key_type)
59             self.append(key_type)