+ return dict ( [ (key, value) for (key, value) in obj.items()
+ if key in db_fields and
+ Row.is_writable(key, value, self.fields) ] )
+
+ def tag_fields (self, obj=None):
+ """
+ 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()
+ if key in self.tags and Row.is_writable(key,value,self.tags) ] )
+
+ # 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)=({},{},[])
+ for column in columns:
+ if column in cls.fields: fields[column]=cls.fields[column]
+ elif column in cls.tags: tags[column]=cls.tags[column]
+ 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
+ @staticmethod
+ def split_fields (fields, dicts):
+ result=[]
+ for x in dicts: result.append({})
+ rejected={}
+ for (field,value) in fields.iteritems():
+ found=False
+ for i in range(len(dicts)):
+ candidate_dict=dicts[i]
+ if field in candidate_dict.keys():
+ result[i][field]=value
+ found=True
+ break
+ if not found: rejected[field]=value
+ result.append(rejected)
+ return result
+
+ ### class initialization : create tag-dependent cross view if needed
+ @classmethod
+ def tagvalue_view_name (cls, tagname):
+ return "tagvalue_view_%s_%s"%(cls.primary_key,tagname)
+
+ @classmethod
+ def tagvalue_view_create_sql (cls,tagname):
+ """
+ returns a SQL sentence that creates a view named after the primary_key and tagname,
+ with 2 columns
+ (*) column 1: primary_key
+ (*) column 2: actual tag value, renamed into tagname
+ """
+
+ 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
+ view_tags_name=cls.view_tags_name
+ tagvalue_view_name=cls.tagvalue_view_name(tagname)
+ return 'CREATE OR REPLACE VIEW %(tagvalue_view_name)s ' \
+ 'as SELECT %(table_name)s.%(primary_key)s,%(view_tags_name)s.value as "%(tagname)s" ' \
+ 'from %(table_name)s right join %(view_tags_name)s using (%(primary_key)s) ' \
+ 'WHERE tagname = \'%(tagname)s\';'%locals()
+
+ @classmethod
+ def class_init (cls,api):
+ cls.tagvalue_views_create (api)
+
+ @classmethod
+ def tagvalue_views_create (cls,api):
+ if not cls.tags: return
+ for tagname in cls.tags.keys():
+ api.db.do(cls.tagvalue_view_create_sql (tagname))
+ api.db.commit()