Skip to content

Commit

Permalink
Merge pull request #7 from sdementen/second_working_code
Browse files Browse the repository at this point in the history
Second working code
  • Loading branch information
sdementen authored Jun 26, 2024
2 parents b1ea21a + 42b2815 commit f2dedbb
Show file tree
Hide file tree
Showing 43 changed files with 864 additions and 94 deletions.
42 changes: 0 additions & 42 deletions .github/workflows/rename_project.yml

This file was deleted.

25 changes: 23 additions & 2 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,31 @@ Changelog
=========


(unreleased)
------------
- During tests, path_schemas to point to tmp folder. [sdementen]
- Use setup.cfg instead of .flake8. [sdementen]
- Add blank in some gnucash book name for testing purposes. [sdementen]
- Mypy fixes. [sdementen]
- Add make CLI implemented in python. [sdementen]
- Fix flake8 issues. [sdementen]
- Fix linter issues. [sdementen]
- Adapt linter to use 140 charts per line. [sdementen]
- Remove the rename_project.yml CI action. [sdementen]
- Add sqlacodegen_v2 as dependency. [sdementen]
- Update setup.py with maturity and description. [sdementen]
- Add some simple tests. [sdementen]
- Add example in README.md. [sdementen]
- Add first working code. [sdementen]
- Add data folder with sample of gnucash sqlite books. [sdementen]
- Release: version 0.1.1 🚀 [sdementen]


0.1.1 (2024-06-24)
------------------
- Release: version 0.1.1 🚀 [sdementen]
- Clean up repo generated from template. [sdementen]
- Release: version 0.1.1 🚀 [Sébastien de Menten]
- Clean up repo generated from template. [Sébastien de Menten]
- ✅ Ready to clone and code. [sdementen]
- Initial commit. [sdementen]


4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ fmt: ## Format code using black & isort.
.PHONY: lint
lint: ## Run pep8, black, mypy linters.
$(ENV_PREFIX)flake8 piecash2/
$(ENV_PREFIX)black -l 79 --check piecash2/
$(ENV_PREFIX)black -l 79 --check tests/
$(ENV_PREFIX)black -l 139 --check piecash2/
$(ENV_PREFIX)black -l 139 --check tests/
$(ENV_PREFIX)mypy --ignore-missing-imports piecash2/

.PHONY: test
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![codecov](https://codecov.io/gh/sdementen/piecash2/branch/main/graph/badge.svg?token=piecash2_token_here)](https://codecov.io/gh/sdementen/piecash2)
[![CI](https://github.com/sdementen/piecash2/actions/workflows/main.yml/badge.svg)](https://github.com/sdementen/piecash2/actions/workflows/main.yml)

piecash using sqlalchemy 2
A python library to work with [GnuCash](https://www.gnucash.org/) books, a successor of the [piecash](https://github.com/sdementen/piecash) library, built on top of SQLAlchemy 2.

## Install it from PyPI

Expand All @@ -15,17 +15,17 @@ pip install piecash2
## Usage

```py
from piecash2 import BaseClass
from piecash2 import base_function
from piecash2 import open_book
BaseClass().base_method()
base_function()
```
# open the gnucash book (sqlite3 file)
Session = open_book("mybook.gnucash")
# retrieve the module
piecash = Session.module
```bash
$ python -m piecash2
#or
$ piecash2
with Session() as session:
# query all accounts in the
for account in session.query(piecash.Account).all():
print(account.name)
```

## Development
Expand Down
11 changes: 11 additions & 0 deletions data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Sample gnucash books

This folder holds a set of gnucash books used for testing, examples and debugging.


## Default empty books

The following gnucash books can be used to introspect the SQL schema:
- reference/default_2_6_21_basic.gnucash: empty gnucash books created with gnucash 2.6.21 and no specific options
- reference/default_2_6_21_full_options.gnucash: empty gnucash books created with gnucash 2.6.21 with multiple options enabled (trading accounts, ...)

Binary file added data/book_prices.gnucash
Binary file not shown.
Binary file added data/book_schtx.gnucash
Binary file not shown.
Binary file added data/complex_sample.gnucash
Binary file not shown.
Binary file added data/default_book.gnucash
Binary file not shown.
Binary file added data/empty_book.gnucash
Binary file not shown.
Binary file added data/ghost_kvp_scheduled_transaction.gnucash
Binary file not shown.
Binary file added data/investment.gnucash
Binary file not shown.
Binary file added data/invoices.gnucash
Binary file not shown.
Binary file added data/reference/2_6/default_2_6_21_basic.gnucash
Binary file not shown.
Binary file not shown.
Binary file added data/reference/3_0/default_3_0_0_basic.gnucash
Binary file not shown.
Binary file not shown.
Binary file added data/simple_sample.272.gnucash
Binary file not shown.
Binary file added data/simple_sample.gnucash
Binary file not shown.
Binary file added data/test book.gnucash
Binary file not shown.
1 change: 1 addition & 0 deletions make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@py.exe make.py %*
104 changes: 104 additions & 0 deletions make.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import os
import shutil
from pathlib import Path

import typer

os.environ["PYTHONIOENCODING"] = "utf-8"

app = typer.Typer()

HERE = Path(__file__).parent


@app.command()
def release(tag: str):
print(f"WARNING: This operation will create version {tag=} and push to github")
typer.confirm("Do you want to continue?", abort=True)
Path("piecash2/VERSION").write_text(tag)
os.system("gitchangelog > HISTORY.md")
os.startfile("HISTORY.md")
typer.confirm("Did you update the changelog?", abort=True)
os.system("git add piecash2/VERSION HISTORY.md")
os.system(f'git commit -m "release: version {tag}')
print(f"creating git tag : {tag}")
os.system(f"git tag {tag}")
os.system("git push -u origin HEAD --tags")
print("Github Actions will detect the new tag and release the new version.")


@app.command()
def lint():
"""lint: ## Run pep8, black, mypy linters."""
os.system("flake8 piecash2/")
os.system("black -l 140 --check piecash2/")
os.system("black -l 140 --check tests/")
os.system("mypy --ignore-missing-imports piecash2/")


@app.command()
def fmt():
"""fmt: ## Format code using black & isort."""
os.system("isort piecash2/")
os.system("black -l 140 piecash2/")
os.system("black -l 140 tests/")


@app.command()
def docs():
"""fmt: ## Format code using black & isort."""
os.system("mkdocs build")
os.startfile(Path(__file__).parent / "site" / "index.html")


@app.command()
def clean():
"""clean: ## Clean up unused files."""
patterns = [
"**/*.pyc",
"**/__pycache__",
"**/Thumbs.db",
"**/*~",
".cache",
".pytest_cache",
".mypy_cache",
".tox",
"build",
"dist",
"*.egg-info",
"htmlcov",
"docs/_build",
]

for fp in patterns:
for f in HERE.glob(fp):
if f.is_file():
f.unlink()
else:
shutil.rmtree(f)


@app.command()
def test():
"""test: ## Run tests and generate coverage report."""
os.system("pytest -v --cov-config .coveragerc --cov=piecash2 -l --tb=short --maxfail=1 tests/")
os.system("coverage xml")
os.system("coverage html")
os.startfile(HERE / "htmlcov" / "index.html")


@app.command()
def schema():
"""schema: ## Generate the schema from the sqlite database using sqlacodegen."""
import piecash2.schema.generation.schema_generation as schema_generation
import piecash2.schema.generated as generated

schema_generation.path_schemas = Path(generated.__file__).parent

print(f"Generating schemas in {schema_generation.path_schemas}")
for book in (HERE / "data").glob("*.gnucash"):
schema_generation.generate_schema(book, schema_generation.get_schema_name(book))


if __name__ == "__main__":
app()
2 changes: 1 addition & 1 deletion piecash2/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.1
0.1.2
5 changes: 5 additions & 0 deletions piecash2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from piecash2.schema.generation.schema_generation import add_book_module_in_path

from .core.book import open_book

__ALL__ = [open_book, add_book_module_in_path]
17 changes: 0 additions & 17 deletions piecash2/base.py

This file was deleted.

Empty file added piecash2/core/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions piecash2/core/book.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sqlite3
from pathlib import Path

import sqlalchemy
import sqlalchemy.orm

from piecash2.schema.generation.schema_generation import import_gnucash


def open_book(book, regenerate_schema=False):
if isinstance(book, str):
book = Path(book)

book_posix = book.as_posix()

# make a backup of the DB in memory
db_memory_name = f":memgeco_{abs(hash(book_posix))}:"
with sqlite3.connect(book_posix) as source:
sqliteconn = f"file:{db_memory_name}?mode=memory&cache=shared"
dest = sqlite3.connect(sqliteconn, uri=True)
source.backup(dest)

engine = sqlalchemy.create_engine(f"sqlite:///{db_memory_name}", echo=False, creator=lambda: sqlite3.connect(sqliteconn, uri=True))

Session = sqlalchemy.orm.sessionmaker(bind=engine, autoflush=True, autocommit=False)

# must execute some query otherwise future call to the Session raise error
with Session() as s:
s.execute(sqlalchemy.text(""))

Session.module = import_gnucash(book, regenerate_schema=regenerate_schema)

return Session
Empty file added piecash2/schema/__init__.py
Empty file.
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions piecash2/schema/generation/sa_schema_post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# mypy: ignore-errors
# list all glasses with a guid property (that can be linked to in Slot/Recurrence through obj_guid)
gl = globals()
name2kls = {n: gl[n] for n in klswithguid_names} # noqa: F821
Loading

0 comments on commit f2dedbb

Please sign in to comment.