# Mark Huang <mlhuang@cs.princeton.edu>
# Copyright (C) 2006 The Trustees of Princeton University
#
-# $Id: KeyTypes.py,v 1.2 2006/10/20 17:44:57 mlhuang Exp $
-#
from PLC.Faults import *
from PLC.Parameter import Parameter
-from PLC.Table import Row, Table
+from PLC.Storage.AlchemyObject import AlchemyObj
-class KeyType(Row):
+class KeyType(AlchemyObj):
"""
Representation of a row in the key_types table. To use,
instantiate with a dict of values.
"""
- table_name = 'key_types'
- primary_key = 'key_type'
- join_tables = ['keys']
+ tablename = 'key_types'
fields = {
- 'key_type': Parameter(str, "Key type", max = 20),
+ 'key_type': Parameter(str, "Key type", max = 20, primary_key=True),
}
def validate_key_type(self, name):
- # Make sure name is not blank
+ # Make sure name is not blank
if not len(name):
raise PLCInvalidArgument, "Key type must be specified"
-
- # Make sure key type does not alredy exist
- conflicts = KeyTypes(self.api, [name])
+
+ # Make sure key type does not alredy exist
+ conflicts = KeyType().select(filter={'key_type': name})
if conflicts:
raise PLCInvalidArgument, "Key type name already in use"
- return name
-
-class KeyTypes(Table):
+ return name
+
+ def sync(self, commit = True, validate=True):
+ AlchemyObj.sync(self, commit=commit, validate=validate)
+ AlchemyObj.insert(self, dict(self))
+
+ def delete(self, commit=True):
+ """
+ Delete existing key type
+ """
+ assert 'key_type' in self
+ AlchemyObj.delete(self, filter={'key_type': self['key_type']})
+
+class KeyTypes(list):
"""
Representation of the key_types table in the database.
"""
- def __init__(self, api, names = None):
- sql = "SELECT %s FROM key_types" % \
- ", ".join(KeyType.fields)
-
- if names:
- # Separate the list into integers and strings
- sql += " WHERE key_type IN (%s)" % ", ".join(api.db.quote(names))
-
- rows = api.db.selectall(sql)
+ def __init__(self, api, key_types = None):
+ if key_types:
+ key_types = KeyType().select(filter={'key_type': key_types})
+ else:
+ key_types = KeyType().select()
- for row in rows:
- self[row['key_type']] = KeyType(api, row)
+ for key_type in key_types:
+ key_type = KeyType(api, object=key_type)
+ self.append(key_type)