Refactor: More f-string for conf

This commit is contained in:
pagedown 2022-01-29 20:18:11 +08:00
parent ba0f61d752
commit 1ca1c2edad
No known key found for this signature in database
GPG Key ID: E921CF18AC8FF6EB
3 changed files with 14 additions and 27 deletions

View File

@ -13,7 +13,7 @@
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 @@
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:

View File

@ -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')

View File

@ -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 @@ def as_rst(
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 @@ def as_rst(
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 @@ def as_conf(self, commented: bool = False, level: int = 0) -> List[str]:
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