2014-03-28, 21:31
Dear all,
I am trying to write yet another python wrapper for the XBMC JSON-RPC so I can send commands and also subscribe to notifications. The reason for this is that I am working on a larger project in which I want to be able to control my HTPC and everything that happens in it (torrent downloading, xbmc updates, etc.) from a single place (you can see the progress in https://github.com/apuignav/pythonhtpc (xbmc is in pythonhtpc/rpcs/xbmclib.py).
I wanted to add automatic validation of the parameters passed to methods and the responses from XBMC in combination with "discovery" using JSONRPC.Introspect(). As far as I understand, this call should return a valid JSON schema to validate against, but I have not been able to manage to do so in a general way. I think I am probably making a mistake or I am not understanding what to expect from this function, so let me try to give some examples that will clarify my problems:
which fails because
However, if i do
everything works beautifully.
In the first case, the returns variable contains
while in the other it gives a simple string
In the case of version, I can solve it with
but of course it is not nice because I need to know the call returns a dictionary with a key called 'version' (I can hack this, but I would like to avoid it if possible). In addition, this would break the Ping case.
In addition to this, I have problems in validating parameters, since 'params' is a list that needs to be converted to
in order to successfully validate params with the form
(however, this is probably my fault).
Can somebody point me to the right path? I have the feeling there is something very stupid I am not understanding... There a few other things I don't understand, but I think if you can help me solve these fundamental problems I will be able to move forward and understand the rest.
Thanks you very much in advance,
Albert
PS: I've found that JSONRPC.Introspect doesn't contain information on the OnScreensaverActivated and OnScreensaverDeactivated notifications, at least in my XBMC version. Is this known?
I am trying to write yet another python wrapper for the XBMC JSON-RPC so I can send commands and also subscribe to notifications. The reason for this is that I am working on a larger project in which I want to be able to control my HTPC and everything that happens in it (torrent downloading, xbmc updates, etc.) from a single place (you can see the progress in https://github.com/apuignav/pythonhtpc (xbmc is in pythonhtpc/rpcs/xbmclib.py).
I wanted to add automatic validation of the parameters passed to methods and the responses from XBMC in combination with "discovery" using JSONRPC.Introspect(). As far as I understand, this call should return a valid JSON schema to validate against, but I have not been able to manage to do so in a general way. I think I am probably making a mistake or I am not understanding what to expect from this function, so let me try to give some examples that will clarify my problems:
Code:
from validictory import validate
schema = xbmc.execute_method('JSONRPC.Introspect')['methods']['JSONRPC.Version']['returns']
returns = xbmc.execute_method('JSONRPC.Version')
validate(returns, schema)
Code:
validictory.validator.FieldValidationError: Required field 'major' is missing
However, if i do
Code:
from validictory import validate
schema = xbmc.execute_method('JSONRPC.Introspect')['methods']['JSONRPC.Ping']['returns']
returns = xbmc.execute_method('JSONRPC.Ping')
validate(returns, schema)
In the first case, the returns variable contains
Code:
{u'version': {u'major': 6, u'minor': 0, u'patch': 3}}
Code:
u'pong'
In the case of version, I can solve it with
Code:
from validictory import validate
schema = xbmc.execute_method('JSONRPC.Introspect')['methods']['JSONRPC.Version']['returns']
returns = xbmc.execute_method('JSONRPC.Version')
validate(returns['version'], schema)
In addition to this, I have problems in validating parameters, since 'params' is a list that needs to be converted to
Code:
{'type': 'object', 'properties': params}
Code:
{'param1': value, 'param2': value}
Can somebody point me to the right path? I have the feeling there is something very stupid I am not understanding... There a few other things I don't understand, but I think if you can help me solve these fundamental problems I will be able to move forward and understand the rest.
Thanks you very much in advance,
Albert
PS: I've found that JSONRPC.Introspect doesn't contain information on the OnScreensaverActivated and OnScreensaverDeactivated notifications, at least in my XBMC version. Is this known?
Code:
>>> xbmc.execute_method('JSONRPC.Introspect')['notifications']['GUI.OnScreensaverActivated']
[2014-03-28 20:25:44,763] htpc.RaspiXBMC::DEBUG [core.py:execute_method:110] Executing method JSONRPC.Introspect with parameters {}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'GUI.OnScreensaverActivated'