Beispiel #1
0
def load_csv_and_prices(holdings_filename, prices_filename, currency):
    """Load the holdings and prices from filenames and convert to a common currency.

    Args:
      holdings_filename: A string, the name of a CSV file containing the list of Holdings.
      prices_filename: A string, the name of a Beancount file containing price directives.
      currency: A string, the target currency to convert all the holdings to.
    Returns:
      Two lists of holdings: a list in the original currencies, and a list all
      converted to the target currency.
    """
    # Load the price database.
    # Generate with "bean-query LEDGER holdings"
    price_entries, errors, options_map = loader.load(prices_filename)
    price_map = prices.build_price_map(price_entries)

    # Load the holdings list.
    # Generate with "bean-query LEDGER print_prices"
    mixed_holdings_list = list(
        holdings_reports.load_from_csv(open(holdings_filename)))

    # Convert all the amounts to a common currency (otherwise summing market
    # values makes no sense).
    holdings_list = holdings.convert_to_currency(price_map, currency,
                                                 mixed_holdings_list)

    return mixed_holdings_list, holdings_list
 def test_load_from_csv(self):
     oss = io.StringIO()
     table_ = holdings_reports.report_holdings(None, False, self.entries,
                                               self.options_map)
     table.table_to_csv(table_, file=oss)
     iss = io.StringIO(oss.getvalue())
     holdings_list = list(holdings_reports.load_from_csv(iss))
     self.assertEqual(2, len(holdings_list))
     self.assertTrue(isinstance(holdings_list, list))
     self.assertTrue(isinstance(holdings_list[0], holdings.Holding))