feat: move utility functions to modules/utils.py and create a new function: buglog

For now I've moved only buglog and to_slug_case here, but a general purpose of this file is to move any redundant/utility code here as a function, so it can be easily used between other modules.

* replace print() in a codebase with new buglog() function
This commit is contained in:
tfuxu 2022-08-23 19:38:09 +02:00
parent 69a286b82a
commit e9ad48f100
9 changed files with 100 additions and 41 deletions

View file

@ -19,7 +19,6 @@
import sys
import json
import os
import re
import traceback
import gi
@ -35,7 +34,7 @@ from .custom_css_group import GradienceCustomCSSGroup
from .constants import rootdir, app_id, rel_ver, version, bugtracker_url, help_url, project_url
from .welcome import GradienceWelcomeWindow
from .presets_manager_window import GradiencePresetWindow
from . import to_slug_case
from .modules.utils import to_slug_case, buglog
class GradienceApplication(Adw.Application):
@ -73,7 +72,7 @@ class GradienceApplication(Adw.Application):
self.settings.get_value("disabled-plugins"))
self.first_run = self.settings.get_boolean("first-run")
print(f"disabled plugins: {self.disabled_plugins}")
buglog(f"disabled plugins: {self.disabled_plugins}")
self.style_manager = Adw.StyleManager.get_default()
@ -127,11 +126,11 @@ class GradienceApplication(Adw.Application):
)
if self.first_run:
print("first run")
buglog("first run")
welcome = GradienceWelcomeWindow(self.win)
welcome.present()
else:
print("normal run")
buglog("normal run")
self.win.present()
def open_preset_directory(self, *_args):
@ -711,7 +710,7 @@ This app is written in Python and uses GTK 4 and libadwaita.
self.set_accels_for_action(f"app.{name}", shortcuts)
def reload_plugins(self):
print("reload plugins")
buglog("reload plugins")
self.win.plugins_group = self.win.plugins_list.to_group()
def show_adwaita_demo(self, *_args):

View file

@ -80,4 +80,12 @@ gradience_sources = [
]
PY_INSTALLDIR.install_sources(gradience_sources, subdir: 'gradience')
#install_data(gradience_sources, install_dir: MODULE_DIR)
#install_data(gradience_sources, install_dir: MODULE_DIR)
# Install modules
gradience_modules = [
'modules/__init__.py',
'modules/utils.py'
]
PY_INSTALLDIR.install_sources(gradience_modules, subdir: 'gradience/modules')

17
src/modules/__init__.py Normal file
View file

@ -0,0 +1,17 @@
# __init__.py
#
# Change the look of Adwaita, with ease
# Copyright (C) 2022 Gradience Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

35
src/modules/utils.py Normal file
View file

@ -0,0 +1,35 @@
# utils.py
#
# Change the look of Adwaita, with ease
# Copyright (C) 2022 Gradience Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import re
from anyascii import anyascii
from gradience.constants import build_type
def to_slug_case(non_slug):
return re.sub(r"[^0-9a-z]+", "-", anyascii(non_slug).lower()).strip("-")
# Use it instead of print(), so there isn't any output in stdout if Gradience was build in release mode
# TODO: Can be replaced with `logging` module [https://docs.python.org/3/library/logging.html] in future
def buglog(*args):
if build_type == "debug":
message = ""
for obj in args:
message += str(obj)
return print(f"[DEBUG]: {message}")

View file

@ -1,6 +1,7 @@
from gi.repository import Gtk, Gdk, Adw
from .modules.utils import buglog
from .constants import rootdir
@ -21,14 +22,14 @@ class GradiencePluginRow(Adw.ActionRow):
@Gtk.Template.Callback()
def on_settings_plugin_clicked(self, *_args):
print("settings")
buglog("settings")
@Gtk.Template.Callback()
def on_remove_plugin_clicked(self, *_args):
print("removed")
buglog("removed")
@Gtk.Template.Callback()
def on_switch_toggled(self, *_args):
print("toggled")
buglog("toggled")
Gtk.Application.get_default().reload_plugins()

View file

@ -16,14 +16,16 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import sys
import os
from pathlib import Path
import importlib
import pkgutil
from .plugin_row import GradiencePluginRow
from gi.repository import Gtk, Adw, Gio, Gdk
import sys
from pathlib import Path
from .modules.utils import buglog
from .plugin_row import GradiencePluginRow
class GradiencePluginsList:
@ -41,7 +43,7 @@ class GradiencePluginsList:
for plugin_id, plugin in self.discoverd_plugins.items():
self.plugins[plugin_id] = plugin.GradiencePlugin()
print(self.plugins)
buglog(self.plugins)
def load_all_custom_settings(self, settings):
for plugin_id, plugin in self.plugins.items():

View file

@ -24,6 +24,8 @@ import traceback
from gi.repository import GLib
from .modules.utils import buglog
class RunAsync(threading.Thread):
def __init__(self, task_func, callback=None, *args, **kwargs):
@ -44,12 +46,12 @@ class RunAsync(threading.Thread):
result = None
error = None
print(f"Running async job [{self.task_func}].")
buglog(f"Running async job [{self.task_func}].")
try:
result = self.task_func(*args, **kwargs)
except Exception as exception:
print("Error while running async job: "
buglog("Error while running async job: "
f"{self.task_func}\nException: {exception}")
error = exception
@ -57,6 +59,6 @@ class RunAsync(threading.Thread):
traceback.print_tb(trace)
traceback_info = '\n'.join(traceback.format_tb(trace))
print([str(exception), traceback_info])
buglog([str(exception), traceback_info])
self.source_id = GLib.idle_add(self.callback, result, error)
return self.source_id

View file

@ -16,10 +16,13 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from gi.repository import Gtk, Adw, Gio, Gdk
from .constants import rootdir
import time
from gi.repository import Gtk, Adw, Gio, Gdk
from .run_async import RunAsync
from .modules.utils import buglog
from .constants import rootdir
@Gtk.Template(resource_path=f"{rootdir}/ui/welcome.ui")
@ -115,15 +118,15 @@ class GradienceWelcomeWindow(Adw.Window):
quit()
def check_adw_gtk3(self):
print("check if adw-gtk3 installed")
buglog("check if adw-gtk3 installed")
return True
def adw_gtk3(self):
if not self.check_adw_gtk3(): # install
print("install adw-gtk3")
buglog("install adw-gtk3")
def configure_system(self):
print("confiure system")
buglog("configure system")
def install_runner(self, widget):
def set_completed(result, error=False):

View file

@ -13,33 +13,25 @@
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Except as contained in this notice, the name(s) of the above copyright
# holders shall not be used in advertising or otherwise to promote the sale,
# use or other dealings in this Software without prior written
# authorization.
import os
from gi.repository import Gtk, Adw, Gio, Gdk
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
from material_color_utilities_python import *
from .error import GradienceError
from .settings_schema import settings_schema
from .palette_shades import GradiencePaletteShades
from .option import GradienceOption
from .app_type_dialog import GradienceAppTypeDialog
from .custom_css_group import GradienceCustomCSSGroup
from material_color_utilities_python import *
from .constants import rootdir, app_id, build_type
from .presets_manager_window import GradiencePresetWindow
from .plugins_list import GradiencePluginsList
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM
import os
from .modules.utils import buglog
from .constants import rootdir, app_id, build_type
@Gtk.Template(resource_path=f"{rootdir}/ui/window.ui")
@ -117,7 +109,7 @@ class GradienceMainWindow(Adw.ApplicationWindow):
def __close_window(self, widegt):
if self.get_application().is_dirty:
print("app is dirty")
buglog("app is dirty")
def on_monet_file_chooser_response(self, widget, response):
if response == Gtk.ResponseType.ACCEPT:
@ -217,7 +209,7 @@ class GradienceMainWindow(Adw.ApplicationWindow):
renderPM.drawToFile(drawing, self.monet_image_file, fmt='PNG')
if self.monet_image_file.endswith(".xml"):
print("XML WIP")
buglog("XML WIP")
try:
self.monet_img = Image.open(self.monet_image_file)