#!/usr/bin/env python3.6 """Bootstrap the application. The first argument determines the script to run - cache: Populate the cache that the worker script consumes. - adjustments: update $0 MAs. - rates: Import currency exchange rates. - checks: update $0 checks. - bvcs: Update booked_vendor_contract_snapshot table. """ import argparse import os from dotenv import find_dotenv from dotenv import load_dotenv load_dotenv(find_dotenv()) from accounting.adapters.log import getlogger # noqa E402 from accounting.bvcs import update_bvcs # noqa E402 from accounting.checkspaid import update_checkspaid # noqa E402 from accounting.data import buffered_cache_release_tracks # noqa E402 from accounting.data import cache_owner_track_counts # noqa E402 from accounting.exchange_rates import import_exchange_rates # noqa E402 from accounting.manual_adjustments import update_manual_adjustments # noqa E402 from accounting.warm_cache import cache_label_contracts # noqa E402 from accounting.warm_cache import cache_exchange_rates # noqa E402 from accounting.warm_cache import cache_owner_contracts # noqa E402 from accounting.warm_cache import cache_statements # noqa E402 from accounting.warm_cache import cache_upc_metadata # noqa E402 from accounting.statements import import_statements # noqa E402 CLI_DESCRIPTION = 'Prepare the environment for the accounting run.' COMMAND_LIST = { 'import_exchange_rates': import_exchange_rates, 'import_statements': import_statements, 'update_adjustments': update_manual_adjustments, 'update_checks': update_checkspaid, 'update_bvcs': update_bvcs, 'cache_label_contracts': cache_label_contracts, 'cache_upc_metadata': cache_upc_metadata, 'cache_exchange_rates': cache_exchange_rates, 'cache_statements': cache_statements, 'cache_owner_contracts': cache_owner_contracts, 'cache_owner_track_counts': cache_owner_track_counts, 'cache_release_tracks': buffered_cache_release_tracks, } def main(): """Parse arguments and kick off the correct script.""" args, errors = get_args() if errors: logger = getlogger() for msg in errors: logger.error(msg) quit() if args.process == 'all': for prep_function in COMMAND_LIST.values(): prep_function() return COMMAND_LIST[args.process]() def get_args(): """Declare and retrieve arguments for the script. Returns: Tuple: args (Namespace), and errors (str[]) """ parser = argparse.ArgumentParser(description=CLI_DESCRIPTION) parser.add_argument('process', type=str) args = parser.parse_args() errors = validate_args(args) return (args, errors) def validate_args(args): """Validate CLI Arguments. Ensures that - filename and process are valid. - filename references a valid file. - process exists. """ error_messages = [] if args.process != 'all' and args.process not in COMMAND_LIST.keys(): cmd_list = '\n- '.join(COMMAND_LIST.keys()) error_messages.append('Process must be in: \n- {}'.format(cmd_list)) if args.process == 'rates': filename = os.environ.get('CURRENCY_EXCHANGE_RATES_FILE') if not filename: error_messages.append('CURRENCY_EXCHANGE_RATES_FILE is required.') elif not os.path.isfile(filename): error_messages.append('File not found: {}'.format(filename)) elif args.process == 'statements': filename = os.environ.get('STATEMENTS_FILE') if not filename: error_messages.append('STATEMENTS_FILE is required.') elif not os.path.isfile(filename): error_messages.append('File not found: {}'.format(filename)) return error_messages if __name__ == '__main__': main()