Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3.X changes using 2to3 lib #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions google/refine/facet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])


Expand Down Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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',
Expand Down
33 changes: 17 additions & 16 deletions google/refine/refine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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})

Expand Down
8 changes: 4 additions & 4 deletions refine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down