Example #1
0
    def _parse(self, sheet):
        try:
            _, row_id, column_id = find_table(sheet, (
                ('3) Котировки покупки и продажи ОМС в "Сбербанк Онлайн":',),
                ("", "", "", ""),
                ("Наименование драгоценного металла", "", "Покупка, руб. за грамм", "Продажа, руб. за грамм"),
            ))
        except RowNotFoundError:
            raise Error("Unable to find rates table.")

        rates = {}

        for row_id, (currency_id, currency_name) in enumerate((
            ("AUR_SBRF", "Золото"),
            ("AGR_SBRF", "Серебро"),
            ("PTR_SBRF", "Платина"),
            ("PDR_SBRF", "Палладий"),
        ), start=row_id):
            if (
                not cmp_columns(sheet, row_id, column_id, (currency_name,)) or
                not cmp_column_types(sheet, row_id, column_id, (TEXT, EMPTY, NUMBER, NUMBER))
            ):
                raise Error("Rates table validation failed.")

            rates[currency_id] = tuple(
                Decimal(value) for value in (sheet.cell_value(row_id, column_id + 3),
                                             sheet.cell_value(row_id, column_id + 2)))

        return rates
Example #2
0
    def _parse(self, sheet):
        try:
            _, row_id, column_id = find_table(sheet, (
                ("Курсы для проведения операций покупки и продажи наличной иностранной",),
                ("валюты за наличную валюту Российской Федерации:",),
                ("Наименование валют", "", "", "Масштаб", "Курс покупки", "Курс продажи"),
            ))
        except RowNotFoundError:
            raise Error("Unable to find rates table.")

        currencies = {
            "Доллар США": "USD_SBRF",
            "Евро":       "EUR_SBRF",
        }

        rates = {}

        while row_id < sheet.nrows:
            cell = sheet.cell(row_id, column_id)
            if cell.ctype == EMPTY:
                break

            if not cmp_column_types(sheet, row_id, column_id, (TEXT, EMPTY, EMPTY, NUMBER, NUMBER, NUMBER)):
                raise Error("Rates table validation failed.")

            try:
                currency_id = currencies[cell.value.strip()]
            except KeyError:
                pass
            else:
                scale = sheet.cell_value(row_id, column_id + 3)
                if scale != 1:
                    raise Error("Got {} rate with invalid scale: {}.", currency_id, scale)

                if currency_id in rates:
                    raise Error("Got a duplicated currency rates for {}.", currency_id)

                rates[currency_id] = tuple(
                    Decimal(value) for value in reversed(sheet.row_values(row_id, column_id + 4, column_id + 6)))

            row_id += 1

        missing_currencies = set(rates.keys()) - set(currencies.values())
        if missing_currencies:
            raise Error("Unable to find rates for the following currencies: {}.", ", ".join(missing_currencies))

        return rates