def do_context(filename, args): """Describe the context that a particular transaction is applied to. Args: filename: A string, which consists in the filename. args: A tuple of the rest of arguments. We're expecting the first argument to be a string which contains either a lineno integer or a filename:lineno combination (which can be used if the location is not in the top-level file). """ from beancount.parser import context from beancount import loader # Check we have the required number of arguments. if len(args) != 1: raise SystemExit("Missing line number argument.") # Load the input files. entries, errors, options_map = loader.load_file(filename) # Parse the arguments, get the line number. match = re.match(r"(.+):(\d+)$", args[0]) if match: search_filename = path.abspath(match.group(1)) lineno = int(match.group(2)) elif re.match(r"(\d+)$", args[0]): # Note: Make sure to use the absolute filename used by the parser to # resolve the file. search_filename = options_map['filename'] lineno = int(args[0]) else: raise SystemExit("Invalid format for location.") str_context = context.render_file_context(entries, options_map, search_filename, lineno) sys.stdout.write(str_context)
def context(filename, location): """Describe transaction context. The transaction is looked up in ledger FILENAME at LOCATION. The LOCATION argument is either a line number or a filename:lineno tuple to indicate a location in a ledger included from the main input file. """ search_filename, lineno = location if search_filename is None: search_filename = filename # Load the input files. entries, errors, options_map = loader.load_file(filename) str_context = render_file_context(entries, options_map, search_filename, lineno) sys.stdout.write(str_context)
def test_context(self, entries, errors, options_map): """ plugin "beancount.plugins.implicit_prices" 2012-01-01 open Assets:US:ETrade:Cash USD 2012-01-01 open Assets:US:ETrade:ITOT ITOT 2012-01-01 open Assets:US:ETrade:GLD GLD 2012-01-01 open Income:US:ETrade:Gains USD 2012-01-01 open Expenses:Financial:Commissions USD 2012-07-30 * "Buy shares of ITOT" Assets:US:ETrade:Cash -343.35 USD Assets:US:ETrade:ITOT 2.00 ITOT {167.20 USD} Expenses:Financial:Commissions 8.95 USD 2012-08-31 * "Buy shares of GLD" Assets:US:ETrade:Cash -784.06 USD Assets:US:ETrade:GLD 7.00 GLD {110.73 USD} Expenses:Financial:Commissions 8.95 USD 2012-08-31 * "Buy shares of ITOT" Assets:US:ETrade:Cash -701.39 USD Assets:US:ETrade:ITOT 4.00 ITOT {173.11 USD} Expenses:Financial:Commissions 8.95 USD 2012-10-13 * "Buy shares of ITOT" Assets:US:ETrade:Cash -2,337.77 USD Assets:US:ETrade:ITOT 13.00 ITOT {179.14 USD} Expenses:Financial:Commissions 8.95 USD 2013-02-01 * "Sell shares of ITOT" Assets:US:ETrade:ITOT -13.00 ITOT {179.14 USD} @ 186.21 USD Assets:US:ETrade:Cash 2,411.78 USD Expenses:Financial:Commissions 8.95 USD Income:US:ETrade:Gains -91.91 USD 2013-02-07 * "Buy shares of ITOT" Assets:US:ETrade:Cash -1,126.21 USD Assets:US:ETrade:ITOT 6.00 ITOT {186.21 USD} ;; * Expenses:Financial:Commissions 8.95 USD 2013-02-07 * "Buy shares of GLD" Assets:US:ETrade:Cash -1,287.70 USD Assets:US:ETrade:GLD 11.00 GLD {116.25 USD} Expenses:Financial:Commissions 8.95 USD """ self.assertFalse(errors) search_filename = entries[0].meta["filename"] search_lineno = entries[-3].meta["lineno"] + 2 str_context = context.render_file_context(entries, options_map, search_filename, search_lineno) print(str_context) self.assertLines( textwrap.dedent(""" ** Transaction Id -------------------------------- Hash:a4d4f63da17fc113b6b3b902a7dbc6a7 Location: <string>:36 ** Balances before transaction -------------------------------- Assets:US:ETrade:Cash -1754.79 USD Assets:US:ETrade:ITOT 2.00 ITOT {167.20 USD, 2012-07-30} Assets:US:ETrade:ITOT 4.00 ITOT {173.11 USD, 2012-08-31} Expenses:Financial:Commissions 44.75 USD ** Average Costs -------------------------------- Assets:US:ETrade:ITOT 6.00 ITOT {171.14 USD, 2012-07-30} ** Unbooked Transaction -------------------------------- 2013-02-07 * "Buy shares of ITOT" Assets:US:ETrade:Cash -1126.21 USD ; -1126.21 USD Assets:US:ETrade:ITOT 6.00 ITOT {186.21 USD, 2013-02-07} ; 1117.2600 USD Expenses:Financial:Commissions 8.95 USD ; 8.95 USD ** Transaction -------------------------------- 2013-02-07 * "Buy shares of ITOT" Assets:US:ETrade:Cash -1126.21 USD ; -1126.21 USD Assets:US:ETrade:ITOT 6.00 ITOT {186.21 USD, 2013-02-07} ; 1117.2600 USD Expenses:Financial:Commissions 8.95 USD ; 8.95 USD ** Residual and Tolerances -------------------------------- Tolerances: ITOT=0.005, USD=0.005 Basis: (1117.2600 USD) ** Balances after transaction -------------------------------- * Assets:US:ETrade:Cash -2881.00 USD Assets:US:ETrade:ITOT 2.00 ITOT {167.20 USD, 2012-07-30} Assets:US:ETrade:ITOT 4.00 ITOT {173.11 USD, 2012-08-31} * Assets:US:ETrade:ITOT 6.00 ITOT {186.21 USD, 2013-02-07} * Expenses:Financial:Commissions 53.70 USD """), str_context)
def test_context(self, entries, errors, options_map): """ plugin "beancount.plugins.implicit_prices" 2012-01-01 open Assets:US:ETrade:Cash USD 2012-01-01 open Assets:US:ETrade:ITOT ITOT 2012-01-01 open Assets:US:ETrade:GLD GLD 2012-01-01 open Income:US:ETrade:Gains USD 2012-01-01 open Expenses:Financial:Commissions USD 2012-08-31 * "Buy shares of GLD" Assets:US:ETrade:Cash -784.06 USD Assets:US:ETrade:GLD 7.00 GLD {110.73 USD} Expenses:Financial:Commissions 8.95 USD 2012-08-31 * "Buy shares of ITOT" Assets:US:ETrade:Cash -701.39 USD Assets:US:ETrade:ITOT 4.00 ITOT {173.11 USD} Expenses:Financial:Commissions 8.95 USD 2012-10-13 * "Buy shares of ITOT" Assets:US:ETrade:Cash -2,337.77 USD Assets:US:ETrade:ITOT 13.00 ITOT {179.14 USD} Expenses:Financial:Commissions 8.95 USD 2013-02-01 * "Sell shares of ITOT" Assets:US:ETrade:ITOT -13.00 ITOT {179.14 USD} @ 186.21 USD Assets:US:ETrade:Cash 2,411.78 USD Expenses:Financial:Commissions 8.95 USD Income:US:ETrade:Gains -91.91 USD 2013-02-07 * "Buy shares of ITOT" Assets:US:ETrade:Cash -1,126.21 USD Assets:US:ETrade:ITOT 6.00 ITOT {186.21 USD} ;; * Expenses:Financial:Commissions 8.95 USD 2013-02-07 * "Buy shares of GLD" Assets:US:ETrade:Cash -1,287.70 USD Assets:US:ETrade:GLD 11.00 GLD {116.25 USD} Expenses:Financial:Commissions 8.95 USD """ self.assertFalse(errors) search_filename = entries[0].meta["filename"] search_lineno = entries[-3].meta["lineno"] + 2 str_context = context.render_file_context(entries, options_map, search_filename, search_lineno) self.assertLines( textwrap.dedent(""" Hash:a62bcf48b818459f06c6d7f9b78029b4 Location: <string>:31 ------------ Balances before transaction Assets:US:ETrade:Cash -1411.44 USD Assets:US:ETrade:ITOT 4.00 ITOT {173.11 USD, 2012-08-31} Expenses:Financial:Commissions 35.80 USD ------------ Transaction 2013-02-07 * "Buy shares of ITOT" Assets:US:ETrade:Cash -1126.21 USD ; -1126.21 USD Assets:US:ETrade:ITOT 6.00 ITOT {186.21 USD, 2013-02-07} ; 1117.2600 USD Expenses:Financial:Commissions 8.95 USD ; 8.95 USD Tolerances: ITOT=0.005, USD=0.005 Basis: (1117.2600 USD) ------------ Balances after transaction * Assets:US:ETrade:Cash -2537.65 USD Assets:US:ETrade:ITOT 4.00 ITOT {173.11 USD, 2012-08-31} * Assets:US:ETrade:ITOT 6.00 ITOT {186.21 USD, 2013-02-07} * Expenses:Financial:Commissions 44.75 USD """), str_context)