new step 'accessors' in the 'service plc start'
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Mon, 29 Nov 2010 15:22:58 +0000 (16:22 +0100)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Mon, 29 Nov 2010 15:22:58 +0000 (16:22 +0100)
this ensures all the tagtypes mentioned in accessors are created

PLC/Accessor.py
PLC/Accessors/Factory.py
plc.d/accessors [new file with mode: 0755]

index d4af37d..fb12f3d 100644 (file)
@@ -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
index 539dbbb..3ad0bc1 100644 (file)
@@ -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 (executable)
index 0000000..ee13adb
--- /dev/null
@@ -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()
+