#
# Functions for interacting with the network_types table in the database
#
-# Mark Huang <mlhuang@cs.princeton.edu>
-# Copyright (C) 2006 The Trustees of Princeton University
#
-# $Id$
-#
-
+from types import StringTypes
from PLC.Faults import *
from PLC.Parameter import Parameter
-from PLC.Table import Row, Table
+from PLC.Storage.AlchemyObject import AlchemyObj
-class NetworkType(Row):
+class NetworkType(AlchemyObj):
"""
Representation of a row in the network_types table. To use,
instantiate with a dict of values.
"""
- table_name = 'network_types'
- primary_key = 'type'
- join_tables = ['nodenetworks']
+ tablename = 'network_types'
fields = {
- 'type': Parameter(str, "Network type", max = 20),
+ 'type': Parameter(str, "Network type", max = 20, primary_key=True),
}
def validate_type(self, name):
- # Make sure name is not blank
+ # Make sure name is not blank
if not len(name):
raise PLCInvalidArgument, "Network type must be specified"
-
- # Make sure network type does not alredy exist
- conflicts = NetworkTypes(self.api, [name])
+
+ # Make sure network type does not alredy exist
+ conflicts = NetworkTypes(self.api, name)
if conflicts:
raise PLCInvalidArgument, "Network type name already in use"
- return name
+ return name
-class NetworkTypes(Table):
+class NetworkTypes(list):
"""
Representation of the network_types table in the database.
"""
def __init__(self, api, types = None):
- Table.__init__(self, api, NetworkType)
-
- sql = "SELECT %s FROM network_types" % \
- ", ".join(NetworkType.fields)
-
- if types:
- sql += " WHERE type IN (%s)" % ", ".join(map(api.db.quote, types))
-
- self.selectall(sql)
+ if not types:
+ network_types = NetworkType().select()
+ elif isinstance(types, StringTypes):
+ network_types = NetworkType().select({'type': types})
+ else:
+ raise PLCInvalidArgument, "Wrong network type filter %r"%types
+
+ self.extend(network_types)