Use f-strings
Avoid unnecessary casts to str()bool()int()
Ensure file closure
Merge nested ifs
Simplify for-assigns-appends with comprehensions and internal functions
Avoid shadowing internal function names
Initialize variables
Return value directly instead of assign then return
Make some methods static
This commit is contained in:
alfrix
2022-07-04 20:50:34 -03:00
committed by Alfredo Monclus
parent 68d4f9c534
commit 6510b2ec6b
55 changed files with 1607 additions and 1798 deletions

View File

@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import contextlib
import gi
import logging
import os
@@ -19,10 +20,7 @@ class KlippyGtk:
self.height = height
self.themedir = os.path.join(pathlib.Path(__file__).parent.resolve().parent, "styles", theme, "images")
if self.screen.vertical_mode:
self.font_ratio = [33, 49]
else:
self.font_ratio = [43, 29]
self.font_ratio = [33, 49] if self.screen.vertical_mode else [43, 29]
self.font_size = int(min(
self.width / self.font_ratio[0],
self.height / self.font_ratio[1]
@@ -50,7 +48,7 @@ class KlippyGtk:
rgb = [int(self.color_list[key]['base'][i:i + 2], 16) for i in range(0, 6, 2)]
self.color_list[key]['rgb'] = rgb
logging.debug("img width: %s height: %s" % (self.img_width, self.img_height))
logging.debug(f"img width: {self.img_width} height: {self.img_height}")
def get_action_bar_width(self):
return self.action_bar_width
@@ -99,9 +97,8 @@ class KlippyGtk:
if self.color_list[device]['state'] > 0:
rgb[1] = rgb[1] + self.color_list[device]['hsplit'] * self.color_list[device]['state']
self.color_list[device]['state'] += 1
color = '{:02X}{:02X}{:02X}'.format(rgb[0], rgb[1], rgb[2])
rgb = [x / 255 for x in rgb]
# logging.debug("Assigning color: %s %s %s" % (device, rgb, color))
# logging.debug(f"Assigning color: {device} {rgb}")
else:
colors = self.color_list[device]['colors']
if self.color_list[device]['state'] >= len(colors):
@@ -109,22 +106,22 @@ class KlippyGtk:
color = colors[self.color_list[device]['state'] % len(colors)]
rgb = [int(color[i:i + 2], 16) / 255 for i in range(0, 6, 2)]
self.color_list[device]['state'] += 1
# logging.debug("Assigning color: %s %s %s" % (device, rgb, color))
return rgb, color
# logging.debug(f"Assigning color: {device} {rgb} {color}")
return rgb
def reset_temp_color(self):
for key in self.color_list:
self.color_list[key]['state'] = 0
def Label(self, label, style=None):
@staticmethod
def Label(label, style=None):
la = Gtk.Label(label)
if style is not None:
la.get_style_context().add_class(style)
return la
def Image(self, image_name, scale=1.0):
filename = os.path.join(self.themedir, str(image_name) + ".svg")
filename = os.path.join(self.themedir, f"{image_name}.svg")
if os.path.exists(filename):
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename,
int(round(self.img_width * scale)),
@@ -132,35 +129,28 @@ class KlippyGtk:
True)
return Gtk.Image.new_from_pixbuf(pixbuf)
else:
logging.error("Unable to find image %s", filename)
logging.error(f"Unable to find image {filename}")
return Gtk.Image()
def PixbufFromFile(self, filename, width_scale=1, height_scale=1):
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename,
int(round(self.img_width * width_scale)),
int(round(self.img_height * height_scale)),
True)
return pixbuf
return GdkPixbuf.Pixbuf.new_from_file_at_scale(
filename,
int(round(self.img_width * width_scale)),
int(round(self.img_height * height_scale)),
True
)
def PixbufFromHttp(self, resource, width_scale=1, height_scale=1):
response = self.screen.apiclient.get_thumbnail_stream(resource)
if response is False:
return None
stream = Gio.MemoryInputStream.new_from_data(response, None)
pixbuf = GdkPixbuf.Pixbuf.new_from_stream_at_scale(stream,
int(round(self.img_width * width_scale)),
int(round(self.img_height * height_scale)),
True)
return pixbuf
def ProgressBar(self, style=None):
bar = Gtk.ProgressBar()
if style is not None:
ctx = bar.get_style_context()
ctx.add_class(style)
return bar
return GdkPixbuf.Pixbuf.new_from_stream_at_scale(
stream,
int(round(self.img_width * width_scale)),
int(round(self.img_height * height_scale)),
True
)
def Button(self, label=None, style=None):
b = Gtk.Button(label=label)
@@ -187,14 +177,11 @@ class KlippyGtk:
b.set_always_show_image(True)
if word_wrap is True:
try:
with contextlib.suppress(Exception):
# Get the label object
child = b.get_children()[0].get_children()[0].get_children()[1]
child.set_line_wrap_mode(Pango.WrapMode.WORD_CHAR)
child.set_line_wrap(True)
except Exception:
pass
if style is not None:
b.get_style_context().add_class(style)
b.connect("clicked", self.screen.reset_screensaver_timeout)
@@ -250,7 +237,8 @@ class KlippyGtk:
b.connect("clicked", self.screen.reset_screensaver_timeout)
return b
def HomogeneousGrid(self, width=None, height=None):
@staticmethod
def HomogeneousGrid(width=None, height=None):
g = Gtk.Grid()
g.set_row_homogeneous(True)
g.set_column_homogeneous(True)
@@ -266,40 +254,11 @@ class KlippyGtk:
b.connect("clicked", self.screen.reset_screensaver_timeout)
return b
def ScrolledWindow(self):
@staticmethod
def ScrolledWindow():
scroll = Gtk.ScrolledWindow()
scroll.set_property("overlay-scrolling", False)
scroll.set_vexpand(True)
scroll.add_events(Gdk.EventMask.TOUCH_MASK)
scroll.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
return scroll
def formatFileName(self, name):
name = name.split('/')[-1] if "/" in name else name
name = name.split('.gcod')[0] if ".gcode" in name else name
if len(name) > 25:
return name[0:25] + "\n" + name[25:50]
return name
def formatTimeString(self, seconds):
time = int(seconds)
text = ""
if int(time / 86400) != 0:
text += str(int(time / 86400)) + "d "
if int(time / 3600) != 0:
text += str(int(time / 3600) % 24) + "h "
if int(time / 60) != 0:
text += str(int(time / 60) % 60) + "m "
else:
text = str(time % 60) + "s"
return text
def formatTemperatureString(self, temp, target):
if temp is None:
logging.debug("Temp is none")
return
if target is None:
target = 0
if (temp - 2 < target < temp + 2) or round(target, 0) == 0:
return str(round(temp, 1)) + "°C" # °C →"
return str(round(temp)) + " °C\n(" + str(round(target)) + ")"