From b2ebef0fbd2acc0c59f006d8cad962dc47250d23 Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Thu, 10 Jun 2010 00:55:06 +0000 Subject: [PATCH] Move the knowledge of the urls for the REST API to one new file htsworkflow.util.api and then update some of the scripts that were using the api to import from the new module. Yes this increases the dependencies, but it does mean it'll be easier to update the urls if we need to change them --- htsworkflow/pipelines/retrieve_config.py | 3 +- htsworkflow/util/api.py | 94 ++++++++++++++++++++++++ scripts/make-library-tree | 34 ++------- 3 files changed, 101 insertions(+), 30 deletions(-) create mode 100644 htsworkflow/util/api.py diff --git a/htsworkflow/pipelines/retrieve_config.py b/htsworkflow/pipelines/retrieve_config.py index 0ef42a5..fbd1187 100644 --- a/htsworkflow/pipelines/retrieve_config.py +++ b/htsworkflow/pipelines/retrieve_config.py @@ -14,6 +14,7 @@ except ImportError, e: import simplejson as json from htsworkflow.frontend.auth import apidata +from htsworkflow.util import api from htsworkflow.util.url import normalize_url from htsworkflow.pipelines.genome_mapper import getAvailableGenomes from htsworkflow.pipelines.genome_mapper import constructMapperDict @@ -36,7 +37,7 @@ def retrieve_flowcell_info(base_host_url, flowcell): """ Return a dictionary describing a """ - url = base_host_url + '/experiments/config/%s/json' % (flowcell) + url = api.flowcell_url(base_host_url, flowcell) try: apipayload = urllib.urlencode(apidata) diff --git a/htsworkflow/util/api.py b/htsworkflow/util/api.py new file mode 100644 index 0000000..f6af79f --- /dev/null +++ b/htsworkflow/util/api.py @@ -0,0 +1,94 @@ +""" +Common functions for accessing the HTS Workflow REST API + +""" +# try to deal with python <2.6 +try: + import json +except ImportError: + import simplejson as json + +import urllib +import urllib2 +import urlparse + +def library_url(root_url, library_id): + """ + Return the url for retrieving information about a specific library. + + Args: + library_id (str): the library id of interest + root_url (str): the root portion of the url, e.g. http://localhost + + Returns: + str. The url to use for this REST api. + + >>> print library_url('http://localhost', '12345') + http://localhost/samples/library/12345/json + + """ + url_fragment = '/samples/library/%s/json' % (library_id,) + url = urlparse.urljoin(root_url, url_fragment) + + return url + + +def flowcell_url(root_url, flowcell_id): + """ + Return the url for retrieving information about a specific flowcell. + + Args: + root_url (str): the root portion of the url, e.g. http://localhost + flowcell_id (str): the flowcell id of interest + + Returns: + str. The url to use for this REST api. + + >>> print flowcell_url('http://localhost', '1234AAXX') + http://localhost/experiments/config/1234AAXX/json + """ + url_fragment = '/experiments/config/%s/json' % (flowcell_id,) + url = urlparse.urljoin(root_url, url_fragment) + + return url + + +def lanes_for_user_url(root_url, username): + """ + Return the url for returning all the lanes associated with a username + + Args: + username (str): a username in your target filesystem + root_url (str): the root portion of the url, e.g. http://localhost + + Returns: + str. The url to use for this REST api. + + >>> print lanes_for_user_url('http://localhost', 'diane') + http://localhost/lanes_for/diane/json + + """ + url_fragment = '/lanes_for/%s/json' % (username,) + url = urlparse.urljoin(root_url, url_fragment) + + return url + +def retrieve_info(url, apidata): + """ + Return a dictionary from the HTSworkflow API + """ + try: + apipayload = urllib.urlencode(apidata) + web = urllib2.urlopen(url, apipayload) + except urllib2.URLError, e: + if e.code == 404: + logging.info("%s was not found" % (url,)) + return None + else: + errmsg = 'URLError: %d %s' % (e.code, e.msg) + raise IOError(errmsg) + + contents = web.read() + headers = web.info() + + return json.loads(contents) diff --git a/scripts/make-library-tree b/scripts/make-library-tree index 822af48..b6edb93 100644 --- a/scripts/make-library-tree +++ b/scripts/make-library-tree @@ -1,11 +1,6 @@ #!/usr/bin/env python from ConfigParser import SafeConfigParser -# try to deal with python <2.6 -try: - import json -except ImportError: - import simplejson as json import logging import os @@ -13,9 +8,8 @@ from optparse import OptionParser import stat import re import shelve -import urllib -import urllib2 -import urlparse + +from htsworkflow.util import api eland_re = re.compile('s_(?P\d)(?P_\d)?_eland_') raw_seq_re = re.compile('woldlab_[0-9]{6}_[^_]+_[\d]+_[\dA-Z]+') @@ -127,25 +121,6 @@ def scan_for_sequences(dirs): return sequences -def retrieve_info(url, apidata): - """ - Return a dictionary from the HTSworkflow API - """ - try: - apipayload = urllib.urlencode(apidata) - web = urllib2.urlopen(url, apipayload) - except urllib2.URLError, e: - if e.code == 404: - logging.info("%s was not found" % (url,)) - return None - else: - errmsg = 'URLError: %d %s' % (e.code, e.msg) - raise IOError(errmsg) - - contents = web.read() - headers = web.info() - - return json.loads(contents) def build_flowcell_db(fcdb_filename, sequences, baseurl, apiid, apikey): """ @@ -161,8 +136,8 @@ def build_flowcell_db(fcdb_filename, sequences, baseurl, apiid, apikey): # get info about flowcell from server or shelf if not fcdb.has_key(flowcell): - url = urlparse.urljoin(baseurl, 'experiments/config/%s/json' % (flowcell,)) - flowcell_info = retrieve_info(url, apidata) + url = api.flowcell_url(baseurl, flowcell) + flowcell_info = api.retrieve_info(url, apidata) if flowcell_info is not None: fcdb[flowcell] = flowcell_info else: @@ -273,6 +248,7 @@ def configure_opts(opts): if opts.sequence_archive is None and \ config_file.has_option(SECTION_NAME, ARCHIVE_OPT): opts.sequence_archive = config_file.get(SECTION_NAME, ARCHIVE_OPT) + opts.sequence_archive = os.path.expanduser(opts.sequence_archive) opts.sequence_archive = os.path.abspath(opts.sequence_archive) opts.library_tree = os.path.join(opts.sequence_archive, 'libraries') -- 2.30.2