""" some helper functions """ from pathlib import Path import openpyxl DEFAULT_RECIPIENTS = [ "frey@imtek.de", ] def excel_value_as_number(value): if value is None: return 0 if isinstance(value, str): try: return float(value) except ValueError: return 0 return value 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 """ workbook = openpyxl.open(excel_file) if sheet is None: all_sheets = workbook.sheetnames sheet = all_sheets[0] return workbook[sheet] def is_empty_excel_value(value): """ is the cell value considered empty """ if value is None: return True if isinstance(value, str) and value.strip() == "": return True return False 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 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