Skip to content

Commit

Permalink
Merge pull request #6 from harshitbansal05/cli-interface
Browse files Browse the repository at this point in the history
Add cli interface for script adding/editing functions
  • Loading branch information
zhangcandrew authored Jul 12, 2019
2 parents 22600db + 384da6f commit 55a661a
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
*.pyc
dist/
build/
retriever_recipes.egg-info
apidocs
*.DS_Store
.cache/*
Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions recipes/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys
from builtins import input

from recipes.lib.get_opts import parser
from recipes.lib.new_json import create_json
from recipes.lib.edit_json import edit_json
from recipes.lib.delete_json import delete_json


def main():
"""This function launches the retriever-recipes."""
if len(sys.argv) == 1:
# If no command line args are passed, show the help options
parser.parse_args(['-h'])
else:
# Otherwise, parse them
args = parser.parse_args()

if args.command == 'new_json':
# Create new JSON script
create_json()
return

if args.command == 'edit_json':
# Edit existing JSON script
json_file = args.dataset.lower()
edit_json(json_file)
return

if args.command == 'delete_json':
# Delete existing JSON script
confirm = input("Really remove " + args.dataset.lower() +
" and all its contents? (y/N): ")
if confirm.lower().strip() in ['y', 'yes']:
json_file = args.dataset.lower()
delete_json(json_file)
return

if __name__ == "__main__":
main()
Empty file added recipes/lib/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions recipes/lib/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ENCODING = 'ISO-8859-1'
SCRIPT_SEARCH_PATHS = ['scripts', './', '../scripts', '../']
File renamed without changes.
18 changes: 14 additions & 4 deletions src/edit_json.py → recipes/lib/edit_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import json
from time import sleep

from utils import clean_input
from recipes.lib.utils import clean_input
from recipes.lib.defaults import SCRIPT_SEARCH_PATHS


def edit_dict(obj, tabwidth=0):
Expand Down Expand Up @@ -169,9 +170,17 @@ def edit_json(script):
Note: Name of script is the dataset name.
"""
json_file = script.replace('-', '_') + '.json'
file_path = None
for search_path in SCRIPT_SEARCH_PATHS:
if os.path.exists(os.path.join(search_path, json_file)):
file_path = search_path
break
if file_path is None:
print("Script not found.")
return
try:
contents = json.load(
open(os.path.join('scripts', json_file), 'r'))
open(os.path.join(file_path, json_file), 'r'))
except (IOError, OSError):
print("Script not found.")
return
Expand All @@ -180,14 +189,15 @@ def edit_json(script):

file_name = contents['name'] + ".json"
file_name = file_name.replace('-', '_')
with open(os.path.join('scripts', file_name), 'w') as output_file:

with open(os.path.join(file_path, file_name), 'w') as output_file:
json.dump(contents,
output_file,
sort_keys=True,
indent=4,
separators=(',', ': '))
output_file.write('\n')
print("\nScript written to " + os.path.join('scripts', file_name))
print("\nScript written to " + os.path.join(file_path, file_name))
output_file.close()


Expand Down
33 changes: 33 additions & 0 deletions recipes/lib/get_opts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import argparse
import argcomplete

from recipes.lib.utils import get_script_version
from recipes.lib.defaults import SCRIPT_SEARCH_PATHS


script_list = []

script_version_list = get_script_version(SCRIPT_SEARCH_PATHS)
for script in script_version_list:
script = script.split(',')[0]
if script.endswith('.json'):
script_list.append(('.'.join(script.split('.')[:-1])).replace('_', '-'))

parser = argparse.ArgumentParser(prog='retriever-recipes')

# ..............................................................
# Subparsers
# ..............................................................
subparsers = parser.add_subparsers(help='sub-command help', dest='command')
new_json_parser = subparsers.add_parser('new_json', help='CLI to create retriever json script')
edit_json_parser = subparsers.add_parser('edit_json', help='CLI to edit retriever json script')
delete_json_parser = subparsers.add_parser('delete_json', help='CLI to remove retriever json script')
help_parser = subparsers.add_parser('help', help='')

# ..............................................................
# Subparsers With Arguments
# ..............................................................
edit_json_parser.add_argument('dataset', nargs=1, help='dataset name', choices=script_list)
delete_json_parser.add_argument('dataset', nargs=1, help='dataset name', choices=script_list)

argcomplete.autocomplete(parser)
3 changes: 2 additions & 1 deletion src/new_json.py → recipes/lib/new_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import json
from os.path import join, exists

from utils import clean_input, is_empty, open_fr, ENCODING
from recipes.lib.utils import clean_input, is_empty, open_fr
from recipes.lib.defaults import ENCODING


def short_names():
Expand Down
48 changes: 23 additions & 25 deletions src/utils.py → recipes/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from os.path import join, exists
from collections import OrderedDict

ENCODING = 'ISO-8859-1'
from recipes.lib.defaults import ENCODING


def is_empty(val):
Expand Down Expand Up @@ -100,32 +100,30 @@ def read_py(script_name, search_path):
return None


def get_script_version():
def get_script_version(search_paths=['scripts']):
"""This function gets the version number of the scripts and returns them in array form."""
search_path = 'scripts'
loaded_files = []
scripts = []
if exists(search_path):
data_packages = [file_i for file_i in os.listdir(search_path) if file_i.endswith(".json")]
for script in data_packages:
script_name = '.'.join(script.split('.')[:-1])
script_version = read_json(os.path.join(search_path, script))
if script_name not in loaded_files and script_version:
scripts.append(','.join([script, str(script_version)]))
loaded_files.append(script_name)

files = [file for file in os.listdir(search_path)
if file[-3:] == ".py" and file[0] != "_" and
('#retriever' in
' '.join(open_fr(join(search_path, file), encoding=ENCODING).readlines()[:2]).lower())
]
for script in files:
script_name = '.'.join(script.split('.')[:-1])
script_version = read_py(script_name, search_path)
if script_name not in loaded_files and script_version:
scripts.append(','.join([script, str(script_version)]))
loaded_files.append(script_name)
else:
print("No")
for search_path in search_paths:
if exists(search_path):
data_packages = [file_i for file_i in os.listdir(search_path) if file_i.endswith(".json")]
for script in data_packages:
script_name = '.'.join(script.split('.')[:-1])
script_version = read_json(os.path.join(search_path, script))
if script_name not in loaded_files and script_version:
scripts.append(','.join([script, str(script_version)]))
loaded_files.append(script_name)

files = [file for file in os.listdir(search_path)
if file[-3:] == ".py" and file[0] != "_" and
('#retriever' in
' '.join(open_fr(join(search_path, file), encoding=ENCODING).readlines()[:2]).lower())
]
for script in files:
script_name = '.'.join(script.split('.')[:-1])
script_version = read_py(script_name, search_path)
if script_name not in loaded_files and script_version:
scripts.append(','.join([script, str(script_version)]))
loaded_files.append(script_name)
scripts = sorted(scripts, key=str.lower)
return scripts
87 changes: 87 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
import platform
import subprocess

from setuptools import setup, find_packages


current_platform = platform.system().lower()

if os.path.exists(".git/hooks"): # check if we are in git repo
subprocess.call("cp hooks/pre-commit .git/hooks/pre-commit", shell=True)
subprocess.call("chmod +x .git/hooks/pre-commit", shell=True)

def read(*names, **kwargs):
return open(
os.path.join(os.path.dirname(__file__), *names),
).read()

setup(
name='retriever-recipes',
version='0.0.1',
description='Retriever Recieps',
long_description='{a}'.format(a=read('README.md')),
long_description_content_type='text/markdown',
author='Harshit Bansal, Apoorva Pandey, Ben Morris, Shivam Negi, Akash Goel, Andrew Zhang, Henry Senyondo, Ethan White',
author_email='[email protected]',
url='https://github.com/weecology/retriever-recipes',
classifiers=[
'Intended Audience :: Science/Research',
'License :: OSI Approved :: MIT License',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX',
'Operating System :: Unix',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: Implementation :: PyPy',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Scientific/Engineering :: GIS',
'Topic :: Scientific/Engineering :: Information Analysis',
'Topic :: Database',
],
packages=find_packages(
exclude=['hooks',
'docs',
'tests',
'scripts',
'docker',
".cache"]),
entry_points={
'console_scripts': [
'retriever-recipes = recipes.__main__:main',
],
},
install_requires=[
'future',
'argcomplete'
],
setup_requires=[],
)

# windows doesn't have bash. No point in using bash-completion
if current_platform != "windows":
# if platform is OS X use "~/.bash_profile"
if current_platform == "darwin":
bash_file = "~/.bash_profile"
# if platform is Linux use "~/.bashrc
elif current_platform == "linux":
bash_file = "~/.bashrc"
# else write and discard
else:
bash_file = "/dev/null"

argcomplete_command = 'eval "$(register-python-argcomplete retriever-recipes)"'
with open(os.path.expanduser(bash_file), "a+") as bashrc:
bashrc.seek(0)
# register retriever for arg-completion if not already registered
# whenever a new shell is spawned
if argcomplete_command not in bashrc.read():
bashrc.write(argcomplete_command + "\n")
bashrc.close()
os.system("activate-global-python-argcomplete")
# register for the current shell
os.system(argcomplete_command)
2 changes: 1 addition & 1 deletion test/test_modified_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from distutils.version import LooseVersion

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from src.utils import get_script_version
from recipes.lib.utils import get_script_version


file_location = os.path.dirname(os.path.realpath(__file__))
Expand Down
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import os

from src.utils import get_script_version
from recipes.lib.utils import get_script_version


def write_version_file(scripts):
Expand Down

0 comments on commit 55a661a

Please sign in to comment.