Refactor: More f-string for conf

This commit is contained in:
pagedown
2022-01-29 20:18:11 +08:00
parent ba0f61d752
commit 1ca1c2edad
3 changed files with 14 additions and 27 deletions

View File

@@ -13,7 +13,7 @@ import subprocess
import sys import sys
import time import time
from functools import partial 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 import nodes
from docutils.parsers.rst.roles import set_classes 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: if kitty_src not in sys.path:
sys.path.insert(0, kitty_src) 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 from kitty.constants import str_version, website_url # noqa
# config {{{ # config {{{
@@ -366,19 +366,6 @@ def link_role(
return [node], [] 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] = {} opt_aliases: Dict[str, str] = {}
shortcut_slugs: Dict[str, Tuple[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] conf_name, opt = target.partition('.')[::2]
if not opt: if not opt:
conf_name, opt = 'kitty', conf_name 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) 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] conf_name, slug = target.partition('.')[::2]
if not slug: if not slug:
conf_name, slug = 'kitty', conf_name conf_name, slug = 'kitty', conf_name
full_name = conf_name + '.' + slug full_name = f'{conf_name}.{slug}'
try: try:
target, stitle = shortcut_slugs[full_name] target, stitle = shortcut_slugs[full_name]
except KeyError: except KeyError:

View File

@@ -418,9 +418,9 @@ def generate_c_conversion(loc: str, ctypes: List[Option]) -> str:
def write_output(loc: str, defn: Definition) -> None: def write_output(loc: str, defn: Definition) -> None:
cls, tc = generate_class(defn, loc) cls, tc = generate_class(defn, loc)
with open(os.path.join(*loc.split('.'), 'options', 'types.py'), 'w') as f: 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: with open(os.path.join(*loc.split('.'), 'options', 'parse.py'), 'w') as f:
f.write(tc + '\n') f.write(f'{tc}\n')
ctypes = [] ctypes = []
for opt in defn.root_group.iter_all_non_groups(): for opt in defn.root_group.iter_all_non_groups():
if isinstance(opt, Option) and opt.ctype: if isinstance(opt, Option) and opt.ctype:
@@ -428,7 +428,7 @@ def write_output(loc: str, defn: Definition) -> None:
if ctypes: if ctypes:
c = generate_c_conversion(loc, ctypes) c = generate_c_conversion(loc, ctypes)
with open(os.path.join(*loc.split('.'), 'options', 'to-c-generated.h'), 'w') as f: 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: def main() -> None:
@@ -454,6 +454,6 @@ def main() -> None:
loc = package_name loc = package_name
cls, tc = generate_class(defn, loc) cls, tc = generate_class(defn, loc)
with open(os.path.join(os.path.dirname(path), 'kitten_options_types.py'), 'w') as f: 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: 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')

View File

@@ -37,8 +37,8 @@ def expand_opt_references(conf_name: str, text: str) -> str:
def expand(m: 'Match[str]') -> str: def expand(m: 'Match[str]') -> str:
ref = m.group(1) ref = m.group(1)
if '<' not in ref and '.' not in ref: if '<' not in ref and '.' not in ref:
full_ref = conf_name + ref # full ref
return f':opt:`{ref} <{full_ref}>`' return f':opt:`{ref} <{conf_name}{ref}>`'
return str(m.group()) return str(m.group())
return re.sub(r':opt:`(.+?)`', expand, text) return re.sub(r':opt:`(.+?)`', expand, text)
@@ -214,7 +214,7 @@ class Option:
if not self.documented: if not self.documented:
return ans return ans
mopts = [self] + option_group 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('.. code-block:: conf')
a('') a('')
sz = max(len(x.name) for x in mopts) 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') raise ValueError(f'The shortcut for {self.name} has no short_text')
sc_text = f'{conf_name}.{self.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)) 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 block_started = False
for sc in [self] + action_group: for sc in [self] + action_group:
if sc.add_to_default and sc.documented: if sc.add_to_default and sc.documented:
@@ -534,7 +534,7 @@ class Group:
ans[i] = ' '.join(parts) ans[i] = ' '.join(parts)
if commented: 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 return ans