added create_network(), delete_network(), create_subnet(), delete_subnet(), process_t...
[plcapi.git] / PLC / NetworkTypes.py
index 8d00b03..3006df0 100644 (file)
@@ -1,28 +1,21 @@
 #
 # 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$
-# $URL$
-#
-
+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 = ['interfaces']
+    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):
@@ -31,24 +24,31 @@ class NetworkType(Row):
             raise PLCInvalidArgument, "Network type must be specified"
 
         # Make sure network type does not alredy exist
-        conflicts = NetworkTypes(self.api, [name])
+        conflicts = NetworkTypes(self.api, name)
         if conflicts:
             raise PLCInvalidArgument, "Network type name already in use"
 
         return name
 
-class NetworkTypes(Table):
+    def sync(self, commit=True, validate=True):
+        AlchemyObj.sync(self, commit=commit, validate=validate)
+        AlchemyObj.insert(self, dict(self)) 
+
+    def delete(self, commit=True):
+        assert 'type' in self
+        AlchemyObj.delete(self, dict(self))
+
+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( [ api.db.quote (t) for t in 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)