checkpointing first - non functional - skeleton for shortcut methods
[plcapi.git] / PLC / Shortcuts / Factory.py
1 # Thierry Parmentelat - INRIA
2 # $Id$
3
4 from types import NoneType
5
6 from PLC.Method import Method
7 from PLC.Auth import Auth
8 from PLC.Parameter import Parameter, Mixed
9
10 from PLC.Faults import *
11
12 from PLC.Nodes import Node
13 from PLC.Interfaces import Interface
14 from PLC.Slices import Slice
15 from PLC.Ilinks import Ilink
16
17 # known classes : { class -> secondary_key }
18 taggable_classes = { Node : 'hostname', Interface : None, Slice: 'login_base', Ilink : None}
19
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
24 #
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
27
28 # returns a tuple (get_method, set_method)
29 # See Shortcuts.py for examples
30
31 def get_set_factory (objclass, methodsuffix, 
32                      tagname, category, description, tag_min_role_id=10,
33                      get_roles=['admin'], set_roles=['admin']):
34     
35     if objclass not in taggable_classes:
36         try:
37             raise PLCInvalidArgument,"PLC.Shortcuts.Factory: unknown class %s"%objclass.__name__
38         except:
39             raise PLCInvalidArgument,"PLC.Shortcuts.Factory: unknown class ??"
40     classname=objclass.__name__
41     get_name = "Get" + classname + methodsuffix
42     set_name = "Set" + classname + methodsuffix
43
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"%\
47                            (classname,tagname)})
48     set_class = type (set_name, (Method,),
49                       {"__doc__":"Shortcut 'set' method designed for %s objects using tag %s"%\
50                            (classname,tagname)})
51     # accepts 
52     get_accepts = [ Auth () ]
53     primary_key=objclass.primary_key
54     secondary_key = taggable_classes[objclass]
55     if not secondary_key:
56         get_accepts += [ objclass.fields[primary_key] ]
57     else:
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") ]
61
62     # returns
63     get_returns = Mixed (Parameter (str), Parameter(NoneType))
64     set_returns = Parameter(NoneType)
65
66     # store in classes
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)
71
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)
76
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'
81         return 'foobar'
82     setattr (get_class,"call",get_call)
83
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'
88         return None
89     setattr (set_class,"call",set_call)
90
91     return ( get_class, set_class )
92