make exception dotted name python3 compliant
[htsworkflow.git] / htsworkflow / pipelines / retrieve_config.py
index 7951752318aebe7d2443c010e64ac24149023167..de9b051c9f6c9026e19957ec3093c37d5784b4f3 100644 (file)
@@ -1,23 +1,20 @@
 #!/usr/bin/env python
 
 import csv
-from ConfigParser import RawConfigParser
+from six.moves.configparser import RawConfigParser
 import logging
 from optparse import OptionParser, IndentedHelpFormatter
 import os
 import sys
 import types
-import urllib
-import urllib2
+import six
+from six.moves import urllib
 
-try:
-    import json
-except ImportError, e:
-    import simplejson as json
+import json
 
 from htsworkflow.auth import apidata
 from htsworkflow.util import api
-from htsworkflow.util import alphanum
+from htsworkflow.util.alphanum import natural_sort_key
 from htsworkflow.util.url import normalize_url
 from htsworkflow.pipelines.genome_mapper import \
      getAvailableGenomes, \
@@ -47,9 +44,9 @@ def retrieve_flowcell_info(base_host_url, flowcell):
     url = api.flowcell_url(base_host_url, flowcell)
 
     try:
-        apipayload = urllib.urlencode(apidata)
-        web = urllib2.urlopen(url, apipayload)
-    except urllib2.URLError, e:
+        apipayload = urllib.parse.urlencode(apidata)
+        web = urllib.request.urlopen(url, apipayload)
+    except urllib.error.HTTPError as e:
         errmsg = 'URLError: %d %s' % (e.code, e.msg)
         LOGGER.error(errmsg)
         LOGGER.error('opened %s' % (url,))
@@ -403,15 +400,13 @@ def format_pooled_libraries(shared, library):
     sequences = library.get('index_sequence', None)
     if sequences is None:
         return []
-    elif (type(sequences) in types.StringTypes and
-          sequences.lower().startswith('err')):
+    elif isinstance(sequences, six.string_types):
         shared['Index'] = ''
         shared['SampleProject'] = library['library_id']
         return [shared]
-    elif (type(sequences) == types.DictType):
+    elif isinstance(sequences, dict):
         pooled = []
-        multiplex_ids = sequences.keys()
-        multiplex_ids.sort(cmp=alphanum.alphanum)
+        multiplex_ids = sorted(sequences.keys(), key=natural_sort_key)
         for multiplex_id in multiplex_ids:
             sample = {}
             sample.update(shared)