From 1ca1c2edad34ef0aec5112d8a8476bc76cd21367 Mon Sep 17 00:00:00 2001 From: pagedown Date: Sat, 29 Jan 2022 20:18:11 +0800 Subject: [PATCH] Refactor: More f-string for conf --- docs/conf.py | 21 ++++----------------- kitty/conf/generate.py | 10 +++++----- kitty/conf/types.py | 10 +++++----- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 7bbcc9190..c0a95b913 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -13,7 +13,7 @@ import subprocess import sys import time from functools import partial -from typing import Any, Callable, Dict, Iterable, List, Match, Optional, Tuple +from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple from docutils import nodes from docutils.parsers.rst.roles import set_classes @@ -28,7 +28,7 @@ kitty_src = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if kitty_src not in sys.path: sys.path.insert(0, kitty_src) -from kitty.conf.types import Definition # noqa +from kitty.conf.types import Definition, expand_opt_references # noqa from kitty.constants import str_version, website_url # noqa # config {{{ @@ -366,19 +366,6 @@ def link_role( return [node], [] -def expand_opt_references(conf_name: str, text: str) -> str: - conf_name += '.' - - def expand(m: Match[str]) -> str: - ref = m.group(1) - if '<' not in ref and '.' not in ref: - # full ref - return f':opt:`{ref} <{conf_name}{ref}>`' - return str(m.group()) - - return re.sub(r':opt:`(.+?)`', expand, text) - - opt_aliases: Dict[str, str] = {} shortcut_slugs: Dict[str, Tuple[str, str]] = {} @@ -416,7 +403,7 @@ def process_opt_link(env: Any, refnode: Any, has_explicit_title: bool, title: st conf_name, opt = target.partition('.')[::2] if not opt: conf_name, opt = 'kitty', conf_name - full_name = conf_name + '.' + opt + full_name = f'{conf_name}.{opt}' return title, opt_aliases.get(full_name, full_name) @@ -424,7 +411,7 @@ def process_shortcut_link(env: Any, refnode: Any, has_explicit_title: bool, titl conf_name, slug = target.partition('.')[::2] if not slug: conf_name, slug = 'kitty', conf_name - full_name = conf_name + '.' + slug + full_name = f'{conf_name}.{slug}' try: target, stitle = shortcut_slugs[full_name] except KeyError: diff --git a/kitty/conf/generate.py b/kitty/conf/generate.py index e8edc2b19..a1c0166a9 100644 --- a/kitty/conf/generate.py +++ b/kitty/conf/generate.py @@ -418,9 +418,9 @@ def generate_c_conversion(loc: str, ctypes: List[Option]) -> str: def write_output(loc: str, defn: Definition) -> None: cls, tc = generate_class(defn, loc) with open(os.path.join(*loc.split('.'), 'options', 'types.py'), 'w') as f: - f.write(cls + '\n') + f.write(f'{cls}\n') with open(os.path.join(*loc.split('.'), 'options', 'parse.py'), 'w') as f: - f.write(tc + '\n') + f.write(f'{tc}\n') ctypes = [] for opt in defn.root_group.iter_all_non_groups(): if isinstance(opt, Option) and opt.ctype: @@ -428,7 +428,7 @@ def write_output(loc: str, defn: Definition) -> None: if ctypes: c = generate_c_conversion(loc, ctypes) with open(os.path.join(*loc.split('.'), 'options', 'to-c-generated.h'), 'w') as f: - f.write(c + '\n') + f.write(f'{c}\n') def main() -> None: @@ -454,6 +454,6 @@ def main() -> None: loc = package_name cls, tc = generate_class(defn, loc) with open(os.path.join(os.path.dirname(path), 'kitten_options_types.py'), 'w') as f: - f.write(cls + '\n') + f.write(f'{cls}\n') with open(os.path.join(os.path.dirname(path), 'kitten_options_parse.py'), 'w') as f: - f.write(tc + '\n') + f.write(f'{tc}\n') diff --git a/kitty/conf/types.py b/kitty/conf/types.py index 03ab3df19..774d666b3 100644 --- a/kitty/conf/types.py +++ b/kitty/conf/types.py @@ -37,8 +37,8 @@ def expand_opt_references(conf_name: str, text: str) -> str: def expand(m: 'Match[str]') -> str: ref = m.group(1) if '<' not in ref and '.' not in ref: - full_ref = conf_name + ref - return f':opt:`{ref} <{full_ref}>`' + # full ref + return f':opt:`{ref} <{conf_name}{ref}>`' return str(m.group()) return re.sub(r':opt:`(.+?)`', expand, text) @@ -214,7 +214,7 @@ class Option: if not self.documented: return ans mopts = [self] + option_group - a('.. opt:: ' + ', '.join(conf_name + '.' + mo.name for mo in mopts)) + a('.. opt:: ' + ', '.join(f'{conf_name}.{mo.name}' for mo in mopts)) a('.. code-block:: conf') a('') sz = max(len(x.name) for x in mopts) @@ -330,7 +330,7 @@ class Mapping: raise ValueError(f'The shortcut for {self.name} has no short_text') sc_text = f'{conf_name}.{self.short_text}' shortcut_slugs[f'{conf_name}.{self.name}'] = (sc_text, self.key_text.replace('kitty_mod', kitty_mod)) - a('.. shortcut:: ' + sc_text) + a(f'.. shortcut:: {sc_text}') block_started = False for sc in [self] + action_group: if sc.add_to_default and sc.documented: @@ -534,7 +534,7 @@ class Group: ans[i] = ' '.join(parts) if commented: - ans = [x if x.startswith('#') or not x.strip() else ('# ' + x) for x in ans] + ans = [x if x.startswith('#') or not x.strip() else (f'# {x}') for x in ans] return ans