added create_network(), delete_network(), create_subnet(), delete_subnet(), process_t...
[plcapi.git] / PLC / BootStates.py
index 11cd3b7..0485aa2 100644 (file)
@@ -7,19 +7,17 @@
 
 from PLC.Faults import *
 from PLC.Parameter import Parameter
-from PLC.Table import Row, Table
+from PLC.Storage.AlchemyObject import AlchemyObj
 
-class BootState(Row):
+class BootState(AlchemyObj):
     """
     Representation of a row in the boot_states table. To use,
     instantiate with a dict of values.
     """
 
-    table_name = 'boot_states'
-    primary_key = 'boot_state'
-    join_tables = ['nodes']
+    tablename = 'boot_states'
     fields = {
-        'boot_state': Parameter(str, "Boot state", max = 20),
+        'boot_state': Parameter(str, "Boot state", max = 20, primary_key=True),
         }
 
     def validate_boot_state(self, name):
@@ -28,24 +26,32 @@ class BootState(Row):
             raise PLCInvalidArgument, "Boot state must be specified"
 
         # Make sure boot state does not alredy exist
-        conflicts = BootStates(self.api, [name])
+        conflicts = BootStates(self.api, name)
         if conflicts:
             raise PLCInvalidArgument, "Boot state name already in use"
 
         return name
 
-class BootStates(Table):
+    def sync(self, commit=True, validate=True):
+        AlchemyObj.sync(self, commit, validate)
+        AlchemyObj.insert(self, dict(self))
+
+    def delete(self, commit=True):
+        assert 'boot_state' in self
+        AlchemyObj.delete(self, dict(self))                
+
+class BootStates(list):
     """
     Representation of the boot_states table in the database.
     """
 
-    def __init__(self, api, boot_states = None):
-        Table.__init__(self, api, BootState)
-
-        sql = "SELECT %s FROM boot_states" % \
-              ", ".join(BootState.fields)
-
-        if boot_states:
-            sql += " WHERE boot_state IN (%s)" % ", ".join( [ api.db.quote (s) for s in boot_states ] )
+    def __init__(self, api, filter = None):
+        if not filter:
+            boot_states = BootState().select()
+        elif isinstance(filter, StringTypes) or isinstance(filter, (list, tuple, set)):
+            boot_states = BootState().select(filter={'boot_state': filter})
+        else:
+            raise PLCInvalidArgument, "Wrong boot state filter %r" % filter
 
-        self.selectall(sql)
+        for boot_state in boot_states:
+            self.append(boot_state)