-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
68 lines (59 loc) · 1.82 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import json
from PyQt5 import QtWidgets
appname = "time-tracker"
SETTINGS_PATH = f"./settings.json"
PROJECT_TEMPLATE={"projects": []}
SETTINGS_TEMPLATE={"data_path": ""}
def open_dialog(ui_dialog):
# Generic function to display a QDialog.
dialog = QtWidgets.QDialog()
dialog.ui = ui_dialog()
dialog.ui.setupUi(dialog)
dialog.exec()
def read_json(path):
try:
f = open(path, "r")
content = json.loads(f.read())
f.close()
return content
except FileNotFoundError:
os.makedirs(USER_CONFIG_DIR, exist_ok=True)
open(path, "r").close()
if "settings.json" in path:
return SETTINGS_TEMPLATE
else:
return PROJECT_TEMPLATE
def get_data_path():
# Reads the settings file to get the data file path
settings = read_json(SETTINGS_PATH)
if "data_path" in settings.keys():
return settings.get("data_path")
else:
set_data_path("") # resets data_path to ""
return ""
def is_valid_file(path):
if os.path.exists(path):
data = read_json(path)
return "projects" in data.keys() and type(data.get("projects")) == list
else:
return False
def set_data_path(path):
# Writes the settings file with a new data file path
settings = {"data_path": path}
with open(SETTINGS_PATH, "w+") as f:
json.dump(settings, f)
def format_timestamp(timestamp):
# Returns a formatted timestamp and the font size for
# it to be displayed.
if "," in timestamp:
days = timestamp.split(",")[0]
hms = timestamp.split(",")[1]
hms = hms.split(".")[0]
return f"{days}, {hms}", "48"
else:
if "." in timestamp:
timestamp = timestamp.split(".")[0]
return timestamp, "60"