1
Fork 0

Fixed config getting and setting

This commit is contained in:
Jake Howard 2015-12-06 14:20:55 +00:00
parent 388831a732
commit cf2ff57970
7 changed files with 36 additions and 19 deletions

View file

@ -19,7 +19,8 @@ def show(key):
@click.argument('key', nargs=1)
@click.argument('value', nargs=-1)
def set(key, value):
key = " ".join(key)
if type(value) == type(()):
value = " ".join(value)
status = config.set(key, value)
if status == "SUCCESS":
print("{} set to {}".format(key, value))

View file

@ -10,4 +10,7 @@ def cli(private):
return 1
repos.clone_public_data()
if private:
if not config.get('private_repo'):
print("private repo not set")
return 0
repos.clone_public_data()

View file

@ -11,7 +11,7 @@ except ImportError:
def get_config_data(filename):
try:
with open(filename) as config_file:
return load(config_file, Loader=Loader)
return load(config_file, Loader=Loader) or {}
except FileNotFoundError:
return {}
except Exception as e:
@ -19,7 +19,7 @@ def get_config_data(filename):
return {}
def write_user_config(refresh_after=False):
def write_user_config():
try:
with open(constants.USER_CONFIG_DIR, 'w') as config_file:
dump(USER_CONFIG, config_file, indent=4, default_flow_style=False, Dumper=Dumper)
@ -28,23 +28,28 @@ def write_user_config(refresh_after=False):
return e
# Create the config file if it doesnt exist already
os.makedirs(constants.DATA_DIR, mode=0o755, exist_ok=True)
open(constants.USER_CONFIG_DIR, 'a').close()
DEFAULT_CONFIG = get_config_data(constants.DEFAULT_CONFIG_DIR)
USER_CONFIG = get_config_data(os.path.expanduser(constants.USER_CONFIG_DIR))
USER_CONFIG = get_config_data(constants.USER_CONFIG_DIR)
def get(key):
if USER_CONFIG and key in USER_CONFIG:
if key in USER_CONFIG:
return USER_CONFIG[key]
elif DEFAULT_CONFIG and key in DEFAULT_CONFIG:
elif key in DEFAULT_CONFIG:
return DEFAULT_CONFIG[key]
return None
def set(key, value, refresh_after=False):
def set(key, value):
if key not in DEFAULT_CONFIG:
return "NO_KEY"
USER_CONFIG[key] = value
return write_user_config(refresh_after)
return write_user_config()
def has_basics():

View file

@ -7,7 +7,7 @@ DATA_DIR = os.path.expanduser("~/dfa/")
# Config
USER_CONFIG_DIR = os.path.join(DATA_DIR, ".dfa.yml")
DEFAULT_CONFIG_DIR = os.path.join(BASE_DIR, 'defaults.yml')
REQUIRED_KEYS = ['public_data']
REQUIRED_KEYS = ['public_repo']
# Data Directories
PRIVATE_DATA_DIR = os.path.join(DATA_DIR, 'private_data')

View file

@ -1,7 +1,3 @@
thing: thing 1
thing 2: thing
stuff:
- thing 1
- thing 2
- thing 3
long things: stuff
public_repo:
private_repo:

View file

@ -1,4 +1,4 @@
import os
import os, shutil
from . import config, constants
@ -12,10 +12,22 @@ def clone_public_data():
def clone_private_data():
exit_code = os.system("git clone -b master --single-branch {} {}"
.format(config.get('private_repo'), constants.PRIVATE_DATA_DIR))
return exit_code
if exit_code != 0:
return exit_code
shutil.copytree(
constants.PRIVATE_DATA_DIR,
constants.PUBLIC_DATA_DIR
)
def has_data(data):
public_path = os.path.join(constants.PUBLIC_DATA_DIR, data)
private_path = os.path.join(constants.PRIVATE_DATA_DIR, data)
return os.path.isdir(public_path) or os.path.isdir(private_path)
def go_to_data(subdir=''):
path = os.path.join(constants.PUBLIC_DATA_DIR, subdir)
os.chdir(path)
return path

View file

@ -4,7 +4,7 @@ set -e
export PATH=env/bin:${PATH}
flake8 project --ignore=E128,E501
flake8 project --ignore=E128,E501,E401
echo ">> Flake8 Tests Completed"
if [ "$1" == "CI" ]; then