def get_configuration(): config = DEFAULT_CONFIG configpath = expand(os.path.join("~", CONFIG_NAME)) if os.path.isfile(configpath): config.update(load_yaml(os.path.join("~", CONFIG_NAME))) external_configs = config['settings'].get('external_hooks', []) for conf in external_configs: if os.path.isfile(expand(conf)): newhooks = load_yaml(conf)['hooks'] if newhooks: config['hooks'].extend(newhooks) return config
def add_external_hook(filename, path, hooktype=DEFAULT_TYPE, enter=[], exit=[], unique=False): ''' Will add an external hook to the YAML specfified YAML file. ''' if os.path.isfile(expand(filename)): config = load_yaml(filename) if 'hooks' not in config: config['hooks'] = [] else: config = {'hooks': []} if unique and config['hooks']: removed = [] for hook in config['hooks']: if hook['path'] == path: removed.append(hook) for hook in removed: config['hooks'].remove(hook) hook = {'path': path, 'type': hooktype} if enter: hook['enter'] = enter if exit: hook['exit'] = exit config['hooks'].append(hook) save_yaml(filename, config)
def remove_external_hook(filename, path): ''' Removes all hooks for a given path. ''' if os.path.isfile(expand(filename)): config = load_yaml(filename) config['hooks'] = [h for h in config['hooks'] if h['path'] != path] save_yaml(filename, config)
def save_yaml(path, config): config = expand_config(config) config_file = open(expand(path), 'w') config_file.write(dump(config, default_flow_style=False)) config_file.close()
def load_yaml(path): config_file = open(expand(path), 'r+') config = load(config_file, Loader=Loader) config_file.close() return expand_config(config)
def expand_config(config): if config['hooks']: for hook in config['hooks']: hook['path'] = expand(hook['path']) return config