[F] AttributeError: 'dict' object has no attribute 'recolor_ascii'

This commit is contained in:
Azalea (on HyDEV-Daisy) 2022-07-02 23:37:18 -04:00
parent d11796ef02
commit 85eaad14f2
4 changed files with 19 additions and 3 deletions

View file

@ -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()

View file

@ -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')

View file

@ -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'),
}

View file

@ -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})