def parse_theme(fname: str, raw: str, exc_class: Type[BaseException] = SystemExit) -> Dict[str, Any]: lines = raw.splitlines() conf = parse_config(lines) bg: Color = conf.get('background', Color()) is_dark = max((bg.red, bg.green, bg.blue)) < 115 ans: Dict[str, Any] = {'name': theme_name_from_file_name(fname)} parser = LineParser() for i, line in enumerate(raw.splitlines()): line = line.strip() if not line: continue try: parser(line, ans) except Exception as e: raise exc_class( f'Failed to parse {fname} line {i+1} with error: {e}') if not parser.keep_going: break if is_dark: ans['is_dark'] = True ans['num_settings'] = len(conf) - len(parse_config(())) if ans['num_settings'] < 1 and fname != 'default.conf': raise exc_class(f'The theme {fname} has no settings') return ans
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType: final_colors: Dict[str, int] = {} cursor_text_color: Optional[int] = None if not opts.reset: colors: Dict[str, Optional[Color]] = {} for spec in args: if '=' in spec: colors.update(parse_config((spec.replace('=', ' '), ))) else: with open(os.path.expanduser(spec), encoding='utf-8', errors='replace') as f: colors.update(parse_config(f)) ctc = colors.pop('cursor_text_color') if isinstance(ctc, Color): cursor_text_color = color_as_int(ctc) final_colors = { k: color_as_int(v) for k, v in colors.items() if isinstance(v, Color) } ans = { 'match_window': opts.match, 'match_tab': opts.match_tab, 'all': opts.all or opts.reset, 'configured': opts.configured or opts.reset, 'colors': final_colors, 'reset': opts.reset, 'dummy': 0 } if cursor_text_color is not None: ans['cursor_text_color'] = cursor_text_color del ans['dummy'] return ans
def parse_colors(args: Iterable[str]) -> Tuple[Dict[str, int], Optional[Union[int, bool]]]: colors: Dict[str, Optional[Color]] = {} for spec in args: if '=' in spec: colors.update(parse_config((spec.replace('=', ' '),))) else: with open(os.path.expanduser(spec), encoding='utf-8', errors='replace') as f: colors.update(parse_config(f)) q = colors.pop('cursor_text_color', False) ctc = color_as_int(q) if isinstance(q, Color) else (False if q is False else None) return {k: color_as_int(v) for k, v in colors.items() if isinstance(v, Color)}, ctc
def handle_result(args, data, target_window_id, boss): colors = {} for spec in args[1:]: if '=' in spec: colors.update(parse_config((spec.replace('=', ' '),))) else: with open(os.path.join(config_dir, spec), encoding='utf-8', errors='replace') as f: colors.update(parse_config(f)) colors = {k: color_as_int(v) for k, v in colors.items() if isinstance(v, Color)} set_colors(boss, boss.window_id_map.get(target_window_id), { 'all': False, 'match_window': False, 'match_tab': False, 'reset': False, 'configured': False, 'colors': colors })
def parse_colors(args: Iterable[str]) -> Dict[str, Optional[int]]: colors: Dict[str, Optional[Color]] = {} nullable_color_map: Dict[str, Optional[int]] = {} for spec in args: if '=' in spec: colors.update(parse_config((spec.replace('=', ' '),))) else: with open(os.path.expanduser(spec), encoding='utf-8', errors='replace') as f: colors.update(parse_config(f)) for k in nullable_colors: q = colors.pop(k, False) if q is not False: val = int(q) if isinstance(q, Color) else None nullable_color_map[k] = val ans: Dict[str, Optional[int]] = {k: int(v) for k, v in colors.items() if isinstance(v, Color)} ans.update(nullable_color_map) return ans
def kitty_opts(self) -> KittyOptions: if self._opts is None: self._opts = KittyOptions( options_dict=parse_config(self.raw.splitlines())) return self._opts
def text_as_opts(text: str) -> KittyOptions: return KittyOptions(options_dict=parse_config(text.splitlines()))