+
+ 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 lvl api calls to associate objects
+ using low lvl 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):
+ """
+ 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.
+ """
+
+ time_format = "%Y-%m-%d %H:%M:%S"
+
+ 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))
+
+ if check_future and timestamp < time.time():
+ raise PLCInvalidArgument, "'%s' not in the future" % human
+
+ return human
+
+ def add_object(self, classobj, join_table, columns = None):
+ """
+ Returns a function that can be used to associate this object
+ with another.
+ """
+
+ def add(self, obj, columns = None, commit = True):
+ """
+ Associate with the specified object.
+ """
+
+ # Various sanity checks
+ assert isinstance(self, Row)
+ assert self.primary_key in self
+ assert join_table in self.join_tables
+ assert isinstance(obj, classobj)
+ assert isinstance(obj, Row)
+ assert obj.primary_key in obj
+ assert join_table in obj.join_tables
+
+ # By default, just insert the primary keys of each object
+ # into the join table.
+ if columns is None:
+ columns = {self.primary_key: self[self.primary_key],
+ obj.primary_key: obj[obj.primary_key]}
+
+ params = []
+ for name, value in columns.iteritems():
+ params.append(self.api.db.param(name, value))
+
+ self.api.db.do("INSERT INTO %s (%s) VALUES(%s)" % \
+ (join_table, ", ".join(columns), ", ".join(params)),
+ columns)
+
+ if commit:
+ self.api.db.commit()
+
+ return add
+
+ add_object = classmethod(add_object)
+
+ def remove_object(self, classobj, join_table):
+ """
+ Returns a function that can be used to disassociate this
+ object with another.
+ """
+
+ def remove(self, obj, commit = True):
+ """
+ Disassociate from the specified object.
+ """
+
+ assert isinstance(self, Row)
+ assert self.primary_key in self
+ assert join_table in self.join_tables
+ assert isinstance(obj, classobj)
+ 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),
+ obj.primary_key, self.api.db.param('obj_id', obj_id)),
+ locals())
+
+ if commit:
+ self.api.db.commit()
+
+ return remove