From b6137dd80391083fcd46d3cda09985a31d69e3b1 Mon Sep 17 00:00:00 2001 From: Paul Makepeace Date: Fri, 27 May 2011 01:15:38 +0100 Subject: [PATCH] Allow REFINE_{HOST,PORT} to be set in code. --- google/refine/refine.py | 4 +- refine.py | 93 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100755 refine.py diff --git a/google/refine/refine.py b/google/refine/refine.py index a86d250..ffded26 100644 --- a/google/refine/refine.py +++ b/google/refine/refine.py @@ -39,7 +39,9 @@ REFINE_PORT = os.environ.get('GOOGLE_REFINE_PORT', '3333') class RefineServer(object): """Communicate with a Refine server.""" - def __init__(self, server='http://%s:%s' % (REFINE_HOST, REFINE_PORT)): + def __init__(self, server=None): + if server is None: + server='http://%s:%s' % (REFINE_HOST, REFINE_PORT) self.server = server[:-1] if server.endswith('/') else server def urlopen(self, command, data=None, project_id=None): diff --git a/refine.py b/refine.py new file mode 100755 index 0000000..8d83850 --- /dev/null +++ b/refine.py @@ -0,0 +1,93 @@ +#!/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... +""" + +# 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 + + +import optparse +import os +import sys +import time + +from google.refine import refine + + +PARSER = optparse.OptionParser(usage='usage: %prog [OPTIONS] [project ID/URL]') +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') + + +def list_projects(): + """Query the Refine server and list projects by ID: name.""" + projects = 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'])) + +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() + +def main(): + "Main." + options, args = PARSER.parse_args() + + if options.host: + refine.REFINE_HOST = options.host + if options.port: + refine.REFINE_PORT = options.port + + if not options.list and len(args) != 1: + PARSER.error('expecting --list or project ID/URL') + if options.list: + list_projects() + else: + project = refine.RefineProject(args[0]) + if options.export: + export_project(project, options) + + +if __name__ == '__main__': + main()