Browse Source

added more helper functions, preparing for web gui

pull/1/head
Holger Frey 5 years ago
parent
commit
f7e86c63b3
  1. 6
      superx_budget/__init__.py
  2. 27
      superx_budget/helpers.py
  3. 17
      superx_budget/overview.py
  4. 4
      tests/conftest.py
  5. 52
      tests/test_helpers.py
  6. 8
      tests/test_overview.py

6
superx_budget/__init__.py

@ -7,5 +7,11 @@ __version__ = "0.0.1" @@ -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

27
superx_budget/helpers.py

@ -1,7 +1,13 @@ @@ -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): @@ -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[...]-<year>.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

17
superx_budget/overview.py

@ -22,12 +22,18 @@ class ProjectOverview: @@ -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): @@ -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)

4
tests/conftest.py

@ -17,12 +17,12 @@ class MockWorkbookSheet: @@ -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

52
tests/test_helpers.py

@ -1,18 +1,6 @@ @@ -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): @@ -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): @@ -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"]

8
tests/test_overview.py

@ -36,6 +36,14 @@ def test_project_overview_project_property(example_budget_data): @@ -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

Loading…
Cancel
Save