Skip to content

Commit

Permalink
Add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Oct 20, 2024
1 parent b5a9edc commit 5ef2d1e
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions rename_activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import csv
import datetime as dt
import os
import pathlib
import sys
from pathlib import Path

from termcolor import cprint


def load_csv(csv_file):
def load_csv(csv_file: str) -> list[dict[str, str]]:
data = []

with open(csv_file) as csv_file:
Expand All @@ -24,7 +24,7 @@ def load_csv(csv_file):
return data


def convert_date_string(input_string):
def convert_date_string(input_string: str) -> str:
# May 22, 2010, 7:39:29 PM
# ->
# 20100522-193929
Expand All @@ -36,19 +36,19 @@ def convert_date_string(input_string):
return new_date


def output_file(activity, ext=None):
def output_file(activity: dict[str, str], ext: str | None = None) -> Path:
# Return name like 20141207-152233-Ride.gpx
new_date = convert_date_string(activity["Activity Date"])

path = pathlib.Path(activity["Filename"])
path = Path(activity["Filename"])
if not ext:
ext = "".join(path.suffixes)
outfile = f"{new_date}-{activity['Activity Type']}{ext}"
outfile = path.parent / outfile
return outfile


if __name__ == "__main__":
def main() -> None:
parser = argparse.ArgumentParser(
description="Rename files, eg. 1836025202.gpx to 20180912-064451-Ride.gpx",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Expand All @@ -73,3 +73,7 @@ def output_file(activity, ext=None):
os.rename(activity["Filename"], outfile)
except FileNotFoundError:
cprint(f"File not found: {infile}", "yellow")


if __name__ == "__main__":
main()

0 comments on commit 5ef2d1e

Please sign in to comment.