From f7e86c63b3b3fb157c1bea376f689e9cb3b6ade9 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Wed, 25 Mar 2020 08:46:19 +0100 Subject: [PATCH] added more helper functions, preparing for web gui --- superx_budget/__init__.py | 6 +++++ superx_budget/helpers.py | 27 ++++++++++++++++++++ superx_budget/overview.py | 17 ++++++++++++- tests/conftest.py | 4 +-- tests/test_helpers.py | 52 +++++++++++++++++++++++++++------------ tests/test_overview.py | 8 ++++++ 6 files changed, 95 insertions(+), 19 deletions(-) diff --git a/superx_budget/__init__.py b/superx_budget/__init__.py index 2cfdb3d..e695864 100644 --- a/superx_budget/__init__.py +++ b/superx_budget/__init__.py @@ -7,5 +7,11 @@ __version__ = "0.0.1" from .budget import parse_budget_file # noqa: F401 from .superx import parse_exported_file # noqa: F401 +from .helpers import ( # noqa: F401 + find_recipients, + find_budget_file, + get_sheet_of_file, +) +from .pyramid import main # noqa: F401 from .overview import create_overview # noqa: F401 from .exceptions import BudgetParserError, SuperXParserError # noqa: F401 diff --git a/superx_budget/helpers.py b/superx_budget/helpers.py index 93b83c1..184d952 100644 --- a/superx_budget/helpers.py +++ b/superx_budget/helpers.py @@ -1,7 +1,13 @@ """ some helper functions """ +from pathlib import Path + import openpyxl +DEFAULT_RECIPIENTS = [ + "frey@imtek.de", +] + def excel_value_as_number(value): if value is None: @@ -40,3 +46,24 @@ def strip_excel_value(value): if isinstance(value, str): return value.strip() return value + + +def find_budget_file(folder, year): + """ searches for a file with the name "budget[...]-.xlsx """ + files = (i for i in Path(folder).iterdir() if i.is_file()) + visible = (i for i in files if not i.name.startswith(".")) + workbooks = (i for i in visible if i.suffix.lower() == ".xlsx") + budgets = (i for i in workbooks if i.name.lower().startswith("budget")) + for path in budgets: + if path.stem.endswith(f"-{year}"): + return path + + +def find_recipients(folder, filename="recipients.txt"): + """ finds the recipients of the budget list """ + file_path = folder / filename + if file_path.is_file(): + with file_path.open() as filehandle: + stripped = (line.strip() for line in filehandle) + return [line for line in stripped if line] + return DEFAULT_RECIPIENTS diff --git a/superx_budget/overview.py b/superx_budget/overview.py index 5f6694b..4386451 100644 --- a/superx_budget/overview.py +++ b/superx_budget/overview.py @@ -22,12 +22,18 @@ class ProjectOverview: """ initializes the class """ self.budget_data = budget_data self.entries = [] + self.found = False @property def project(self): """ returns the project number """ return self.budget_data.project + @property + def row(self): + """ returns the excel row number """ + return self.budget_data.row + @property def expenses(self): """ returns the accumulated expenses """ @@ -70,8 +76,17 @@ def _create_entries_from_superx(overview_map, superx_export_data): return overview_map +def _set_found_state(overview_map, superx_export): + superx_projects = {line.project for line in superx_export.data} + found_projects = superx_projects.intersection(set(overview_map.keys())) + for project in found_projects: + overview_map[project].found = True + return overview_map + + def create_overview(budget_list, superx_export): """ create a overview map with budget entries from the parsed raw data """ - overview_map = _create_overview_map(budget_list) + tmp_map = _create_overview_map(budget_list) + overview_map = _set_found_state(tmp_map, superx_export) material_expenses = _filter_superx_material_expenses(superx_export) return _create_entries_from_superx(overview_map, material_expenses) diff --git a/tests/conftest.py b/tests/conftest.py index d6b32fc..f904f71 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,12 +17,12 @@ class MockWorkbookSheet: @pytest.fixture def example_root(request): root_dir = Path(request.config.rootdir) - yield root_dir / "test data" + yield root_dir / "test_data" @pytest.fixture def budget_example_file(example_root): - return example_root / "Verbrauchsmittel-Toto-2020.xlsx" + return example_root / "Budget-Vorlage-2020.xlsx" @pytest.fixture diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 0563bad..d4823a1 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -1,18 +1,6 @@ import pytest -@pytest.fixture -def example_file(example_root): - return example_root / "Verbrauchsmittel-Toto-2020.xlsx" - - -@pytest.fixture -def example_workbook(example_file): - import openpyxl - - yield openpyxl.open(example_file) - - @pytest.mark.parametrize( "input,expected", [ @@ -53,20 +41,20 @@ def test_strip_excel_value(input, expected): assert result == expected -def test_get_sheet_of_file_first(example_file): +def test_get_sheet_of_file_first(budget_example_file): from superx_budget.helpers import get_sheet_of_file - sheet = get_sheet_of_file(example_file) # sheet=None + sheet = get_sheet_of_file(budget_example_file) # sheet=None first_row = next(sheet.values) first_cell = first_row[0] assert first_cell.strip() == "Nr." -def test_get_sheet_of_file_named(example_file): +def test_get_sheet_of_file_named(budget_example_file): from superx_budget.helpers import get_sheet_of_file - sheet = get_sheet_of_file(example_file, sheet="Safeguard I") + sheet = get_sheet_of_file(budget_example_file, sheet="Safeguard I") first_row = next(sheet.values) first_cell = first_row[0] @@ -91,3 +79,35 @@ def test_excel_value_as_number(input, expected): result = excel_value_as_number(input) assert result == expected + + +def test_find_budget_file_found(example_root): + from superx_budget.helpers import find_budget_file + + result = find_budget_file(example_root, 2020) + + assert result.name == "Budget-Vorlage-2020.xlsx" + + +def test_find_budget_file_not_found(example_root): + from superx_budget.helpers import find_budget_file + + result = find_budget_file(example_root, 2019) + + assert result is None + + +def test_find_recipients_ok(example_root): + from superx_budget.helpers import find_recipients + + result = find_recipients(example_root, "recipients.txt") + + assert result == ["frey@imtek.de", "foo@example.com", "bar@example.com"] + + +def test_find_recipients_default(example_root): + from superx_budget.helpers import find_recipients + + result = find_recipients(example_root, "unknown file") + + assert result == ["frey@imtek.de"] diff --git a/tests/test_overview.py b/tests/test_overview.py index 2af3358..b95be30 100644 --- a/tests/test_overview.py +++ b/tests/test_overview.py @@ -36,6 +36,14 @@ def test_project_overview_project_property(example_budget_data): assert over.project == example_budget_data.project +def test_project_overview_row_property(example_budget_data): + from superx_budget.overview import ProjectOverview + + over = ProjectOverview(example_budget_data) + + assert over.row == example_budget_data.row + + def test_project_overview_add(example_budget_data): from superx_budget.overview import ProjectOverview, OverviewBudgetEntry