Converting Python scripts to classes

This commit is contained in:
2023-12-25 20:22:31 +01:00
parent 7c5e86e8dc
commit 1a8a986674
12 changed files with 263 additions and 1674 deletions

View File

@@ -1,67 +1,87 @@
import os, json
from enums.PreferenceEnums import PrefenceEnums
# Location of the Cinnamon preference file since Cinnamon 5.4
pref_location = os.path.expanduser("~") + \
"/.config/cinnamon/spices/cinnamon-dynamic-wallpaper@TobiZog/cinnamon-dynamic-wallpaper@TobiZog.json"
class Cinnamon_Pref_Handler:
def __init__(self) -> None:
# Location of the Cinnamon preference file since Cinnamon 5.4
self.pref_location = os.path.expanduser("~") + \
"/.config/cinnamon/spices/cinnamon-dynamic-wallpaper@TobiZog/cinnamon-dynamic-wallpaper@TobiZog.json"
def write_to_preferences(parameter: PrefenceEnums, value: str):
""" Write a preference value to the JSON file
def write_to_preferences(self, parameter: PrefenceEnums, value: str):
""" Write a preference value to the JSON file
Args:
parameter (PrefenceEnums): Name of the parameter
value (str): Value to write
"""
with open(pref_location, "r") as pref_file:
pref_data = json.load(pref_file)
if parameter in pref_data:
pref_data[parameter]["value"] = value
else:
pref_data[parameter] = {
"type": "entry",
"default": "",
"description": "",
"value": value
}
with open(pref_location, "w") as pref_file:
json.dump(pref_data, pref_file, separators=(',', ':'), indent=4)
def read_str_from_preferences(parameter: PrefenceEnums) -> str:
""" Read a value from the JSON file
Args:
parameter (PrefenceEnums): Name of the parameter to get
Returns:
str: Value of the parameter
"""
try:
with open(pref_location, "r") as pref_file:
Args:
parameter (PrefenceEnums): Name of the parameter
value (str): Value to write
"""
with open(self.pref_location, "r") as pref_file:
pref_data = json.load(pref_file)
except:
return ""
if parameter in pref_data:
return pref_data[parameter]["value"]
else:
return ""
def read_int_from_preferences(parameter: PrefenceEnums) -> int:
value = read_str_from_preferences(parameter)
if parameter in pref_data:
pref_data[parameter]["value"] = value
else:
pref_data[parameter] = {
"type": "entry",
"default": "",
"description": "",
"value": value
}
if value == "":
return 0
else:
return int(value)
def read_float_from_preferences(parameter: PrefenceEnums) -> float:
value = read_str_from_preferences(parameter)
with open(self.pref_location, "w") as pref_file:
json.dump(pref_data, pref_file, separators=(',', ':'), indent=4)
if value == "":
return 0.0
else:
return float(value)
def read_str_from_preferences(self, parameter: PrefenceEnums) -> str:
""" Read a value from the JSON file
Args:
parameter (PrefenceEnums): Name of the parameter to get
Returns:
str: Value of the parameter
"""
try:
with open(self.pref_location, "r") as pref_file:
pref_data = json.load(pref_file)
except:
return ""
if parameter in pref_data:
return pref_data[parameter]["value"]
else:
return ""
def read_int_from_preferences(self, parameter: PrefenceEnums) -> int:
""" Read a value from the JSON file
Args:
parameter (PrefenceEnums): Name of the parameter to get
Returns:
str: Value of the parameter
"""
value = self.read_str_from_preferences(parameter)
if value == "":
return 0
else:
return int(value)
def read_float_from_preferences(self, parameter: PrefenceEnums) -> float:
""" Read a value from the JSON file
Args:
parameter (PrefenceEnums): Name of the parameter to get
Returns:
str: Value of the parameter
"""
value = self.read_str_from_preferences(parameter)
if value == "":
return 0.0
else:
return float(value)

View File

@@ -1,129 +0,0 @@
import math
image_code = []
colors = [
"00193d",
"05597f",
"54babf",
"bfe3c2",
"ffbf6b",
"fdb55c",
"f37f73",
"b45bbc",
"7e38ce",
"00285f"
]
bar_pos_x = []
def create_bar_chart(image_width: int, image_height: int, times: list):
""" Create a time bar chart
Args:
image_width (int): Width of the image in pixel
image_height (int): Height of the image in pixel
times (list): List of start times of the periods in minutes since midnight
"""
create_bar(image_width, image_height, times)
create_polylines(image_width, image_height)
create_time_markers(image_width, image_height)
# Write to file
image_code.insert(0, '<svg xmlns="http://www.w3.org/2000/svg" width="%s" height="%s">' % (image_width, image_height))
image_code.append('</svg>')
file = open("time_bar.svg", "w")
for i in image_code:
file.write(i + '\n')
def create_bar(image_width: int, image_height: int, times: list):
""" Generates the code for the horizontal multi-color bar chart
Args:
image_width (int): Total width of the image
image_height (int): Total height of the image
times (list): List of start times of the periods, in minutes
"""
x = 0
y = 40
width = 0
height = image_height - 80
times.append(1440)
# Adding the bar parts
for i in range(1, len(times)):
width = math.ceil((((100 / 1440) * (times[i] - times[i - 1]) / 100) * image_width))
image_code.append(
'<rect fill="#%s" x="%s" y="%s" width="%s" height="%s"/>' % (colors[i - 1], x, y, width, height)
)
bar_pos_x.append(x)
x += width
def create_time_markers(image_width: int, image_height: int):
""" Generates the code for the vertical hour markers
Args:
image_width (int): Total width of the image
image_height (int): Total height of the image
"""
for i in range(0, 8):
image_code.append(
'<line x1="%s" y1="40" x2="%s" y2="%s" stroke="gray" stroke-width="2" />' %
(i * (image_width // 8), i * (image_width // 8), image_height - 40)
)
image_code.append(
'<text x="%s" y="%s" fill="gray" font-size="20" font-family="Liberation Sans">%s</text>' %
(i * (image_width // 8) + 5, image_height - 45, i * 3)
)
def create_polylines(image_width: int, image_height: int):
""" Generates the code for the polylines which connect the images with the bar sections
Args:
image_width (int): Total width of the image
image_height (int): Total height of the image
"""
bar_x_start = 0
bar_pos_x.append(image_width)
for i in range(0, len(bar_pos_x) - 1):
# X-Middle of a bar
bar_mid = bar_x_start + (bar_pos_x[i + 1] - bar_x_start) / 2
# Position of the image in the window
image_x = (image_width - 32) / 10 + ((i // 2) % 5) * image_width / 5
# i == 0, 2, 4, ... => Upper Polylines
if (i % 2 == 0):
polyline_y = 0
else:
polyline_y = image_height
if i == 0 or i == 8:
polyline_x = 30
elif i == 2 or i == 6:
polyline_x = 20
elif i == 1 or i == 9:
polyline_x = image_height - 30
elif i == 3 or i == 7:
polyline_x = image_height - 20
elif i == 5:
polyline_x = image_height - 10
else:
polyline_x = 10
image_code.append(
'<polyline points="%s,%s %s,%s %s,%s %s,%s" stroke="#%s" fill="none" stroke-width="5" />' %
(image_x, polyline_y, image_x, polyline_x, bar_mid, polyline_x, bar_mid, image_height / 2, colors[i])
)
# Store the end point of the bar as start point of the next
bar_x_start = bar_pos_x[i + 1]

View File

@@ -0,0 +1,132 @@
import math
class Time_Bar_Chart:
def __init__(self) -> None:
self.image_code = []
self.colors = [
"00193d",
"05597f",
"54babf",
"bfe3c2",
"ffbf6b",
"fdb55c",
"f37f73",
"b45bbc",
"7e38ce",
"00285f"
]
self.bar_pos_x = []
def create_bar_chart(self, image_width: int, image_height: int, times: list):
""" Create a time bar chart
Args:
image_width (int): Width of the image in pixel
image_height (int): Height of the image in pixel
times (list): List of start times of the periods in minutes since midnight
"""
self.create_bar(image_width, image_height, times)
self.create_polylines(image_width, image_height)
self.create_time_markers(image_width, image_height)
# Write to file
self.image_code.insert(0, '<svg xmlns="http://www.w3.org/2000/svg" width="%s" height="%s">' % (image_width, image_height))
self.image_code.append('</svg>')
file = open("time_bar.svg", "w")
for i in self.image_code:
file.write(i + '\n')
def create_bar(self, image_width: int, image_height: int, times: list):
""" Generates the code for the horizontal multi-color bar chart
Args:
image_width (int): Total width of the image
image_height (int): Total height of the image
times (list): List of start times of the periods, in minutes
"""
x = 0
y = 40
width = 0
height = image_height - 80
times.append(1440)
# Adding the bar parts
for i in range(1, len(times)):
width = math.ceil((((100 / 1440) * (times[i] - times[i - 1]) / 100) * image_width))
self.image_code.append(
'<rect fill="#%s" x="%s" y="%s" width="%s" height="%s"/>' % (self.colors[i - 1], x, y, width, height)
)
self.bar_pos_x.append(x)
x += width
def create_time_markers(self, image_width: int, image_height: int):
""" Generates the code for the vertical hour markers
Args:
image_width (int): Total width of the image
image_height (int): Total height of the image
"""
for i in range(0, 8):
self.image_code.append(
'<line x1="%s" y1="40" x2="%s" y2="%s" stroke="gray" stroke-width="2" />' %
(i * (image_width // 8), i * (image_width // 8), image_height - 40)
)
self.image_code.append(
'<text x="%s" y="%s" fill="gray" font-size="20" font-family="Liberation Sans">%s</text>' %
(i * (image_width // 8) + 5, image_height - 45, i * 3)
)
def create_polylines(self, image_width: int, image_height: int):
""" Generates the code for the polylines which connect the images with the bar sections
Args:
image_width (int): Total width of the image
image_height (int): Total height of the image
"""
bar_x_start = 0
self.bar_pos_x.append(image_width)
for i in range(0, len(self.bar_pos_x) - 1):
# X-Middle of a bar
bar_mid = bar_x_start + (self.bar_pos_x[i + 1] - bar_x_start) / 2
# Position of the image in the window
image_x = (image_width - 32) / 10 + ((i // 2) % 5) * image_width / 5
# i == 0, 2, 4, ... => Upper Polylines
if (i % 2 == 0):
polyline_y = 0
else:
polyline_y = image_height
if i == 0 or i == 8:
polyline_x = 30
elif i == 2 or i == 6:
polyline_x = 20
elif i == 1 or i == 9:
polyline_x = image_height - 30
elif i == 3 or i == 7:
polyline_x = image_height - 20
elif i == 5:
polyline_x = image_height - 10
else:
polyline_x = 10
self.image_code.append(
'<polyline points="%s,%s %s,%s %s,%s %s,%s" stroke="#%s" fill="none" stroke-width="5" />' %
(image_x, polyline_y, image_x, polyline_x, bar_mid, polyline_x, bar_mid, image_height / 2, self.colors[i])
)
# Store the end point of the bar as start point of the next
bar_x_start = self.bar_pos_x[i + 1]