Skip to content

Commit

Permalink
New: Read .cfg files in current directory or in python script directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuinigeRijder committed Jan 24, 2024
1 parent 2a2a7f2 commit ec10586
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
5 changes: 2 additions & 3 deletions dailystats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""
import configparser
from dataclasses import dataclass
from os import path
import re
import sys
import traceback
Expand All @@ -15,6 +14,7 @@
import gspread
from monitor_utils import (
get,
get_filepath,
log,
arg_has,
get_vin_arg,
Expand Down Expand Up @@ -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()

Expand Down
5 changes: 2 additions & 3 deletions debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
5 changes: 2 additions & 3 deletions monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
- charging pattern over time
- visited places
"""
from os import path
import re
import sys
import io
Expand All @@ -44,6 +43,7 @@
from monitor_utils import (
arg_has,
get,
get_filepath,
get_last_line,
get_safe_datetime,
get_safe_float,
Expand Down Expand Up @@ -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"]
Expand Down
19 changes: 16 additions & 3 deletions monitor_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
monitor utils
"""
import configparser
import errno
import sys
import os
from datetime import datetime, timezone
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""
from copy import deepcopy
from io import TextIOWrapper
from os import path
import sys
import configparser
import traceback
Expand All @@ -15,6 +14,7 @@
import gspread
from dateutil import parser
from monitor_utils import (
get_filepath,
log,
arg_has,
get,
Expand Down Expand Up @@ -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"])
Expand Down

0 comments on commit ec10586

Please sign in to comment.