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
13 from PLC.Roles import Roles, Role
16 class Accessor (object) :
17 """This is placeholder for storing accessor-related tag checkers.
18 Methods in this class are defined by the accessors factory
20 This is implemented as a singleton, so we can cache results over time"""
24 def __init__ (self, api):
29 def has_cache (self,tagname): return self.cache.has_key(tagname)
30 def get_cache (self,tagname): return self.cache[tagname]
31 def set_cache (self,tagname,tag_id): self.cache[tagname]=tag_id
33 def locate_or_create_tag (self, tagname, category, description, roles):
34 "search tag type from tagname & create if needed"
37 if self.has_cache (tagname):
38 return self.get_cache(tagname)
40 tag_types = TagTypes (self.api, {'tagname':tagname})
42 tag_type = tag_types[0]
44 # not found: create it
45 tag_type_fields = {'tagname':tagname,
46 'category' : category,
47 'description' : description}
48 tag_type = TagType (self.api, tag_type_fields)
52 role_obj=Roles (self.api, role)[0]
53 tag_type.add_role(role_obj)
55 # xxx todo find a more appropriate way of notifying this
56 print "Accessor.locate_or_create_tag: Could not add role %r to tag_type %s"%(role,tagname)
57 tag_type_id = tag_type['tag_type_id']
58 self.set_cache(tagname,tag_type_id)
63 # make it a singleton so we can cache stuff in there over time
64 def AccessorSingleton (api):
65 if not Accessor._instance:
66 Accessor._instance = Accessor(api)
67 return Accessor._instance