feat: move from cssutils to in-house solution

This commit replaces cssutils module with a GTK CSS "parser" based on regex expressions
This commit is contained in:
tfuxu 2022-10-14 23:13:46 +02:00
parent 901dada916
commit aa89bd0729
4 changed files with 50 additions and 66 deletions

View file

@ -24,6 +24,11 @@
"pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"material-color-utilities-python\" --no-build-isolation"
],
"sources": [
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/f8/43/b85d103acc0bfb54939f801908bf83354085579e8422eeaa22c017950c02/regex-2022.9.13.tar.gz",
"sha256": "f07373b6e56a6f3a0df3d75b651a278ca7bd357a796078a26a958ea1ce0588fd"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/8c/92/2975b464d9926dc667020ed1abfa6276e68c3571dcb77e43347e15ee9eed/Pillow-9.2.0.tar.gz",
@ -33,11 +38,6 @@
"type": "file",
"url": "https://files.pythonhosted.org/packages/31/65/a8e0f3e2bad0d4eabeb1931b22cdae08344a955f28022dc83420a128683c/material_color_utilities_python-0.1.5-py2.py3-none-any.whl",
"sha256": "48abd8695a1355ab3ad43fe314ca8664c66282a86fbf94a717571273bf422bdf"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/f8/43/b85d103acc0bfb54939f801908bf83354085579e8422eeaa22c017950c02/regex-2022.9.13.tar.gz",
"sha256": "f07373b6e56a6f3a0df3d75b651a278ca7bd357a796078a26a958ea1ce0588fd"
}
]
},
@ -48,21 +48,16 @@
"pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"svglib\" --no-build-isolation"
],
"sources": [
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/f4/24/2a3e3df732393fed8b3ebf2ec078f05546de641fe1b667ee316ec1dcf3b7/webencodings-0.5.1-py2.py3-none-any.whl",
"sha256": "a0af1213f3c2226497a97e2b3aa01a7e4bee4f403f95be16fc9acd2947514a78"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/8c/92/2975b464d9926dc667020ed1abfa6276e68c3571dcb77e43347e15ee9eed/Pillow-9.2.0.tar.gz",
"sha256": "75e636fd3e0fb872693f23ccb8a5ff2cd578801251f3a4f6854c6a5d437d3c04"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/9d/3a/e39436efe51894243ff145a37c4f9a030839b97779ebcc4f13b3ba21c54e/cssselect2-0.7.0-py3-none-any.whl",
"sha256": "fd23a65bfd444595913f02fc71f6b286c29261e354c41d722ca7a261a49b5969"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/70/bb/7a2c7b4f8f434aa1ee801704bf08f1e53d7b5feba3d5313ab17003477808/lxml-4.9.1.tar.gz",
"sha256": "fe749b052bb7233fe5d072fcb549221a8cb1a16725c47c37e42b0b9cb3ff2c3f"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/a6/d0/bdee3e8a7ba5f2dc8bacea10f4fdd1264b1dd9e53d96d318773180634a00/reportlab-3.6.11.tar.gz",
@ -70,8 +65,8 @@
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/ee/cd/8b19f6299541862deea19b26a9efe269ff843fa15a9fd833ff0fec2ac22c/svglib-1.4.1.tar.gz",
"sha256": "48c24706c23bb4262173b6fa49eabb10afa15b8412f14283120549517ccfa314"
"url": "https://files.pythonhosted.org/packages/70/bb/7a2c7b4f8f434aa1ee801704bf08f1e53d7b5feba3d5313ab17003477808/lxml-4.9.1.tar.gz",
"sha256": "fe749b052bb7233fe5d072fcb549221a8cb1a16725c47c37e42b0b9cb3ff2c3f"
},
{
"type": "file",
@ -80,8 +75,13 @@
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/f4/24/2a3e3df732393fed8b3ebf2ec078f05546de641fe1b667ee316ec1dcf3b7/webencodings-0.5.1-py2.py3-none-any.whl",
"sha256": "a0af1213f3c2226497a97e2b3aa01a7e4bee4f403f95be16fc9acd2947514a78"
"url": "https://files.pythonhosted.org/packages/9d/3a/e39436efe51894243ff145a37c4f9a030839b97779ebcc4f13b3ba21c54e/cssselect2-0.7.0-py3-none-any.whl",
"sha256": "fd23a65bfd444595913f02fc71f6b286c29261e354c41d722ca7a261a49b5969"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/ee/cd/8b19f6299541862deea19b26a9efe269ff843fa15a9fd833ff0fec2ac22c/svglib-1.4.1.tar.gz",
"sha256": "48c24706c23bb4262173b6fa49eabb10afa15b8412f14283120549517ccfa314"
}
]
},
@ -99,20 +99,6 @@
}
]
},
{
"name": "python3-cssutils",
"buildsystem": "simple",
"build-commands": [
"pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"cssutils\" --no-build-isolation"
],
"sources": [
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/28/4e/6c6d07b1ab43f7b8b939406b562c652c651a66699b8a01fc6b774cbf69de/cssutils-2.6.0-py3-none-any.whl",
"sha256": "30c72f3a5c5951a11151640600aae7b3bf10e4c0d5c87f5bc505c2cd4a26e0c2"
}
]
},
{
"name": "python3-Jinja2",
"buildsystem": "simple",
@ -122,13 +108,13 @@
"sources": [
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/bc/c3/f068337a370801f372f2f8f6bad74a5c140f6fda3d9de154052708dd3c65/Jinja2-3.1.2-py3-none-any.whl",
"sha256": "6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61"
"url": "https://files.pythonhosted.org/packages/1d/97/2288fe498044284f39ab8950703e88abbac2abbdf65524d576157af70556/MarkupSafe-2.1.1.tar.gz",
"sha256": "7f91197cc9e48f989d12e4e6fbc46495c446636dfc81b9ccf50bb0ec74b91d4b"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/1d/97/2288fe498044284f39ab8950703e88abbac2abbdf65524d576157af70556/MarkupSafe-2.1.1.tar.gz",
"sha256": "7f91197cc9e48f989d12e4e6fbc46495c446636dfc81b9ccf50bb0ec74b91d4b"
"url": "https://files.pythonhosted.org/packages/bc/c3/f068337a370801f372f2f8f6bad74a5c140f6fda3d9de154052708dd3c65/Jinja2-3.1.2-py3-none-any.whl",
"sha256": "6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61"
}
]
}

View file

@ -32,7 +32,7 @@ from gradience.ui.app_type_dialog import GradienceAppTypeDialog
from gradience.ui.custom_css_group import GradienceCustomCSSGroup
from gradience.ui.presets_manager_window import GradiencePresetWindow
from gradience.ui.preferences_window import GradiencePreferencesWindow
from gradience.utils.css import load_preset_from_css
from gradience.utils.css import parse_css
from gradience.utils.utils import to_slug_case, buglog, run_command
from gradience.utils.preset import Preset, presets_dir
from gradience.settings_schema import settings_schema
@ -276,7 +276,7 @@ class GradienceApplication(Adw.Application):
def load_preset_from_css(self):
try:
variables, palette, custom_css = load_preset_from_css(
variables, palette, custom_css = parse_css(
os.path.join(
os.environ.get("XDG_CONFIG_HOME",
os.environ["HOME"] + "/.config"),
@ -293,6 +293,7 @@ class GradienceApplication(Adw.Application):
self.preset = Preset(preset=preset)
self.load_preset_variables_from_preset()
except OSError: # fallback to adwaita
buglog("Custom preset not found. Fallback to Adwaita")
if self.style_manager.get_dark():
self.load_preset_from_resource(
f"{rootdir}/presets/adwaita-dark.json")

View file

@ -16,11 +16,10 @@
# 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 logging
import re
import cssutils
# Adwaita palette color name dict
# Adwaita named palette colors dict
COLORS = [
"blue_",
"green_",
@ -33,15 +32,11 @@ COLORS = [
"dark_",
]
# Override cssutils preferences
cssutils.ser.prefs.minimizeColorHash = False
cssutils.ser.prefs.indentClosingBrace = False
cssutils.ser.prefs.omitLastSemicolon = False
# Regular expressions
define_color = re.compile(r"(@define-color .*[^\s])")
not_define_color = re.compile(r"(^(?:(?!@define-color).)*$)")
# Set cssutils module logging to level FATAL
cssutils.log.setLevel(logging.FATAL)
def load_preset_from_css(path):
def parse_css(path):
css = ""
variables = {}
palette = {}
@ -49,19 +44,22 @@ def load_preset_from_css(path):
for color in COLORS:
palette[color] = {}
with open(path, "r", encoding="utf-8") as f:
sheet = cssutils.parseString(f.read())
for rule in sheet:
css_text = rule.cssText
if rule.type == rule.UNKNOWN_RULE:
if css_text.startswith("@define-color"):
name, color = css_text.split(" ", 1)[1].split(" ", 1)
for color_name in COLORS:
if name.startswith(color_name):
palette[name[:-1]][name[-1:]] = color[:-1]
break
else:
with open(path, "r", encoding="utf-8") as sheet:
for line in sheet:
cdefine_match = re.search(define_color, line)
not_cdefine_match = re.search(not_define_color, line)
if cdefine_match != None: # If @define-color variable declarations were found
palette_part = cdefine_match.__getitem__(1) # Get the second item of the re.Match object
name, color = palette_part.split(" ", 1)[1].split(" ", 1)
for color_name in COLORS:
if name.startswith(color_name): # Palette colors
palette[name[:-1]][name[-1:]] = color[:-1]
break
else: # Other color variables
variables[name] = color[:-1]
elif rule.type == rule.STYLE_RULE:
css += f"\n{rule.cssText}\n"
return variables, palette, css
elif not_cdefine_match != None: # If CSS rules were found
css_part = not_cdefine_match.__getitem__(1)
css += f"{css_part}\n"
sheet.close()
return variables, palette, css

View file

@ -30,5 +30,4 @@ anyascii~=0.3
material-color-utilities-python
svglib
yapsy
cssutils
Jinja2