Ejemplo n.º 1
0
    def test_example(self):
        """
        Test converted example with hledger
        """
        with tempfile.NamedTemporaryFile("w",
                                         suffix=".beancount",
                                         encoding="utf-8") as beanfile:
            # Generate an example Beancount file.
            example.write_example_file(
                datetime.date(1980, 1, 1),
                datetime.date(2010, 1, 1),
                datetime.date(2014, 1, 1),
                reformat=True,
                file=beanfile,
            )
            beanfile.flush()

            # Convert the file to HLedger format.
            #
            # Note: don't bother parsing for now, just a smoke test to make sure
            # we don't fail on run.
            with tempfile.NamedTemporaryFile("w",
                                             suffix=".hledger") as lgrfile:
                result = beancount2ledger.convert_file(beanfile.name,
                                                       "hledger")
                lgrfile.write(result)
                lgrfile.flush()
Ejemplo n.º 2
0
def cli():
    """
    Main function for CLI access.
    """

    if "hledger" in sys.argv[0]:
        default = "hledger"
    else:
        default = "ledger"

    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "-f",
        "--format",
        dest="format",
        action="store",
        choices=("ledger", "hledger"),
        default=default,
        help=f"output format (default: {default})",
    )
    parser.add_argument("file", help="beancount file", type=str)
    parser.add_argument("-c",
                        "--config",
                        help="config file",
                        type=argparse.FileType("r"))
    parser.add_argument(
        "-V",
        "--version",
        action="version",
        version=f"%(prog)s {beancount2ledger.__version__}",
    )

    with contextlib.ExitStack() as stack:
        args = parser.parse_args()
        in_file = args.file

        # beancount loader does not accept file objects, hence to support reading from
        # stdin we write its content to a tempfile and call the loader on its path
        if in_file == "-":
            tmpfile = stack.enter_context(
                NamedTemporaryFile(
                    prefix="tmp.beancount2ledger.",
                    mode="w",
                    encoding=locale.getpreferredencoding(),
                ))
            tmpfile.write(sys.stdin.read())
            tmpfile.flush()
            in_file = tmpfile.name

        config = get_config(args.config)
        output = beancount2ledger.convert_file(in_file,
                                               args.format,
                                               config=config)
        print(output)
Ejemplo n.º 3
0
    def test_example(self):
        with tempfile.NamedTemporaryFile('w',
                                         suffix='.beancount',
                                         encoding='utf-8') as beanfile:
            # Generate an example Beancount file.
            example.write_example_file(datetime.date(1980, 1, 1),
                                       datetime.date(2010, 1, 1),
                                       datetime.date(2014, 1, 1),
                                       reformat=True,
                                       file=beanfile)
            beanfile.flush()

            # Convert the file to Ledger format.
            with tempfile.NamedTemporaryFile('w', suffix='.ledger') as lgrfile:
                result = beancount2ledger.convert_file(beanfile.name)
                lgrfile.write(result)
                lgrfile.flush()

                # FIXME: Use a proper temp dir.
                shutil.copyfile(lgrfile.name, '/tmp/test.ledger')
                self.check_parses_ledger(lgrfile.name)
Ejemplo n.º 4
0
def cli():
    """
    Main function for CLI access.
    """

    if "hledger" in sys.argv[0]:
        default = "hledger"
    else:
        default = "ledger"

    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('-f',
                        "--format",
                        dest="format",
                        action="store",
                        choices=("ledger", "hledger"),
                        default=default,
                        help=f"output format (default: {default})")
    parser.add_argument('file',
                        help='beancount file',
                        type=argparse.FileType('r'))
    parser.add_argument('-c',
                        '--config',
                        help='config file',
                        type=argparse.FileType('r'))
    parser.add_argument('-V',
                        "--version",
                        action="version",
                        version=f"%(prog)s {beancount2ledger.__version__}")

    args = parser.parse_args()
    config = get_config(args.config)
    output = beancount2ledger.convert_file(args.file.name,
                                           args.format,
                                           config=config)
    print(output)