Clean code up
- Remove unnecessary database query methods - Remove horrible db column names - Convert static methods to class methods
This commit is contained in:
parent
d45acedc5a
commit
3e33256e65
58
Database.py
58
Database.py
|
@ -1,58 +0,0 @@
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
|
|
||||||
class Database(object):
|
|
||||||
|
|
||||||
def __init__(self, path):
|
|
||||||
self.conn = sqlite3.connect(path)
|
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
self.conn.close()
|
|
||||||
|
|
||||||
def create_table(self, name, row_list):
|
|
||||||
"""creates table 'name' with rows from 'row_list' if it doesn't exist"""
|
|
||||||
|
|
||||||
if self.table_exists(name):
|
|
||||||
return True
|
|
||||||
|
|
||||||
else:
|
|
||||||
rows = ""
|
|
||||||
separator = ", "
|
|
||||||
for i in range(0, len(row_list)):
|
|
||||||
if i == len(row_list) - 1:
|
|
||||||
separator = ""
|
|
||||||
rows += "%s%s" % (row_list[i], separator)
|
|
||||||
|
|
||||||
self.conn.execute("CREATE TABLE %s(%s)" % (name, rows))
|
|
||||||
if self.table_exists(name):
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def table_exists(self, name):
|
|
||||||
"""returns true if table 'name' exists"""
|
|
||||||
|
|
||||||
result = self.conn.execute("""SELECT * FROM sqlite_master
|
|
||||||
WHERE type='table' AND name=?""", (name,))
|
|
||||||
if result.fetchone() is None:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
def insert(self, log_entry, table):
|
|
||||||
result = self.conn.execute("""INSERT INTO %s(NAME,
|
|
||||||
FINGERPRINT, START_TIME, DURATION, FILE_COUNT) VALUES(?,?,?,?,?)"""
|
|
||||||
% table,
|
|
||||||
(log_entry.name,
|
|
||||||
log_entry.fingerprint,
|
|
||||||
log_entry.datetime_string(),
|
|
||||||
log_entry.duration,
|
|
||||||
log_entry.file_count))
|
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def query(self, query):
|
|
||||||
return self.conn.execute(query).fetchall()
|
|
||||||
|
|
||||||
def query_year(self, table, year):
|
|
||||||
return self.query("""SELECT * FROM %s WHERE strftime(\"%%Y\",
|
|
||||||
START_TIME) == \"%s\"""" % (table, year))
|
|
73
LogEntry.py
73
LogEntry.py
|
@ -1,73 +0,0 @@
|
||||||
from datetime import *
|
|
||||||
import re
|
|
||||||
|
|
||||||
|
|
||||||
class LogEntry(object):
|
|
||||||
|
|
||||||
def __init__(self, name, fingerprint, datetime_string, duration_string,
|
|
||||||
file_count):
|
|
||||||
self.name = name
|
|
||||||
self.fingerprint = fingerprint
|
|
||||||
self.datetime = get_datetime(datetime_string)
|
|
||||||
self.duration = get_duration(duration_string)
|
|
||||||
self.file_count = file_count
|
|
||||||
|
|
||||||
def print_to_file(self, filename):
|
|
||||||
x = open(filename, "w")
|
|
||||||
|
|
||||||
x.write("name: %s\n" % self.name)
|
|
||||||
x.write("fingerprint: %s\n" % self.fingerprint)
|
|
||||||
x.write("date: %s time: %s\n" %
|
|
||||||
(self.datetime.date(), self.datetime.time()))
|
|
||||||
x.write("duration: %s\n" % self.duration)
|
|
||||||
x.write("file_count: %s\n" % self.file_count)
|
|
||||||
|
|
||||||
x.close()
|
|
||||||
|
|
||||||
def datetime_string(self):
|
|
||||||
x = self.datetime
|
|
||||||
return "%04d-%02d-%02d %02d:%02d:%02d" % (x.year, x.month, x.day,
|
|
||||||
x.hour, x.minute, x.second)
|
|
||||||
|
|
||||||
|
|
||||||
def get_duration(duration_string):
|
|
||||||
total_seconds = 0.0
|
|
||||||
|
|
||||||
seconds = re.search(r"((\d+)\.(\d+)|(\d+))\s(second|seconds)",
|
|
||||||
duration_string)
|
|
||||||
minutes = re.search(r"((\d+)\.(\d+)|(\d+))\s(minute|minutes)",
|
|
||||||
duration_string)
|
|
||||||
hours = re.search(r"((\d+)\.(\d+)|(\d+))\s(hour|hours)",
|
|
||||||
duration_string)
|
|
||||||
|
|
||||||
if seconds is not None:
|
|
||||||
seconds = seconds.group().strip(" seconds")
|
|
||||||
seconds = float(seconds)
|
|
||||||
total_seconds += seconds
|
|
||||||
if minutes is not None:
|
|
||||||
minutes = minutes.group().strip(" minutes")
|
|
||||||
minutes = float(minutes)
|
|
||||||
total_seconds += minutes * 60
|
|
||||||
if hours is not None:
|
|
||||||
hours = hours.group().strip(" hours")
|
|
||||||
hours = float(hours)
|
|
||||||
total_seconds += hours * 3600
|
|
||||||
|
|
||||||
return total_seconds
|
|
||||||
|
|
||||||
|
|
||||||
def get_datetime(datetime_string):
|
|
||||||
date_string = re.search(r"....-..-..", datetime_string).group()
|
|
||||||
time_string = re.search(r"..:..:..", datetime_string).group()
|
|
||||||
|
|
||||||
year = int(date_string[0:4])
|
|
||||||
month = int(date_string[5:7])
|
|
||||||
day = int(date_string[8:10])
|
|
||||||
|
|
||||||
hour = int(time_string[0:2])
|
|
||||||
minute = int(time_string[3:5])
|
|
||||||
second = int(time_string[6:8])
|
|
||||||
|
|
||||||
converted_datetime = datetime(year, month, day, hour, minute, second)
|
|
||||||
|
|
||||||
return converted_datetime
|
|
42
main.py
42
main.py
|
@ -1,42 +0,0 @@
|
||||||
from sys import stdin
|
|
||||||
from LogEntry import *
|
|
||||||
from Database import *
|
|
||||||
|
|
||||||
raw_borg_output = stdin.readlines()
|
|
||||||
|
|
||||||
# attribute names of log based on borg output
|
|
||||||
attributes = {"Archive name: ": "",
|
|
||||||
"Archive fingerprint: ": "",
|
|
||||||
"Time (start): ": "",
|
|
||||||
"Duration: ": "",
|
|
||||||
"Number of files: ": ""}
|
|
||||||
|
|
||||||
for i in range(0, len(raw_borg_output)):
|
|
||||||
|
|
||||||
# Look for lines matching attribute names
|
|
||||||
for key in attributes:
|
|
||||||
if raw_borg_output[i].startswith(key):
|
|
||||||
attributes[key] = raw_borg_output[i] \
|
|
||||||
.strip(key) \
|
|
||||||
.rstrip()
|
|
||||||
|
|
||||||
borg_output = LogEntry(attributes["Archive name: "],
|
|
||||||
attributes["Archive fingerprint: "],
|
|
||||||
attributes["Time (start): "],
|
|
||||||
attributes["Duration: "],
|
|
||||||
attributes["Number of files: "])
|
|
||||||
|
|
||||||
borg_output.print_to_file("borg.txt")
|
|
||||||
|
|
||||||
database = Database("borg.db")
|
|
||||||
|
|
||||||
row_list = ["ID INTEGER PRIMARY KEY",
|
|
||||||
"NAME TEXT NOT NULL",
|
|
||||||
"FINGERPRINT TEXT NOT NULL",
|
|
||||||
"START_TIME TEXT NOT NULL",
|
|
||||||
"DURATION REAL NOT NULL",
|
|
||||||
"FILE_COUNT INTEGER NOT NULL"]
|
|
||||||
|
|
||||||
database.create_table("log_entries", row_list)
|
|
||||||
|
|
||||||
database.insert(borg_output, "log_entries")
|
|
32
src/database.py
Normal file
32
src/database.py
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
from logentry import LogEntry
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
|
class Database(object):
|
||||||
|
def __init__(self, path):
|
||||||
|
self.conn = sqlite3.connect(path)
|
||||||
|
self.table_name = "log"
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
self.conn.close()
|
||||||
|
|
||||||
|
def commit(self):
|
||||||
|
self.conn.commit()
|
||||||
|
|
||||||
|
def create_log_table(self):
|
||||||
|
query = f"""CREATE TABLE IF NOT EXISTS {self.table_name} (
|
||||||
|
logID integer PRIMARY KEY,
|
||||||
|
name text NOT NULL,
|
||||||
|
fingerprint text NOT NULL,
|
||||||
|
start text NOT NULL,
|
||||||
|
end text NOT NULL,
|
||||||
|
duration long NOT NULL,
|
||||||
|
filecount long NOT NULL);"""
|
||||||
|
self.conn.execute(query)
|
||||||
|
self.commit()
|
||||||
|
|
||||||
|
def insert(self, log_entry: LogEntry):
|
||||||
|
query = f"INSERT INTO {self.table_name} (name, fingerprint, start, end, duration, filecount) VALUES(?,?,?,?,?)"
|
||||||
|
self.conn.execute(query, (log_entry.name, log_entry.fingerprint, log_entry.start_time, "",
|
||||||
|
log_entry.duration, log_entry.file_count))
|
||||||
|
self.commit()
|
69
src/logentry.py
Normal file
69
src/logentry.py
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
from datetime import *
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
class LogEntry(object):
|
||||||
|
|
||||||
|
def __init__(self, name, fingerprint, start_time, duration_string,
|
||||||
|
file_count):
|
||||||
|
self.name = name
|
||||||
|
self.fingerprint = fingerprint
|
||||||
|
self.start_time = self.get_datetime(start_time)
|
||||||
|
self.duration = self.get_duration(duration_string)
|
||||||
|
self.file_count = file_count
|
||||||
|
|
||||||
|
def print_to_file(self, filename):
|
||||||
|
with open(filename, "w") as file:
|
||||||
|
file.writelines([f"name: {self.name}",
|
||||||
|
f"fingerprint: {self.fingerprint}",
|
||||||
|
f"start: {self.start_time.date()} time: {self.start_time.time()}",
|
||||||
|
f"duration: {self.duration}",
|
||||||
|
f"file_count: {self.file_count}"])
|
||||||
|
|
||||||
|
def datetime_string(self):
|
||||||
|
s = self.start_time
|
||||||
|
return "%04d-%02d-%02d %02d:%02d:%02d" % (s.year, s.month, s.day,
|
||||||
|
s.hour, s.minute, s.second)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_duration(duration_string):
|
||||||
|
total_seconds = 0.0
|
||||||
|
|
||||||
|
seconds = re.search(r"((\d+)\.(\d+)|(\d+))\s(second|seconds)",
|
||||||
|
duration_string)
|
||||||
|
minutes = re.search(r"((\d+)\.(\d+)|(\d+))\s(minute|minutes)",
|
||||||
|
duration_string)
|
||||||
|
hours = re.search(r"((\d+)\.(\d+)|(\d+))\s(hour|hours)",
|
||||||
|
duration_string)
|
||||||
|
|
||||||
|
if seconds is not None:
|
||||||
|
seconds = seconds.group().strip(" seconds")
|
||||||
|
seconds = float(seconds)
|
||||||
|
total_seconds += seconds
|
||||||
|
if minutes is not None:
|
||||||
|
minutes = minutes.group().strip(" minutes")
|
||||||
|
minutes = float(minutes)
|
||||||
|
total_seconds += minutes * 60
|
||||||
|
if hours is not None:
|
||||||
|
hours = hours.group().strip(" hours")
|
||||||
|
hours = float(hours)
|
||||||
|
total_seconds += hours * 3600
|
||||||
|
|
||||||
|
return total_seconds
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_datetime(datetime_string):
|
||||||
|
date_string = re.search(r"....-..-..", datetime_string).group()
|
||||||
|
time_string = re.search(r"..:..:..", datetime_string).group()
|
||||||
|
|
||||||
|
year = int(date_string[0:4])
|
||||||
|
month = int(date_string[5:7])
|
||||||
|
day = int(date_string[8:10])
|
||||||
|
|
||||||
|
hour = int(time_string[0:2])
|
||||||
|
minute = int(time_string[3:5])
|
||||||
|
second = int(time_string[6:8])
|
||||||
|
|
||||||
|
converted_datetime = datetime(year, month, day, hour, minute, second)
|
||||||
|
|
||||||
|
return converted_datetime
|
38
src/main.py
Normal file
38
src/main.py
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
from sys import stdin
|
||||||
|
from src.logentry import LogEntry
|
||||||
|
from src.database import Database
|
||||||
|
|
||||||
|
|
||||||
|
def main(input_lines: list):
|
||||||
|
raw_borg_output = input_lines
|
||||||
|
|
||||||
|
borg_log_entry = create_log_entry(raw_borg_output)
|
||||||
|
borg_log_entry.print_to_file("borg.txt")
|
||||||
|
|
||||||
|
database = Database("borg.db")
|
||||||
|
database.insert(borg_log_entry)
|
||||||
|
|
||||||
|
|
||||||
|
def create_log_entry(raw_borg_output: list):
|
||||||
|
attributes = {"Archive name: ": "",
|
||||||
|
"Archive fingerprint: ": "",
|
||||||
|
"Time (start): ": "",
|
||||||
|
"Duration: ": "",
|
||||||
|
"Number of files: ": ""}
|
||||||
|
|
||||||
|
for i in range(0, len(raw_borg_output)):
|
||||||
|
for key in attributes:
|
||||||
|
if raw_borg_output[i].startswith(key):
|
||||||
|
attributes[key] = raw_borg_output[i] \
|
||||||
|
.strip(key) \
|
||||||
|
.rstrip()
|
||||||
|
|
||||||
|
return LogEntry(attributes["Archive name: "],
|
||||||
|
attributes["Archive fingerprint: "],
|
||||||
|
attributes["Time (start): "],
|
||||||
|
attributes["Duration: "],
|
||||||
|
attributes["Number of files: "])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main(stdin.readlines())
|
Loading…
Reference in New Issue
Block a user