def test_print_extracted_entries_duplictes(self): entries, error, options = parser.parse_string( textwrap.dedent(''' 1970-01-01 * "Test" Assets:Tests 10.00 USD 1970-01-01 * "Test" Assets:Tests 10.00 USD ''')) # Mark the second entry as duplicate entries[1].meta[extract.DUPLICATE] = True extracted = [ ('/path/to/test.csv', entries), ] output = io.StringIO() extract.print_extracted_entries(extracted, output) self.assertEqual( output.getvalue(), textwrap.dedent('''\ ;; -*- mode: beancount -*- **** /path/to/test.csv 1970-01-01 * "Test" Assets:Tests 10.00 USD ; 1970-01-01 * "Test" ; Assets:Tests 10.00 USD '''))
def test_print_extracted_entries(self): entries, error, options = parser.parse_string( textwrap.dedent(''' 1970-01-01 * "Test" Assets:Tests 10.00 USD''')) extracted = [ ('/path/to/test.csv', entries), ('/path/to/empty.pdf', []), ] output = io.StringIO() extract.print_extracted_entries(extracted, output) self.assertEqual( output.getvalue(), textwrap.dedent('''\ ;; -*- mode: beancount -*- **** /path/to/test.csv 1970-01-01 * "Test" Assets:Tests 10.00 USD **** /path/to/empty.pdf '''))
def test_print_extracted_entries(self, mock_extract_from_file): entries, _, __ = parser.parse_string(""" 2016-02-01 * "A" Assets:Account1 11.11 USD Assets:Account2 -11.11 USD 2016-02-01 * "B" Assets:Account1 22.22 USD Assets:Account3 -22.22 USD """) mock_extract_from_file.return_value = entries entries[-2].meta[extract.DUPLICATE_META] = True oss = io.StringIO() extract.print_extracted_entries(entries, oss) self.assertEqual( textwrap.dedent("""\ ; 2016-02-01 * "A" ; Assets:Account1 11.11 USD ; Assets:Account2 -11.11 USD 2016-02-01 * "B" Assets:Account1 22.22 USD Assets:Account3 -22.22 USD """).strip(), oss.getvalue().strip())
def _extract(ctx, src, output, existing, reverse, failfast, quiet): """Extract transactions from documents. Walk the SRC list of files or directories and extract the ledger entries from each file identified by one of the configured importers. The entries are written to the specified output file or to the standard output in Beancount ledger format in sections associated to the source document. """ verbosity = -quiet log = utils.logger(verbosity, err=True) errors = exceptions.ExceptionsTrap(log) # Load the ledger, if one is specified. existing_entries = loader.load_file(existing)[0] if existing else [] extracted = [] for filename in _walk(src, log): with errors: importer = identify.identify(ctx.importers, filename) if not importer: log('') # Newline. continue # Signal processing of this document. log(' ...', nl=False) # Extract entries. entries = extract.extract_from_file(importer, filename, existing_entries) extracted.append((filename, entries)) log(' OK', fg='green') if failfast and errors: break # Invoke hooks. hooks = [extract.find_duplicate_entries ] if ctx.hooks is None else ctx.hooks for func in hooks: extracted = func(extracted, existing_entries) # Reverse sort order, if requested. if reverse: for filename, entries in extracted: entries.reverse() # Serialize entries. extract.print_extracted_entries(extracted, output) if errors: sys.exit(1)