added create_network(), delete_network(), create_subnet(), delete_subnet(), process_t...
[plcapi.git] / PLC / NetworkTypes.py
index a3416c8..3006df0 100644 (file)
@@ -1,55 +1,54 @@
 #
 # 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: NetworkTypes.py,v 1.2 2006/10/20 17:47:34 mlhuang Exp $
-#
-
+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):
+    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, names = None):
-        sql = "SELECT %s FROM network_types" % \
-              ", ".join(NetworkType.fields)
-        
-        if names:
-            # Separate the list into integers and strings
-            sql += " WHERE type IN (%s)" % ", ".join(api.db.quote(names))
-
-        rows = api.db.selectall(sql)
+    def __init__(self, api, types = None):
+        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 
 
-        for row in rows:
-            self[row['type']] = NetworkType(api, row)
+        self.extend(network_types)