Improved code quality

This commit is contained in:
ArtyIF 2022-07-24 21:43:32 +03:00
parent 935e58f09a
commit f6b8ff04b5
7 changed files with 39 additions and 48 deletions

View file

@ -27,7 +27,6 @@
# authorization. # authorization.
from gi.repository import Gtk, Adw from gi.repository import Gtk, Adw
import json
@Gtk.Template(resource_path='/com/github/ArtyIF/AdwCustomizer/ui/app_type_dialog.ui') @Gtk.Template(resource_path='/com/github/ArtyIF/AdwCustomizer/ui/app_type_dialog.ui')
class AdwcustomizerAppTypeDialog(Adw.MessageDialog): class AdwcustomizerAppTypeDialog(Adw.MessageDialog):
@ -52,4 +51,3 @@ class AdwcustomizerAppTypeDialog(Adw.MessageDialog):
"gtk4": self.gtk4_app_type.get_active(), "gtk4": self.gtk4_app_type.get_active(),
"gtk3": self.gtk3_app_type.get_active() "gtk3": self.gtk3_app_type.get_active()
} }

View file

@ -26,8 +26,7 @@
# use or other dealings in this Software without prior written # use or other dealings in this Software without prior written
# authorization. # authorization.
from gi.repository import Gtk, Gdk, Gio, Adw from gi.repository import Gtk, Adw
import json
@Gtk.Template(resource_path='/com/github/ArtyIF/AdwCustomizer/ui/custom_css_group.ui') @Gtk.Template(resource_path='/com/github/ArtyIF/AdwCustomizer/ui/custom_css_group.ui')
class AdwcustomizerCustomCSSGroup(Adw.PreferencesGroup): class AdwcustomizerCustomCSSGroup(Adw.PreferencesGroup):
@ -49,6 +48,6 @@ class AdwcustomizerCustomCSSGroup(Adw.PreferencesGroup):
Gtk.Application.get_default().update_custom_css_text(list(self.custom_css.keys())[self.app_type_dropdown.get_selected()], buffer.props.text) Gtk.Application.get_default().update_custom_css_text(list(self.custom_css.keys())[self.app_type_dropdown.get_selected()], buffer.props.text)
@Gtk.Template.Callback() @Gtk.Template.Callback()
def on_dropdown_notify(self, widget, pspec): def on_dropdown_notify(self, _, pspec):
if pspec.name == "selected": if pspec.name == "selected":
self.custom_css_text_view.get_buffer().set_text(list(self.custom_css.values())[self.app_type_dropdown.get_selected()]) self.custom_css_text_view.get_buffer().set_text(list(self.custom_css.values())[self.app_type_dropdown.get_selected()])

View file

@ -26,8 +26,7 @@
# use or other dealings in this Software without prior written # use or other dealings in this Software without prior written
# authorization. # authorization.
from gi.repository import Gtk, Gdk, Gio, Adw from gi.repository import Gtk
import json
@Gtk.Template(resource_path='/com/github/ArtyIF/AdwCustomizer/ui/error.ui') @Gtk.Template(resource_path='/com/github/ArtyIF/AdwCustomizer/ui/error.ui')
class AdwcustomizerError(Gtk.ListBoxRow): class AdwcustomizerError(Gtk.ListBoxRow):

View file

@ -27,12 +27,12 @@
# authorization. # authorization.
import sys import sys
import gi
import json import json
import os import os
import re import re
import traceback import traceback
import gi
gi.require_version('Gtk', '4.0') gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1') gi.require_version('Adw', '1')
gi.require_version('Xdp', '1.0') gi.require_version('Xdp', '1.0')
@ -130,26 +130,26 @@ class AdwcustomizerApplication(Adw.Application):
for file in os.listdir(os.environ['XDG_CONFIG_HOME'] + "/presets/"): for file in os.listdir(os.environ['XDG_CONFIG_HOME'] + "/presets/"):
if file.endswith(".json"): if file.endswith(".json"):
try: try:
with open(os.environ['XDG_CONFIG_HOME'] + "/presets/" + file, 'r') as f: with open(os.environ['XDG_CONFIG_HOME'] + "/presets/" + file, 'r') as file:
preset_text = f.read() preset_text = file.read()
preset = json.loads(preset_text) preset = json.loads(preset_text)
if preset.get('variables') is None: if preset.get('variables') is None:
raise KeyError('variables') raise KeyError('variables')
if preset.get('palette') is None: if preset.get('palette') is None:
raise KeyError('palette') raise KeyError('palette')
self.custom_presets[file.replace('.json', '')] = preset['name'] self.custom_presets[file.replace('.json', '')] = preset['name']
except Exception as e: except Exception as ex:
self.global_errors.append({ self.global_errors.append({
"error": "Failed to load preset: " + file, "error": "Failed to load preset: " + file,
"element": str(e), "element": str(ex),
"line": traceback.format_exception(e)[2].strip() "line": traceback.format_exception(ex)[2].strip()
}) })
self.props.active_window.update_errors(self.global_errors) self.props.active_window.update_errors(self.global_errors)
custom_menu_section = Gio.Menu() custom_menu_section = Gio.Menu()
for preset in self.custom_presets.keys(): for preset, preset_name in self.custom_presets.items():
menu_item = Gio.MenuItem() menu_item = Gio.MenuItem()
menu_item.set_label(self.custom_presets[preset]) menu_item.set_label(preset_name)
if not preset.startswith("error"): if not preset.startswith("error"):
menu_item.set_action_and_target_value("app.load_preset", GLib.Variant('s', "custom-" + preset)) menu_item.set_action_and_target_value("app.load_preset", GLib.Variant('s', "custom-" + preset))
else: else:
@ -165,10 +165,10 @@ class AdwcustomizerApplication(Adw.Application):
self.is_ready = True self.is_ready = True
def open_preset_directory(self, *args): def open_preset_directory(self, *_):
parent = XdpGtk4.parent_new_gtk(self.props.active_window) parent = XdpGtk4.parent_new_gtk(self.props.active_window)
def open_dir_callback(result, task): def open_dir_callback(_, result):
self.portal.open_uri_finish(task) self.portal.open_uri_finish(result)
self.portal.open_uri( self.portal.open_uri(
parent, parent,
"file://" + os.environ['XDG_CONFIG_HOME'] + "/presets/", "file://" + os.environ['XDG_CONFIG_HOME'] + "/presets/",
@ -179,8 +179,8 @@ class AdwcustomizerApplication(Adw.Application):
def load_preset_from_file(self, preset_path): def load_preset_from_file(self, preset_path):
preset_text = "" preset_text = ""
with open(preset_path, 'r') as f: with open(preset_path, 'r') as file:
preset_text = f.read() preset_text = file.read()
self.load_preset_variables(json.loads(preset_text)) self.load_preset_variables(json.loads(preset_text))
def load_preset_from_resource(self, preset_path): def load_preset_from_resource(self, preset_path):
@ -217,7 +217,7 @@ class AdwcustomizerApplication(Adw.Application):
parsing_errors = [] parsing_errors = []
gtk_css = self.generate_gtk_css("gtk4") gtk_css = self.generate_gtk_css("gtk4")
css_provider = Gtk.CssProvider() css_provider = Gtk.CssProvider()
def on_error(provider, section, error): def on_error(_, section, error):
start_location = section.get_start_location().chars start_location = section.get_start_location().chars
end_location = section.get_end_location().chars end_location = section.get_end_location().chars
line_number = section.get_end_location().lines line_number = section.get_end_location().lines
@ -235,14 +235,14 @@ class AdwcustomizerApplication(Adw.Application):
Gtk.StyleContext.add_provider_for_display(Gdk.Display.get_default(), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER + 1) Gtk.StyleContext.add_provider_for_display(Gdk.Display.get_default(), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER + 1)
self.current_css_provider = css_provider self.current_css_provider = css_provider
def load_preset_action(self, widget, *args): def load_preset_action(self, _, *args):
if args[0].get_string().startswith("custom-"): if args[0].get_string().startswith("custom-"):
self.load_preset_from_file(os.environ['XDG_CONFIG_HOME'] + "/presets/" + args[0].get_string().replace("custom-", "", 1) + ".json") self.load_preset_from_file(os.environ['XDG_CONFIG_HOME'] + "/presets/" + args[0].get_string().replace("custom-", "", 1) + ".json")
else: else:
self.load_preset_from_resource('/com/github/ArtyIF/AdwCustomizer/presets/' + args[0].get_string() + '.json') self.load_preset_from_resource('/com/github/ArtyIF/AdwCustomizer/presets/' + args[0].get_string() + '.json')
Gio.SimpleAction.set_state(self.lookup_action("load_preset"), args[0]) Gio.SimpleAction.set_state(self.lookup_action("load_preset"), args[0])
def show_apply_color_scheme_dialog(self, widget, _): def show_apply_color_scheme_dialog(self, *_):
dialog = AdwcustomizerAppTypeDialog("Apply this color scheme?", dialog = AdwcustomizerAppTypeDialog("Apply this color scheme?",
"Warning: any custom CSS files for those app types will be irreversibly overwritten!", "Warning: any custom CSS files for those app types will be irreversibly overwritten!",
"apply", "Apply", Adw.ResponseAppearance.SUGGESTED, "apply", "Apply", Adw.ResponseAppearance.SUGGESTED,
@ -250,7 +250,7 @@ class AdwcustomizerApplication(Adw.Application):
dialog.connect("response", self.apply_color_scheme) dialog.connect("response", self.apply_color_scheme)
dialog.present() dialog.present()
def show_reset_color_scheme_dialog(self, widget, _): def show_reset_color_scheme_dialog(self, *_):
dialog = AdwcustomizerAppTypeDialog("Reset applied color scheme?", dialog = AdwcustomizerAppTypeDialog("Reset applied color scheme?",
"Make sure you have the current settings saved as a preset.", "Make sure you have the current settings saved as a preset.",
"reset", "Reset", Adw.ResponseAppearance.DESTRUCTIVE, "reset", "Reset", Adw.ResponseAppearance.DESTRUCTIVE,
@ -258,7 +258,7 @@ class AdwcustomizerApplication(Adw.Application):
dialog.connect("response", self.reset_color_scheme) dialog.connect("response", self.reset_color_scheme)
dialog.present() dialog.present()
def show_save_preset_dialog(self, widget, _): def show_save_preset_dialog(self, *_):
dialog = Adw.MessageDialog(transient_for=self.props.active_window, dialog = Adw.MessageDialog(transient_for=self.props.active_window,
heading="Save preset as...", heading="Save preset as...",
body="Saving to <tt>$XDG_CONFIG_HOME/presets/</tt>", body="Saving to <tt>$XDG_CONFIG_HOME/presets/</tt>",
@ -272,7 +272,7 @@ class AdwcustomizerApplication(Adw.Application):
dialog.set_close_response("cancel") dialog.set_close_response("cancel")
preset_entry = Gtk.Entry(placeholder_text="Preset Name") preset_entry = Gtk.Entry(placeholder_text="Preset Name")
def on_preset_entry_change(self, *args): def on_preset_entry_change(*_):
if len(preset_entry.get_text()) == 0: if len(preset_entry.get_text()) == 0:
dialog.set_body("Saving to <tt>$XDG_CONFIG_HOME/presets/</tt>") dialog.set_body("Saving to <tt>$XDG_CONFIG_HOME/presets/</tt>")
dialog.set_response_enabled("save", False) dialog.set_response_enabled("save", False)
@ -286,27 +286,27 @@ class AdwcustomizerApplication(Adw.Application):
dialog.present() dialog.present()
def save_preset(self, widget, response, entry): def save_preset(self, _, response, entry):
if response == "save": if response == "save":
with open(os.environ['XDG_CONFIG_HOME'] + "/presets/" + to_slug_case(entry.get_text()) + ".json", 'w') as f: with open(os.environ['XDG_CONFIG_HOME'] + "/presets/" + to_slug_case(entry.get_text()) + ".json", 'w') as file:
object_to_write = { object_to_write = {
"name": entry.get_text(), "name": entry.get_text(),
"variables": self.variables, "variables": self.variables,
"palette": self.palette, "palette": self.palette,
"custom_css": self.custom_css "custom_css": self.custom_css
} }
f.write(json.dumps(object_to_write, indent=4)) file.write(json.dumps(object_to_write, indent=4))
def apply_color_scheme(self, widget, response): def apply_color_scheme(self, widget, response):
if response == "apply": if response == "apply":
if widget.get_app_types()["gtk4"]: if widget.get_app_types()["gtk4"]:
gtk4_css = self.generate_gtk_css("gtk4") gtk4_css = self.generate_gtk_css("gtk4")
with open(os.environ['XDG_CONFIG_HOME'] + "/gtk-4.0/gtk.css", 'w') as f: with open(os.environ['XDG_CONFIG_HOME'] + "/gtk-4.0/gtk.css", 'w') as file:
f.write(gtk4_css) file.write(gtk4_css)
if widget.get_app_types()["gtk3"]: if widget.get_app_types()["gtk3"]:
gtk3_css = self.generate_gtk_css("gtk3") gtk3_css = self.generate_gtk_css("gtk3")
with open(os.environ['XDG_CONFIG_HOME'] + "/gtk-3.0/gtk.css", 'w') as f: with open(os.environ['XDG_CONFIG_HOME'] + "/gtk-3.0/gtk.css", 'w') as file:
f.write(gtk3_css) file.write(gtk3_css)
def reset_color_scheme(self, widget, response): def reset_color_scheme(self, widget, response):
if response == "reset": if response == "reset":
@ -323,7 +323,7 @@ class AdwcustomizerApplication(Adw.Application):
except: except:
pass pass
def show_about_window(self, widget, _): def show_about_window(self, *_):
about = Adw.AboutWindow(transient_for=self.props.active_window, about = Adw.AboutWindow(transient_for=self.props.active_window,
application_name='Adwaita Manager', application_name='Adwaita Manager',
application_icon='com.github.ArtyIF.AdwCustomizer', application_icon='com.github.ArtyIF.AdwCustomizer',

View file

@ -26,8 +26,7 @@
# use or other dealings in this Software without prior written # use or other dealings in this Software without prior written
# authorization. # authorization.
from gi.repository import Gtk, Gdk, Gio, Adw from gi.repository import Gtk, Gdk, Adw
import json
@Gtk.Template(resource_path='/com/github/ArtyIF/AdwCustomizer/ui/option.ui') @Gtk.Template(resource_path='/com/github/ArtyIF/AdwCustomizer/ui/option.ui')
class AdwcustomizerOption(Adw.ActionRow): class AdwcustomizerOption(Adw.ActionRow):
@ -62,16 +61,16 @@ class AdwcustomizerOption(Adw.ActionRow):
self.explanation_button.set_visible(False) self.explanation_button.set_visible(False)
@Gtk.Template.Callback() @Gtk.Template.Callback()
def on_color_value_changed(self, *args): def on_color_value_changed(self, *_):
self.update_value(self.color_value.get_rgba().to_string(), update_from="color_value") self.update_value(self.color_value.get_rgba().to_string(), update_from="color_value")
@Gtk.Template.Callback() @Gtk.Template.Callback()
def on_text_value_changed(self, *args): def on_text_value_changed(self, *_):
self.update_value(self.text_value.get_text(), update_from="text_value") self.update_value(self.text_value.get_text(), update_from="text_value")
@Gtk.Template.Callback() @Gtk.Template.Callback()
def on_text_value_toggled(self, *args): def on_text_value_toggled(self, *_):
if (self.text_value_toggle.get_active()): if self.text_value_toggle.get_active():
self.value_stack.set_visible_child(self.text_value) self.value_stack.set_visible_child(self.text_value)
else: else:
self.value_stack.set_visible_child(self.color_value) self.value_stack.set_visible_child(self.color_value)
@ -96,4 +95,3 @@ class AdwcustomizerOption(Adw.ActionRow):
if Gtk.Application.get_default().is_ready and kwargs.get("update_from") == "text_value" and new_value != "": if Gtk.Application.get_default().is_ready and kwargs.get("update_from") == "text_value" and new_value != "":
Gtk.Application.get_default().variables[self.get_name()] = new_value Gtk.Application.get_default().variables[self.get_name()] = new_value
Gtk.Application.get_default().reload_variables() Gtk.Application.get_default().reload_variables()

View file

@ -26,8 +26,7 @@
# use or other dealings in this Software without prior written # use or other dealings in this Software without prior written
# authorization. # authorization.
from gi.repository import Gtk, Gdk, Gio, Adw from gi.repository import Gtk, Gdk, Adw
import json
@Gtk.Template(resource_path='/com/github/ArtyIF/AdwCustomizer/ui/palette_shades.ui') @Gtk.Template(resource_path='/com/github/ArtyIF/AdwCustomizer/ui/palette_shades.ui')
class AdwcustomizerPaletteShades(Adw.ActionRow): class AdwcustomizerPaletteShades(Adw.ActionRow):
@ -50,10 +49,10 @@ class AdwcustomizerPaletteShades(Adw.ActionRow):
self.color_pickers[str(i)] = picker self.color_pickers[str(i)] = picker
self.add_suffix(picker) self.add_suffix(picker)
def on_color_changed(self, *args): def on_color_changed(self, *_):
shades = {} shades = {}
for picker_key in self.color_pickers.keys(): for picker_key, picker in self.color_pickers.items():
shades[picker_key] = self.color_pickers[picker_key].get_rgba().to_string() shades[picker_key] = picker.get_rgba().to_string()
self.update_shades(shades, update_from="color_value") self.update_shades(shades, update_from="color_value")
def update_shades(self, shades, **kwargs): def update_shades(self, shades, **kwargs):

View file

@ -27,7 +27,6 @@
# authorization. # authorization.
from gi.repository import Gtk from gi.repository import Gtk
import json
from .error import AdwcustomizerError from .error import AdwcustomizerError
@Gtk.Template(resource_path='/com/github/ArtyIF/AdwCustomizer/ui/window.ui') @Gtk.Template(resource_path='/com/github/ArtyIF/AdwCustomizer/ui/window.ui')
@ -54,4 +53,3 @@ class AdwcustomizerMainWindow(Gtk.ApplicationWindow):
self.errors_button.set_visible(len(errors) > 0) self.errors_button.set_visible(len(errors) > 0)
for error in errors: for error in errors:
self.errors_list.append(AdwcustomizerError(error["error"], error["element"], error["line"])) self.errors_list.append(AdwcustomizerError(error["error"], error["element"], error["line"]))