From 0b83e1cf09b930a90c58b6fa191eaf4008a560a1 Mon Sep 17 00:00:00 2001 From: "David A. Desrosiers" Date: Sat, 25 May 2024 19:53:44 -0400 Subject: [PATCH 1/2] Refactoring cleanup, pythonic updates, black formatting --- withings_sync/garmin.py | 46 +++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/withings_sync/garmin.py b/withings_sync/garmin.py index e17b286..0c49564 100644 --- a/withings_sync/garmin.py +++ b/withings_sync/garmin.py @@ -1,56 +1,52 @@ """This module handles the Garmin connectivity.""" + +import io import logging -import garth import os -import io -log = logging.getLogger("garmin") +import garth +log = logging.getLogger("garmin") -HOME = os.getenv('HOME', '.') -GARMIN_SESSION = os.getenv('GARMIN_SESSION', HOME + '/.garmin_session') +HOME = os.getenv("HOME", ".") +GARMIN_SESSION = os.path.join(HOME, ".garmin_session") class LoginSucceeded(Exception): - """Used to raise on LoginSucceeded""" + """Raised when login succeeds.""" class LoginFailed(Exception): - """Used to raise on LoginFailed""" + """Raised when login fails.""" class APIException(Exception): - """Used to raise on APIException""" + """Raised for API exceptions.""" class GarminConnect: - """Main GarminConnect class""" + """Main GarminConnect class.""" def __init__(self) -> None: self.client = garth.Client() def login(self, email=None, password=None): - logged_in = False if os.path.exists(GARMIN_SESSION): self.client.load(GARMIN_SESSION) - try: - self.client.username - logged_in = True - except Exception: - pass - - if not logged_in: - try: - self.client.login(email, password) - self.client.dump(GARMIN_SESSION) - except Exception as ex: - raise APIException("Authentication failure: {}. Did you enter correct credentials?".format(ex)) + if hasattr(self.client, "username"): + return + try: + self.client.login(email, password) + self.client.dump(GARMIN_SESSION) + except Exception as ex: + raise APIException( + f"Authentication failure: {ex}. Did you enter correct credentials?" + ) def upload_file(self, ffile): - """upload fit file to Garmin connect""" - # Convert the fitfile to a in-memory file for upload + """Upload fit file to Garmin Connect.""" fit_file = io.BytesIO(ffile.getvalue()) - fit_file.name = 'withings.fit' + fit_file.name = "withings.fit" self.client.upload(fit_file) return True From 94eddbb4ac469ba9f77e40d8eed113104836053b Mon Sep 17 00:00:00 2001 From: longstone <2110765+longstone@users.noreply.github.com> Date: Tue, 20 Aug 2024 11:07:44 +0200 Subject: [PATCH 2/2] fix: make GARMIN_SESSION working --- withings_sync/garmin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/withings_sync/garmin.py b/withings_sync/garmin.py index 0c49564..ae5f455 100644 --- a/withings_sync/garmin.py +++ b/withings_sync/garmin.py @@ -9,7 +9,7 @@ log = logging.getLogger("garmin") HOME = os.getenv("HOME", ".") -GARMIN_SESSION = os.path.join(HOME, ".garmin_session") +GARMIN_SESSION = os.getenv('GARMIN_SESSION', os.path.join(HOME, ".garmin_session")) class LoginSucceeded(Exception):