From 85eaad14f2d8b3108c78599541903a849fddc520 Mon Sep 17 00:00:00 2001 From: "Azalea (on HyDEV-Daisy)" Date: Sat, 2 Jul 2022 23:37:18 -0400 Subject: [PATCH] [F] AttributeError: 'dict' object has no attribute 'recolor_ascii' --- hyfetch/main.py | 2 +- hyfetch/models.py | 7 ++++++- hyfetch/neofetch_util.py | 8 +++++++- hyfetch/serializer.py | 5 +++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/hyfetch/main.py b/hyfetch/main.py index d60ac279..39044a37 100755 --- a/hyfetch/main.py +++ b/hyfetch/main.py @@ -27,7 +27,7 @@ def check_config() -> Config: :return: Config object """ if CONFIG_PATH.is_file(): - return Config(**json.loads(CONFIG_PATH.read_text('utf-8'))) + return Config.from_dict(json.loads(CONFIG_PATH.read_text('utf-8'))) return create_config() diff --git a/hyfetch/models.py b/hyfetch/models.py index 2ad68835..faef1318 100644 --- a/hyfetch/models.py +++ b/hyfetch/models.py @@ -7,7 +7,7 @@ from typing_extensions import Literal from .color_util import AnsiMode from .constants import CONFIG_PATH from .neofetch_util import ColorAlignment -from .serializer import json_stringify +from .serializer import json_stringify, from_dict @dataclass @@ -18,6 +18,11 @@ class Config: lightness: float | None = None color_align: ColorAlignment = ColorAlignment('horizontal') + @classmethod + def from_dict(cls, d: dict): + d['color_align'] = ColorAlignment.from_dict(d['color_align']) + return from_dict(cls, d) + def save(self): CONFIG_PATH.parent.mkdir(exist_ok=True, parents=True) CONFIG_PATH.write_text(json_stringify(self), 'utf-8') diff --git a/hyfetch/neofetch_util.py b/hyfetch/neofetch_util.py index a6b80f8b..51d87bfe 100644 --- a/hyfetch/neofetch_util.py +++ b/hyfetch/neofetch_util.py @@ -1,5 +1,6 @@ from __future__ import annotations +import inspect import os import platform import re @@ -15,6 +16,7 @@ from typing_extensions import Literal from hyfetch.color_util import color from .constants import GLOBAL_CFG from .presets import ColorProfile +from .serializer import from_dict RE_NEOFETCH_COLOR = re.compile('\\${c[0-9]}') @@ -67,6 +69,10 @@ class ColorAlignment: # Foreground/background ascii color index fore_back: tuple[int, int] = () + @classmethod + def from_dict(cls, d: dict): + return from_dict(cls, d) + def recolor_ascii(self, asc: str, preset: ColorProfile) -> str: """ Use the color alignment to recolor an ascii art @@ -177,7 +183,7 @@ def run_neofetch(preset: ColorProfile, alignment: ColorAlignment): color_alignments = { 'fedora': ColorAlignment('horizontal', fore_back=(2, 1)), 'ubuntu': ColorAlignment('horizontal', fore_back=(2, 1)), - 'nixos': ColorAlignment('custom', {1: 1, 2: 0}), + 'NixOS.*': ColorAlignment('custom', {1: 1, 2: 0}), # 'arch': ColorAlignment('horizontal'), # 'centos': ColorAlignment('horizontal'), } diff --git a/hyfetch/serializer.py b/hyfetch/serializer.py index 28cf4f37..a392707c 100644 --- a/hyfetch/serializer.py +++ b/hyfetch/serializer.py @@ -1,6 +1,7 @@ from __future__ import annotations import dataclasses +import inspect import json from datetime import datetime, date @@ -41,3 +42,7 @@ def json_stringify(obj: object, indent: int | None = None) -> str: :return: Json strings """ return json.dumps(obj, indent=indent, cls=EnhancedJSONEncoder, ensure_ascii=False) + + +def from_dict(cls, d: dict): + return cls(**{k: v for k, v in d.items() if k in inspect.signature(cls).parameters})