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