#!/bin/python3 ######################### # # timeTrack.py # by 4nima # v.0.1 # ######################### # simple time tracking with database ######################### import datetime import sqlite3 import json import os import logging class TimeTrack: def __init__(self, DATABASE='timetrack.db', CONFIG='timetrack.conf'): self.DATABASE = DATABASE self.CONFIG = CONFIG self.USER = 0 self.LOGFILE = 'timetrack.log' logging.basicConfig(filename=self.LOGFILE, level=logging.DEBUG, format='%(asctime)s - %(process)d-%(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S' ) self.load_config() self.db_setup() ## Läd oder erstellt das config file def load_config(self): if os.path.isfile(self.CONFIG): logging.info('Config file found') try: with open(self.CONFIG) as config_data: data = json.load(config_data) except ValueError: logging.error('Config file has no valid JSON syntax') print('TimeTrack wird beendet. Fehler in der JSON-Konfig') quit() else: logging.info('Config file loaded successfully') self.USER = data['user'] else: logging.info('Config file not found') ##> ausgabe nicht in einer zeile config = { 'user' : 1, 'default' : 'interactive' } ##> logging erweitert falls nicht möglich with open(self.CONFIG, "w") as outfile: json.dump(config, outfile) logging.info('config file successfully created') def db_setup(self): if os.path.isfile(self.DATABASE): logging.info('DB was found') else: logging.info('DB not found, create new') sql = [] sql.append(""" CREATE TABLE IF NOT EXISTS time_entries ( id INTEGER PRIMARY KEY AUTOINCREMENT, starttime DATETIME NOT NULL, endtime DATETIME NOT NULL, user_id INTEGER NOT NULL, activity TEXT, catrgory_id INTEGER, client_id INTEGER, lock BOOLEAN ) """) sql.append(""" CREATE TABLE IF NOT EXISTS active_events ( id INTEGER PRIMARY KEY AUTOINCREMENT, starttime DATETIME NOT NULL, user_id INT NOT NULL ) """) sql.append(""" CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, worktime INT, worktime_span INT ) """) sql.append(""" CREATE TABLE IF NOT EXISTS clients ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL ) """) sql.append(""" CREATE TABLE IF NOT EXISTS categories ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL ) """) connect = sqlite3.connect(self.DATABASE) cursor = connect.cursor() for SQL in sql: logging.debug('Run SQL') try: cursor.execute(SQL) except: logging.error('Fail to execute SQL command') logging.debug(SQL) else: logging.info('Success execute SQL command') connect.commit connect.close() ##> Kürzer möglich ? def db_connect(self, SQL, DATA=''): pass def create_user(self, USER=''): if USER == '': username = input('Enter your Name: ') else: username = USER while self.get_users(NAME=username): print('Name ist schon vergeben') username = input('Wähle einen anderen Namen: ') connect = sqlite3.connect(self.DATABASE) cursor = connect.cursor() sql = "INSERT INTO users ( name ) values ( ? )" try: cursor.execute(sql, [username]) except: logging.error('Fail to execute SQL command') logging.debug(sql) else: logging.info('Success execute SQL command') connect.commit() connect.close() ##> verbugt noch nicht funktional def get_users(self, UID=0, NAME=''): if not UID == 0: logging.info('UID Used') data = UID sql = "SELECT name FROM users WHERE id = ?" elif not NAME == '': logging.info('NAME used') data = NAME sql = "SELECT name FROM users WHERE name = ?" else: logging.info('nothing used') data = '' sql = "SELECT * FROM users" connect = sqlite3.connect(self.DATABASE) cursor = connect.cursor() try: if data: cursor.execute(sql, [data]) else: cursor.execute(sql) except: logging.error('Fail to execute SQL command') logging.debug(sql) else: logging.info('Success execute SQL command') connect.commit() row = cursor.fetchall() output = [] for data in row: print(data) output.append(data) connect.close() return output def set_user(self): pass test = TimeTrack() test.create_user() #test.get_users(NAME=input('Test:'))