WIP: config file support
This commit is contained in:
@ -1,16 +1,21 @@
|
||||
#!/usr/bin/env python3.7
|
||||
import os
|
||||
import csv
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from itertools import chain
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import tomllib
|
||||
|
||||
import click
|
||||
import requests
|
||||
from peewee import fn, JOIN
|
||||
from textual.logging import TextualHandler
|
||||
|
||||
from xdg.BaseDirectory import xdg_config_home
|
||||
|
||||
|
||||
from .db import (
|
||||
db,
|
||||
HamsterCategory,
|
||||
@ -21,19 +26,30 @@ from .db import (
|
||||
KimaiActivity,
|
||||
HamsterActivityKimaiMapping,
|
||||
HamsterFactKimaiImport,
|
||||
HamsterFactTag,
|
||||
)
|
||||
from .kimaiapi import KimaiAPI, Timesheet
|
||||
from .sync import sync
|
||||
|
||||
CONFIG_FILE = Path(xdg_config_home) / "hamstertools.toml"
|
||||
|
||||
HAMSTER_DIR = Path.home() / ".local/share/hamster"
|
||||
HAMSTER_FILE = HAMSTER_DIR / "hamster.db"
|
||||
|
||||
db.init(HAMSTER_FILE)
|
||||
|
||||
|
||||
@click.group()
|
||||
@click.group(context_settings={"auto_envvar_prefix": "HAMSTERTOOL"})
|
||||
@click.option("-d", "--debug", is_flag=True)
|
||||
def cli(debug):
|
||||
@click.option("--config", default=CONFIG_FILE, type=click.Path())
|
||||
@click.option("--kimai-api-key", envvar="KIMAI_API_KEY")
|
||||
@click.option("--kimai-username", envvar="KIMAI_USERNAME")
|
||||
@click.option("--kimai-api-key", envvar="KIMAI_API_KEY")
|
||||
@click.pass_context
|
||||
def cli(ctx, config, debug, kimai_api_url, kimai_username, kimai_api_key):
|
||||
if os.path.exists(config):
|
||||
with open(config, "rb") as f:
|
||||
ctx.default_map = tomllib.load(f)
|
||||
if debug:
|
||||
logging.basicConfig()
|
||||
logging.getLogger().setLevel(logging.DEBUG)
|
||||
@ -310,8 +326,12 @@ def find_duplicates():
|
||||
|
||||
|
||||
@cli.group()
|
||||
def kimai():
|
||||
pass
|
||||
@click.pass_context
|
||||
def kimai(ctx, kimai_username=None, kimai_api_key=None):
|
||||
ctx.ensure_object(dict)
|
||||
|
||||
ctx.obj['KIMAI_USERNAME'] = kimai_username
|
||||
ctx.obj['KIMAI_API_KEY'] = kimai_api_key
|
||||
|
||||
|
||||
def _get_kimai_mapping_file(path, category_search=None):
|
||||
@ -354,7 +374,6 @@ def _get_kimai_mapping_file(path, category_search=None):
|
||||
multiple=True,
|
||||
)
|
||||
@click.argument("username")
|
||||
@click.argument("api_key", envvar="KIMAI_API_KEY")
|
||||
@click.option("--just-errors", "just_errors", is_flag=True, help="Only display errors")
|
||||
@click.option("--ignore-activities", is_flag=True, help="Ignore missing activities")
|
||||
def check(username, api_key, just_errors, ignore_activities, mapping_path=None):
|
||||
@ -643,7 +662,7 @@ def _csv(
|
||||
@click.argument("after")
|
||||
@click.argument("before")
|
||||
def _import(search, after, before):
|
||||
api = KimaiAPI()
|
||||
api = KimaiAPI(username=KIMAI_USERNAME, api_key=KIMAI_API_KEY)
|
||||
|
||||
SEARCH = "auto"
|
||||
|
||||
@ -651,6 +670,8 @@ def _import(search, after, before):
|
||||
HamsterFact.select(HamsterFact, HamsterActivity, HamsterCategory)
|
||||
.join(HamsterActivity, JOIN.LEFT_OUTER)
|
||||
.join(HamsterCategory, JOIN.LEFT_OUTER)
|
||||
.switch(HamsterFact)
|
||||
.join(HamsterFactTag, JOIN.LEFT_OUTER)
|
||||
.where(
|
||||
(HamsterFact.start_time > datetime.strptime(after, "%Y-%m-%d"))
|
||||
& (HamsterFact.start_time < datetime.strptime(before, "%Y-%m-%d"))
|
||||
@ -719,15 +740,12 @@ def _import(search, after, before):
|
||||
description=f.description
|
||||
if f.description != ""
|
||||
else mapping.kimai_description,
|
||||
# tags=f.tags if f.tags != '' else mapping.kimai_tags
|
||||
tags=" ".join([t.tag.name for t in f.tags]) if f.tags != '' else mapping.kimai_tags
|
||||
)
|
||||
r = t.upload().json()
|
||||
if r.get("code", 200) != 200:
|
||||
print(r)
|
||||
print(f"{f.id} ({f.activity.category.name} » {f.activity.name})")
|
||||
from pdb import set_trace
|
||||
|
||||
set_trace()
|
||||
|
||||
else:
|
||||
HamsterFactKimaiImport.create(hamster_fact=f, kimai_id=r["id"]).save()
|
||||
@ -758,8 +776,12 @@ def reset():
|
||||
|
||||
|
||||
@db_.command("sync")
|
||||
def kimai_db_sync():
|
||||
sync()
|
||||
@click.pass_context
|
||||
def kimai_db_sync(ctx):
|
||||
sync(
|
||||
username=ctx.obj['KIMAI_USERNAME'],
|
||||
api_key=ctx.obj['KIMAI_API_KEY']
|
||||
)
|
||||
|
||||
|
||||
@db_.command()
|
||||
@ -800,10 +822,15 @@ def mapping2db(mapping_path=None, global_=False):
|
||||
|
||||
|
||||
@cli.command()
|
||||
def app():
|
||||
@click.pass_context
|
||||
def app(ctx):
|
||||
from .app import HamsterToolsApp
|
||||
|
||||
app = HamsterToolsApp()
|
||||
app = HamsterToolsApp(
|
||||
kimai_api_url=ctx.obj["KIMAI_API_URL"],
|
||||
kimai_username=ctx.obj["KIMAI_USERNAME"],
|
||||
kimai_api_key=ctx.obj["KIMAI_API_KEY"]
|
||||
)
|
||||
app.run()
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user