2011-05-27 02:15:38 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
Script to provide a command line interface to a Refine server.
|
|
|
|
|
|
|
|
Examples,
|
|
|
|
|
|
|
|
refine --list # show list of Refine projects, ID: name
|
|
|
|
refine --export 1234... > project.tsv
|
|
|
|
refine --export --output=project.xls 1234...
|
2011-05-27 02:56:18 +02:00
|
|
|
refine --apply trim.json 1234...
|
2011-05-27 02:15:38 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Copyright (c) 2011 Paul Makepeace, Real Programmers. All rights reserved.
|
|
|
|
|
|
|
|
# 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-05-27 02:20:26 +02:00
|
|
|
|
2011-05-27 02:15:38 +02:00
|
|
|
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
from google.refine import refine
|
|
|
|
|
|
|
|
|
2011-06-08 19:06:50 +02:00
|
|
|
PARSER = optparse.OptionParser(
|
|
|
|
usage='usage: %prog [--help | OPTIONS] [project ID/URL]')
|
2011-05-27 02:15:38 +02:00
|
|
|
PARSER.add_option('-H', '--host', dest='host',
|
|
|
|
help='Google Refine hostname')
|
|
|
|
PARSER.add_option('-P', '--port', dest='port',
|
|
|
|
help='Google Refine port')
|
|
|
|
PARSER.add_option('-o', '--output', dest='output',
|
|
|
|
help='Output filename')
|
|
|
|
# Options that are more like commands
|
|
|
|
PARSER.add_option('-l', '--list', dest='list', action='store_true',
|
|
|
|
help='List projects')
|
|
|
|
PARSER.add_option('-E', '--export', dest='export', action='store_true',
|
|
|
|
help='Export project')
|
2011-05-27 02:20:26 +02:00
|
|
|
PARSER.add_option('-f', '--apply', dest='apply',
|
|
|
|
help='Apply a JSON commands file to a project')
|
2011-05-27 02:15:38 +02:00
|
|
|
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-05-27 02:15:38 +02:00
|
|
|
def list_projects():
|
|
|
|
"""Query the Refine server and list projects by ID: name."""
|
|
|
|
projects = refine.Refine(refine.RefineServer()).list_projects().items()
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-05-27 02:15:38 +02:00
|
|
|
def date_to_epoch(json_dt):
|
2013-10-09 20:04:24 +02:00
|
|
|
"""Convert a JSON date time into seconds-since-epoch."""
|
2011-05-27 02:15:38 +02:00
|
|
|
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']))
|
|
|
|
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-05-27 02:15:38 +02:00
|
|
|
def export_project(project, options):
|
|
|
|
"""Dump a project to stdout or options.output file."""
|
|
|
|
export_format = 'tsv'
|
|
|
|
if options.output:
|
|
|
|
ext = os.path.splitext(options.output)[1][1:] # 'xls'
|
|
|
|
if ext:
|
|
|
|
export_format = ext.lower()
|
|
|
|
output = open(options.output, 'wb')
|
|
|
|
else:
|
|
|
|
output = sys.stdout
|
|
|
|
output.writelines(project.export(export_format=export_format))
|
|
|
|
output.close()
|
2011-05-27 02:20:26 +02:00
|
|
|
|
2013-10-09 20:04:24 +02:00
|
|
|
|
|
|
|
#noinspection PyPep8Naming
|
2011-05-27 02:15:38 +02:00
|
|
|
def main():
|
2013-10-09 20:04:24 +02:00
|
|
|
"""Main."""
|
2011-05-27 02:15:38 +02:00
|
|
|
options, args = PARSER.parse_args()
|
|
|
|
|
|
|
|
if options.host:
|
|
|
|
refine.REFINE_HOST = options.host
|
|
|
|
if options.port:
|
|
|
|
refine.REFINE_PORT = options.port
|
2011-05-27 02:20:26 +02:00
|
|
|
|
2011-05-27 02:15:38 +02:00
|
|
|
if not options.list and len(args) != 1:
|
2011-06-08 19:07:39 +02:00
|
|
|
PARSER.print_usage()
|
2011-05-27 02:15:38 +02:00
|
|
|
if options.list:
|
|
|
|
list_projects()
|
2011-06-08 19:07:39 +02:00
|
|
|
if args:
|
2011-05-27 02:15:38 +02:00
|
|
|
project = refine.RefineProject(args[0])
|
2011-05-27 02:20:26 +02:00
|
|
|
if options.apply:
|
|
|
|
response = project.apply_operations(options.apply)
|
|
|
|
if response != 'ok':
|
|
|
|
print >>sys.stderr, 'Failed to apply %s: %s' % (options.apply,
|
|
|
|
response)
|
2011-05-27 02:15:38 +02:00
|
|
|
if options.export:
|
|
|
|
export_project(project, options)
|
|
|
|
|
2011-06-08 19:07:39 +02:00
|
|
|
return project
|
2011-05-27 02:20:26 +02:00
|
|
|
|
2011-05-27 02:15:38 +02:00
|
|
|
if __name__ == '__main__':
|
2011-06-08 19:07:39 +02:00
|
|
|
# return project so that it's available interactively, python -i refine.py
|
2013-10-09 20:04:24 +02:00
|
|
|
refine_project = main()
|