From dba2a2b70c5a6db88f02a3c6b23187af0c67b4ee Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Wed, 29 May 2024 13:19:21 +0200 Subject: [PATCH] feat: proper config method --- README.md | 22 +++++++++++++++++++++- git_time_tracking/__main__.py | 24 +++++++++++++++++------- git_time_tracking/utils.py | 4 ++++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5356b73..c83b9ec 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,23 @@ # Git Time Tracking -This is a tool, allowing you to view the commits you have made in a set of repositories. This is useful if you need to write down your times for work. \ No newline at end of file +This is a tool, allowing you to view the commits you have made in a set of repositories. This is useful if you need to write down your times for work. + +## Installation + +You can install the program by using pip: + +```bash +pip install git-time-tracking +``` + +> Make sure nano is installed on your system, as it is used for editing the config file. + +## Usage + +You can see all the cli options by running: + +```bash +git_time_tracking --help +``` + +The first time running the program, you will be thrown into `nano` (has to be installed). Here you can specify the repositories you want to track, and some other settings. diff --git a/git_time_tracking/__main__.py b/git_time_tracking/__main__.py index 388bf3d..a6e471d 100644 --- a/git_time_tracking/__main__.py +++ b/git_time_tracking/__main__.py @@ -1,20 +1,30 @@ import argparse import datetime import subprocess +from pathlib import Path -from .utils import SETTINGS, console +from .utils import (CONFIG_PATH, SETTINGS, SETTINGS_EXISTED, console, + load_config, save_config) + + +def configure(): + save_config() + subprocess.call(['nano', str(CONFIG_PATH)]) + load_config() -GIT_COMMAND = """ -git log " -""" def main(): # get the user input parser = argparse.ArgumentParser() - parser.add_argument("date", help="The date to get the logs for") + parser.add_argument("--date", help="The date to get the logs for", nargs="?", const=None) + parser.add_argument("--author", help="The author to get the logs for", nargs="?", const=None) + parser.add_argument("--config", action=argparse.BooleanOptionalAction, help="Configure the settings") args = parser.parse_args() - parsed_date = raw_date = args.date.replace("date=", "") + if not SETTINGS_EXISTED or args.config: + configure() + + parsed_date = raw_date = args.date.replace("date=", "") if args.date is not None else datetime.datetime.now().strftime(SETTINGS["date_format"]) try: date = datetime.datetime.strptime(raw_date, SETTINGS["date_format"]).date() parsed_date = date.strftime("%Y-%m-%d") @@ -24,7 +34,7 @@ def main(): if SETTINGS["git_author"] is None: SETTINGS["git_author"] = console.input("Whats your name? ").strip() - parsed_author = SETTINGS["git_author"].replace("'", "") + parsed_author = (args.author or SETTINGS["git_author"]).replace("'", "") pretty_author = parsed_author if not SETTINGS["censor_author"] else "*" * len(parsed_author) console.print("date:\t", parsed_date, style="bold") diff --git a/git_time_tracking/utils.py b/git_time_tracking/utils.py index 40f4218..c179e60 100644 --- a/git_time_tracking/utils.py +++ b/git_time_tracking/utils.py @@ -9,6 +9,7 @@ from .__about__ import __name__ console: Console = Console() +SETTINGS_EXISTED = False CONFIG_PATH = Path.home() / ".config" / __name__ / "config.toml" SETTINGS = { "git_directories": [], @@ -18,7 +19,10 @@ SETTINGS = { } def load_config(): + global SETTINGS_EXISTED + if CONFIG_PATH.exists(): + SETTINGS_EXISTED = True with CONFIG_PATH.open("r", encoding="utf-8") as f: config = toml.load(f) SETTINGS.update(config)