Source code for caspia.toolbox.cli.paths

import os
import pathlib

import click


[docs]def config_option(default=None): def decorator(f): def get_default(): global_storage = os.environ.get('CSP_CONFIG_DIR', None) if default and global_storage: return pathlib.Path(global_storage) / default else: raise click.MissingParameter('config location not set') def map_value(ctx, param, val): return pathlib.Path(val) if default: helpstr = f'Path to configuration. Defaults to $CSP_CONFIG_DIR/{default}' else: helpstr = f'Path to configuration.' return click.option('--config', default=get_default, callback=map_value, help=helpstr)(f) return decorator
[docs]def storage_option(default=None): def decorator(f): def get_default(): global_storage = os.environ.get('CSP_STORAGE_DIR', None) if default and global_storage: return pathlib.Path(global_storage) / default else: raise click.MissingParameter('storage location not set') if default: helpstr = f'Path to storage directory. Defaults to $CSP_STORAGE_DIR/{default}' else: helpstr = f'Path to storage directory.' def map_value(ctx, param, val): path = pathlib.Path(val) os.makedirs(path, exist_ok=True) return path return click.option('--storage', default=get_default, callback=map_value, help=helpstr)(f) return decorator