X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FTable.py;h=434db2e9c291136554baf62efb1dbede3d37403a;hb=1a14a3aeb60941f70274d39a2b4329141075c6e1;hp=0da2015909ea76ec6a2283a9aaa3bd49b51fd169;hpb=584d1aaf398c2d74497d68bc5f9d4ce0fee405c3;p=plcapi.git diff --git a/PLC/Table.py b/PLC/Table.py index 0da2015..434db2e 100644 --- a/PLC/Table.py +++ b/PLC/Table.py @@ -1,4 +1,5 @@ # $Id$ +# $URL$ from types import StringTypes, IntType, LongType import time import calendar @@ -228,8 +229,8 @@ class Row(dict): 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 +240,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 (,{'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 @@ -259,17 +284,6 @@ class Row(dict): result.append(rejected) return result - # compute the accepts part of an update method from a list of column names, and a (list of) field 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): @@ -280,18 +294,19 @@ class Row(dict): """ returns a SQL sentence that creates a view named after the primary_key and tagname, with 2 columns - (*) column 1: name=self.primary_key - (*) column 2: name=tagname value=tagvalue + (*) 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 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.tagvalue as "%(tagname)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() @@ -404,7 +419,7 @@ class Table(list): tag_columns={} else: (columns,tag_columns,rejected) = classobj.parse_columns(columns) - if not columns: + if not columns and not tag_columns: raise PLCInvalidArgument, "No valid return fields specified for class %s"%classobj.__name__ if rejected: raise PLCInvalidArgument, "unknown column(s) specified %r in %s"%(rejected,classobj.__name__)