def load_config(name: str): file_to_load = ConfigFile(saves_dir.get_or_create(), name, ConfigFile.YAML) if not file_to_load.exists(): raise PycrittyError(f'Config "{name}" not found') conf = yaml_io.read(file_to_load) if conf is None or len(conf) < 1: log.warn(f'"{file_to_load}" has no content') else: yaml_io.write(conf, config_file) log.ok(f'Config "{name}" applied')
def change_font(self, font: str): if 'font' not in self.config: self.config['font'] = {} log.warn(f'"font" prop was not present in {resources.config_file}') fonts_file = resources.fonts_file.get_or_create() fonts = yaml_io.read(fonts_file) if fonts is None: raise ConfigError( f'Failed changing font, file "{fonts_file}" is empty' ) if 'fonts' not in fonts: raise ConfigError( f'Could not change font, "font" config not found in {fonts_file}' ) fonts = fonts['fonts'] if font not in fonts: raise ConfigError( f'Config for font "{font}" not found in {fonts_file}' ) font_types = ['normal', 'bold', 'italic'] if isinstance(fonts[font], str): font_name = fonts[font] fonts[font] = {} for t in font_types: fonts[font][t] = font_name if not isinstance(fonts[font], Mapping): raise ConfigError( f'Font "{font}" has wrong format at file {fonts_file}' ) for t in font_types: if t not in fonts[font]: raise ConfigError(f'Font "{font}" does not have "{t}" property') if t not in self.config['font']: self.config['font'][t] = {'family': 'tmp'} self.config['font'][t]['family'] = fonts[font][t] log.ok(f'Font {font} applied')
def save_config( name: str, read_from: Union[str, Path, ConfigFile, None], dest_parent=saves_dir, override=False, ): read_from = read_from or config_file dest_file = ConfigFile(dest_parent.get_or_create(), name, ConfigFile.YAML) word_to_use = "Theme" if dest_parent == themes_dir else "Config" if dest_file.exists() and not override: raise PycrittyError( f'{word_to_use} "{name}" already exists, use -o to override') conf = yaml_io.read(read_from) if conf is None or len(conf) < 1: log.warn(f'"{read_from}" has no content') else: dest_file.create() yaml_io.write(conf, dest_file) log.ok(f"{word_to_use} saved =>", log.Color.BLUE, dest_file)
def change_theme(self, theme: str): theme_file = resources.get_theme(theme) if not theme_file.exists(): raise PycrittyError(f'Theme "{theme}" not found') theme_yaml = yaml_io.read(theme_file) if theme_yaml is None: raise ConfigError(f'File {theme_file} is empty') if 'colors' not in theme_yaml: raise ConfigError(f'{theme_file} does not contain color config') expected_colors = [ 'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', ] expected_props = { 'primary': ['background', 'foreground'], 'normal': expected_colors, 'bright': expected_colors, } for k in expected_props: if k not in theme_yaml['colors']: log.warn(f'Missing "colors:{k}" for theme "{theme}"') continue for v in expected_props[k]: if v not in theme_yaml['colors'][k]: log.warn(f'Missing "colors:{k}:{v}" for theme "{theme}"') self.config['colors'] = theme_yaml['colors'] log.ok(f'Theme {theme} applied')
def __init__(self): self.config = yaml_io.read(resources.config_file.get_or_create()) if self.config is None: self.config = {}