|
|
|
@ -1,16 +1,13 @@
@@ -1,16 +1,13 @@
|
|
|
|
|
"""some helper functions""" |
|
|
|
|
|
|
|
|
|
from pathlib import Path |
|
|
|
|
from typing import TypeVar |
|
|
|
|
|
|
|
|
|
import openpyxl |
|
|
|
|
|
|
|
|
|
ExcelItem = TypeVar("ExcelItem") |
|
|
|
|
|
|
|
|
|
DEFAULT_RECIPIENTS = ["frey@imtek.de"] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def excel_value_as_number(value: ExcelItem) -> float | int: |
|
|
|
|
def excel_value_as_number(value): |
|
|
|
|
if value is None: |
|
|
|
|
return 0 |
|
|
|
|
if isinstance(value, str): |
|
|
|
@ -21,7 +18,7 @@ def excel_value_as_number(value: ExcelItem) -> float | int:
@@ -21,7 +18,7 @@ def excel_value_as_number(value: ExcelItem) -> float | int:
|
|
|
|
|
return value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_sheet_of_file(excel_file: str, sheet: str | None = None) -> ExcelItem: |
|
|
|
|
def get_sheet_of_file(excel_file, sheet=None): |
|
|
|
|
"""returns a sheet from an excel FileCache |
|
|
|
|
|
|
|
|
|
if name is set to None, the function returns the first sheet |
|
|
|
@ -33,21 +30,21 @@ def get_sheet_of_file(excel_file: str, sheet: str | None = None) -> ExcelItem:
@@ -33,21 +30,21 @@ def get_sheet_of_file(excel_file: str, sheet: str | None = None) -> ExcelItem:
|
|
|
|
|
return workbook[sheet] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_empty_excel_value(value: ExcelItem) -> bool: |
|
|
|
|
def is_empty_excel_value(value): |
|
|
|
|
"""is the cell value considered empty""" |
|
|
|
|
if value is None: |
|
|
|
|
return True |
|
|
|
|
return isinstance(value, str) and value.strip() == "" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def strip_excel_value(value: ExcelItem) -> str | ExcelItem: |
|
|
|
|
def strip_excel_value(value): |
|
|
|
|
"""remove whitespace from an excel value if it is a string""" |
|
|
|
|
if isinstance(value, str): |
|
|
|
|
return value.strip() |
|
|
|
|
return value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_budget_file_name(path_or_name: str | Path) -> bool: |
|
|
|
|
def is_budget_file_name(path_or_name): |
|
|
|
|
"""checks if a filename has the format "budget[...]-<year>.xlsx""" |
|
|
|
|
path = Path(path_or_name) |
|
|
|
|
if path.suffix.lower() != ".xlsx": |
|
|
|
@ -61,14 +58,14 @@ def is_budget_file_name(path_or_name: str | Path) -> bool:
@@ -61,14 +58,14 @@ def is_budget_file_name(path_or_name: str | Path) -> bool:
|
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def list_budget_files(folder: str | Path) -> list[Path]: |
|
|
|
|
def list_budget_files(folder): |
|
|
|
|
"""lists all files 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(".")) |
|
|
|
|
return [i for i in visible if is_budget_file_name(i)] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_budget_file(folder: str | Path, year: str | int) -> Path | None: |
|
|
|
|
def find_budget_file(folder, year): |
|
|
|
|
"""searches for a file with the name "budget[...]-<year>.xlsx""" |
|
|
|
|
for path in list_budget_files(folder): |
|
|
|
|
if path.stem.endswith(f"-{year}"): |
|
|
|
@ -76,9 +73,7 @@ def find_budget_file(folder: str | Path, year: str | int) -> Path | None:
@@ -76,9 +73,7 @@ def find_budget_file(folder: str | Path, year: str | int) -> Path | None:
|
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_recipients( |
|
|
|
|
folder: str | Path, filename: str = "recipients.txt" |
|
|
|
|
) -> list[str]: |
|
|
|
|
def find_recipients(folder, filename="recipients.txt"): |
|
|
|
|
"""finds the recipients of the budget list""" |
|
|
|
|
file_path = folder / filename |
|
|
|
|
if file_path.is_file(): |
|
|
|
|