4
4
5
5
from ._errors import TooFewArguments
6
6
from ._errors import TooManyArguments
7
+ from ._errors import ObjectNotFound
8
+ from ._errors import RotException
7
9
8
10
9
11
def static_init (cls ):
@@ -24,9 +26,16 @@ def __contains__(self, obj):
24
26
25
27
26
28
def rot (* args ):
27
- stdout = subprocess .check_output (["/opt/bin/rot" ] + list (args ))
28
- if stdout :
29
- return json .loads (stdout )
29
+ try :
30
+ stdout = subprocess .check_output (["/opt/bin/rot" ] + list (args ))
31
+ if stdout :
32
+ return json .loads (stdout )
33
+ except subprocess .CalledProcessError as e :
34
+ stdout = e .output .decode ("utf-8" )
35
+ if "org.freedesktop.DBus.Error.UnknownObject" in stdout :
36
+ raise ObjectNotFound (stdout ) from e
37
+
38
+ raise RotException (stdout ) from e
30
39
31
40
return None
32
41
@@ -44,6 +53,21 @@ def fn(self):
44
53
setattr (obj .__class__ , name , property (fn ))
45
54
46
55
56
+ def _convert (_type , val ):
57
+ if _type is None :
58
+ return None
59
+
60
+ from ._base import APIObject
61
+
62
+ if issubclass (_type , APIObject ):
63
+ return _type (val )
64
+
65
+ if _type in (str , bool , int ):
66
+ return val
67
+
68
+ raise NotImplementedError (_type )
69
+
70
+
47
71
def _createMethod (cls , name , retType , * signature ):
48
72
def fn (cls , * args ):
49
73
if len (args ) > len (signature ):
@@ -68,23 +92,11 @@ def fn(cls, *args):
68
92
elif issubclass (argType , dict ):
69
93
qtype = "QVariant"
70
94
else :
71
- raise NotImplementedError ()
95
+ raise NotImplementedError (argType )
72
96
73
97
arguments .append (f"{ qtype } :{ json .dumps (args [0 ])} " )
74
98
75
- res = rot (* cls .rotArgs , "call" , name , * arguments )
76
- if retType is None :
77
- return None
78
-
79
- from ._base import APIObject
80
-
81
- if issubclass (APIObject , retType ):
82
- return retType (res )
83
-
84
- if retType in (str , bool , int ):
85
- return res
86
-
87
- raise NotImplementedError ()
99
+ return _convert (retType , rot (* cls .rotArgs , "call" , name , * arguments ))
88
100
89
101
return fn
90
102
@@ -105,3 +117,18 @@ def _registerMethod(obj, name, retType, *signature):
105
117
name ,
106
118
_createMethod (obj , name , retType , * signature ),
107
119
)
120
+
121
+
122
+ def _listenMethod (obj , name , * signature ):
123
+ if len (signature ) > 1 :
124
+ raise NotImplementedError ()
125
+
126
+ # TODO cache these instead of creating them every time
127
+ def fn ():
128
+ res = rot (* obj .rotArgs , "--once" , "listen" , name )
129
+ if signature :
130
+ return _convert (signature [0 ], res )
131
+
132
+ return res
133
+
134
+ return fn
0 commit comments