4.5.0r6
[enigma2.git] / usr / lib / enigma2 / python / API.py
1 from __future__ import print_function
2 from Tools.BoundFunction import boundFunction
3
4 def getFunctionTree(my_api, name = None):
5         functions = my_api.getFunctions()
6         subapis = my_api.getSubAPIs()
7         resultlist = []
8         for function in functions:
9                 item = None
10                 if name is None:
11                         item = function
12                 else:
13                         item = "%s.%s" %(name, function)
14                 resultlist.append(item)
15         for subapi in subapis:
16                 subapiuri = ""
17                 if name is None:
18                         subapiuri = subapi
19                 else:
20                         subapiuri = "%s.%s" % (name, subapi)
21                 resultlist += getFunctionTree(my_api.call(subapi), subapiuri)
22         return resultlist
23
24 def getSubAPITree(my_api, name = None):
25         functions = my_api.getFunctions()
26         subapis = my_api.getSubAPIs()
27         resultlist = []
28         if len(functions) > 0:
29                 resultlist = [name]
30                 for subapi in subapis:
31                         if name is None:
32                                 subapiuri = subapi
33                         else:
34                                 subapiuri = "%s.%s" % (name, subapi)
35                         resultlist += getSubAPITree(my_api.call(subapi), subapiuri)
36         return resultlist
37
38 def registerAPIs():
39         from APIs import SystemInfo, ServiceData
40         modules = [SystemInfo, ServiceData]
41         
42         for module in modules:
43                 module.registerAPIs(api)
44
45         print("APIs:", getSubAPITree(api, "api"))
46         print("Functions:", getFunctionTree(api, "api"))
47
48 session = None
49
50 class API(object):
51         def __init__(self):
52                 self.__calls = {}
53                 self.__parameters_type = {}
54                 self.__return_type = {}
55                 self.__needsSession = {}
56                 self.__version = None
57                 self.__session = None
58
59         def __setattr__(self, name, value):
60                 if name in ("_%s__calls" % self.__class__.__name__, "version"):
61                         super.__setattr__(self, name, value)
62                 else:
63                         self.__calls[name] = value
64
65         def __getattr__(self, name):
66                 call = self.__calls.get(name, None)
67                 return call
68
69         def __getSub(self, matcher):
70                 calls = []
71                 for key in self.__calls.keys():
72                         if matcher(self.__calls[key]):
73                                 calls.append(key)
74                 return calls
75
76         def __setVersion(self, version):
77                 assert isinstance(version, int)
78                 self.__version = version
79
80         def __getVersion(self):
81                 return self.__version
82         
83         def setSession(self, current_session):
84                 global session
85                 session = current_session
86
87         def getFunctions(self):
88                 functions = self.__getSub(lambda x: not isinstance(x, type(self)))
89                 functions.remove("_%s__version" % self.__class__.__name__)
90                 functions.remove("_%s__return_type" % self.__class__.__name__)
91                 functions.remove("_%s__parameters_type" % self.__class__.__name__)
92                 return functions
93
94         def getSubAPIs(self):
95                 return self.__getSub(lambda x: isinstance(x, type(self)))
96
97         def add_call(self, name, call, parameters_type, return_type, needsSession = False):
98                 split = name.split(".")
99                 if len(split) > 1:
100                         subapiname = split[0]
101                         subapi = self.__calls.get(subapiname, API())
102                         subapi.add_call('.'.join(split[1:]), call, parameters_type, return_type, needsSession = needsSession)
103
104                         self.__calls[subapiname] = subapi
105                 else:
106                         self.__calls[name] = call
107                         self.__parameters_type[name] = parameters_type
108                         self.__return_type[name] = return_type
109                         self.__needsSession[name] = needsSession
110
111         def call(self, name):
112                 if name is None:
113                         return self
114                 split = name.split(".")
115                 if len(split) > 1:
116                         subapi = self.__calls.get(split[0], None)
117                         if subapi is not None:
118                                 return subapi.call('.'.join(split[1:]))
119                         return None
120                 else:
121                         call = self.__calls.get(name, None)
122                         if call is not None:
123                                 if self.__needsSession.get(name, False): # subapi
124                                         call = boundFunction(call, session)
125                         return call 
126
127         version = property(__getVersion, __setVersion)
128
129 api = API()
130