From a11d3e68233d207a7efaa43e95e6e6508b1fba0d Mon Sep 17 00:00:00 2001 From: "J. Rast" Date: Sat, 7 Oct 2023 22:57:38 +0200 Subject: [PATCH] Fix for #141: Support for multiple garmin users --- withings_sync/garmin.py | 10 +++++++--- withings_sync/sync.py | 6 +++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/withings_sync/garmin.py b/withings_sync/garmin.py index ec213bd..e17b286 100644 --- a/withings_sync/garmin.py +++ b/withings_sync/garmin.py @@ -7,6 +7,10 @@ log = logging.getLogger("garmin") +HOME = os.getenv('HOME', '.') +GARMIN_SESSION = os.getenv('GARMIN_SESSION', HOME + '/.garmin_session') + + class LoginSucceeded(Exception): """Used to raise on LoginSucceeded""" @@ -27,8 +31,8 @@ def __init__(self) -> None: def login(self, email=None, password=None): logged_in = False - if os.path.exists('./garmin_session'): - self.client.load('./garmin_session') + if os.path.exists(GARMIN_SESSION): + self.client.load(GARMIN_SESSION) try: self.client.username logged_in = True @@ -38,7 +42,7 @@ def login(self, email=None, password=None): if not logged_in: try: self.client.login(email, password) - self.client.dump('./garmin_session') + self.client.dump(GARMIN_SESSION) except Exception as ex: raise APIException("Authentication failure: {}. Did you enter correct credentials?".format(ex)) diff --git a/withings_sync/sync.py b/withings_sync/sync.py index 54bc19a..711450e 100644 --- a/withings_sync/sync.py +++ b/withings_sync/sync.py @@ -10,6 +10,11 @@ from datetime import date, datetime from importlib.metadata import version +# Load the environment variables from a .env (dotenv) file. +# This is done prior to importing other modules such that all variables, +# also the ones accessed in those modules, can be set in the dotenv file. +dotenv.load_dotenv() + from withings_sync.withings2 import WithingsAccount from withings_sync.garmin import GarminConnect from withings_sync.trainerroad import TrainerRoad @@ -30,7 +35,6 @@ def load_variable(env_var, secrets_file): # value read from the secrets file. return os.getenv(env_var, value) -dotenv.load_dotenv() GARMIN_USERNAME = load_variable('GARMIN_USERNAME', "/run/secrets/garmin_username") GARMIN_PASSWORD = load_variable('GARMIN_PASSWORD', "/run/secrets/garmin_password")