-
-
Save cmccandless/cdd8741fe258ea3d3f3ccc5b97ef538f to your computer and use it in GitHub Desktop.
Add, Edit, Remove, Deprecate exercises to config.json
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
#!/usr/bin/env python | |
import json | |
import subprocess | |
import sys | |
USAGE = 'config.py <a[dd]|e[dit]|r[emove]|deprecate> <exercise-slug>' | |
DEFAULT_TEMPLATE = [ | |
'core', | |
'difficulty', | |
'slug', | |
'topics', | |
'unlocked_by', | |
'uuid' | |
] | |
def get_or_default(_dict, key, default=None): | |
if _dict is None: | |
return default | |
try: | |
return _dict[key] | |
except KeyError: | |
return default | |
def get_exercise_index(exercises, key): | |
for i in range(len(exercises)): | |
if key(exercises[i]): | |
return i | |
return -1 | |
def edit_exercise(slug, exercise=None, template=DEFAULT_TEMPLATE): | |
results = {} | |
if slug is None: | |
slug = input('exercise slug: ') | |
if slug == '': | |
return None | |
for k in template: | |
current = get_or_default(exercise, k) | |
if k == 'uuid': | |
if exercise is None: | |
print('Generating uuid...') | |
results[k] = subprocess.check_output('configlet uuid') | |
results[k] = results[k].decode().strip() | |
else: | |
results[k] = current | |
print('uuid ({})'.format(results[k])) | |
elif k == 'slug': | |
results[k] = slug | |
print('slug ({})'.format(slug)) | |
else: | |
result = input('{} ({}): '.format(k, current)) | |
if result == 'null': | |
result = None | |
elif result == '': | |
result = current | |
else: | |
result = eval(result) | |
results[k] = result | |
return results | |
def main(command, slug, *args): | |
with open('config.json', 'r') as f: | |
config_data = json.load(f) | |
try: | |
template = list(config_data['exercises'][0].keys()) | |
except IndexError: | |
template = DEFAULT_TEMPLATE | |
def get_current_exercise(exercise): | |
return get_or_default(exercise, 'slug') == slug | |
def get_deprecated_exercise(exercise): | |
return get_or_default(config_data['exercises'][deprecated_index], | |
'deprecated', | |
False) | |
current_index = get_exercise_index(config_data['exercises'], | |
get_current_exercise) | |
if command in ['a', 'add']: | |
if current_index >= 0: | |
print('exercise \'{}\' already exists!'.format(slug)) | |
sys.exit(1) | |
exercise = edit_exercise(slug, template=template) | |
deprecated_index = get_exercise_index(config_data['exercises'], | |
get_deprecated_exercise) | |
config_data['exercises'].insert(deprecated_index, exercise) | |
elif command in ['e', 'edit']: | |
if current_index < 0: | |
print('exercise \'{}\' not found!'.format(slug)) | |
sys.exit(1) | |
current = config_data['exercises'][current_index] | |
edited = edit_exercise(slug, current, template) | |
config_data['exercises'][current_index] = edited | |
elif command in ['r', 'remove']: | |
if current_index < 0: | |
print('exercise \'{}\' not found!'.format(slug)) | |
sys.exit(1) | |
del config_data['exercises'][current_index] | |
elif command == 'deprecate': | |
if current_index < 0: | |
print('exercise \'{}\' not found!'.format(slug)) | |
sys.exit(1) | |
current = config_data['exercises'][current_index] | |
del config_data['exercises'][current_index] | |
for k in list(current.keys()): | |
if k not in ['uuid', 'slug']: | |
del current[k] | |
current['deprecated'] = True | |
config_data['exercises'].append(current) | |
else: | |
print(USAGE) | |
sys.exit(1) | |
with open('config.json', 'w') as f: | |
f.write(json.dumps(config_data, indent=2)) | |
if __name__ == '__main__': | |
sys.exit(main(*sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment