Skip to content

Commit

Permalink
Allow setting of the 'timeout' for an operation
Browse files Browse the repository at this point in the history
Timeout can now be set globally for a 'MythTVService' session or
can be set for each operation.
Especially uUseful when retrieving metadata.
  • Loading branch information
rcrdnalor committed May 29, 2020
1 parent cfd4277 commit ca2db07
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions mythtv/bindings/python/MythTV/mythservices.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,16 @@ class MythTVService( MythServiceData ):
See 'MythTV/services_api/send.py' for logging of the services_api.
"""

def __init__(self, service, host, port=6544, opname=None, optype=None, **opdata):
def __init__(self, service, host, port=6544, opname=None, optype=None, timeout=None, **opdata):
self.service = service
self.host = host
self.port = port
self.operation = None
# set timeout globally for that session:
self.operation_timeout = timeout
self.operation_result = None
MythServiceData.__init__(self, service, host, port=port)
# initialize connection via MythServiceAPI
# initialize connection via MythServiceAPI:
self.request = ServiceAPI(host, port)
# evaluate operation kwargs (needs python 3.6+):
if opname and optype:
Expand All @@ -587,7 +589,7 @@ def __init__(self, service, host, port=6544, opname=None, optype=None, **opdata)
else:
op["opdata"] = {}
opdict = self.encode_operation(op)
self.perform_operation(opdict)
self.perform_operation(opdict, timeout=timeout)
except:
raise MythError("Unable to perform operation '%s': '%s': '%s'!"
%(opname, optype, opdata))
Expand Down Expand Up @@ -654,7 +656,7 @@ def _eval_response(self, eroot):
self._process(self.data)
return(res)

def perform_operation(self, opdict):
def perform_operation(self, opdict, timeout=None):
"""
Actually performs the operation given by a dictionary.
like http://<hostip>:6544/Dvr/GetRecordedList?Descending=True&Count=3
Expand All @@ -667,9 +669,17 @@ def perform_operation(self, opdict):
Complex responses will be objectified within this class.
The version of these responses is stored in 'self.operation_version'.
The result of this method is stored in 'self.operation_result'.
Optionally, a 'timeout' value can be set for this operation.
"""
self.operation = opdict['opname']
messagetype = opdict['optype']
# define session options valid for 'POST' and 'GET' operations:
opts = {'rawxml': True}
if self.operation_timeout:
opts['timeout'] = self.operation_timeout
if timeout:
opts['timeout'] = timeout

with self.request as api_request:
result = None
if messagetype == 'GET':
Expand All @@ -680,7 +690,6 @@ def perform_operation(self, opdict):
rest = urlencode(opdict['opdata'])
else:
rest = None
opts = {'rawxml': True}
try:
result = api_request(endpoint=endpoint, rest=rest, opts = opts)
except RuntimeWarning as warning:
Expand All @@ -692,6 +701,8 @@ def perform_operation(self, opdict):
return(result)
else:
raise MythError("Unknown result from 'send' operation")
except RuntimeError as e:
raise MythError(e.args)
eroot = etree.fromstring(result)
#print(etree.tostring(eroot, pretty_print=True, encoding='unicode'))
if eroot.tag in self.schema_dict.keys():
Expand All @@ -701,7 +712,7 @@ def perform_operation(self, opdict):
result = self._eval_response(eroot)
elif messagetype == 'POST':
endpoint = '%s/%s'%(self.service, self.operation)
opts = {'rawxml': True, 'wrmi': True}
opts['wrmi'] = True
if opdict['opdata'] is not None:
post = opdict['opdata']
else:
Expand Down

0 comments on commit ca2db07

Please sign in to comment.