2 # Thierry Parmentelat - INRIA
5 # just a placeholder for storing accessor-related tag checkers
6 # this is filled by the accessors factory
8 # NOTE. If you ever come to manually delete a TagType that was created
9 # by the Factory, you need to restart your python instance / web server
10 # as the cached information then becomes wrong
12 from PLC.TagTypes import TagTypes, TagType
15 class Accessor (object) :
16 """This is placeholder for storing accessor-related tag checkers.
17 Methods in this class are defined by the accessors factory
19 This is implemented as a singleton, so we can cache results over time"""
23 def __init__ (self, api):
28 def has_cache (self,tagname): return self.cache.has_key(tagname)
29 def get_cache (self,tagname): return self.cache[tagname]
30 def set_cache (self,tagname,tag_id): self.cache[tagname]=tag_id
32 def locate_or_create_tag (self,tagname,category, description, min_role_id):
33 "search tag type from tagname & create if needed"
36 if self.has_cache (tagname):
37 return self.get_cache(tagname)
39 tag_types = TagTypes (self.api, {'tagname':tagname})
41 tag_type = tag_types[0]
43 # not found: create it
44 tag_type_fields = {'tagname':tagname,
45 'category' : category,
46 'description' : description,
47 'min_role_id': min_role_id}
48 tag_type = TagType (self.api, tag_type_fields)
50 tag_type_id = tag_type['tag_type_id']
51 self.set_cache(tagname,tag_type_id)
56 # make it a singleton so we can cache stuff in there over time
57 def AccessorSingleton (api):
58 if not Accessor._instance:
59 Accessor._instance = Accessor(api)
60 return Accessor._instance