Allow REFINE_{HOST,PORT} to be set in code.
This commit is contained in:
parent
83f2649221
commit
b6137dd803
|
@ -39,7 +39,9 @@ REFINE_PORT = os.environ.get('GOOGLE_REFINE_PORT', '3333')
|
||||||
class RefineServer(object):
|
class RefineServer(object):
|
||||||
"""Communicate with a Refine server."""
|
"""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
|
self.server = server[:-1] if server.endswith('/') else server
|
||||||
|
|
||||||
def urlopen(self, command, data=None, project_id=None):
|
def urlopen(self, command, data=None, project_id=None):
|
||||||
|
|
|
@ -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 <http://www.gnu.org/licenses/>
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
Loading…
Reference in New Issue