From cf2ff57970c261b004948cc8368db2336b2c0ca0 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 6 Dec 2015 14:20:55 +0000 Subject: [PATCH] Fixed config getting and setting --- project/config/cli.py | 3 ++- project/sync/cli.py | 3 +++ project/utils/config.py | 19 ++++++++++++------- project/utils/constants.py | 2 +- project/utils/defaults.yml | 10 +++------- project/utils/repos.py | 16 ++++++++++++++-- scripts/runtests | 2 +- 7 files changed, 36 insertions(+), 19 deletions(-) diff --git a/project/config/cli.py b/project/config/cli.py index 820d805..2e73770 100644 --- a/project/config/cli.py +++ b/project/config/cli.py @@ -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)) diff --git a/project/sync/cli.py b/project/sync/cli.py index ee42314..b5dab73 100644 --- a/project/sync/cli.py +++ b/project/sync/cli.py @@ -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() diff --git a/project/utils/config.py b/project/utils/config.py index 7cadc94..ba7d0f0 100644 --- a/project/utils/config.py +++ b/project/utils/config.py @@ -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(): diff --git a/project/utils/constants.py b/project/utils/constants.py index d626f13..a5d7195 100644 --- a/project/utils/constants.py +++ b/project/utils/constants.py @@ -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') diff --git a/project/utils/defaults.yml b/project/utils/defaults.yml index 7f4e101..ebd2de8 100644 --- a/project/utils/defaults.yml +++ b/project/utils/defaults.yml @@ -1,7 +1,3 @@ -thing: thing 1 -thing 2: thing -stuff: - - thing 1 - - thing 2 - - thing 3 -long things: stuff +public_repo: + +private_repo: diff --git a/project/utils/repos.py b/project/utils/repos.py index 82884a6..42d40d1 100644 --- a/project/utils/repos.py +++ b/project/utils/repos.py @@ -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 diff --git a/scripts/runtests b/scripts/runtests index 1553f31..d832593 100755 --- a/scripts/runtests +++ b/scripts/runtests @@ -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