Skip to content

Commit ca2db07

Browse files
committed
Allow setting of the 'timeout' for an operation
Timeout can now be set globally for a 'MythTVService' session or can be set for each operation. Especially uUseful when retrieving metadata.
1 parent cfd4277 commit ca2db07

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

mythtv/bindings/python/MythTV/mythservices.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -567,14 +567,16 @@ class MythTVService( MythServiceData ):
567567
See 'MythTV/services_api/send.py' for logging of the services_api.
568568
"""
569569

570-
def __init__(self, service, host, port=6544, opname=None, optype=None, **opdata):
570+
def __init__(self, service, host, port=6544, opname=None, optype=None, timeout=None, **opdata):
571571
self.service = service
572572
self.host = host
573573
self.port = port
574574
self.operation = None
575+
# set timeout globally for that session:
576+
self.operation_timeout = timeout
575577
self.operation_result = None
576578
MythServiceData.__init__(self, service, host, port=port)
577-
# initialize connection via MythServiceAPI
579+
# initialize connection via MythServiceAPI:
578580
self.request = ServiceAPI(host, port)
579581
# evaluate operation kwargs (needs python 3.6+):
580582
if opname and optype:
@@ -587,7 +589,7 @@ def __init__(self, service, host, port=6544, opname=None, optype=None, **opdata)
587589
else:
588590
op["opdata"] = {}
589591
opdict = self.encode_operation(op)
590-
self.perform_operation(opdict)
592+
self.perform_operation(opdict, timeout=timeout)
591593
except:
592594
raise MythError("Unable to perform operation '%s': '%s': '%s'!"
593595
%(opname, optype, opdata))
@@ -654,7 +656,7 @@ def _eval_response(self, eroot):
654656
self._process(self.data)
655657
return(res)
656658

657-
def perform_operation(self, opdict):
659+
def perform_operation(self, opdict, timeout=None):
658660
"""
659661
Actually performs the operation given by a dictionary.
660662
like http://<hostip>:6544/Dvr/GetRecordedList?Descending=True&Count=3
@@ -667,9 +669,17 @@ def perform_operation(self, opdict):
667669
Complex responses will be objectified within this class.
668670
The version of these responses is stored in 'self.operation_version'.
669671
The result of this method is stored in 'self.operation_result'.
672+
Optionally, a 'timeout' value can be set for this operation.
670673
"""
671674
self.operation = opdict['opname']
672675
messagetype = opdict['optype']
676+
# define session options valid for 'POST' and 'GET' operations:
677+
opts = {'rawxml': True}
678+
if self.operation_timeout:
679+
opts['timeout'] = self.operation_timeout
680+
if timeout:
681+
opts['timeout'] = timeout
682+
673683
with self.request as api_request:
674684
result = None
675685
if messagetype == 'GET':
@@ -680,7 +690,6 @@ def perform_operation(self, opdict):
680690
rest = urlencode(opdict['opdata'])
681691
else:
682692
rest = None
683-
opts = {'rawxml': True}
684693
try:
685694
result = api_request(endpoint=endpoint, rest=rest, opts = opts)
686695
except RuntimeWarning as warning:
@@ -692,6 +701,8 @@ def perform_operation(self, opdict):
692701
return(result)
693702
else:
694703
raise MythError("Unknown result from 'send' operation")
704+
except RuntimeError as e:
705+
raise MythError(e.args)
695706
eroot = etree.fromstring(result)
696707
#print(etree.tostring(eroot, pretty_print=True, encoding='unicode'))
697708
if eroot.tag in self.schema_dict.keys():
@@ -701,7 +712,7 @@ def perform_operation(self, opdict):
701712
result = self._eval_response(eroot)
702713
elif messagetype == 'POST':
703714
endpoint = '%s/%s'%(self.service, self.operation)
704-
opts = {'rawxml': True, 'wrmi': True}
715+
opts['wrmi'] = True
705716
if opdict['opdata'] is not None:
706717
post = opdict['opdata']
707718
else:

0 commit comments

Comments
 (0)