X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FAccessor.py;h=fb12f3d8f74a2d04ecd81268938e5570db07ae78;hb=326ac47eff96d1758a7c8aeed4852fd51d52935c;hp=39e7872ded8fde18275f0e62269582c1c1041df7;hpb=a7ba28837e8c6f949f29aff64a0297ac48cbe065;p=plcapi.git diff --git a/PLC/Accessor.py b/PLC/Accessor.py index 39e7872..fb12f3d 100644 --- a/PLC/Accessor.py +++ b/PLC/Accessor.py @@ -21,6 +21,8 @@ This is implemented as a singleton, so we can cache results over time""" _instance = None + tag_locators={} + def __init__ (self, api): self.api=api # 'tagname'=>'tag_id' @@ -28,7 +30,7 @@ This is implemented as a singleton, so we can cache results over time""" def has_cache (self,tagname): return self.cache.has_key(tagname) def get_cache (self,tagname): return self.cache[tagname] - def set_cache (self,tagname,tag_id): self.cache[tagname]=tag_id + def set_cache (self,tagname,tag_type): self.cache[tagname]=tag_type def locate_or_create_tag (self, tagname, category, description, roles): "search tag type from tagname & create if needed" @@ -54,10 +56,26 @@ This is implemented as a singleton, so we can cache results over time""" except: # xxx todo find a more appropriate way of notifying this print "Accessor.locate_or_create_tag: Could not add role %r to tag_type %s"%(role,tagname) - tag_type_id = tag_type['tag_type_id'] - self.set_cache(tagname,tag_type_id) - return tag_type_id + self.set_cache(tagname,tag_type) + return tag_type + + # a locator is a function that retrieves - or creates - a tag_type instance + @staticmethod + def register_tag_locator (name, tag_locator): + Accessor.tag_locators[name]=tag_locator + @staticmethod + def retrieve_tag_locator (name): + return Accessor.tag_locators[name] + + # this is designed to be part of the 'service plc start' sequence + # it ensures the creation of all the tagtypes defined + # in the various accessors + # it's not easy to have define_accessors do this because at + # load-time as we do not have an instance of API yet + def run_all_tag_locators (self): + for (name, tag_locator) in Accessor.tag_locators.items(): + tag_locator(self) #################### # make it a singleton so we can cache stuff in there over time