remove simplejson dependency
[plcapi.git] / PLC / Table.py
index 57f6c8b..8f434bf 100644 (file)
@@ -1,11 +1,12 @@
-# $Id$
 from types import StringTypes, IntType, LongType
 import time
 import calendar
 
+from PLC.Timestamp import Timestamp
 from PLC.Faults import *
 from PLC.Parameter import Parameter
 
+
 class Row(dict):
     """
     Representation of a row in a database table. To use, optionally
@@ -65,58 +66,38 @@ class Row(dict):
             if value is not None and hasattr(self, 'validate_' + key):
                 validate = getattr(self, 'validate_' + key)
                 self[key] = validate(value)
-       
-    def separate_types(self, items):
-       """
-       Separate a list of different typed objects. 
-       Return a list for each type (ints, strs and dicts)
-       """
-       
-       if isinstance(items, (list, tuple, set)):
-           ints = filter(lambda x: isinstance(x, (int, long)), items)
-           strs = filter(lambda x: isinstance(x, StringTypes), items)
-           dicts = filter(lambda x: isinstance(x, dict), items)
-           return (ints, strs, dicts)          
-       else:
-           raise PLCInvalidArgument, "Can only separate list types" 
-               
-
-    def associate(self, *args):
-       """
-       Provides a means for high level api calls to associate objects
-        using low level calls.
-       """
 
-       if len(args) < 3:
-           raise PLCInvalidArgumentCount, "auth, field, value must be specified"
-       elif hasattr(self, 'associate_' + args[1]):
-           associate = getattr(self, 'associate_'+args[1])
-           associate(*args)
-       else:
-           raise PLCInvalidArguemnt, "No such associate function associate_%s" % args[1]
-
-    def validate_timestamp(self, timestamp, check_future = False):
+    def separate_types(self, items):
         """
-        Validates the specified GMT timestamp string (must be in
-        %Y-%m-%d %H:%M:%S format) or number (seconds since UNIX epoch,
-        i.e., 1970-01-01 00:00:00 GMT). If check_future is True,
-        raises an exception if timestamp is not in the future. Returns
-        a GMT timestamp string.
+        Separate a list of different typed objects.
+        Return a list for each type (ints, strs and dicts)
         """
 
-        time_format = "%Y-%m-%d %H:%M:%S"
+        if isinstance(items, (list, tuple, set)):
+            ints = filter(lambda x: isinstance(x, (int, long)), items)
+            strs = filter(lambda x: isinstance(x, StringTypes), items)
+            dicts = filter(lambda x: isinstance(x, dict), items)
+            return (ints, strs, dicts)
+        else:
+            raise PLCInvalidArgument, "Can only separate list types"
 
-       if isinstance(timestamp, StringTypes):
-           # calendar.timegm() is the inverse of time.gmtime()
-           timestamp = calendar.timegm(time.strptime(timestamp, time_format))
 
-        # Human readable timestamp string
-       human = time.strftime(time_format, time.gmtime(timestamp))
+    def associate(self, *args):
+        """
+        Provides a means for high level api calls to associate objects
+        using low level calls.
+        """
 
-       if check_future and timestamp < time.time():
-            raise PLCInvalidArgument, "'%s' not in the future" % human
+        if len(args) < 3:
+            raise PLCInvalidArgumentCount, "auth, field, value must be specified"
+        elif hasattr(self, 'associate_' + args[1]):
+            associate = getattr(self, 'associate_'+args[1])
+            associate(*args)
+        else:
+            raise PLCInvalidArguemnt, "No such associate function associate_%s" % args[1]
 
-       return human
+    def validate_timestamp (self, timestamp):
+        return Timestamp.sql_validate(timestamp)
 
     def add_object(self, classobj, join_table, columns = None):
         """
@@ -136,7 +117,7 @@ class Row(dict):
             assert isinstance(obj, classobj)
             assert isinstance(obj, Row)
             assert obj.primary_key in obj
-           assert join_table in obj.join_tables
+            assert join_table in obj.join_tables
 
             # By default, just insert the primary keys of each object
             # into the join table.
@@ -154,7 +135,7 @@ class Row(dict):
 
             if commit:
                 self.api.db.commit()
-    
+
         return add
 
     add_object = classmethod(add_object)
@@ -169,7 +150,7 @@ class Row(dict):
             """
             Disassociate from the specified object.
             """
-    
+
             assert isinstance(self, Row)
             assert self.primary_key in self
             assert join_table in self.join_tables
@@ -177,10 +158,10 @@ class Row(dict):
             assert isinstance(obj, Row)
             assert obj.primary_key in obj
             assert join_table in obj.join_tables
-    
+
             self_id = self[self.primary_key]
             obj_id = obj[obj.primary_key]
-    
+
             self.api.db.do("DELETE FROM %s WHERE %s = %s AND %s = %s" % \
                            (join_table,
                             self.primary_key, self.api.db.param('self_id', self_id),
@@ -224,12 +205,12 @@ class Row(dict):
         Return the fields of obj that are mentioned in tags
         """
         if obj is None: obj=self
-        
-        return dict ( [ (key,value) for (key,value) in obj.iteritems() 
+
+        return dict ( [ (key,value) for (key,value) in obj.iteritems()
                         if key in self.tags and Row.is_writable(key,value,self.tags) ] )
-    
-    # takes in input a list of columns, returns 2 dicts and one list
-    # fields, tags, rejected
+
+    # takes as input a list of columns, sort native fields from tags
+    # returns 2 dicts and one list : fields, tags, rejected
     @classmethod
     def parse_columns (cls, columns):
         (fields,tags,rejected)=({},{},[])
@@ -239,6 +220,30 @@ class Row(dict):
             else: rejected.append(column)
         return (fields,tags,rejected)
 
+    # compute the 'accepts' part of a method, from a list of column names, and a fields dict
+    # use exclude=True to exclude the column names instead
+    # typically accepted_fields (Node.fields,['hostname','model',...])
+    @staticmethod
+    def accepted_fields (update_columns, fields_dict, exclude=False):
+        result={}
+        for (k,v) in fields_dict.iteritems():
+            if (not exclude and k in update_columns) or (exclude and k not in update_columns):
+                result[k]=v
+        return result
+
+    # filter out user-provided fields that are not part of the declared acceptance list
+    # keep it separate from split_fields for simplicity
+    # typically check_fields (<user_provided_dict>,{'hostname':Parameter(str,...),'model':Parameter(..)...})
+    @staticmethod
+    def check_fields (user_dict, accepted_fields):
+# avoid the simple, but silent, version
+#        return dict ([ (k,v) for (k,v) in user_dict.items() if k in accepted_fields ])
+        result={}
+        for (k,v) in user_dict.items():
+            if k in accepted_fields: result[k]=v
+            else: raise PLCInvalidArgument ('Trying to set/change unaccepted key %s'%k)
+        return result
+
     # given a dict (typically passed to an Update method), we check and sort
     # them against a list of dicts, e.g. [Node.fields, Node.related_fields]
     # return is a list that contains n+1 dicts, last one has the rejected fields
@@ -254,22 +259,11 @@ class Row(dict):
                 if field in candidate_dict.keys():
                     result[i][field]=value
                     found=True
-                    break 
+                    break
             if not found: rejected[field]=value
         result.append(rejected)
         return result
 
-    # compute the accepts part of an update method from a list of column names, and a (list of) fields dict
-    @staticmethod
-    def accepted_fields (can_update_columns, fields):
-        result={}
-        if not isinstance(fields,list): fields = [fields]
-        for dict in fields:
-            for (k,v) in dict.iteritems():
-                if k in can_update_columns:
-                    result[k]=v
-        return result
-
     ### class initialization : create tag-dependent cross view if needed
     @classmethod
     def tagvalue_view_name (cls, tagname):
@@ -278,13 +272,14 @@ class Row(dict):
     @classmethod
     def tagvalue_view_create_sql (cls,tagname):
         """
-        returns a SQL sentence that creates a view named after the primary_key and tagname, 
+        returns a SQL sentence that creates a view named after the primary_key and tagname,
         with 2 columns
-        (*) column 1: primary_key 
+        (*) column 1: primary_key
         (*) column 2: actual tag value, renamed into tagname
         """
 
-        if not cls.view_tags_name: return ""
+        if not cls.view_tags_name:
+            raise Exception, 'WARNING: class %s needs to set view_tags_name'%cls.__name__
 
         table_name=cls.table_name
         primary_key=cls.primary_key
@@ -318,13 +313,15 @@ class Row(dict):
         y = self.db_fields(y)
         return dict.__eq__(x, y)
 
-    def sync(self, commit = True, insert = None):
+    # validate becomes optional on sept. 2010
+    # we find it useful to use DeletePerson on duplicated entries
+    def sync(self, commit = True, insert = None, validate=True):
         """
         Flush changes back to the database.
         """
 
         # Validate all specified fields
-        self.validate()
+        if validate: self.validate()
 
         # Filter out fields that cannot be set or updated directly
         db_fields = self.db_fields()
@@ -339,15 +336,15 @@ class Row(dict):
         if not self.has_key(self.primary_key) or \
            keys == [self.primary_key] or \
            insert is True:
-           
-           # If primary key id is a serial int and it isnt included, get next id
-           if self.fields[self.primary_key].type in (IntType, LongType) and \
-              self.primary_key not in self:
-               pk_id = self.api.db.next_id(self.table_name, self.primary_key)
-               self[self.primary_key] = pk_id
-               db_fields[self.primary_key] = pk_id
-               keys = db_fields.keys()
-               values = [self.api.db.param(key, value) for (key, value) in db_fields.items()]
+
+            # If primary key id is a serial int and it isnt included, get next id
+            if self.fields[self.primary_key].type in (IntType, LongType) and \
+               self.primary_key not in self:
+                pk_id = self.api.db.next_id(self.table_name, self.primary_key)
+                self[self.primary_key] = pk_id
+                db_fields[self.primary_key] = pk_id
+                keys = db_fields.keys()
+                values = [self.api.db.param(key, value) for (key, value) in db_fields.items()]
             # Insert new row
             sql = "INSERT INTO %s (%s) VALUES (%s)" % \
                   (self.table_name, ", ".join(keys), ", ".join(values))