Example #1
0
    def test_ofxWriter(self) -> None:

        # Create sample statement:
        statement = Statement("BID", "ACCID", "LTL")
        statement.broker_id = "BROKERID"
        statement.end_date = datetime(2021, 5, 1)

        invest_line = InvestStatementLine(
            "3",
            datetime(2021, 1, 1),
            "Sample 3",
            "BUYSTOCK",
            "BUY",
            "AAPL",
            Decimal("-416.08"),
        )
        invest_line.units = Decimal("3")
        invest_line.unit_price = Decimal("138.28")
        invest_line.fees = Decimal("1.24")
        invest_line.assert_valid()
        statement.invest_lines.append(invest_line)

        invest_line = InvestStatementLine(
            "4",
            datetime(2021, 1, 1),
            "Sample 4",
            "SELLSTOCK",
            "SELL",
            "MSFT",
            Decimal("1127.87"),
        )
        invest_line.units = Decimal("-5")
        invest_line.unit_price = Decimal("225.63")
        invest_line.fees = Decimal("0.28")
        invest_line.assert_valid()
        statement.invest_lines.append(invest_line)

        invest_line = InvestStatementLine(
            "5",
            datetime(2021, 1, 1),
            "Sample 5",
            "INCOME",
            "DIV",
            "MSFT",
            Decimal("0.79"),
        )
        invest_line.fees = Decimal("0.5")
        invest_line.assert_valid()
        statement.invest_lines.append(invest_line)

        # Create writer:
        writer = ofx.OfxWriter(statement)

        # Set the generation time so it is always predictable
        writer.genTime = datetime(2021, 5, 1, 0, 0, 0)

        assert prettyPrint(writer.toxml()) == SIMPLE_OFX
Example #2
0
def convert(args: argparse.Namespace) -> int:
    appui = ui.UI()
    config = configuration.read()

    if config is None:
        # No configuration mode
        settings = {}
        pname = args.type
    else:
        # Configuration is loaded
        if args.type not in config:
            log.error("No section '%s' in config file." % args.type)
            log.error(
                "Edit configuration using ofxstatement edit-config and "
                "add section [%s]." % args.type
            )
            return 1  # error

        settings = dict(config[args.type])

        pname = settings.get("plugin", None)
        if not pname:
            log.error("Specify 'plugin' setting for section [%s]" % args.type)
            return 1  # error

    # pick and configure plugin
    try:
        p = plugin.get_plugin(pname, appui, settings)
    except plugin.PluginNotRegistered:
        log.error("No plugin named '%s' is found" % pname)
        return 1  # error

    # process the input and produce output
    parser = p.get_parser(args.input)
    try:
        statement = parser.parse()
    except exceptions.ParseError as e:
        log.error("Parse error on line %s: %s" % (e.lineno, e.message))
        return 2  # parse error

    # Validate the statement
    try:
        statement.assert_valid()
    except exceptions.ValidationError as e:
        log.error("Statement validation error: %s" % (e.message))
        return 2  # Validation error

    with smart_open(args.output, settings.get("encoding", None)) as out:
        writer = ofx.OfxWriter(statement)
        out.write(writer.toxml())

    log.info("Conversion completed: %s" % args.input)
    return 0  # success
Example #3
0
    def test_ofxWriter(self):

        # Create sample statement:
        statement = Statement("BID", "ACCID", "LTL")
        statement.lines.append(StatementLine(
            "1", datetime(2012, 2, 12), "Sample 1", 15.4))
        line = StatementLine("2", datetime(2012, 2, 12), "Sample 2", 25.0)
        line.payee = ''
        line.bank_account_to = BankAccount("SNORAS", "LT1232")
        line.bank_account_to.branch_id = "VNO"
        statement.lines.append(line)

        # Create writer:
        writer = ofx.OfxWriter(statement)

        # Set the generation time so it is always predictable
        writer.genTime = datetime(2012, 3, 3, 0, 0, 0)

        assert prettyPrint(writer.toxml()) == SIMPLE_OFX
Example #4
0
    def test_ofxWriter(self) -> None:

        # Create sample statement:
        statement = Statement("BID", "ACCID", "LTL")
        statement.lines.append(
            StatementLine("1", datetime(2012, 2, 12), "Sample 1",
                          Decimal("15.4")))
        line = StatementLine("2", datetime(2012, 2, 12), "Sample 2",
                             Decimal("25.0"))
        line.payee = ""
        line.bank_account_to = BankAccount("SNORAS", "LT1232")
        line.bank_account_to.branch_id = "VNO"
        line.currency = Currency("USD")
        line.orig_currency = Currency("EUR", Decimal("3.4543"))
        statement.lines.append(line)

        # Create writer:
        writer = ofx.OfxWriter(statement)

        # Set the generation time so it is always predictable
        writer.genTime = datetime(2012, 3, 3, 0, 0, 0)

        assert prettyPrint(writer.toxml()) == SIMPLE_OFX