implement SliceTags
authorTony Mack <tmack@paris.CS.Princeton.EDU>
Fri, 28 Sep 2012 00:30:16 +0000 (20:30 -0400)
committerTony Mack <tmack@paris.CS.Princeton.EDU>
Fri, 28 Sep 2012 00:30:16 +0000 (20:30 -0400)
PLC/SliceTags.py

index e5070fd..120fde7 100644 (file)
@@ -1,56 +1,69 @@
-#
-# Thierry Parmentelat - INRIA
-#
+from types import StringTypes
+import time
+import re
+import datetime
+
 from PLC.Faults import *
-from PLC.Parameter import Parameter
+from PLC.Parameter import Parameter, Mixed
 from PLC.Filter import Filter
-from PLC.Table import Row, Table
-# seems to cause import loops
-#from PLC.Slices import Slice, Slices
-from PLC.Nodes import Node, Nodes
-from PLC.NodeGroups import NodeGroup, NodeGroups
-from PLC.TagTypes import TagType, TagTypes
-
-class SliceTag(Row):
+from PLC.Debug import profile
+from PLC.Timestamp import Timestamp
+from PLC.Storage.AlchemyObject import AlchemyObj
+
+class SliceTags(AlchemyObj):
     """
-    Representation of a row in the slice_tag table. To use,
-    instantiate with a dict of values.
+    Representation of a row in the slice_tags table. To use, optionally
+    instantiate with a dict of values. Update as you would a
+    dict. Commit to the database with sync().To use, instantiate
+    with a dict of values.
     """
 
-    table_name = 'slice_tag'
-    primary_key = 'slice_tag_id'
+    tablename = 'slice_tags'
     fields = {
-        'slice_tag_id': Parameter(int, "Slice tag identifier"),
-        'slice_id': Parameter(int, "Slice identifier"),
+        'slice_tag_id': Parameter(int, "Slice Tag identifier", primary_key=True),
+        'slice_id': Parameter(int, "Slice identifier", indexed=True),
         'name': Parameter(str, "Slice name"),
-        'node_id': Node.fields['node_id'],
-        'nodegroup_id': NodeGroup.fields['nodegroup_id'],
-        'tag_type_id': TagType.fields['tag_type_id'],
-        'tagname': TagType.fields['tagname'],
-        'description': TagType.fields['description'],
-        'category': TagType.fields['category'],
+        'node_id': Parameter(int, "Node identifier", nullok=True),
+        'nodegroup_id': Parameter(int, "Node Group identifier", nullok=True),
+        'tag_type_id': Parameter(int, "Tag type identifier"),
+        'tagname': Parameter(str, "Tag type name"),
+        'description': Parameter(str, "Tag type description"),
+        'category': Parameter(str, "Tag type category"),
         'value': Parameter(str, "Slice attribute value"),
         }
 
-class SliceTags(Table):
+    tags = {}
+
+    def sync(self, commit = True, validate=True):
+        """
+        Add the record
+        """
+        AlchemyObj.sync(self, commit, validate)
+        AlchemyObj.insert(self, dict(self))
+
+    def delete(self, commit = True):
+        """
+        Delete existing slice.
+        """
+        AlchemyObj.delete(self, dict(self))
+
+
+class SliceTags(list):
     """
-    Representation of row(s) from the slice_tag table in the
+    Representation of row(s) from the slices table in the
     database.
     """
 
-    def __init__(self, api, slice_tag_filter = None, columns = None):
-        Table.__init__(self, api, SliceTag, columns)
-
-        sql = "SELECT %s FROM view_slice_tags WHERE True" % \
-              ", ".join(self.columns)
-
-        if slice_tag_filter is not None:
-            if isinstance(slice_tag_filter, (list, tuple, set, int, long)):
-                slice_tag_filter = Filter(SliceTag.fields, {'slice_tag_id': slice_tag_filter})
-            elif isinstance(slice_tag_filter, dict):
-                slice_tag_filter = Filter(SliceTag.fields, slice_tag_filter)
-            else:
-                raise PLCInvalidArgument, "Wrong slice tag filter %r"%slice_tag_filter
-            sql += " AND (%s) %s" % slice_tag_filter.sql(api)
+    def __init__(self, api, filter = None, columns = None):
+         
+        # the view that we're selecting upon: start with view_slices
+        if not filter:
+            slice_tags = SliceTags().select()
+        elif isinstance(filter, dict):
+            slices_tags = SliceTags().select(filter=filter)
+        else:
+            raise PLCInvalidArgument, "Wrong slice_tag filter %r"%filter
 
-        self.selectall(sql)
+        for slice_tag in slice_tags:
+            self.append(slice_tag)