
commit 5c5c59f1e7b16ed1f7a5131ec072e5646d086e44 Author: Damian Johnson <atagar@torproject.org> Date: Sun Aug 4 11:38:28 2013 -0700 Adding a 'block' argument to the Query class I'm finding it to be pretty common to run... query = Query(my_resource) query.run(True) if not query.error: # do something else: # report error Adding an argument to the Query constructor so we can drop the following run(). --- stem/descriptor/remote.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/stem/descriptor/remote.py b/stem/descriptor/remote.py index 30ac578..23a1f8c 100644 --- a/stem/descriptor/remote.py +++ b/stem/descriptor/remote.py @@ -143,17 +143,17 @@ class Query(object): query = Query( '/tor/server/all.z', - descriptor_type = 'server-descriptor 1.0', + block = True, timeout = 30, ) print "Current relays:" - try: - for desc in query.run(): + if not query.error: + for desc in query: print desc.fingerprint - except Exception as exc: - print "Unable to retrieve the server descriptors: %s" % exc + else: + print "Unable to retrieve the server descriptors: %s" % query.error ... while iterating fails silently... @@ -216,9 +216,11 @@ class Query(object): which to parse a :class:`~stem.descriptor.networkstatus.NetworkStatusDocument` :param bool start: start making the request when constructed (default is **True**) + :param bool block: only return after the request has been completed, this is + the same as running **query.run(True)** (default is **False**) """ - def __init__(self, resource, descriptor_type = None, endpoints = None, retries = 2, fall_back_to_authority = False, timeout = None, start = True, validate = True, document_handler = stem.descriptor.DocumentHandler.ENTRIES): + def __init__(self, resource, descriptor_type = None, endpoints = None, retries = 2, fall_back_to_authority = False, timeout = None, start = True, block = False, validate = True, document_handler = stem.descriptor.DocumentHandler.ENTRIES): if not resource.startswith('/'): raise ValueError("Resources should start with a '/': %s" % resource) @@ -252,6 +254,9 @@ class Query(object): if start: self.start() + if block: + self.run(True) + def start(self): """ Starts downloading the scriptors if we haven't started already.