print GPG errors to log
[plcapi.git] / PLC / Table.py
index 9524463..9041c26 100644 (file)
@@ -1,3 +1,6 @@
+import time
+import calendar
+
 from PLC.Faults import *
 from PLC.Parameter import Parameter
 
@@ -39,7 +42,7 @@ class Row(dict):
         mandatory_fields = self.api.db.fields(self.table_name, notnull = True, hasdef = False)
         for field in mandatory_fields:
             if not self.has_key(field) or self[field] is None:
-                raise PLCInvalidArgument, field + " must be specified and cannot be unset"
+                raise PLCInvalidArgument, field + " must be specified and cannot be unset in class %s"%self.__class__.__name__
 
         # Validate values before committing
         for key, value in self.iteritems():
@@ -47,6 +50,19 @@ class Row(dict):
                 validate = getattr(self, 'validate_' + key)
                 self[key] = validate(value)
 
+    time_format = "%Y-%m-%d %H:%M:%S"
+    def validate_timestamp (self, timestamp, check_future=False):
+       # in case we try to sync the same object twice
+       if isinstance(timestamp,str):
+           # calendar.timegm is the inverse of time.gmtime, in that it computes in UTC
+           # surprisingly enough, no other method in the time module behaves this way
+            # this method is documented in the time module's documentation
+           timestamp = calendar.timegm (time.strptime (timestamp,Row.time_format))
+       human = time.strftime (Row.time_format, time.gmtime(timestamp))
+       if check_future and (timestamp < time.time()):
+            raise PLCInvalidArgument, "%s: date must be in the future"%human
+       return human
+
     def sync(self, commit = True, insert = None):
         """
         Flush changes back to the database.