Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[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 # $Id: KeyTypes.py 5574 2007-10-25 20:33:17Z thierry $
8 #
9
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Table import Row, Table
13
14 class KeyType(Row):
15     """
16     Representation of a row in the key_types table. To use,
17     instantiate with a dict of values.
18     """
19
20     table_name = 'key_types'
21     primary_key = 'key_type'
22     join_tables = ['keys']
23     fields = {
24         'key_type': Parameter(str, "Key type", max = 20),
25         }
26
27     def validate_key_type(self, name):
28         # Make sure name is not blank
29         if not len(name):
30             raise PLCInvalidArgument, "Key type must be specified"
31         
32         # Make sure key type does not alredy exist
33         conflicts = KeyTypes(self.api, [name])
34         if conflicts:
35             raise PLCInvalidArgument, "Key type name already in use"
36
37         return name
38         
39 class KeyTypes(Table):
40     """
41     Representation of the key_types table in the database.
42     """
43
44     def __init__(self, api, key_types = None):
45         Table.__init__(self, api, KeyType)
46
47         sql = "SELECT %s FROM key_types" % \
48               ", ".join(KeyType.fields)
49         
50         if key_types:
51             sql += " WHERE key_type IN (%s)" % ", ".join(map(api.db.quote, key_types))
52
53         self.selectall(sql)