From ec10586ec1e1fe24ef290071f879b84bfcfafd43 Mon Sep 17 00:00:00 2001 From: Zuinige Rijder Date: Wed, 24 Jan 2024 09:10:34 +0100 Subject: [PATCH] New: Read .cfg files in current directory or in python script directory --- dailystats.py | 5 ++--- debug.py | 5 ++--- monitor.py | 5 ++--- monitor_utils.py | 19 ++++++++++++++++--- summary.py | 7 +++---- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/dailystats.py b/dailystats.py index d9b186a..81c01a0 100644 --- a/dailystats.py +++ b/dailystats.py @@ -4,7 +4,6 @@ """ import configparser from dataclasses import dataclass -from os import path import re import sys import traceback @@ -15,6 +14,7 @@ import gspread from monitor_utils import ( get, + get_filepath, log, arg_has, get_vin_arg, @@ -82,8 +82,7 @@ def dbg(line: str) -> bool: _ = D and dbg(f"DAILYSTATS_CSV_FILE: {DAILYSTATS_CSV_FILE.name}") parser = configparser.ConfigParser() -SCRIPT_DIRNAME = path.abspath(path.dirname(__file__)) -parser.read(f"{SCRIPT_DIRNAME}/monitor.cfg") +parser.read(get_filepath("monitor.cfg")) monitor_settings = dict(parser.items("monitor")) ODO_METRIC = get(monitor_settings, "odometer_metric", "km").lower() diff --git a/debug.py b/debug.py index cbf22a8..8a9e243 100644 --- a/debug.py +++ b/debug.py @@ -3,15 +3,14 @@ import configparser from datetime import datetime import logging -from os import path from hyundai_kia_connect_api import VehicleManager, Vehicle +from monitor_utils import get_filepath logging.basicConfig(level=logging.DEBUG) # == read monitor settings in monitor.cfg ================== parser = configparser.ConfigParser() -SCRIPT_DIRNAME = path.abspath(path.dirname(__file__)) -parser.read(f"{SCRIPT_DIRNAME}/monitor.cfg") +parser.read(get_filepath("monitor.cfg")) monitor_settings = dict(parser.items("monitor")) REGION = monitor_settings["region"] diff --git a/monitor.py b/monitor.py index 165ee1c..d50f705 100644 --- a/monitor.py +++ b/monitor.py @@ -30,7 +30,6 @@ - charging pattern over time - visited places """ -from os import path import re import sys import io @@ -44,6 +43,7 @@ from monitor_utils import ( arg_has, get, + get_filepath, get_last_line, get_safe_datetime, get_safe_float, @@ -73,8 +73,7 @@ # == read monitor in monitor.cfg =========================== parser = configparser.ConfigParser() -SCRIPT_DIRNAME = path.abspath(path.dirname(__file__)) -parser.read(f"{SCRIPT_DIRNAME}/monitor.cfg") +parser.read(get_filepath("monitor.cfg")) monitor_settings = dict(parser.items("monitor")) REGION = monitor_settings["region"] diff --git a/monitor_utils.py b/monitor_utils.py index b6cd327..b0cb4e8 100644 --- a/monitor_utils.py +++ b/monitor_utils.py @@ -3,6 +3,7 @@ monitor utils """ import configparser +import errno import sys import os from datetime import datetime, timezone @@ -46,6 +47,19 @@ def sleep(retries: int) -> int: return retries +def get_filepath(filename: str) -> str: + """get_filepath""" + if os.path.isfile(filename): # current directory + filepath = filename + else: # script directory + script_dirname = os.path.abspath(os.path.dirname(__file__)) + filepath = f"{script_dirname}/{filename}" + + if not os.path.isfile(filepath): + raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filepath) + return filepath + + def km_to_mile(kilometer: float) -> float: """Convert km to mile""" mile = kilometer * 0.6213711922 @@ -256,11 +270,10 @@ def read_translations() -> dict: """read translations""" translations: dict = {} parser = configparser.ConfigParser() - script_dirname = os.path.abspath(os.path.dirname(__file__)) - parser.read(f"{script_dirname}/monitor.cfg") + parser.read(get_filepath("monitor.cfg")) monitor_settings = dict(parser.items("monitor")) language = monitor_settings["language"].lower().strip() - translations_csv_file = Path(f"{script_dirname}/monitor.translations.csv") + translations_csv_file = Path(get_filepath("monitor.translations.csv")) with translations_csv_file.open("r", encoding="utf-8") as inputfile: linecount = 0 column = 1 diff --git a/summary.py b/summary.py index 1cece4b..7ea82ab 100644 --- a/summary.py +++ b/summary.py @@ -4,7 +4,6 @@ """ from copy import deepcopy from io import TextIOWrapper -from os import path import sys import configparser import traceback @@ -15,6 +14,7 @@ import gspread from dateutil import parser from monitor_utils import ( + get_filepath, log, arg_has, get, @@ -112,12 +112,11 @@ def dbg(line: str) -> bool: # == read monitor in monitor.cfg =========================== config_parser = configparser.ConfigParser() -SCRIPT_DIRNAME = path.abspath(path.dirname(__file__)) -config_parser.read(f"{SCRIPT_DIRNAME}/monitor.cfg") +config_parser.read(get_filepath("monitor.cfg")) monitor_settings = dict(config_parser.items("monitor")) ODO_METRIC = get(monitor_settings, "odometer_metric", "km").lower() -config_parser.read(f"{SCRIPT_DIRNAME}/summary.cfg") +config_parser.read(get_filepath("summary.cfg")) summary_settings = dict(config_parser.items("summary")) NET_BATTERY_SIZE_KWH = to_float(summary_settings["net_battery_size_kwh"])