Skip to content

Commit

Permalink
Merge pull request #20 from lcfd/development-mode
Browse files Browse the repository at this point in the history
Development mode
  • Loading branch information
lcfd authored Oct 14, 2023
2 parents c863752 + 46d4dfb commit b3b879e
Show file tree
Hide file tree
Showing 14 changed files with 423 additions and 114 deletions.
2 changes: 2 additions & 0 deletions cli/trakcli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
__app_name__ = "trak"
__version__ = "0.0.2"
__website__ = "https://usetrak.com"
__git_repository__ = "https://github.com/lcfd/trak"
1 change: 0 additions & 1 deletion cli/trakcli/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from trakcli import __app_name__

from trakcli.main import app

app(prog_name=__app_name__)
57 changes: 57 additions & 0 deletions cli/trakcli/callbacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import typer
from rich import print as rprint
from rich.align import Align
from rich.panel import Panel

from trakcli.__init__ import __app_name__, __git_repository__, __version__, __website__


def version_callback(value: bool) -> None:
"""
Print the application version.
"""
if value:
rprint(
Panel(
renderable=Align.center(f"{__app_name__} v{__version__}"),
title=__app_name__,
padding=(2),
),
)
raise typer.Exit()


def website_callback(value: bool) -> None:
"""
Launch the usetrak.com website.
"""
if value:
typer.launch(__website__)
raise typer.Exit()


def repository_callback(value: bool) -> None:
"""
Launch the usetrak.com website.
"""
if value:
typer.launch(__git_repository__)
raise typer.Exit()


def issues_callback(value: bool) -> None:
"""
Launch issues page.
"""
if value:
typer.launch("https://github.com/lcfd/trak/issues")
raise typer.Exit()


def report_bug_callback(value: bool) -> None:
"""
Launch report bug page.
"""
if value:
typer.launch("https://github.com/lcfd/trak/issues/new")
raise typer.Exit()
Empty file added cli/trakcli/config/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions cli/trakcli/config/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import json

import typer
from rich import print as rprint
from rich.json import JSON
from rich.panel import Panel

from trakcli.config.main import CONFIG_FILE_PATH
from trakcli.database.basic import get_json_file_content

app = typer.Typer()


@app.command()
def show():
"""Show the config file."""

rprint(
Panel(
title=f"Your config file {CONFIG_FILE_PATH}",
renderable=JSON(json.dumps(get_json_file_content(CONFIG_FILE_PATH))),
)
)


if __name__ == "__main__":
app()
8 changes: 6 additions & 2 deletions cli/trakcli/config.py → cli/trakcli/config/main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
from pathlib import Path

from trakcli.database.basic import get_json_file_content

#
# Paths
#


TRAK_FOLDER = Path.home() / ".trak"

DB_FILE_PATH = TRAK_FOLDER / "db.json"
DEV_DB_FILE_PATH = TRAK_FOLDER / "dev_db.json"
CONFIG_FILE_PATH = TRAK_FOLDER / "config.json"

# Read the config at CONFIG_FILE_PATH
CONFIG = get_json_file_content(CONFIG_FILE_PATH)

#
# Configuration helpers
#


def init_config(p: Path) -> int:
"""Create the to-do database."""
"""Init the config file."""

try:
p.parent.mkdir(parents=True, exist_ok=True)
Expand Down
Empty file.
43 changes: 43 additions & 0 deletions cli/trakcli/database/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import json
from pathlib import Path
from rich import print as rprint


def get_json_file_content(file_path: Path):
with open(file_path, "r") as db:
db_content = db.read()

return json.loads(db_content)


def show_json_file_content(file_path: Path):
"""Show the content of a JSON file."""

with open(file_path, "r") as db:
db_content = db.read()

parsed_json = json.loads(db_content)
rprint(parsed_json)


def manage_field_in_json_file(
file_path: Path, field_name: str, field_value: str | int | float | bool
):
"""Manage the content of a single object JSON file."""

with open(file_path, "r") as db:
db_content = db.read()

parsed_json = json.loads(db_content)
if field_name:
parsed_json[field_name] = field_value

with open(file_path, "w") as db:
json.dump(parsed_json, db, indent=2, separators=(",", ": "))


def overwrite_json_file(file_path: Path, content: dict | list[dict]):
"""Fill a JSON file with the provided content. It's a complete overwrite."""

with open(file_path, "w") as db:
json.dump(content, db, indent=2, separators=(",", ": "))
63 changes: 30 additions & 33 deletions cli/trakcli/database.py → cli/trakcli/database/database.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,49 @@
import json
from datetime import datetime, timedelta
from pathlib import Path
from typing import NamedTuple

from rich import padding, print as rprint
from rich import padding
from rich import print as rprint
from rich.console import Console
from rich.panel import Panel
from rich.table import Table

from trakcli.config import DB_FILE_PATH
from trakcli.config.main import CONFIG, DB_FILE_PATH, DEV_DB_FILE_PATH
from trakcli.database.models import Record
from trakcli.utils.format_date import format_date
from trakcli.utils.print_with_padding import print_with_padding
from rich.console import Console
from rich.table import Table

from trakcli.utils.same_week import same_week

file_path_to_use = DEV_DB_FILE_PATH if CONFIG["development"] else DB_FILE_PATH

#
# Database operations
#


class Record(NamedTuple):
project: str = ""
start: str = ""
end: str = ""
billable: bool = False
category: str = ""
tag: str = ""


def add_track_field(record: Record):
"""..."""
def add_session(record: Record):
"""Add a new session."""

with open(DB_FILE_PATH, "r") as db:
with open(file_path_to_use, "r") as db:
db_content = db.read()

parsed_json = json.loads(db_content)
parsed_json.append(record._asdict())

with open(DB_FILE_PATH, "w") as db:
with open(file_path_to_use, "w") as db:
json.dump(parsed_json, db, indent=2, separators=(",", ": "))


def stop_track_field():
def stop_trak_session():
"""Stop tracking the current project."""

with open(DB_FILE_PATH, "r") as db:
with open(file_path_to_use, "r") as db:
db_content = db.read()

parsed_json = json.loads(db_content)
parsed_json[-1]["end"] = datetime.now().isoformat()

with open(DB_FILE_PATH, "w") as db:
with open(file_path_to_use, "w") as db:
json.dump(parsed_json, db, indent=2, separators=(",", ": "))


Expand All @@ -60,14 +53,16 @@ def tracking_already_started():
If it's already running return the record.
"""

with open(DB_FILE_PATH, "r") as db:
with open(file_path_to_use, "r") as db:
db_content = db.read()
parsed_json = json.loads(db_content)

try:
last_record = parsed_json[-1]
except IndexError:
return False
except KeyError:
return False

if last_record["end"] == "":
return last_record
Expand All @@ -76,7 +71,7 @@ def tracking_already_started():


def get_current_session():
with open(DB_FILE_PATH, "r") as db:
with open(file_path_to_use, "r") as db:
db_content = db.read()

parsed_json = json.loads(db_content)
Expand All @@ -85,6 +80,8 @@ def get_current_session():
last_record = parsed_json[-1]
except IndexError:
return False
except KeyError:
return False

if last_record["end"] == "":
return last_record
Expand All @@ -101,7 +98,7 @@ def get_record_collection(
):
"""Get a collection of records, filtered by paramenters."""

with open(DB_FILE_PATH, "r") as db:
with open(file_path_to_use, "r") as db:
db_content = db.read()

parsed_json = json.loads(db_content)
Expand Down Expand Up @@ -179,11 +176,11 @@ def trunc_datetime(someDate):

table = Table(title=f"[bold]{project}[/bold]")

table.add_column("Start", justify="right", style="cyan", no_wrap=True)
table.add_column("End", style="cyan", no_wrap=True)
table.add_column("Category", style="magenta")
table.add_column("Tag", style="magenta")
table.add_column("Hours", style="magenta", no_wrap=True)
table.add_column("Start", justify="right", style="green", no_wrap=True)
table.add_column("End", style="orange3", no_wrap=True)
table.add_column("Category", style="steel_blue1")
table.add_column("Tag", style="steel_blue3")
table.add_column("Hours", style="yellow", no_wrap=True)
table.add_column("Billable")

acc_seconds = 0
Expand Down Expand Up @@ -225,16 +222,16 @@ def trunc_datetime(someDate):
def check_if_database_exists():
"""Check if the json db files exists."""

return Path.exists(DB_FILE_PATH)
return Path.exists(file_path_to_use)


def init_database(p: Path) -> int:
def init_database(p: Path, initial_value: str = "[]") -> int:
"""Create the application database."""

try:
p.parent.mkdir(parents=True, exist_ok=True)
with p.open("w", encoding="utf-8") as f:
f.write("[]")
f.write(initial_value)
return 0
except OSError:
return 1
20 changes: 20 additions & 0 deletions cli/trakcli/database/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import NamedTuple


class Record(NamedTuple):
project: str = ""
start: str = ""
end: str = ""
billable: bool = False
category: str = ""
tag: str = ""


# SPOILER

# class Project(NamedTuple):
# short_name: str
# name: str = ""
# description: str = ""
# customer: str = ""
# hour_rate: str = ""
Empty file added cli/trakcli/dev/__init__.py
Empty file.
Loading

0 comments on commit b3b879e

Please sign in to comment.