auth fix
[plcapi.git] / PLC / Legacy / v42legacy.py
1 # $Id: $
2
3 from PLC.Method import Method
4 from PLC.Auth import Auth
5 import PLC.Auth
6
7 # apply rename on list (columns) or dict (filter) args
8 def patch (arg,rename):
9     if isinstance(arg,list):
10         for i in range(0,len(arg)):
11             arg[i] = patch(arg[i],rename)
12         return arg
13     if isinstance(arg,dict):
14         return dict ( [ (rename(k),v) for (k,v) in arg.iteritems() ] )
15     return rename(arg)
16
17 def make_class (legacyname,newname,path,import_deep,v42rename,v43rename):
18     # locate new class
19     newclass=getattr(import_deep(path+newname),newname)
20     setattr(newclass,"__origcall",getattr(newclass,"call"))
21     # create class for legacy name
22     legacyclass = type(legacyname,(newclass,), 
23                        {"__doc__":"Legacy method - please use %s instead"%newname})
24     for internal in ["roles","accepts","returns"]:
25         setattr(legacyclass,internal,getattr(newclass,internal))
26     # turn off type checking, as introspection code fails on wrapped_call
27     setattr(legacyclass,"skip_typecheck",True)
28     # rewrite call
29     def wrapped_call (self,auth,*args, **kwds):
30         #print "%s: self.caller = %s, auth=%s, self.api=%s, self=%s" % (legacyname,self.caller,auth,self.api,self)
31         if not hasattr(self,"auth"):
32             self.auth = None
33
34         if self.auth == None and auth <> None:
35             self.auth = auth
36
37         if self.auth <> None and self.caller == None:
38             a = PLC.Auth.map_auth(auth)
39             print "a = %s" % a
40             a.check(self,auth,*args)
41
42         newargs=[patch(x,v42rename) for x in args]
43         newkwds=dict ( [ (k,patch(v,v42rename)) for (k,v) in kwds.iteritems() ] )
44         results = self.__origcall(auth,*newargs,**newkwds)
45         return patch(results,v43rename)
46     setattr(legacyclass,"call",wrapped_call)
47
48     return legacyclass
49