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.reports 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 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 an integer as a string. """ from beancount.reports import context from beancount import loader # Parse the arguments, get the line number. if len(args) != 1: raise SystemExit("Missing line number argument.") lineno = int(args[0]) # Load the input file. entries, errors, options_map = loader.load_file(filename) # Note: Make sure to use the absolute filename used by the parser to resolve # the file. str_context = context.render_file_context(entries, options_map, options_map['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-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)