forked from openfusion-dev/ogre
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openfusion-dev#33 from dmtucker/next
Create ogre.cli from ogre.__main__
- Loading branch information
Showing
5 changed files
with
182 additions
and
147 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
graft ogre | ||
global-exclude *.py[cod] __pycache__ | ||
include LICENSE | ||
graft ogre |
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,145 +1,9 @@ | ||
""" | ||
Make queries using OGRe directly. | ||
# coding: utf-8 | ||
|
||
See https://ogre.readthedocs.org/en/latest/ for more information. | ||
""" | ||
"""This module defines the behavior of python -m.""" | ||
|
||
import argparse | ||
import json | ||
import logging | ||
import os | ||
from ogre import OGRe | ||
from __future__ import absolute_import | ||
|
||
import ogre.cli | ||
|
||
def main(): | ||
|
||
"""Process arguments and invoke OGRe to fetch some data.""" | ||
|
||
parser = argparse.ArgumentParser(description="OpenFusion GeoJSON Retriever") | ||
parser.add_argument( | ||
"--keys", | ||
help="Specify API keys.", | ||
default=None, | ||
) | ||
parser.add_argument( | ||
"-s", "--sources", | ||
help="Specify public APIs to get content from (required)." + | ||
" 'Twitter' is currently the only supported source.", | ||
action="append", | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"-m", "--media", | ||
help="Specify content mediums to fetch." + | ||
" 'image', 'sound', 'text', and 'video' are supported.", | ||
default=None, | ||
action="append", | ||
) | ||
parser.add_argument( | ||
"-k", "--keyword", | ||
help="Specify search criteria.", | ||
default="", | ||
) | ||
parser.add_argument( | ||
"-q", "--quantity", | ||
help="Specify a quota of results to fetch.", | ||
type=int, | ||
default=15, | ||
) | ||
parser.add_argument( | ||
"-l", "--location", | ||
help="Specify a place (latitude, longitude, radius, unit) to search." + | ||
" 'km' and 'mi' are supported units.", | ||
default=None, | ||
nargs=4, | ||
) | ||
parser.add_argument( | ||
"-i", "--interval", | ||
help="Specify a period of time (earliest, latest) to search." + | ||
" Each moment should be a POSIX timestamp.", | ||
default=None, | ||
nargs=2, | ||
) | ||
parser.add_argument( | ||
"--hard", | ||
help="Fail hard (Raise exceptions instead of returning empty).", | ||
action="store_true", | ||
default=False, | ||
) | ||
parser.add_argument( | ||
"--insecure", | ||
help="Prefer HTTP.", | ||
action="store_true", | ||
default=False, | ||
) | ||
parser.add_argument( | ||
"--limit", | ||
help="Specify a query limit.", | ||
default=None, | ||
) | ||
parser.add_argument( | ||
"--log", | ||
help="Specify a log level.", | ||
default=None, | ||
) | ||
parser.add_argument( | ||
"--strict", | ||
help="Ensure resulting media is specifically requested.", | ||
action="store_true", | ||
default=False, | ||
) | ||
args = parser.parse_args() | ||
|
||
if args.keys is not None: | ||
args.keys = json.loads(args.keys) | ||
else: | ||
args.keys = { | ||
"Twitter": { | ||
"consumer_key": os.environ.get("TWITTER_CONSUMER_KEY"), | ||
"access_token": os.environ.get("TWITTER_ACCESS_TOKEN"), | ||
}, | ||
} | ||
if args.media is None: | ||
args.media = ("image", "sound", "text", "video") | ||
if args.location is not None: | ||
args.location[0] = float(args.location[0]) | ||
args.location[1] = float(args.location[1]) | ||
args.location[2] = float(args.location[2]) | ||
if args.interval is not None: | ||
args.interval[0] = float(args.interval[0]) | ||
args.interval[1] = float(args.interval[1]) | ||
if args.limit is not None: | ||
args.limit = int(args.limit) | ||
if args.log is not None: | ||
args.log = getattr(logging, args.log.upper()) | ||
else: | ||
args.log = logging.WARN | ||
|
||
logging.basicConfig( | ||
level=args.log, | ||
format="%(asctime)s.%(msecs)03d %(name)s %(levelname)s: %(message)s", | ||
datefmt="%Y-%m-%d %H:%M:%S", | ||
) | ||
|
||
print( | ||
json.dumps( | ||
OGRe(args.keys).fetch( | ||
sources=args.sources, | ||
media=args.media, | ||
keyword=args.keyword, | ||
quantity=args.quantity, | ||
location=args.location, | ||
interval=args.interval, | ||
fail_hard=args.hard, | ||
query_limit=args.limit, | ||
secure=args.insecure, | ||
strict_media=args.strict, | ||
), | ||
indent=4, | ||
separators=(",", ": "), | ||
), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
ogre.cli.main() |
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 |
---|---|---|
@@ -0,0 +1,150 @@ | ||
""" | ||
Make queries using OGRe directly. | ||
See https://ogre.readthedocs.org/en/latest/ for more information. | ||
""" | ||
|
||
import argparse | ||
import json | ||
import logging | ||
import os | ||
import sys | ||
|
||
from ogre import OGRe | ||
|
||
|
||
def cli(parser=None): | ||
"""Define a CLI.""" | ||
if parser is None: | ||
parser = argparse.ArgumentParser(description="OpenFusion GeoJSON Retriever") | ||
parser.add_argument( | ||
"--keys", | ||
help="Specify API keys.", | ||
default=None, | ||
) | ||
parser.add_argument( | ||
"-s", "--sources", | ||
help="Specify public APIs to get content from (required)." + | ||
" 'Twitter' is currently the only supported source.", | ||
action="append", | ||
required=True, | ||
) | ||
parser.add_argument( | ||
"-m", "--media", | ||
help="Specify content mediums to fetch." + | ||
" 'image', 'sound', 'text', and 'video' are supported.", | ||
default=None, | ||
action="append", | ||
) | ||
parser.add_argument( | ||
"-k", "--keyword", | ||
help="Specify search criteria.", | ||
default="", | ||
) | ||
parser.add_argument( | ||
"-q", "--quantity", | ||
help="Specify a quota of results to fetch.", | ||
type=int, | ||
default=15, | ||
) | ||
parser.add_argument( | ||
"-l", "--location", | ||
help="Specify a place (latitude, longitude, radius, unit) to search." + | ||
" 'km' and 'mi' are supported units.", | ||
default=None, | ||
nargs=4, | ||
) | ||
parser.add_argument( | ||
"-i", "--interval", | ||
help="Specify a period of time (earliest, latest) to search." + | ||
" Each moment should be a POSIX timestamp.", | ||
default=None, | ||
nargs=2, | ||
) | ||
parser.add_argument( | ||
"--hard", | ||
help="Fail hard (Raise exceptions instead of returning empty).", | ||
action="store_true", | ||
default=False, | ||
) | ||
parser.add_argument( | ||
"--insecure", | ||
help="Prefer HTTP.", | ||
action="store_true", | ||
default=False, | ||
) | ||
parser.add_argument( | ||
"--limit", | ||
help="Specify a query limit.", | ||
default=None, | ||
) | ||
parser.add_argument( | ||
"--log", | ||
help="Specify a log level.", | ||
default=None, | ||
) | ||
parser.add_argument( | ||
"--strict", | ||
help="Ensure resulting media is specifically requested.", | ||
action="store_true", | ||
default=False, | ||
) | ||
return parser | ||
|
||
|
||
def main(argv=None): | ||
"""Process arguments and invoke OGRe to fetch some data.""" | ||
|
||
if argv is None: | ||
argv = sys.argv[1:] | ||
args = cli().parse_args(argv) | ||
|
||
if args.keys is not None: | ||
args.keys = json.loads(args.keys) | ||
else: | ||
args.keys = { | ||
"Twitter": { | ||
"consumer_key": os.environ.get("TWITTER_CONSUMER_KEY"), | ||
"access_token": os.environ.get("TWITTER_ACCESS_TOKEN"), | ||
}, | ||
} | ||
if args.media is None: | ||
args.media = ("image", "sound", "text", "video") | ||
if args.location is not None: | ||
args.location[0] = float(args.location[0]) | ||
args.location[1] = float(args.location[1]) | ||
args.location[2] = float(args.location[2]) | ||
if args.interval is not None: | ||
args.interval[0] = float(args.interval[0]) | ||
args.interval[1] = float(args.interval[1]) | ||
if args.limit is not None: | ||
args.limit = int(args.limit) | ||
if args.log is not None: | ||
args.log = getattr(logging, args.log.upper()) | ||
else: | ||
args.log = logging.WARN | ||
|
||
logging.basicConfig( | ||
level=args.log, | ||
format="%(asctime)s.%(msecs)03d %(name)s %(levelname)s: %(message)s", | ||
datefmt="%Y-%m-%d %H:%M:%S", | ||
) | ||
|
||
print( | ||
json.dumps( | ||
OGRe(args.keys).fetch( | ||
sources=args.sources, | ||
media=args.media, | ||
keyword=args.keyword, | ||
quantity=args.quantity, | ||
location=args.location, | ||
interval=args.interval, | ||
fail_hard=args.hard, | ||
query_limit=args.limit, | ||
secure=args.insecure, | ||
strict_media=args.strict, | ||
), | ||
indent=4, | ||
separators=(",", ": "), | ||
), | ||
) |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# coding: utf-8 | ||
|
||
"""Tests for ogre.cli""" | ||
|
||
from __future__ import absolute_import | ||
|
||
import pytest | ||
|
||
import ogre.cli | ||
|
||
|
||
def test___main__(): | ||
"""Test python -m functionality.""" | ||
with pytest.raises(SystemExit) as excinfo: | ||
import ogre.__main__ # pylint: disable=redefined-outer-name, unused-variable | ||
assert excinfo.value != 0 | ||
|
||
|
||
def test_empty(): | ||
"""Test invocation with no arguments.""" | ||
with pytest.raises(SystemExit) as excinfo: | ||
ogre.cli.main() | ||
assert excinfo.value != 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