diff --git a/google/refine/facet.py b/google/refine/facet.py index 54850a3..67ea232 100644 --- a/google/refine/facet.py +++ b/google/refine/facet.py @@ -40,11 +40,11 @@ def __init__(self, column, facet_type, **options): self.type = facet_type self.name = column self.column_name = column - for k, v in options.items(): + for k, v in list(options.items()): setattr(self, k, v) def as_dict(self): - return dict([(to_camel(k), v) for k, v in self.__dict__.items() + return dict([(to_camel(k), v) for k, v in list(self.__dict__.items()) if v is not None]) @@ -159,8 +159,8 @@ class FacetResponse(object): """Class for unpacking an individual facet response.""" def __init__(self, facet): self.name = None - for k, v in facet.items(): - if isinstance(k, bool) or isinstance(k, basestring): + for k, v in list(facet.items()): + if isinstance(k, bool) or isinstance(k, str): setattr(self, from_camel(k), v) self.choices = {} @@ -268,7 +268,7 @@ def __init__(self, criteria=None): criteria = [criteria] for criterion in criteria: # A string criterion defaults to a string sort on that column - if isinstance(criterion, basestring): + if isinstance(criterion, str): criterion = { 'column': criterion, 'valueType': 'string', diff --git a/google/refine/refine.py b/google/refine/refine.py index c7c9b91..6e4f5ea 100644 --- a/google/refine/refine.py +++ b/google/refine/refine.py @@ -23,12 +23,12 @@ import gzip import os import re -import StringIO +from io import StringIO import time -import urllib -import urllib2_file -import urllib2 -import urlparse +import urllib.request, urllib.parse, urllib.error +# import urllib2_file +# import urllib.request, urllib.error, urllib.parse +# import urllib.parse from google.refine import facet from google.refine import history @@ -74,17 +74,18 @@ def urlopen(self, command, data=None, params=None, project_id=None): else: params['project'] = project_id if params: - url += '?' + urllib.urlencode(params) - req = urllib2.Request(url) + url += '?' + urllib.parse.urlencode(params) + req = urllib.request.Request(url, data = data, headers={'Accept-Encoding': 'gzip'}) if data: - req.add_data(data) # data = urllib.urlencode(data) - #req.add_header('Accept-Encoding', 'gzip') + req = urllib.request.Request(url, data = data, headers={'Accept-Encoding': 'gzip'}) + else: + req = urllib.request.Request(url, headers={'Accept-Encoding': 'gzip'}) try: - response = urllib2.urlopen(req) - except urllib2.HTTPError as e: + response = urllib.request.urlopen(req) + except urllib.error.HTTPError as e: raise Exception('HTTP %d "%s" for %s\n\t%s' % (e.code, e.msg, e.geturl(), data)) - except urllib2.URLError as e: - raise urllib2.URLError( + except urllib.error.URLError as e: + raise urllib.error.URLError( '%s for %s. No Refine server reachable/running; ENV set?' % (e.reason, self.server)) if response.info().get('Content-Encoding', None) == 'gzip': @@ -272,8 +273,8 @@ def s(opt): 'create-project-from-upload', options, params ) # expecting a redirect to the new project containing the id in the url - url_params = urlparse.parse_qs( - urlparse.urlparse(response.geturl()).query) + url_params = urllib.parse.parse_qs( + urllib.parse.urlparse(response.geturl()).query) if 'project' in url_params: project_id = url_params['project'][0] return RefineProject(self.server, project_id) @@ -429,7 +430,7 @@ def apply_operations(self, file_path, wait=True): def export(self, export_format='tsv'): """Return a fileobject of a project's data.""" - url = ('export-rows/' + urllib.quote(self.project_name()) + '.' + + url = ('export-rows/' + urllib.parse.quote(self.project_name()) + '.' + export_format) return self.do_raw(url, data={'format': export_format}) diff --git a/refine.py b/refine.py index 57f9e4c..b4140fe 100755 --- a/refine.py +++ b/refine.py @@ -53,14 +53,14 @@ def list_projects(): """Query the Refine server and list projects by ID: name.""" - projects = refine.Refine(refine.RefineServer()).list_projects().items() + projects = list(refine.Refine(refine.RefineServer()).list_projects().items()) def date_to_epoch(json_dt): """Convert a JSON date time into seconds-since-epoch.""" return time.mktime(time.strptime(json_dt, '%Y-%m-%dT%H:%M:%SZ')) projects.sort(key=lambda v: date_to_epoch(v[1]['modified']), reverse=True) for project_id, project_info in projects: - print('{0:>14}: {1}'.format(project_id, project_info['name'])) + print(('{0:>14}: {1}'.format(project_id, project_info['name']))) def export_project(project, options): @@ -96,8 +96,8 @@ def main(): if options.apply: response = project.apply_operations(options.apply) if response != 'ok': - print >>sys.stderr, 'Failed to apply %s: %s' % (options.apply, - response) + print('Failed to apply %s: %s' % (options.apply, + response), file=sys.stderr) if options.export: export_project(project, options) diff --git a/setup.py b/setup.py index 2387be7..8470cf9 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ def read(filename): return open(os.path.join(os.path.dirname(__file__), filename)).read() setup(name='refine-client', - version='0.2.1', + version='0.2.2', description=('The OpenRefine Python Client Library provides an ' 'interface to communicating with an OpenRefine server.'), long_description=read('README.rst'),