def change_font_size(self, size: float): if size <= 0: raise ConfigError('Font size cannot be negative or zero') if 'font' not in self.config: self.config['font'] = {} log.warn(f'"font" prop was not present in {resources.config_file}') self.config['font']['size'] = size log.ok(f'Font size set to {size:.1f}')
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_opacity(self, opacity: float): if opacity < 0.0 or opacity > 1.0: raise ConfigError('Opacity should be between 0.0 and 1.0') if 'window' not in self.config: self.config['window'] = {} log.warn(f'"window" prop was not present in {resources.config_file}') if 'opacity' not in self.config['window']: self.config['window']['opacity'] = {} log.warn(f'"opacity" prop was not present in {resources.config_file}') self.config['window']['opacity'] = opacity log.ok(f'Opacity set to {opacity:.2f}')
def change_font_offset(self, offset=(0, 0)): if len(offset) != 2: raise ConfigError('Wrong offset format, should be (x, y)') x, y = offset if 'font' not in self.config: self.config['font'] = {} if 'offset' not in self.config['font']: log.warn('"offset" prop was not set') self.config['font']['offset'] = {} self.config['font']['offset']['x'] = x self.config['font']['offset']['y'] = y log.ok(f'Offset set to x: {x}, y: {y}')
def change_padding(self, padding=(1, 1)): if len(padding) != 2: raise ConfigError('Padding should only have an x and y value') x, y = padding if 'window' not in self.config: self.config['window'] = {} log.warn(f'"window" prop was not present in {resources.config_file}') if 'padding' not in self.config['window']: self.config['window']['padding'] = {} log.warn(f'"padding" prop was not present in {resources.config_file}') self.config['window']['padding']['x'] = x self.config['window']['padding']['y'] = y log.ok(f'Padding set to x: {x}, y: {y}')
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')