From: Thierry Parmentelat Date: Mon, 29 Nov 2010 15:22:58 +0000 (+0100) Subject: new step 'accessors' in the 'service plc start' X-Git-Tag: plcapi-5.0-19~19 X-Git-Url: http://git.onelab.eu/?p=plcapi.git;a=commitdiff_plain;h=fe32171e8722932e55d6c3e5b0df2acc34a1939f new step 'accessors' in the 'service plc start' this ensures all the tagtypes mentioned in accessors are created --- diff --git a/PLC/Accessor.py b/PLC/Accessor.py index d4af37d..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' @@ -57,6 +59,23 @@ This is implemented as a singleton, so we can cache results over time""" 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 diff --git a/PLC/Accessors/Factory.py b/PLC/Accessors/Factory.py index 539dbbb..3ad0bc1 100644 --- a/PLC/Accessors/Factory.py +++ b/PLC/Accessors/Factory.py @@ -131,20 +131,20 @@ def define_accessors (module, objclass, methodsuffix, tagname, # locate the tag and create it if needed # this method is attached to the Accessor class - def locate_or_create_tag (self): + def tag_locator (self): return self.locate_or_create_tag (tagname=tagname, category=category, description=description, roles=set_roles) # attach it to the Accessor class - setattr(Accessor,locator_name,locate_or_create_tag) + Accessor.register_tag_locator(locator_name,tag_locator) # body of the get method def get_call (self, auth, id_or_name): # locate the tag, see above - locator = getattr(Accessor,locator_name) - tag_type = locator(AccessorSingleton(self.api)) + tag_locator = Accessor.retrieve_tag_locator(locator_name) + tag_type = tag_locator(AccessorSingleton(self.api)) tag_type_id=tag_type['tag_type_id'] filter = {'tag_type_id':tag_type_id} @@ -179,8 +179,8 @@ def define_accessors (module, objclass, methodsuffix, tagname, primary_id = obj[primary_key] # locate the tag, see above - locator = getattr(Accessor,locator_name) - tag_type = locator(AccessorSingleton(self.api)) + tag_locator = Accessor.retrieve_tag_locator(locator_name) + tag_type = tag_locator(AccessorSingleton(self.api)) tag_type_id = tag_type['tag_type_id'] # check authorization diff --git a/plc.d/accessors b/plc.d/accessors new file mode 100755 index 0000000..ee13adb --- /dev/null +++ b/plc.d/accessors @@ -0,0 +1,14 @@ +#!/usr/bin/plcsh +# -*- python -*- + +# +# priority: 901 +# + +from PLC.Accessor import AccessorSingleton + +if __name__ == '__main__': + print "Ensuring creationg of accessor tags" + print 'api=%r'%api + AccessorSingleton(api).run_all_tag_locators() +