Example #1
0
    def price(self, filename, lineno, date, currency, amount, kvlist):
        """Process a price directive.

        Args:
          filename: the current filename.
          lineno: the current line number.
          date: a datetime object.
          currency: the currency to be priced.
          amount: an instance of Amount, that is the price of the currency.
          kvlist: a list of KeyValue instances.
        Returns:
          A new Price object.
        """
        meta = new_metadata(filename, lineno, kvlist)
        return Price(meta, date, currency, amount)
Example #2
0
    def extract(self, file: _FileMemo, existing_entries=None) -> List[Price]:
        commodity = re.sub(r'\.price$', '', path.basename(file.name))
        prices = []
        entry = 1

        with open(file.name) as csv_file:
            reader = csv.DictReader(csv_file, delimiter=',')

            for row in reader:
                label = row['Label']
                symbol = row['Symbol']

                response = get_prices(label, symbol)

                dates = response['Dates']
                elements = response['Elements']

                parsed_dates = parse_dates(dates)

                for element in elements:
                    series = element['ComponentSeries']
                    close = find_series_by_type('Close', series)

                    if close is not None:
                        values = close['Values']

                        for idx, value in enumerate(values):
                            price = Price(meta=new_metadata(file.name, entry),
                                          date=parsed_dates[idx],
                                          currency=commodity,
                                          amount=Amount(
                                              D('{0:.3f}'.format(value)),
                                              self.currency))

                            prices.append(price)
                            entry += 1

        return prices
Example #3
0
    def prepare(self, journal: JournalEditor, results: SourceResults):
        account_to_id, id_to_account = description_based_source.get_account_mapping(
            journal.accounts, 'healthequity_account_id')

        def convert_account(entry: RawEntry):
            account_id = entry.account
            if isinstance(entry, CashTransaction):
                suffix = 'Cash'
            else:
                suffix = entry.units.currency
            full_account = id_to_account[account_id] + ':' + suffix
            account_to_id[full_account] = account_id
            return entry._replace(account=full_account)

        balances = [convert_account(entry) for entry in self.raw_balances]
        transactions = [
            convert_account(entry) for entry in self.raw_transactions
        ]

        description_based_source.get_pending_and_invalid_entries(
            raw_entries=transactions,
            journal_entries=journal.all_entries,
            account_set=account_to_id.keys(),
            get_key_from_posting=get_key_from_posting,
            get_key_from_raw_entry=get_key_from_raw_entry,
            make_import_result=lambda x: make_import_result(
                x,
                accounts=journal.accounts,
                account_to_id=account_to_id,
                id_to_account=id_to_account),
            results=results)

        balance_entries = collections.OrderedDict(
        )  # type: Dict[Tuple[datetime.date, str, str], ImportResult]

        for entry in transactions:
            date = entry.date + datetime.timedelta(days=1)
            balance_entries[(date, entry.account,
                             entry.balance.currency)] = ImportResult(
                                 date=date,
                                 entries=[
                                     Balance(date=date,
                                             meta=None,
                                             account=entry.account,
                                             amount=entry.balance,
                                             tolerance=None,
                                             diff_amount=None)
                                 ],
                                 info=get_info(entry))

        for entry in balance_entries.values():
            results.add_pending_entry(entry)

        for balance in balances:
            # Skip outputting recent balances --- just output prices.
            # All transactions provide a balance.

            # output.append(
            #     ImportResult(
            #         date=balance.date,
            #         entries=[Balance(
            #             date=balance.date,
            #             meta=None,
            #             account=balance.account,
            #             amount=balance.units,
            #             tolerance=None,
            #             diff_amount=None)]))
            results.add_pending_entry(
                ImportResult(date=balance.date,
                             info=get_info(balance),
                             entries=[
                                 Price(date=balance.date,
                                       meta=None,
                                       currency=balance.units.currency,
                                       amount=balance.price)
                             ]))
Example #4
0
 def get_directive(self) -> Price:
     return Price(meta=None,
                  date=self.date,
                  currency=self.symbol,
                  amount=self.price)