Files
timetrack/timeTrack.py

238 lines
7.3 KiB
Python

#!/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):
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
)
""")
self.db_connect(sql)
##> Kürzer möglich ?
def db_connect(self, SQL, DATA=''):
if os.path.isfile(self.DATABASE):
logging.info('DB was found')
else:
logging.info('DB not found, create new')
connect = sqlite3.connect(self.DATABASE)
cursor = connect.cursor()
if isinstance(SQL, list):
logging.info('Multible SQL commands found')
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
else:
logging.info('Single SQL command found')
sql = SQL
if DATA == '':
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()
else:
logging.info('Data found')
data = DATA
if isinstance(DATA, list):
logging.debug('Data is list')
if isinstance(DATA[0], list):
try:
cursor.executemany(sql, data)
except:
logging.error('Fail to execute SQL command')
logging.debug(sql)
else:
logging.info('Success execute SQL command')
logging.debug(sql)
connect.commit()
else:
try:
cursor.execute(sql, data)
except:
logging.error('Fail to execute SQL command')
logging.debug(sql)
else:
logging.info('Success execute SQL command')
logging.debug(sql)
connect.commit()
else:
logging.debug('Convert Data to list')
logging.debug('Run SQL')
try:
cursor.execute(sql, [data])
except:
logging.error('Fail to execute SQL command')
logging.debug(sql)
logging.debug(data)
else:
logging.info('Success execute SQL command')
logging.debug(sql)
connect.commit()
output = []
for data in cursor:
output.append(data)
connect.close()
return output
def create_user(self, USER=''):
if USER == '':
data = input('Enter your Name: ')
else:
data = USER
while self.get_users(NAME=data):
print('Name ist schon vergeben')
data = input('Wähle einen anderen Namen: ')
sql = """
INSERT INTO users (
name
) values (
?
)
"""
self.db_connect(sql, data)
##> 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')
sql = """
SELECT * FROM users WHERE
"""
return self.db_connect(sql, data)
def set_user(self):
pass
test = TimeTrack()
test.create_user()
#test.get_users(NAME=input('Test:'))