Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
MRG: Merge pull request #51 from cortadocodes/feature-dockerise-package
Browse files Browse the repository at this point in the history
Feature: Dockerise package
  • Loading branch information
Marcus Lugg authored May 14, 2019
2 parents 6f2371f + 31041be commit 4c6c78d
Show file tree
Hide file tree
Showing 24 changed files with 410 additions and 650 deletions.
51 changes: 22 additions & 29 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,58 +1,51 @@
version: 2.1
executors:
docker-python:
docker:
- image: circleci/python:3.7

jobs:

linting:
executor: docker-python
docker:
- image: circleci/python:3.7
steps:
- checkout
- run:
name: Run code linting
command: |
python3 -m venv venv
. venv/bin/activate
pip install flake8
mkdir lint_reports
flake8 --output-file lint_reports/flake8 --count
- store_artifacts:
path: lint_reports
pip install flake8 | cat && test ${PIPESTATUS} -eq 0
flake8 --count
testing:
executor: docker-python
docker:
- image: circleci/python:3.7
steps:
- checkout
- restore_cache:
keys:
- dependencies-{{ .Branch }}-{{ checksum "setup.py" }}
- dependencies-
- setup_remote_docker
- run:
name: Install package
name: Pull and start postgres container
command: |
POSTGRES_DB=postgres POSTGRES_USER=postgres POSTGRES_PASSWORD=password \
docker run -d -p 5433:5432 --name postgres-finances-automation postgres:11
- run:
name: Install app
command: |
python3 -m venv venv
. venv/bin/activate
pip install .[develop] | cat && test ${PIPESTATUS} -eq 0
- save_cache:
paths:
- ./venv
key: dependencies-{{ .Branch }}-{{ checksum "setup.py" }}
pip3 install -e .[development] | cat && test ${PIPESTATUS} -eq 0
- run:
name: Run tests
command: |
. venv/bin/activate
mkdir test-reports
pytest \
finances_automation/tests/ \
--cov=finances_automation \
--cov-report html:test-reports/coverage_html \
--junitxml=test-reports/junit.xml
. venv/bin/activate
POSTGRES_PASSWORD=password POSTGRES_HOST=$DOCKER_HOST \
pytest finances_automation/tests/ \
--ignore=finances_automation/tests/test_repositories.py \
--junit-xml=test-reports/junit.xml \
--cov=finances-automation \
--cov-report html
- store_test_results:
path: test-reports
- store_artifacts:
path: test-reports

workflows:
version: 2
Expand Down
18 changes: 12 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
api-docs/
data/
htmlcov/
test-reports/
venv/

.idea/
.ipynb_checkpoints/

.coverage
.DS_Store
.env

*__pycache__/

*.egg-info/
.idea
.ipynb_checkpoints/
*.pytest_cache/
*__pycache__/
.coverage
htmlcov/
*.log

*.log
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,56 @@
# Finances automation

[![CircleCI](https://circleci.com/gh/cortadocodes/finances-automation.svg?style=svg)](https://circleci.com/gh/cortadocodes/finances-automation)
Docker repository: [cortadocodes/finances-automation](https://cloud.docker.com/repository/docker/cortadocodes/finances-automation)

[![CircleCI](https://circleci.com/gh/cortadocodes/finances-automation/tree/master.svg?style=svg)](https://circleci.com/gh/cortadocodes/finances-automation/tree/master)

## Installation
### Pull and start the database container
To pull and start the persistent `postgres` database container, run
```bash
POSTGRES_DB=postgres POSTGRES_USER=postgres POSTGRES_PASSWORD=<password> \
docker run -d -p 5433:5432 \
-v <absolute_path_to_desired_data_location_on_host>:/var/lib/postgresql/data \
--name postgres-finances-automation postgres:11
```
where `<password>` is your choice of password for the database, and `<absolute_path_to_desired_data_location_on_host>`
is the location in which to store the database cluster on your machine. The database password can be stored locally by
setting it as an environment variable, or declared with each command run.

After the database has been pulled and started for the first time, it can be stopped by
```bash
docker stop postgres-finances-automation
```
and started again by
```bash
docker start postgres-finances-automation
```
The database container must be running for any of the below commands to work properly.

### Install the package
```bash
pip3 install -e .[development]
```
or
```bash
pip3 install setup.py
```

## Configuration/initialisation
The configuration for the database, tables, categories and more is included in `finances_automation/configuration.py`.
To initialise the database with the tables from the configuration file, run
```bash
python3 finances_automation/initialise_database.py
```
from the repository root.

## Usage
From the repository root and with the `POSTGRES_PASSWORD` set, run
```bash
finances-automation --help
```

The output will look like this:
```
usage: finances-automation [-h] {parse,categorise,analyse,view_latest} ...
Expand All @@ -19,6 +67,12 @@ Subcommands:
view_latest View the latest entries to a table.
```

## Running tests
From the repository root, run:
```bash
pytest
```

## Aims
The aim of this project is to automate the manual review of my finances I carry out each month in Excel. The
project will involve:
Expand Down
2 changes: 0 additions & 2 deletions finances_automation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
__VERSION__ = '1.0.0'
__DESCRIPTION__ = 'Automate your finances analysis.'
3 changes: 1 addition & 2 deletions finances_automation/command/command.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import argparse

from finances_automation import __DESCRIPTION__
from finances_automation.command import command_parsers


Expand All @@ -9,7 +8,7 @@ def start():
:return None:
"""
main_parser = argparse.ArgumentParser(prog='finances-automation', description=__DESCRIPTION__)
main_parser = argparse.ArgumentParser(prog='finances-automation', description='Automate your finances analysis.')
subparsers = main_parser.add_subparsers(title='Subcommands')

subparsers, parse_statement = command_parsers.set_up_parse(subparsers)
Expand Down
12 changes: 8 additions & 4 deletions finances_automation/command/commands/get_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ def get_overview(cli_args):
:param tuple cli_args:
:return None:
"""
table_names = 'current_transactions', 'credit_transactions'
table_names = 'current_transactions', 'credit_transactions'
print_last_recorded_balance(table_names)
print_dates_of_most_recent_data(table_names)


def print_last_recorded_balance(table_names):
""" Print the latest recorded balance of the given tables.
Expand All @@ -23,9 +24,11 @@ def print_last_recorded_balance(table_names):
for table_name in table_names:
repository = TransactionsRepository(Table.get_from_config(table_name))
latest_balance = repository.get_latest_balance()
print('Latest balance for {}: {}'.format(table_name, float(latest_balance[0])))
print('Latest balance for {}: {}'.format(table_name, latest_balance))

print()


def print_dates_of_most_recent_data(table_names):
""" Print the dates of the most recently parsed and most recently categorised transactions for the given tables.
Expand All @@ -42,14 +45,15 @@ def print_dates_of_most_recent_data(table_names):
latest_parsed_dates,
latest_categorised_dates
):

print(
'Most recent {} data:'
'\n- Parsed: {}'
'\n- Categorised: {}'
.format(
table_name,
dt.datetime.strftime(latest_parsed_date[0], '%d/%m/%Y'),
dt.datetime.strftime(latest_categorised_date[0], '%d/%m/%Y')
dt.datetime.strftime(latest_parsed_date, '%d/%m/%Y') if latest_parsed_date else None,
dt.datetime.strftime(latest_categorised_date, '%d/%m/%Y') if latest_categorised_date else None
),
end='\n\n'
)
13 changes: 5 additions & 8 deletions finances_automation/command/commands/view_latest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import pandas as pd

from finances_automation import configuration as conf
from finances_automation.entities.database import Database
from finances_automation.entities.table import Table
from finances_automation.repositories import TransactionsRepository


pd.set_option('display.max_columns', 500)
Expand All @@ -17,21 +16,19 @@ def view_latest(cli_args):
:param list(str) columns:
"""
table = Table.get_from_config(cli_args.table_name)
columns = cli_args.columns or list(table.schema.keys())
columns = cli_args.columns
limit = cli_args.n

db = Database(conf.db_name, conf.db_cluster, conf.user)

if columns == ['*']:
columns = db.get_table_column_names(table)
columns = list(table.schema.keys())
columns.remove('id')

elif 'date' not in columns:
columns = ['date'] + columns

data = db.select_from(table, columns)
data = TransactionsRepository(table).get_latest_entries(columns, limit)

df = pd.DataFrame(data=data, columns=columns).sort_values(by=table.date_columns[0], ascending=False).head(limit)
df = pd.DataFrame(data=data, columns=columns).sort_values(by=table.date_columns[0], ascending=False)

print('\n')
print(df.to_string(index=False))
14 changes: 10 additions & 4 deletions finances_automation/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@


package_root = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
db_host = os.environ['POSTGRES_HOST'] or 'localhost'

db_name = 'finances'
db_cluster = os.path.join(package_root, 'data', 'database_cluster')
user = 'Marcus1'
psql_log_location = os.path.join(package_root, 'psql.log')

db_config = {
'db_host': db_host,
'port': 5433,
'dbname': 'postgres',
'user': 'postgres',
'password': os.environ['POSTGRES_PASSWORD']
}

categories = {
'income': [
Expand Down Expand Up @@ -153,6 +158,7 @@
}
}


table_names = [table_name for table_name in table_configurations]

transaction_table_names = [
Expand Down
Empty file.
27 changes: 0 additions & 27 deletions finances_automation/dashboard/app.py

This file was deleted.

13 changes: 0 additions & 13 deletions finances_automation/dashboard/support.py

This file was deleted.

Loading

0 comments on commit 4c6c78d

Please sign in to comment.