-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#114 Fixing piping issue & refactored CLI
- Loading branch information
1 parent
64d8617
commit d2c4faa
Showing
5 changed files
with
37 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,43 @@ | ||
import argparse | ||
import fileinput | ||
import itertools | ||
#!/usr/bin/python | ||
# coding: utf8 | ||
|
||
import click | ||
import json | ||
import geocoder | ||
import sys | ||
from .api import get | ||
import os | ||
|
||
|
||
providers = ['google', 'bing', 'osm', 'here', 'w3w', 'opencage'] | ||
methods = ['geocode', 'reverse', 'elevation', 'timezone'] | ||
outputs = ['json', 'osm', 'geojson', 'wkt'] | ||
|
||
def peek(iterable): | ||
iterator = iter(iterable) | ||
item = next(iterator) | ||
new_iterator = itertools.chain([item], iterator) | ||
return item, new_iterator | ||
|
||
@click.command() | ||
@click.argument('location', nargs=-1) | ||
@click.option('--provider', '-p', default='bing', type=click.Choice(providers)) | ||
@click.option('--method', '-m', default='geocode', type=click.Choice(methods)) | ||
@click.option('--output', '-o', default='json', type=click.Choice(outputs)) | ||
def cli(location, provider, method, output): | ||
"Geocode an arbitrary number of strings from Command Line." | ||
|
||
def cli(): | ||
parser = argparse.ArgumentParser(description="Geocode an arbitrary number" | ||
" of strings from Command Line.") | ||
parser.add_argument('input', | ||
type=str, | ||
nargs="*", | ||
help="Filename(s) or strings to be geocoded") | ||
parser.add_argument('-p', '--provider', | ||
help="provider (choose from: bing," | ||
"geonames, google, mapquest, nokia, osm, tomtom, " | ||
"geolytica, arcgis, yahoo, ottawa)", | ||
default='bing') | ||
parser.add_argument('-m', '--method', | ||
type=str, | ||
help="Output type (choose from: geocode, reverse)", | ||
default='geocode') | ||
parser.add_argument('-o', '--outfile', | ||
help="Output file (default stdout)", | ||
default=sys.stdout) | ||
parser.add_argument('-t', '--type', | ||
type=str, | ||
help="Output type (choose from: json, osm, geojson)", | ||
default='json') | ||
parser.add_argument('--pretty', | ||
help="Prettify JSON output", | ||
action="store_true") | ||
args = parser.parse_args() | ||
# Read multiple files & user input location | ||
locations = [] | ||
for item in location: | ||
if os.path.exists(item): | ||
with open(item, 'rb') as f: | ||
locations += f.read().splitlines() | ||
else: | ||
locations.append(item) | ||
|
||
# User input data | ||
if args.input: | ||
# Geocode results from user input | ||
for location in locations: | ||
g = geocoder.get(location.strip(), provider=provider, method=method) | ||
try: | ||
sys.argv = [sys.argv[1]] + args.input | ||
input = fileinput.input() | ||
_, input = peek(input) | ||
click.echo(json.dumps(g.__getattribute__(output))) | ||
except IOError: | ||
input = args.input | ||
else: | ||
print('[ERROR] Please include a location or a <File Path>.\n' | ||
'$ geocode "Ottawa ON\n"' | ||
'$ geocode "textfile.txt"') | ||
sys.exit() | ||
|
||
for item in input: | ||
item = item.strip() | ||
g = get(item, provider=args.provider, method=args.method) | ||
|
||
# User input output | ||
args.type = args.type.lower() | ||
type_lookup = { | ||
'json': g.json, | ||
'geojson': g.geojson, | ||
'osm': g.osm, | ||
} | ||
if args.type in type_lookup: | ||
output = type_lookup.get(args.type.lower(), '') | ||
else: | ||
print('[ERROR] Please provide a valid type.\n' | ||
'Ex: geojson, osm, json\n' | ||
'$ geocode "Ottawa ON" --type geojson') | ||
# When invalid command is entered a broken pipe error occurs | ||
sys.exit() | ||
|
||
# User define Pretty output | ||
if args.pretty: | ||
params = {'indent': 4} | ||
else: | ||
params = {} | ||
|
||
args.outfile.write("{}\n".format(json.dumps(output, **params))) | ||
if __name__ == '__main__': | ||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
requests>=2.5.3 | ||
ratelim>=0.1.6 | ||
ratelim>=0.1.6 | ||
click>=3.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters