4 # Thierry Parmentelat - INRIA
7 # just a placeholder for storing accessor-related tag checkers
8 # this is filled by the accessors factory
10 from PLC.TagTypes import TagTypes, TagType
13 class Accessor (object) :
14 """This is placeholder for storing accessor-related tag checkers
15 methods in this class are defined by the accessors factory
17 This is implemented as a singleton, so we can cache results over time"""
21 def __init__ (self, api):
26 def has_cache (self,tagname): return self.cache.has_key(tagname)
27 def get_cache (self,tagname): return self.cache[tagname]
28 def set_cache (self,tagname,tag_id): self.cache[tagname]=tag_id
30 def locate_or_create_tag (self,tagname,category, description, min_role_id):
31 "search tag type from tagname & create if needed"
34 if self.has_cache (tagname):
35 return self.get_cache(tagname)
37 tag_types = TagTypes (self.api, {'tagname':tagname})
39 tag_type = tag_types[0]
41 # not found: create it
42 tag_type_fields = {'tagname':tagname,
43 'category' : category,
44 'description' : description,
45 'min_role_id': min_role_id}
46 tag_type = TagType (self.api, tag_type_fields)
48 tag_type_id = tag_type['tag_type_id']
49 self.set_cache(tagname,tag_type_id)
54 # make it a singleton so we can cache stuff in there over time
55 def AccessorSingleton (api):
56 if not Accessor._instance:
57 Accessor._instance = Accessor(api)
58 return Accessor._instance