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('key', nargs=1)
@click.argument('value', nargs=-1) @click.argument('value', nargs=-1)
def set(key, value): def set(key, value):
key = " ".join(key) if type(value) == type(()):
value = " ".join(value)
status = config.set(key, value) status = config.set(key, value)
if status == "SUCCESS": if status == "SUCCESS":
print("{} set to {}".format(key, value)) print("{} set to {}".format(key, value))

View file

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

View file

@ -11,7 +11,7 @@ except ImportError:
def get_config_data(filename): def get_config_data(filename):
try: try:
with open(filename) as config_file: with open(filename) as config_file:
return load(config_file, Loader=Loader) return load(config_file, Loader=Loader) or {}
except FileNotFoundError: except FileNotFoundError:
return {} return {}
except Exception as e: except Exception as e:
@ -19,7 +19,7 @@ def get_config_data(filename):
return {} return {}
def write_user_config(refresh_after=False): def write_user_config():
try: try:
with open(constants.USER_CONFIG_DIR, 'w') as config_file: with open(constants.USER_CONFIG_DIR, 'w') as config_file:
dump(USER_CONFIG, config_file, indent=4, default_flow_style=False, Dumper=Dumper) 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 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) 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): def get(key):
if USER_CONFIG and key in USER_CONFIG: if key in USER_CONFIG:
return USER_CONFIG[key] return USER_CONFIG[key]
elif DEFAULT_CONFIG and key in DEFAULT_CONFIG: elif key in DEFAULT_CONFIG:
return DEFAULT_CONFIG[key] return DEFAULT_CONFIG[key]
return None return None
def set(key, value, refresh_after=False): def set(key, value):
if key not in DEFAULT_CONFIG: if key not in DEFAULT_CONFIG:
return "NO_KEY" return "NO_KEY"
USER_CONFIG[key] = value USER_CONFIG[key] = value
return write_user_config(refresh_after) return write_user_config()
def has_basics(): def has_basics():

View file

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

View file

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

View file

@ -1,4 +1,4 @@
import os import os, shutil
from . import config, constants from . import config, constants
@ -12,10 +12,22 @@ def clone_public_data():
def clone_private_data(): def clone_private_data():
exit_code = os.system("git clone -b master --single-branch {} {}" exit_code = os.system("git clone -b master --single-branch {} {}"
.format(config.get('private_repo'), constants.PRIVATE_DATA_DIR)) .format(config.get('private_repo'), constants.PRIVATE_DATA_DIR))
if exit_code != 0:
return exit_code return exit_code
shutil.copytree(
constants.PRIVATE_DATA_DIR,
constants.PUBLIC_DATA_DIR
)
def has_data(data): def has_data(data):
public_path = os.path.join(constants.PUBLIC_DATA_DIR, data) public_path = os.path.join(constants.PUBLIC_DATA_DIR, data)
private_path = os.path.join(constants.PRIVATE_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) 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} export PATH=env/bin:${PATH}
flake8 project --ignore=E128,E501 flake8 project --ignore=E128,E501,E401
echo ">> Flake8 Tests Completed" echo ">> Flake8 Tests Completed"
if [ "$1" == "CI" ]; then if [ "$1" == "CI" ]; then