2011-04-23 06:00:10 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
Client library to communicate with a Refine server.
|
|
|
|
"""
|
|
|
|
|
2011-04-26 08:02:44 +02:00
|
|
|
# Copyright (c) 2011 Paul Makepeace, Real Programmers. All rights reserved.
|
|
|
|
|
2011-05-01 19:24:12 +02:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
2011-06-08 19:05:55 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
import csv
|
|
|
|
import json
|
|
|
|
import gzip
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import StringIO
|
2011-06-08 19:17:16 +02:00
|
|
|
import time
|
2011-04-23 06:00:10 +02:00
|
|
|
import urllib
|
|
|
|
import urllib2_file
|
|
|
|
import urllib2
|
|
|
|
import urlparse
|
|
|
|
|
2011-04-26 08:02:44 +02:00
|
|
|
from google.refine import facet
|
2011-04-27 00:20:21 +02:00
|
|
|
from google.refine import history
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2013-10-10 13:41:10 +02:00
|
|
|
REFINE_HOST = os.environ.get('OPENREFINE_HOST', os.environ.get('GOOGLE_REFINE_HOST', '127.0.0.1'))
|
|
|
|
REFINE_PORT = os.environ.get('OPENREFINE_PORT', os.environ.get('GOOGLE_REFINE_PORT', '3333'))
|
2011-04-25 08:49:19 +02:00
|
|
|
|
2011-04-26 20:20:47 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
class RefineServer(object):
|
|
|
|
"""Communicate with a Refine server."""
|
|
|
|
|
2011-06-10 14:53:46 +02:00
|
|
|
@staticmethod
|
|
|
|
def url():
|
|
|
|
"""Return the URL to the Refine server."""
|
2011-06-23 11:49:59 +02:00
|
|
|
server = 'http://' + REFINE_HOST
|
2011-06-10 14:53:46 +02:00
|
|
|
if REFINE_PORT != '80':
|
|
|
|
server += ':' + REFINE_PORT
|
|
|
|
return server
|
|
|
|
|
2011-05-27 02:15:38 +02:00
|
|
|
def __init__(self, server=None):
|
|
|
|
if server is None:
|
2013-10-09 20:04:24 +02:00
|
|
|
server = self.url()
|
2011-04-23 06:00:10 +02:00
|
|
|
self.server = server[:-1] if server.endswith('/') else server
|
2011-06-10 19:49:06 +02:00
|
|
|
self.__version = None # see version @property below
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2011-06-10 16:38:16 +02:00
|
|
|
def urlopen(self, command, data=None, params=None, project_id=None):
|
|
|
|
"""Open a Refine URL and with optional query params and POST data.
|
|
|
|
|
|
|
|
data: POST data dict
|
|
|
|
param: query params dict
|
|
|
|
project_id: project ID as string
|
|
|
|
|
|
|
|
Returns urllib2.urlopen iterable."""
|
2011-04-23 06:00:10 +02:00
|
|
|
url = self.server + '/command/core/' + command
|
|
|
|
if data is None:
|
|
|
|
data = {}
|
2011-06-10 16:38:16 +02:00
|
|
|
if params is None:
|
|
|
|
params = {}
|
2011-04-23 06:00:10 +02:00
|
|
|
if project_id:
|
2011-04-25 00:36:19 +02:00
|
|
|
# XXX haven't figured out pattern on qs v body
|
2011-06-10 16:38:16 +02:00
|
|
|
if 'delete' in command or data:
|
2011-04-23 06:00:10 +02:00
|
|
|
data['project'] = project_id
|
|
|
|
else:
|
2011-06-10 16:38:16 +02:00
|
|
|
params['project'] = project_id
|
|
|
|
if params:
|
|
|
|
url += '?' + urllib.urlencode(params)
|
2011-04-23 06:00:10 +02:00
|
|
|
req = urllib2.Request(url)
|
|
|
|
if data:
|
2013-10-09 20:04:24 +02:00
|
|
|
req.add_data(data) # data = urllib.urlencode(data)
|
2011-04-23 06:00:10 +02:00
|
|
|
#req.add_header('Accept-Encoding', 'gzip')
|
2011-04-28 16:43:15 +02:00
|
|
|
try:
|
|
|
|
response = urllib2.urlopen(req)
|
2013-10-12 20:34:10 +02:00
|
|
|
except urllib2.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:
|
2011-04-28 19:13:19 +02:00
|
|
|
raise urllib2.URLError(
|
|
|
|
'%s for %s. No Refine server reachable/running; ENV set?' %
|
2013-10-12 20:34:10 +02:00
|
|
|
(e.reason, self.server))
|
2011-04-23 06:00:10 +02:00
|
|
|
if response.info().get('Content-Encoding', None) == 'gzip':
|
|
|
|
# Need a seekable filestream for gzip
|
|
|
|
gzip_fp = gzip.GzipFile(fileobj=StringIO.StringIO(response.read()))
|
|
|
|
# XXX Monkey patch response's filehandle. Better way?
|
|
|
|
urllib.addbase.__init__(response, gzip_fp)
|
|
|
|
return response
|
2011-04-24 18:43:11 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
def urlopen_json(self, *args, **kwargs):
|
|
|
|
"""Open a Refine URL, optionally POST data, and return parsed JSON."""
|
2011-04-25 02:19:45 +02:00
|
|
|
response = json.loads(self.urlopen(*args, **kwargs).read())
|
2011-06-09 14:03:15 +02:00
|
|
|
if 'code' in response and response['code'] not in ('ok', 'pending'):
|
2013-10-12 19:38:16 +02:00
|
|
|
error_message = ('server ' + response['code'] + ': ' +
|
|
|
|
response.get('message', response.get('stack', response)))
|
|
|
|
raise Exception(error_message)
|
2011-04-25 02:19:45 +02:00
|
|
|
return response
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2011-06-10 19:49:06 +02:00
|
|
|
def get_version(self):
|
|
|
|
"""Return version data.
|
|
|
|
|
|
|
|
{"revision":"r1836","full_version":"2.0 [r1836]",
|
|
|
|
"full_name":"Google Refine 2.0 [r1836]","version":"2.0"}"""
|
|
|
|
return self.urlopen_json('get-version')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def version(self):
|
|
|
|
if self.__version is None:
|
|
|
|
self.__version = self.get_version()['version']
|
|
|
|
return self.__version
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
class Refine:
|
|
|
|
"""Class representing a connection to a Refine server."""
|
2011-06-23 11:50:18 +02:00
|
|
|
def __init__(self, server):
|
2011-04-23 06:00:10 +02:00
|
|
|
if isinstance(server, RefineServer):
|
|
|
|
self.server = server
|
|
|
|
else:
|
|
|
|
self.server = RefineServer(server)
|
|
|
|
|
|
|
|
def list_projects(self):
|
2011-04-26 08:27:22 +02:00
|
|
|
"""Return a dict of projects indexed by id.
|
2011-04-24 18:43:11 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
{u'1877818633188': {
|
|
|
|
'id': u'1877818633188', u'name': u'akg',
|
|
|
|
u'modified': u'2011-04-07T12:30:07Z',
|
|
|
|
u'created': u'2011-04-07T12:30:07Z'
|
|
|
|
},
|
2011-04-26 08:27:22 +02:00
|
|
|
"""
|
|
|
|
# It's tempting to add in an index by name but there can be
|
|
|
|
# projects with the same name.
|
|
|
|
return self.server.urlopen_json('get-all-project-metadata')['projects']
|
|
|
|
|
|
|
|
def get_project_name(self, project_id):
|
|
|
|
"""Returns project name given project_id."""
|
2011-04-23 06:00:10 +02:00
|
|
|
projects = self.list_projects()
|
2011-04-26 08:27:22 +02:00
|
|
|
return projects[project_id]['name']
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2011-04-26 08:27:22 +02:00
|
|
|
def open_project(self, project_id):
|
|
|
|
"""Open a Refine project."""
|
|
|
|
return RefineProject(self.server, project_id)
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2013-10-13 20:28:50 +02:00
|
|
|
# These aren't used yet but are included for reference
|
2013-10-12 19:38:16 +02:00
|
|
|
new_project_defaults = {
|
|
|
|
'text/line-based/*sv': {
|
|
|
|
'encoding': '',
|
2013-10-13 20:28:50 +02:00
|
|
|
'separator': ',',
|
2013-10-12 19:38:16 +02:00
|
|
|
'ignore_lines': -1,
|
|
|
|
'header_lines': 1,
|
|
|
|
'skip_data_lines': 0,
|
|
|
|
'limit': -1,
|
|
|
|
'store_blank_rows': True,
|
|
|
|
'guess_cell_value_types': True,
|
|
|
|
'process_quotes': True,
|
|
|
|
'store_blank_cells_as_nulls': True,
|
|
|
|
'include_file_sources': False},
|
|
|
|
'text/line-based': {
|
|
|
|
'encoding': '',
|
|
|
|
'lines_per_row': 1,
|
|
|
|
'ignore_lines': -1,
|
|
|
|
'limit': -1,
|
|
|
|
'skip_data_lines': -1,
|
|
|
|
'store_blank_rows': True,
|
|
|
|
'store_blank_cells_as_nulls': True,
|
|
|
|
'include_file_sources': False},
|
|
|
|
'text/line-based/fixed-width': {
|
|
|
|
'encoding': '',
|
|
|
|
'column_widths': [20],
|
|
|
|
'ignore_lines': -1,
|
|
|
|
'header_lines': 0,
|
|
|
|
'skip_data_lines': 0,
|
|
|
|
'limit': -1,
|
|
|
|
'guess_cell_value_types': False,
|
|
|
|
'store_blank_rows': True,
|
|
|
|
'store_blank_cells_as_nulls': True,
|
|
|
|
'include_file_sources': False},
|
|
|
|
'text/line-based/pc-axis': {
|
|
|
|
'encoding': '',
|
|
|
|
'limit': -1,
|
|
|
|
'skip_data_lines': -1,
|
|
|
|
'include_file_sources': False},
|
|
|
|
'text/rdf+n3': {'encoding': ''},
|
|
|
|
'text/xml/ods': {
|
|
|
|
'sheets': [],
|
|
|
|
'ignore_lines': -1,
|
|
|
|
'header_lines': 1,
|
|
|
|
'skip_data_lines': 0,
|
|
|
|
'limit': -1,
|
|
|
|
'store_blank_rows': True,
|
|
|
|
'store_blank_cells_as_nulls': True,
|
|
|
|
'include_file_sources': False},
|
|
|
|
'binary/xls': {
|
|
|
|
'xml_based': False,
|
|
|
|
'sheets': [],
|
|
|
|
'ignore_lines': -1,
|
|
|
|
'header_lines': 1,
|
|
|
|
'skip_data_lines': 0,
|
|
|
|
'limit': -1,
|
|
|
|
'store_blank_rows': True,
|
|
|
|
'store_blank_cells_as_nulls': True,
|
|
|
|
'include_file_sources': False}
|
|
|
|
}
|
|
|
|
|
|
|
|
def new_project(self, project_file=None, project_url=None, project_name=None, project_format='text/line-based/*sv',
|
|
|
|
encoding='',
|
|
|
|
separator=',',
|
|
|
|
ignore_lines=-1,
|
|
|
|
header_lines=1,
|
|
|
|
skip_data_lines=0,
|
|
|
|
limit=-1,
|
|
|
|
store_blank_rows=True,
|
|
|
|
guess_cell_value_types=True,
|
|
|
|
process_quotes=True,
|
|
|
|
store_blank_cells_as_nulls=True,
|
|
|
|
include_file_sources=False,
|
|
|
|
**opts):
|
2013-10-09 20:04:24 +02:00
|
|
|
|
|
|
|
if (project_file and project_url) or (not project_file and not project_url):
|
2011-04-23 06:00:10 +02:00
|
|
|
raise ValueError('One (only) of project_file and project_url must be set')
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
def s(opt):
|
|
|
|
if isinstance(opt, bool):
|
2013-10-13 20:28:50 +02:00
|
|
|
return 'true' if opt else 'false'
|
2011-04-23 06:00:10 +02:00
|
|
|
if opt is None:
|
|
|
|
return ''
|
|
|
|
return str(opt)
|
|
|
|
options = {
|
2013-10-12 19:38:16 +02:00
|
|
|
'format': project_format,
|
|
|
|
'encoding': s(encoding),
|
2011-04-25 02:19:45 +02:00
|
|
|
'separator': s(separator),
|
2013-10-13 20:28:50 +02:00
|
|
|
'ignore-lines': s(ignore_lines),
|
2011-04-25 02:19:45 +02:00
|
|
|
'header-lines': s(header_lines),
|
2013-10-13 20:28:50 +02:00
|
|
|
'skip-data-lines': s(skip_data_lines),
|
|
|
|
'limit': s(limit),
|
|
|
|
'guess-value-type': s(guess_cell_value_types),
|
2013-10-12 19:38:16 +02:00
|
|
|
'process-quotes': s(process_quotes),
|
|
|
|
'store-blank-rows': s(store_blank_rows),
|
|
|
|
'store-blank-cells-as-nulls': s(store_blank_cells_as_nulls),
|
|
|
|
'include-file-sources': s(include_file_sources),
|
2011-04-23 06:00:10 +02:00
|
|
|
}
|
2013-10-13 20:28:50 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
if project_url is not None:
|
|
|
|
options['url'] = project_url
|
|
|
|
elif project_file is not None:
|
|
|
|
options['project-file'] = {
|
|
|
|
'fd': open(project_file),
|
|
|
|
'filename': project_file,
|
|
|
|
}
|
|
|
|
if project_name is None:
|
2011-04-26 08:27:22 +02:00
|
|
|
# make a name for itself by stripping extension and directories
|
2011-04-23 06:00:10 +02:00
|
|
|
project_name = (project_file or 'New project').rsplit('.', 1)[0]
|
|
|
|
project_name = os.path.basename(project_name)
|
|
|
|
options['project-name'] = project_name
|
|
|
|
response = self.server.urlopen('create-project-from-upload', options)
|
|
|
|
# expecting a redirect to the new project containing the id in the url
|
2011-04-26 08:27:22 +02:00
|
|
|
url_params = urlparse.parse_qs(
|
|
|
|
urlparse.urlparse(response.geturl()).query)
|
2011-04-23 06:00:10 +02:00
|
|
|
if 'project' in url_params:
|
|
|
|
project_id = url_params['project'][0]
|
2011-04-26 08:27:22 +02:00
|
|
|
return RefineProject(self.server, project_id)
|
2011-04-23 06:00:10 +02:00
|
|
|
else:
|
|
|
|
raise Exception('Project not created')
|
|
|
|
|
|
|
|
|
2011-04-25 17:51:01 +02:00
|
|
|
def RowsResponseFactory(column_index):
|
|
|
|
"""Factory for the parsing the output from get_rows().
|
2011-04-26 03:22:12 +02:00
|
|
|
|
2011-04-25 17:51:01 +02:00
|
|
|
Uses the project's model's row cell index so that a row can be used
|
|
|
|
as a dict by column name."""
|
2011-04-26 03:22:12 +02:00
|
|
|
|
2011-04-25 17:51:01 +02:00
|
|
|
class RowsResponse(object):
|
|
|
|
class RefineRows(object):
|
|
|
|
class RefineRow(object):
|
|
|
|
def __init__(self, row_response):
|
|
|
|
self.flagged = row_response['flagged']
|
|
|
|
self.starred = row_response['starred']
|
|
|
|
self.index = row_response['i']
|
|
|
|
self.row = [c['v'] if c else None
|
|
|
|
for c in row_response['cells']]
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-04-25 17:51:01 +02:00
|
|
|
def __getitem__(self, column):
|
2011-04-26 06:30:20 +02:00
|
|
|
# Trailing nulls seem to be stripped from row data
|
|
|
|
try:
|
|
|
|
return self.row[column_index[column]]
|
|
|
|
except IndexError:
|
|
|
|
return None
|
2011-04-25 17:51:01 +02:00
|
|
|
|
|
|
|
def __init__(self, rows_response):
|
|
|
|
self.rows_response = rows_response
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-04-25 17:51:01 +02:00
|
|
|
def __iter__(self):
|
|
|
|
for row_response in self.rows_response:
|
|
|
|
yield self.RefineRow(row_response)
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-04-26 20:54:44 +02:00
|
|
|
def __getitem__(self, index):
|
|
|
|
return self.RefineRow(self.rows_response[index])
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-04-25 17:51:01 +02:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.rows_response)
|
|
|
|
|
|
|
|
def __init__(self, response):
|
|
|
|
self.mode = response['mode']
|
|
|
|
self.filtered = response['filtered']
|
|
|
|
self.start = response['start']
|
|
|
|
self.limit = response['limit']
|
|
|
|
self.total = response['total']
|
|
|
|
self.rows = self.RefineRows(response['rows'])
|
|
|
|
|
|
|
|
return RowsResponse
|
2011-04-24 18:43:11 +02:00
|
|
|
|
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
class RefineProject:
|
2013-10-10 13:41:10 +02:00
|
|
|
"""An OpenRefine project."""
|
2011-04-25 08:49:19 +02:00
|
|
|
|
2011-04-26 21:44:32 +02:00
|
|
|
def __init__(self, server, project_id=None):
|
2011-04-23 06:00:10 +02:00
|
|
|
if not isinstance(server, RefineServer):
|
2011-04-26 21:44:32 +02:00
|
|
|
if '/project?project=' in server:
|
|
|
|
server, project_id = server.split('/project?project=')
|
2011-05-27 00:24:56 +02:00
|
|
|
server = RefineServer(server)
|
|
|
|
elif re.match(r'\d+$', server): # just digits => project ID
|
|
|
|
server, project_id = RefineServer(), server
|
|
|
|
else:
|
|
|
|
server = RefineServer(server)
|
2011-04-23 06:00:10 +02:00
|
|
|
self.server = server
|
2011-04-26 08:27:22 +02:00
|
|
|
if not project_id:
|
|
|
|
raise Exception('Missing Refine project ID')
|
2011-04-23 06:00:10 +02:00
|
|
|
self.project_id = project_id
|
2011-04-26 08:02:44 +02:00
|
|
|
self.engine = facet.Engine()
|
|
|
|
self.sorting = facet.Sorting()
|
2011-04-27 00:20:21 +02:00
|
|
|
self.history_entry = None
|
2011-04-25 17:59:18 +02:00
|
|
|
# following filled in by get_models()
|
2011-06-23 11:51:12 +02:00
|
|
|
self.key_column = None
|
2011-04-25 21:45:20 +02:00
|
|
|
self.has_records = False
|
2011-04-27 00:59:32 +02:00
|
|
|
self.columns = None
|
2011-04-27 06:04:09 +02:00
|
|
|
self.column_order = {} # map of column names to order in UI
|
2011-04-25 17:59:18 +02:00
|
|
|
self.rows_response_factory = None # for parsing get_rows()
|
|
|
|
self.get_models()
|
2011-06-09 14:07:18 +02:00
|
|
|
# following filled in by get_reconciliation_services
|
|
|
|
self.recon_services = None
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2011-04-26 08:27:22 +02:00
|
|
|
def project_name(self):
|
2011-04-28 20:00:04 +02:00
|
|
|
return Refine(self.server).get_project_name(self.project_id)
|
|
|
|
|
|
|
|
def project_url(self):
|
|
|
|
"""Return a URL to the project."""
|
|
|
|
return '%s/project?project=%s' % (self.server.server, self.project_id)
|
2011-04-26 08:27:22 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
def do_raw(self, command, data):
|
|
|
|
"""Issue a command to the server & return a response object."""
|
2011-06-10 16:38:16 +02:00
|
|
|
return self.server.urlopen(command, project_id=self.project_id,
|
|
|
|
data=data)
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2011-04-25 07:28:30 +02:00
|
|
|
def do_json(self, command, data=None, include_engine=True):
|
2011-04-23 06:00:10 +02:00
|
|
|
"""Issue a command to the server, parse & return decoded JSON."""
|
2011-04-25 07:28:30 +02:00
|
|
|
if include_engine:
|
|
|
|
if data is None:
|
|
|
|
data = {}
|
|
|
|
data['engine'] = self.engine.as_json()
|
2011-04-27 00:20:21 +02:00
|
|
|
response = self.server.urlopen_json(command,
|
|
|
|
project_id=self.project_id,
|
|
|
|
data=data)
|
|
|
|
if 'historyEntry' in response:
|
|
|
|
# **response['historyEntry'] won't work as keys are unicode :-/
|
|
|
|
he = response['historyEntry']
|
|
|
|
self.history_entry = history.HistoryEntry(he['id'], he['time'],
|
|
|
|
he['description'])
|
|
|
|
return response
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2011-04-23 18:49:18 +02:00
|
|
|
def get_models(self):
|
2011-04-25 16:32:11 +02:00
|
|
|
"""Fill out column metadata.
|
2011-04-26 03:22:12 +02:00
|
|
|
|
2011-06-10 16:38:44 +02:00
|
|
|
Column structure is a list of columns in their order.
|
|
|
|
The cellIndex is an index for that column's data into the list returned
|
|
|
|
from get_rows()."""
|
2011-04-25 07:28:30 +02:00
|
|
|
response = self.do_json('get-models', include_engine=False)
|
2011-04-23 18:49:18 +02:00
|
|
|
column_model = response['columnModel']
|
2011-06-10 16:38:44 +02:00
|
|
|
column_index = {} # map of column name to index into get_rows() data
|
2011-04-27 00:59:32 +02:00
|
|
|
self.columns = [column['name'] for column in column_model['columns']]
|
2011-04-25 17:59:18 +02:00
|
|
|
for i, column in enumerate(column_model['columns']):
|
|
|
|
name = column['name']
|
2011-04-25 16:32:11 +02:00
|
|
|
self.column_order[name] = i
|
2011-04-25 17:59:18 +02:00
|
|
|
column_index[name] = column['cellIndex']
|
2011-04-23 18:49:18 +02:00
|
|
|
self.key_column = column_model['keyColumnName']
|
2011-04-25 21:45:20 +02:00
|
|
|
self.has_records = response['recordModel'].get('hasRecords', False)
|
2011-04-25 17:51:01 +02:00
|
|
|
self.rows_response_factory = RowsResponseFactory(column_index)
|
2011-04-23 18:49:18 +02:00
|
|
|
# TODO: implement rest
|
2011-04-25 17:59:18 +02:00
|
|
|
return response
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2011-06-10 16:39:20 +02:00
|
|
|
def get_preference(self, name):
|
|
|
|
"""Returns the (JSON) value of a given preference setting."""
|
|
|
|
response = self.server.urlopen_json('get-preference',
|
|
|
|
params={'name': name})
|
|
|
|
return json.loads(response['value'])
|
2011-06-09 14:07:18 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
def wait_until_idle(self, polling_delay=0.5):
|
|
|
|
while True:
|
2011-06-10 16:39:54 +02:00
|
|
|
response = self.do_json('get-processes', include_engine=False)
|
2011-04-25 16:32:11 +02:00
|
|
|
if 'processes' in response and len(response['processes']) > 0:
|
2011-04-23 06:00:10 +02:00
|
|
|
time.sleep(polling_delay)
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
def apply_operations(self, file_path, wait=True):
|
2013-10-09 20:04:24 +02:00
|
|
|
json_data = open(file_path).read()
|
|
|
|
response_json = self.do_json('apply-operations', {'operations': json_data})
|
2011-04-25 08:49:19 +02:00
|
|
|
if response_json['code'] == 'pending' and wait:
|
|
|
|
self.wait_until_idle()
|
|
|
|
return 'ok'
|
2013-10-09 20:04:24 +02:00
|
|
|
return response_json['code'] # can be 'ok' or 'pending'
|
2011-04-24 18:43:11 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
def export(self, export_format='tsv'):
|
|
|
|
"""Return a fileobject of a project's data."""
|
2011-04-26 08:27:22 +02:00
|
|
|
url = ('export-rows/' + urllib.quote(self.project_name()) + '.' +
|
2011-04-25 07:28:30 +02:00
|
|
|
export_format)
|
2011-06-10 16:38:16 +02:00
|
|
|
return self.do_raw(url, data={'format': export_format})
|
2011-04-23 06:00:10 +02:00
|
|
|
|
|
|
|
def export_rows(self, **kwargs):
|
|
|
|
"""Return an iterable of parsed rows of a project's data."""
|
|
|
|
return csv.reader(self.export(**kwargs), dialect='excel-tab')
|
|
|
|
|
|
|
|
def delete(self):
|
2011-04-25 07:28:30 +02:00
|
|
|
response_json = self.do_json('delete-project', include_engine=False)
|
2011-04-23 06:00:10 +02:00
|
|
|
return 'code' in response_json and response_json['code'] == 'ok'
|
2011-04-24 18:43:11 +02:00
|
|
|
|
2011-04-25 00:36:19 +02:00
|
|
|
def compute_facets(self, facets=None):
|
2011-04-27 06:04:09 +02:00
|
|
|
"""Compute facets as per the project's engine.
|
|
|
|
|
|
|
|
The response object has two attributes, mode & facets. mode is one of
|
|
|
|
'row-based' or 'record-based'. facets is a magic list of facets in the
|
|
|
|
same order as they were specified in the Engine. Magic allows the
|
|
|
|
original Engine's facet as index into the response, e.g.,
|
|
|
|
|
|
|
|
name_facet = TextFacet('name')
|
|
|
|
response = project.compute_facets(name_facet)
|
|
|
|
response.facets[name_facet] # same as response.facets[0]
|
|
|
|
"""
|
2011-04-24 18:43:11 +02:00
|
|
|
if facets:
|
2011-04-26 22:55:07 +02:00
|
|
|
self.engine.set_facets(facets)
|
2011-04-25 07:28:30 +02:00
|
|
|
response = self.do_json('compute-facets')
|
2011-04-27 05:52:13 +02:00
|
|
|
return self.engine.facets_response(response)
|
2011-04-24 18:43:11 +02:00
|
|
|
|
2011-04-25 08:49:19 +02:00
|
|
|
def get_rows(self, facets=None, sort_by=None, start=0, limit=10):
|
2011-04-25 07:08:53 +02:00
|
|
|
if facets:
|
2011-04-26 22:55:07 +02:00
|
|
|
self.engine.set_facets(facets)
|
2011-04-25 08:49:19 +02:00
|
|
|
if sort_by is not None:
|
2011-04-26 08:02:44 +02:00
|
|
|
self.sorting = facet.Sorting(sort_by)
|
2011-04-25 08:49:19 +02:00
|
|
|
response = self.do_json('get-rows', {'sorting': self.sorting.as_json(),
|
|
|
|
'start': start, 'limit': limit})
|
2011-04-25 17:51:01 +02:00
|
|
|
return self.rows_response_factory(response)
|
2011-04-24 18:43:11 +02:00
|
|
|
|
2011-04-25 08:49:19 +02:00
|
|
|
def reorder_rows(self, sort_by=None):
|
|
|
|
if sort_by is not None:
|
2011-04-26 08:02:44 +02:00
|
|
|
self.sorting = facet.Sorting(sort_by)
|
2011-04-25 08:49:19 +02:00
|
|
|
response = self.do_json('reorder-rows',
|
|
|
|
{'sorting': self.sorting.as_json()})
|
2011-04-25 21:45:20 +02:00
|
|
|
# clear sorting
|
2011-04-26 08:02:44 +02:00
|
|
|
self.sorting = facet.Sorting()
|
2011-04-25 08:49:19 +02:00
|
|
|
return response
|
|
|
|
|
2011-04-25 07:08:53 +02:00
|
|
|
def remove_rows(self, facets=None):
|
|
|
|
if facets:
|
2011-04-26 22:55:07 +02:00
|
|
|
self.engine.set_facets(facets)
|
2011-04-25 07:28:30 +02:00
|
|
|
return self.do_json('remove-rows')
|
2011-04-25 07:08:53 +02:00
|
|
|
|
2011-04-25 02:19:45 +02:00
|
|
|
def text_transform(self, column, expression, on_error='set-to-blank',
|
|
|
|
repeat=False, repeat_count=10):
|
|
|
|
response = self.do_json('text-transform', {
|
2011-04-25 07:28:30 +02:00
|
|
|
'columnName': column, 'expression': expression,
|
|
|
|
'onError': on_error, 'repeat': repeat,
|
2011-04-25 02:19:45 +02:00
|
|
|
'repeatCount': repeat_count})
|
2011-04-25 02:45:53 +02:00
|
|
|
return response
|
|
|
|
|
|
|
|
def edit(self, column, edit_from, edit_to):
|
|
|
|
edits = [{'from': [edit_from], 'to': edit_to}]
|
|
|
|
return self.mass_edit(column, edits)
|
|
|
|
|
|
|
|
def mass_edit(self, column, edits, expression='value'):
|
|
|
|
"""edits is [{'from': ['foo'], 'to': 'bar'}, {...}]"""
|
|
|
|
edits = json.dumps(edits)
|
|
|
|
response = self.do_json('mass-edit', {
|
2011-04-25 07:28:30 +02:00
|
|
|
'columnName': column, 'expression': expression, 'edits': edits})
|
2011-04-25 02:45:53 +02:00
|
|
|
return response
|
2011-04-25 05:43:45 +02:00
|
|
|
|
|
|
|
clusterer_defaults = {
|
|
|
|
'binning': {
|
|
|
|
'type': 'binning',
|
|
|
|
'function': 'fingerprint',
|
|
|
|
'params': {},
|
|
|
|
},
|
|
|
|
'knn': {
|
|
|
|
'type': 'knn',
|
|
|
|
'function': 'levenshtein',
|
|
|
|
'params': {
|
|
|
|
'radius': 1,
|
|
|
|
'blocking-ngram-size': 6,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-04-25 05:43:45 +02:00
|
|
|
def compute_clusters(self, column, clusterer_type='binning',
|
|
|
|
function=None, params=None):
|
|
|
|
"""Returns a list of clusters of {'value': ..., 'count': ...}."""
|
|
|
|
clusterer = self.clusterer_defaults[clusterer_type]
|
|
|
|
if params is not None:
|
|
|
|
clusterer['params'] = params
|
|
|
|
if function is not None:
|
|
|
|
clusterer['function'] = function
|
|
|
|
clusterer['column'] = column
|
|
|
|
response = self.do_json('compute-clusters', {
|
|
|
|
'clusterer': json.dumps(clusterer)})
|
|
|
|
return [[{'value': x['v'], 'count': x['c']} for x in cluster]
|
|
|
|
for cluster in response]
|
|
|
|
|
2011-04-25 07:08:53 +02:00
|
|
|
def annotate_one_row(self, row, annotation, state=True):
|
|
|
|
if annotation not in ('starred', 'flagged'):
|
|
|
|
raise ValueError('annotation must be one of starred or flagged')
|
2013-10-09 20:04:24 +02:00
|
|
|
state = 'true' if state is True else 'false'
|
2011-04-25 07:08:53 +02:00
|
|
|
return self.do_json('annotate-one-row', {'row': row.index,
|
|
|
|
annotation: state})
|
|
|
|
|
|
|
|
def flag_row(self, row, flagged=True):
|
|
|
|
return self.annotate_one_row(row, 'flagged', flagged)
|
|
|
|
|
|
|
|
def star_row(self, row, starred=True):
|
|
|
|
return self.annotate_one_row(row, 'starred', starred)
|
|
|
|
|
2011-04-25 16:27:28 +02:00
|
|
|
def add_column(self, column, new_column, expression='value',
|
|
|
|
column_insert_index=None, on_error='set-to-blank'):
|
|
|
|
if column_insert_index is None:
|
2011-04-25 17:51:01 +02:00
|
|
|
column_insert_index = self.column_order[column] + 1
|
2013-10-09 20:04:24 +02:00
|
|
|
response = self.do_json('add-column', {
|
|
|
|
'baseColumnName': column, 'newColumnName': new_column,
|
|
|
|
'expression': expression, 'columnInsertIndex': column_insert_index,
|
|
|
|
'onError': on_error})
|
2011-04-25 16:32:11 +02:00
|
|
|
self.get_models()
|
2011-04-25 16:27:28 +02:00
|
|
|
return response
|
2011-04-25 21:45:20 +02:00
|
|
|
|
2011-04-26 06:30:20 +02:00
|
|
|
def split_column(self, column, separator=',', mode='separator',
|
|
|
|
regex=False, guess_cell_type=True,
|
|
|
|
remove_original_column=True):
|
2013-10-09 20:04:24 +02:00
|
|
|
response = self.do_json('split-column', {
|
|
|
|
'columnName': column, 'separator': separator, 'mode': mode,
|
|
|
|
'regex': regex, 'guessCellType': guess_cell_type,
|
2011-04-26 06:30:20 +02:00
|
|
|
'removeOriginalColumn': remove_original_column})
|
|
|
|
self.get_models()
|
|
|
|
return response
|
|
|
|
|
2011-04-26 03:22:12 +02:00
|
|
|
def rename_column(self, column, new_column):
|
|
|
|
response = self.do_json('rename-column', {'oldColumnName': column,
|
2011-04-26 05:15:35 +02:00
|
|
|
'newColumnName': new_column})
|
|
|
|
self.get_models()
|
|
|
|
return response
|
|
|
|
|
|
|
|
def reorder_columns(self, new_column_order):
|
|
|
|
"""Takes an array of column names in the new order."""
|
|
|
|
response = self.do_json('reorder-columns', {
|
|
|
|
'columnNames': new_column_order})
|
2011-04-26 03:22:12 +02:00
|
|
|
self.get_models()
|
|
|
|
return response
|
|
|
|
|
2011-04-26 06:30:20 +02:00
|
|
|
def move_column(self, column, index):
|
|
|
|
"""Move column to a new position."""
|
2011-04-27 00:59:32 +02:00
|
|
|
if index == 'end':
|
|
|
|
index = len(self.columns) - 1
|
2011-04-26 06:30:20 +02:00
|
|
|
response = self.do_json('move-column', {'columnName': column,
|
|
|
|
'index': index})
|
|
|
|
self.get_models()
|
|
|
|
return response
|
|
|
|
|
2011-04-25 21:45:20 +02:00
|
|
|
def blank_down(self, column):
|
|
|
|
response = self.do_json('blank-down', {'columnName': column})
|
|
|
|
self.get_models()
|
2011-04-26 03:22:12 +02:00
|
|
|
return response
|
|
|
|
|
|
|
|
def fill_down(self, column):
|
|
|
|
response = self.do_json('fill-down', {'columnName': column})
|
|
|
|
self.get_models()
|
|
|
|
return response
|
|
|
|
|
2013-10-09 20:04:24 +02:00
|
|
|
def transpose_columns_into_rows(
|
|
|
|
self, start_column, column_count,
|
|
|
|
combined_column_name, separator=':', prepend_column_name=True,
|
|
|
|
ignore_blank_cells=True):
|
|
|
|
|
2011-04-26 03:22:12 +02:00
|
|
|
response = self.do_json('transpose-columns-into-rows', {
|
|
|
|
'startColumnName': start_column, 'columnCount': column_count,
|
|
|
|
'combinedColumnName': combined_column_name,
|
|
|
|
'prependColumnName': prepend_column_name,
|
|
|
|
'separator': separator, 'ignoreBlankCells': ignore_blank_cells})
|
|
|
|
self.get_models()
|
|
|
|
return response
|
2011-04-26 05:15:35 +02:00
|
|
|
|
|
|
|
def transpose_rows_into_columns(self, column, row_count):
|
|
|
|
response = self.do_json('transpose-rows-into-columns', {
|
|
|
|
'columnName': column, 'rowCount': row_count})
|
|
|
|
self.get_models()
|
|
|
|
return response
|
|
|
|
|
2011-06-08 19:05:55 +02:00
|
|
|
# Reconciliation
|
|
|
|
# http://code.google.com/p/google-refine/wiki/ReconciliationServiceApi
|
|
|
|
def guess_types_of_column(self, column, service):
|
|
|
|
"""Query the reconciliation service for what it thinks this column is.
|
|
|
|
|
2011-06-09 14:07:18 +02:00
|
|
|
service: reconciliation endpoint URL
|
2011-06-08 19:05:55 +02:00
|
|
|
|
|
|
|
Returns [
|
2011-06-09 14:07:18 +02:00
|
|
|
{"id":"/domain/type","name":"Type Name","score":10.2,"count":18},
|
2011-06-08 19:05:55 +02:00
|
|
|
...
|
|
|
|
]
|
|
|
|
"""
|
|
|
|
response = self.do_json('guess-types-of-column', {
|
2011-06-09 14:07:18 +02:00
|
|
|
'columnName': column, 'service': service}, include_engine=False)
|
2011-06-08 19:05:55 +02:00
|
|
|
return response['types']
|
2011-06-09 14:07:18 +02:00
|
|
|
|
|
|
|
def get_reconciliation_services(self):
|
2011-06-10 16:39:20 +02:00
|
|
|
response = self.get_preference('reconciliation.standardServices')
|
2011-06-09 14:07:18 +02:00
|
|
|
self.recon_services = response
|
|
|
|
return response
|
|
|
|
|
|
|
|
def get_reconciliation_service_by_name_or_url(self, name):
|
|
|
|
recon_services = self.get_reconciliation_services()
|
|
|
|
for recon_service in recon_services:
|
|
|
|
if recon_service['name'] == name or recon_service['url'] == name:
|
|
|
|
return recon_service
|
|
|
|
return None
|
|
|
|
|
2013-10-09 20:04:24 +02:00
|
|
|
def reconcile(self, column, service, reconciliation_type=None,
|
|
|
|
reconciliation_config=None):
|
2011-06-09 14:07:18 +02:00
|
|
|
"""Perform a reconciliation asynchronously.
|
|
|
|
|
|
|
|
config: {
|
|
|
|
"mode": "standard-service",
|
|
|
|
"service": "http://.../reconcile/",
|
|
|
|
"identifierSpace": "http://.../ns/authority",
|
|
|
|
"schemaSpace": "http://.../ns/type",
|
|
|
|
"type": {
|
|
|
|
"id": "/domain/type",
|
|
|
|
"name": "Type Name"
|
|
|
|
},
|
|
|
|
"autoMatch": true,
|
|
|
|
"columnDetails": []
|
|
|
|
}
|
|
|
|
|
|
|
|
Returns typically {'code': 'pending'}; call wait_until_idle() to wait
|
|
|
|
for reconciliation to complete.
|
|
|
|
"""
|
|
|
|
# Create a reconciliation config by looking up recon service info
|
2013-10-09 20:04:24 +02:00
|
|
|
if reconciliation_config is None:
|
2011-06-09 14:07:18 +02:00
|
|
|
service = self.get_reconciliation_service_by_name_or_url(service)
|
2013-10-09 20:04:24 +02:00
|
|
|
if reconciliation_type is None:
|
2011-06-09 14:07:18 +02:00
|
|
|
raise ValueError('Must have at least one of config or type')
|
2013-10-09 20:04:24 +02:00
|
|
|
reconciliation_config = {
|
2011-06-09 14:07:18 +02:00
|
|
|
'mode': 'standard-service',
|
|
|
|
'service': service['url'],
|
|
|
|
'identifierSpace': service['identifierSpace'],
|
|
|
|
'schemaSpace': service['schemaSpace'],
|
|
|
|
'type': {
|
2013-10-09 20:04:24 +02:00
|
|
|
'id': reconciliation_type['id'],
|
|
|
|
'name': reconciliation_type['name'],
|
2011-06-09 14:07:18 +02:00
|
|
|
},
|
|
|
|
'autoMatch': True,
|
|
|
|
'columnDetails': [],
|
|
|
|
}
|
|
|
|
return self.do_json('reconcile', {
|
2013-10-09 20:04:24 +02:00
|
|
|
'columnName': column, 'config': json.dumps(reconciliation_config)})
|