1 # Thierry Parmentelat - INRIA
4 from types import NoneType
6 from PLC.Method import Method
7 from PLC.Auth import Auth
8 from PLC.Parameter import Parameter, Mixed
10 from PLC.Faults import *
12 from PLC.Nodes import Node
13 from PLC.Interfaces import Interface
14 from PLC.Slices import Slice
15 from PLC.Ilinks import Ilink
17 # known classes : { class -> secondary_key }
18 taggable_classes = { Node : 'hostname', Interface : None, Slice: 'login_base', Ilink : None}
20 # generates 2 method classes:
21 # Get<classname><methodsuffix> (auth, id_or_name) -> tagvalue or None
22 # Set<classname><methodsuffix> (auth, id_or_name, tagvalue) -> None
23 # tagvalue is always a string, no cast nor typecheck for now
25 # note: tag_min_role_id gets attached to the tagtype instance,
26 # while get_roles and set_roles get attached to the created methods
28 # returns a tuple (get_method, set_method)
29 # See Shortcuts.py for examples
31 def get_set_factory (objclass, methodsuffix,
32 tagname, category, description, tag_min_role_id=10,
33 get_roles=['admin'], set_roles=['admin']):
35 if objclass not in taggable_classes:
37 raise PLCInvalidArgument,"PLC.Shortcuts.Factory: unknown class %s"%objclass.__name__
39 raise PLCInvalidArgument,"PLC.Shortcuts.Factory: unknown class ??"
40 classname=objclass.__name__
41 get_name = "Get" + classname + methodsuffix
42 set_name = "Set" + classname + methodsuffix
44 # create method objects under PLC.Method.Method
45 get_class = type (get_name, (Method,),
46 {"__doc__":"Shortcut 'get' method designed for %s objects using tag %s"%\
48 set_class = type (set_name, (Method,),
49 {"__doc__":"Shortcut 'set' method designed for %s objects using tag %s"%\
52 get_accepts = [ Auth () ]
53 primary_key=objclass.primary_key
54 secondary_key = taggable_classes[objclass]
56 get_accepts += [ objclass.fields[primary_key] ]
58 get_accepts += [ Mixed (objclass.fields[primary_key], objclass.fields[secondary_key]) ]
59 # for set, idem + one additional arg
60 set_accepts = get_accepts + [ Parameter (str,"New tag value") ]
63 get_returns = Mixed (Parameter (str), Parameter(NoneType))
64 set_returns = Parameter(NoneType)
67 setattr(get_class,'roles',get_roles)
68 setattr(get_class,'accepts',get_accepts)
69 setattr(get_class,'returns', get_returns)
70 setattr(get_class,'skip_typecheck',True)
72 setattr(set_class,'roles',set_roles)
73 setattr(set_class,'accepts',set_accepts)
74 setattr(set_class,'returns', set_returns)
75 setattr(set_class,'skip_typecheck',True)
77 # body of the get method
78 def get_call (self, auth, id_or_name):
79 print 'Automagical Shortcut get method',classname,get_name,tagname,primary_key,secondary_key
80 print 'Warning: PLC/Shortcuts/Factory is an ongoing work'
82 setattr (get_class,"call",get_call)
84 # body of the set method
85 def set_call (self, auth, id_or_name, tagvalue):
86 print 'Automagical Shortcut set method',classname,get_name,tagname,primary_key,secondary_key
87 print 'Warning: PLC/Shortcuts/Factory is an ongoing work'
89 setattr (set_class,"call",set_call)
91 return ( get_class, set_class )