def test_load_cache_override_filename_pattern(self): orig_load_file = loader._load_file prev_env = os.getenv('BEANCOUNT_LOAD_CACHE_FILENAME') os.environ['BEANCOUNT_LOAD_CACHE_FILENAME'] = '__{filename}__' loader.initialize() try: with test_utils.tempdir() as tmp: test_utils.create_temporary_files( tmp, { 'apples.beancount': """ 2014-01-01 open Assets:Apples """ }) filename = path.join(tmp, 'apples.beancount') entries, errors, options_map = loader.load_file(filename) self.assertEqual({'__apples.beancount__', 'apples.beancount'}, set(os.listdir(tmp))) finally: # Restore pre-test values. loader._load_file = orig_load_file if prev_env is None: del os.environ['BEANCOUNT_LOAD_CACHE_FILENAME'] else: os.environ['BEANCOUNT_LOAD_CACHE_FILENAME'] = prev_env
def main(filename: str, verbose: bool, no_cache: bool, cache_filename: str, auto: bool): """Parse, check and realize a beancount ledger. This also measures the time it takes to run all these steps. """ use_cache = not no_cache # Insert auto plugins. This is convenient for importers because when # generating a subset of transactions oftentimes we don't have the # contextual account and commodity creation routines. See {4ec6a3205b6c}. if auto: loader.PLUGINS_AUTO.extend(loader.DEFAULT_PLUGINS_AUTO) if verbose: logging.basicConfig(level=logging.INFO, format='%(levelname)-8s: %(message)s') # Override loader caching setup. if not use_cache or cache_filename: loader.initialize(use_cache, cache_filename) with misc_utils.log_time('beancount.loader (total)', logging.info): # Load up the file, print errors, checking and validation are invoked # automatically. entries, errors, _ = loader.load_file( filename, log_timings=logging.info, log_errors=sys.stderr, # Force slow and hardcore validations, just for check. extra_validations=validation.HARDCORE_VALIDATIONS) # Exit with an error code if there were any errors. sys.exit(1 if errors else 0)
def main(filename, verbose, no_cache, cache_filename): """Parse, check and realize a beancount ledger. This also measures the time it takes to run all these steps. """ use_cache = not no_cache if verbose: logging.basicConfig(level=logging.INFO, format='%(levelname)-8s: %(message)s') # Override loader caching setup. if not use_cache or cache_filename: loader.initialize(use_cache, cache_filename) with misc_utils.log_time('beancount.loader (total)', logging.info): # Load up the file, print errors, checking and validation are invoked # automatically. entries, errors, _ = loader.load_file( filename, log_timings=logging.info, log_errors=sys.stderr, # Force slow and hardcore validations, just for check. extra_validations=validation.HARDCORE_VALIDATIONS) # Exit with an error code if there were any errors. sys.exit(1 if errors else 0)
def test_load_cache_override_filename_pattern_by_argument(self): with test_utils.tempdir() as tmp: cache_filename = path.join(tmp, "__{filename}__") loader.initialize(use_cache=True, cache_filename=cache_filename) test_utils.create_temporary_files( tmp, { 'apples.beancount': """ 2014-01-01 open Assets:Apples """ }) filename = path.join(tmp, 'apples.beancount') entries, errors, options_map = loader.load_file(filename) self.assertEqual({'__apples.beancount__', 'apples.beancount'}, set(os.listdir(tmp)))
def test_load_cache_override_filename_pattern_by_env_var(self): with test_utils.environ('BEANCOUNT_LOAD_CACHE_FILENAME', '__{filename}__'): loader.initialize(use_cache=True) with test_utils.tempdir() as tmp: test_utils.create_temporary_files( tmp, { 'apples.beancount': """ 2014-01-01 open Assets:Apples """ }) filename = path.join(tmp, 'apples.beancount') entries, errors, options_map = loader.load_file(filename) self.assertEqual({'__apples.beancount__', 'apples.beancount'}, set(os.listdir(tmp)))
def main(): parser = version.ArgumentParser(description=__doc__) parser.add_argument('filename', help='Beancount input filename.') parser.add_argument('-v', '--verbose', action='store_true', help='Print timings.') # Note: These are useful during development. We need to devise a global # mechanism that will work from all the invocation programs, embedded in the # loader. parser.add_argument('-C', '--no-cache', action='store_false', dest='use_cache', default=True, help='Disable the cache from the command-line.') parser.add_argument('--cache-filename', action='store', help='Override the name of the cache') opts = parser.parse_args() if opts.verbose: logging.basicConfig(level=logging.INFO, format='%(levelname)-8s: %(message)s') # Override loader caching setup if disabled or if the filename is # overridden. if not opts.use_cache or opts.cache_filename: loader.initialize(opts.use_cache, opts.cache_filename) with misc_utils.log_time('beancount.loader (total)', logging.info): # Load up the file, print errors, checking and validation are invoked # automatically. entries, errors, _ = loader.load_file( opts.filename, log_timings=logging.info, log_errors=sys.stderr, # Force slow and hardcore validations, just for check. extra_validations=validation.HARDCORE_VALIDATIONS) # Exit with an error code if there were any errors, so this can be used in a # shell conditional. return 1 if errors else 0
def test_load_cache_disable(self): with test_utils.tempdir() as tmp: cache_filename = path.join(tmp, "__{filename}__") for kwargs in [ dict(use_cache=False), dict(use_cache=False, cache_filename=cache_filename) ]: loader.initialize(**kwargs) test_utils.create_temporary_files( tmp, { 'apples.beancount': """ 2014-01-01 open Assets:Apples """ }) filename = path.join(tmp, 'apples.beancount') entries, errors, options_map = loader.load_file(filename) self.assertEqual({'apples.beancount'}, set(os.listdir(tmp)))
def main(filename: str, verbose: bool, no_cache: bool, cache_filename: str, auto: bool): """Parse, check and realize a beancount ledger. This also measures the time it takes to run all these steps. """ use_cache = not no_cache try: if auto: # Insert auto plugins. This is convenient for importers # because when generating a subset of transactions # oftentimes we don't have the contextual account and # commodity creation routines. See {4ec6a3205b6c}. old_plugins_auto = loader.PLUGINS_AUTO[:] loader.PLUGINS_AUTO.extend(loader.DEFAULT_PLUGINS_AUTO) if verbose: logging.basicConfig(level=logging.INFO, format='%(levelname)-8s: %(message)s') # Override loader caching setup. if not use_cache or cache_filename: loader.initialize(use_cache, cache_filename) with misc_utils.log_time('beancount.loader (total)', logging.info): # Load up the file, print errors, checking and validation # are invoked automatically. entries, errors, _ = loader.load_file( filename, log_timings=logging.info, log_errors=sys.stderr, # Force slow and hardcore validations, just for check. extra_validations=validation.HARDCORE_VALIDATIONS) finally: if auto: # Remove auto plugins. This is not necessary when this # code is run as script but it is needed when run as part # of the test suite (which does not span a new Python # interpreter for each script invocation test). loader.PLUGINS_AUTO[:] = old_plugins_auto # Exit with an error code if there were any errors. sys.exit(1 if errors else 0)