Example #1
0
def test_short_account_names_raise_error_when_duplicate_account_names(
        book_complex):
    # no exception
    piecash.ledger(book_complex, short_account_names=True)

    # exception as two accounts have the same short name
    book_complex.accounts[0].name = book_complex.accounts[1].name
    book_complex.flush()
    with pytest.raises(
            ValueError,
            match="You have duplicate short names in your book. "
            "You cannot use the 'short_account_names' option.",
    ):
        piecash.ledger(book_complex, short_account_names=True)
Example #2
0
def ledger(book, output):
    """Export to ledger-cli format.

    This scripts export a GnuCash BOOK to the ledget-cli format.
    """
    with piecash.open_book(book, open_if_lock=True) as data:
        output.write(piecash.ledger(data))
Example #3
0
def test_out_write(book_complex, options):
    ledger_output = piecash.ledger(book_complex, **options)

    file_name = "file_template_full" + "".join(
        f".{k}_{v}" for k, v in options.items()) + ".ledger"

    # to generate the first time the expected output of the test
    (REFERENCE / file_name).write_text(ledger_output, encoding="utf-8")

    assert ledger_output == (REFERENCE / file_name).read_text(encoding="utf-8")
Example #4
0
def ledger(book, output, locale, commodity_notes, short_account_names):
    """Export to ledger-cli format.

    This scripts export a GnuCash BOOK to the ledget-cli format.
    """
    with piecash.open_book(book, open_if_lock=True) as data:
        output.write(
            piecash.ledger(data,
                           locale=locale,
                           commodity_notes=commodity_notes,
                           short_account_names=short_account_names))
Example #5
0
 def import_transactions(self,
                         book_path,
                         csv_path,
                         dry=False,
                         verborse=True):
     with piecash.open_book(book_path, readonly=dry) as book:
         accMap = self.persobalbook.generate_account_map(book)
         with self.transactionsReader.open(csv_path,
                                           accMap) as tr_candidates:
             transactions = self.execute_transactions(accMap, tr_candidates)
         if verborse:
             book.flush()
             for transaction in transactions:
                 print(piecash.ledger(transaction))
         if not dry:
             book.save()
Example #6
0
#!/usr/local/bin/python
"""original script from https://github.com/MatzeB/pygnucash/blob/master/gnucash2ledger.py by Matthias Braun [email protected]
 adapted for:
 - python 3 support
 - new string formatting
"""
import argparse
import sys
import codecs

import piecash

if sys.version_info.major == 2:
    out = codecs.getwriter('UTF-8')(sys.stdout)
else:
    out = sys.stdout

parser = argparse.ArgumentParser(description="Generate a ledger-cli representation of a gnucash book")
parser.add_argument("gnucash_filename",
                    help="the name of the gnucash file to process")
args = parser.parse_args()

with piecash.open_book(args.gnucash_filename, open_if_lock=True) as data:
    out.write(piecash.ledger(data))
Example #7
0
 def test_out_write(self):
     with piecash.open_book(file_template_full, open_if_lock=True) as data:
         out.write(piecash.ledger(data))
Example #8
0
 def test_out_write(self):
     with piecash.open_book( file_template_full, open_if_lock=True ) as data:
         out.write(piecash.ledger(data))
Example #9
0
#!/usr/local/bin/python
"""original script from https://github.com/MatzeB/pygnucash/blob/master/gnucash2ledger.py by Matthias Braun [email protected]
 adapted for:
 - python 3 support
 - new string formatting
"""
import argparse
import sys
import codecs

import piecash

if sys.version_info.major == 2:
    out = codecs.getwriter('UTF-8')(sys.stdout)
else:
    out = sys.stdout

parser = argparse.ArgumentParser(
    description="Generate a ledger-cli representation of a gnucash book")
parser.add_argument("gnucash_filename",
                    help="the name of the gnucash file to process")
args = parser.parse_args()

with piecash.open_book(args.gnucash_filename, open_if_lock=True) as data:
    out.write(piecash.ledger(data))
    USD = mybook.currencies(mnemonic="USD")
    # define the amount as Decimal
    amount = Decimal("25.35")
    # retrieve accounts
    to_account = mybook.accounts(fullname="Expenses:Some Expense Account")
    from_account = mybook.accounts(fullname="Assets:Current Assets:Checking")
    # create the transaction with its two splits
    Transaction(
        post_date=today,
        enter_date=today,
        currency=USD,
        description="Transaction Description!",
        splits=[
            Split(account=to_account,
                  value=amount,
                  memo="Split Memo!"),
            Split(account=from_account,
                  value=-amount,
                  memo="Other Split Memo!"),
        ]
    )
    # save the book
    mybook.save()

from piecash import ledger

# check the book by exporting to ledger format
with open_book("../gnucash_books/simple_book_transaction_creation.gnucash",
               open_if_lock=True) as mybook:
    print(ledger(mybook))