Simplify log statements.

Make use of format specifiers in the logging library rather than relying on
string.format(), which makes log output unnecessarily verbose.
This commit is contained in:
Peter Gardfjäll 2020-04-07 12:49:44 +02:00
parent 4ee0cda314
commit c00ccd3887
7 changed files with 38 additions and 38 deletions

View File

@ -88,7 +88,7 @@ def _not_found_activities(backup_dir):
if os.path.isfile(_not_found):
with open(_not_found, mode="r") as f:
failed_activities = [line.strip() for line in f.readlines()]
log.debug("{} tried but failed activities in {}".format(len(failed_activities), _not_found))
log.debug("%d tried but failed activities in %s", len(failed_activities), _not_found)
return failed_activities
@ -117,7 +117,7 @@ def download(client, activity, retryer, backup_dir, export_formats=None):
id = activity[0]
if 'json_summary' in export_formats:
log.debug("getting json summary for {}".format(id))
log.debug("getting json summary for %s", id)
activity_summary = retryer.call(client.get_activity_summary, id)
dest = os.path.join(
@ -126,7 +126,7 @@ def download(client, activity, retryer, backup_dir, export_formats=None):
f.write(json.dumps(activity_summary, ensure_ascii=False, indent=4))
if 'json_details' in export_formats:
log.debug("getting json details for {}".format(id))
log.debug("getting json details for %s", id)
activity_details = retryer.call(client.get_activity_details, id)
dest = os.path.join(backup_dir, export_filename(activity, 'json_details'))
with codecs.open(dest, encoding="utf-8", mode="w") as f:
@ -135,7 +135,7 @@ def download(client, activity, retryer, backup_dir, export_formats=None):
not_found_path = os.path.join(backup_dir, not_found_file)
with open(not_found_path, mode="a") as not_found:
if 'gpx' in export_formats:
log.debug("getting gpx for {}".format(id))
log.debug("getting gpx for %s", id)
activity_gpx = retryer.call(client.get_activity_gpx, id)
dest = os.path.join(backup_dir, export_filename(activity, 'gpx'))
if activity_gpx is None:
@ -145,7 +145,7 @@ def download(client, activity, retryer, backup_dir, export_formats=None):
f.write(activity_gpx)
if 'tcx' in export_formats:
log.debug("getting tcx for {}".format(id))
log.debug("getting tcx for %s", id)
activity_tcx = retryer.call(client.get_activity_tcx, id)
dest = os.path.join(backup_dir, export_filename(activity, 'tcx'))
if activity_tcx is None:
@ -155,7 +155,7 @@ def download(client, activity, retryer, backup_dir, export_formats=None):
f.write(activity_tcx)
if 'fit' in export_formats:
log.debug("getting fit for {}".format(id))
log.debug("getting fit for %s", id)
activity_fit = retryer.call(client.get_activity_fit, id)
dest = os.path.join(
backup_dir, export_filename(activity, 'fit'))

View File

@ -116,11 +116,11 @@ class GarminClient(object):
headers = {'origin': 'https://sso.garmin.com'}
auth_response = self.session.post(
SSO_LOGIN_URL, headers=headers, params=request_params, data=form_data)
log.debug("got auth response: {}".format(auth_response.text))
log.debug("got auth response: %s", auth_response.text)
if auth_response.status_code != 200:
raise ValueError("authentication failure: did you enter valid credentials?")
auth_ticket_url = self._extract_auth_ticket_url(auth_response.text)
log.debug("auth ticket url: '{}'".format(auth_ticket_url))
log.debug("auth ticket url: '%s'", auth_ticket_url)
log.info("claiming auth ticket ...")
response = self.session.get(auth_ticket_url)
@ -185,7 +185,7 @@ class GarminClient(object):
:returns: A list of activity identifiers (along with their starting timestamps).
:rtype: tuples of (int, datetime)
"""
log.debug("fetching activities {} through {} ...".format(start_index, start_index + max_limit - 1))
log.debug("fetching activities %d through %d ...", start_index, start_index + max_limit - 1)
response = self.session.get(
"https://connect.garmin.com/modern/proxy/activitylist-service/activities/search/activities",
params={"start": start_index, "limit": max_limit})
@ -205,7 +205,7 @@ class GarminClient(object):
# make sure UTC timezone gets set
timestamp_utc = timestamp_utc.replace(tzinfo=dateutil.tz.tzutc())
entries.append((id, timestamp_utc))
log.debug("got {} activities.".format(len(entries)))
log.debug("got %d activities.", len(entries))
return entries
@require_session
@ -222,8 +222,8 @@ class GarminClient(object):
response = self.session.get(
"https://connect.garmin.com/modern/proxy/activity-service/activity/{}".format(activity_id))
if response.status_code != 200:
log.error(u"failed to fetch json summary for activity {}: {}\n{}".format(
activity_id, response.status_code, response.text))
log.error(u"failed to fetch json summary for activity %s: %d\n%s",
activity_id, response.status_code, response.text)
raise Exception(u"failed to fetch json summary for activity {}: {}\n{}".format(
activity_id, response.status_code, response.text))
return json.loads(response.text)

View File

@ -35,7 +35,7 @@ def incremental_backup(username: str,
"""
# if no --format was specified, all formats are to be backed up
format = format if format else export_formats
log.info("backing up formats: {}".format(", ".join(format)))
log.info("backing up formats: %s", ", ".join(format))
if not os.path.isdir(backup_dir):
os.makedirs(backup_dir)
@ -50,23 +50,23 @@ def incremental_backup(username: str,
with GarminClient(username, password) as client:
# get all activity ids and timestamps from Garmin account
log.info("scanning activities for {} ...".format(username))
log.info("scanning activities for %s ...", username)
activities = set(retryer.call(client.list_activities))
log.info("account has a total of {} activities".format(len(activities)))
log.info("account has a total of %d activities", len(activities))
missing_activities = garminexport.backup.need_backup(activities, backup_dir, format)
backed_up = activities - missing_activities
log.info("{} contains {} backed up activities".format(backup_dir, len(backed_up)))
log.info("%s contains %d backed up activities", backup_dir, len(backed_up))
log.info("activities that aren't backed up: {}".format(len(missing_activities)))
log.info("activities that aren't backed up: %d", len(missing_activities))
for index, activity in enumerate(missing_activities):
id, start = activity
log.info("backing up activity {} from {} ({} out of {}) ...".format(
id, start, index + 1, len(missing_activities)))
log.info("backing up activity %s from %s (%d out of %d) ...",
id, start, index + 1, len(missing_activities))
try:
garminexport.backup.download(client, activity, retryer, backup_dir, format)
except Exception as e:
log.error("failed with exception: {}".format(e))
log.error("failed with exception: %s", e)
if not ignore_errors:
raise

View File

@ -195,21 +195,21 @@ class Retryer(object):
while True:
try:
attempts += 1
log.info('{{}}: attempt {} ...'.format(name, attempts))
log.info('{%s}: attempt %d ...', name, attempts)
returnval = function(*args, **kw)
if self.returnval_predicate(returnval):
# return value satisfies predicate, we're done!
log.debug('{{}}: success: "{}"'.format(name, returnval))
log.debug('{%s}: success: "%s"', name, returnval)
return returnval
log.debug('{{}}: failed: return value: {}'.format(name, returnval))
log.debug('{%s}: failed: return value: %s', name, returnval)
except Exception as e:
if not self.error_strategy.should_suppress(e):
raise e
log.debug('{{}}: failed: error: {}'.format(name, e))
log.debug('{%s}: failed: error: %s', name, e)
elapsed_time = datetime.now() - start
# should we make another attempt?
if not self.stop_strategy.should_continue(attempts, elapsed_time):
raise GaveUpError('{{}}: gave up after {} failed attempt(s)'.format(name, attempts))
delay = self.delay_strategy.next_delay(attempts)
log.info('{{}}: waiting {} seconds for next attempt'.format(name, delay.total_seconds()))
log.info('{%s}: waiting %d seconds for next attempt', name, delay.total_seconds())
time.sleep(delay.total_seconds())

View File

@ -64,7 +64,7 @@ if __name__ == "__main__":
args.password = getpass.getpass("Enter password: ")
with GarminClient(args.username, args.password) as client:
log.info("fetching activity {} ...".format(args.activity))
log.info("fetching activity %s ...", args.activity)
summary = client.get_activity_summary(args.activity)
# set up a retryer that will handle retries of failed activity downloads
retryer = Retryer(
@ -75,5 +75,5 @@ if __name__ == "__main__":
garminexport.backup.download(
client, (args.activity, start_time), retryer, args.destination, export_formats=[args.format])
except Exception as e:
log.error("failed with exception: {}".format(e))
log.error("failed with exception: %s", e)
raise

View File

@ -11,7 +11,7 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)-15s [%(levelname)s] %
log = logging.getLogger(__name__)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Export all Garmin Connect activities")
# positional args
@ -26,22 +26,22 @@ if __name__ == "__main__":
if not args.password:
args.password = getpass.getpass("Enter password: ")
try:
with GarminClient(args.username, args.password) as client:
log.info("activities:")
activity_ids = client.list_activities()
log.info("num ids: {}".format(len(activity_ids)))
log.info("num ids: %d", len(activity_ids))
log.info(activity_ids)
latest_activity, latest_activity_start = activity_ids[0]
activity = client.get_activity_summary(latest_activity)
log.info("activity id: {}".format(activity["activity"]["activityId"]))
log.info("activity name: '{}'".format(activity["activity"]["activityName"]))
log.info("activity description: '{}'".format(activity["activity"]["activityDescription"]))
log.info("activity id: %s", activity["activity"]["activityId"])
log.info("activity name: '%s'", activity["activity"]["activityName"])
log.info("activity description: '%s'", activity["activity"]["activityDescription"])
log.info(json.dumps(client.get_activity_details(latest_activity), indent=4))
log.info(client.get_activity_gpx(latest_activity))
except Exception as e:
log.error("failed with exception: {}".format(e))
finally:
log.error("failed with exception: %s", e)
finally:
log.info("done")

View File

@ -56,15 +56,15 @@ if __name__ == "__main__":
with GarminClient(args.username, args.password) as client:
for activity in args.activity:
log.info("uploading activity file {} ...".format(activity.name))
log.info("uploading activity file %s ...", activity.name)
try:
id = client.upload_activity(activity, name=args.name, description=args.description,
private=args.private, activity_type=args.type)
except Exception as e:
log.error("upload failed: {!r}".format(e))
else:
log.info("upload successful: https://connect.garmin.com/modern/activity/{}".format(id))
log.info("upload successful: https://connect.garmin.com/modern/activity/%s", id)
except Exception as e:
log.error("failed with exception: {}".format(e))
log.error("failed with exception: %s", e)
raise