Refactor: Replace string concat with f-string

This commit is contained in:
pagedown 2022-01-27 17:57:25 +08:00
parent b5ad030a14
commit cc84ee9734
No known key found for this signature in database
GPG Key ID: E921CF18AC8FF6EB

View File

@ -81,13 +81,13 @@ class Options(argparse.Namespace):
def emphasis(text: str) -> str: def emphasis(text: str) -> str:
if sys.stdout.isatty(): if sys.stdout.isatty():
text = '\033[32m' + text + '\033[39m' text = f'\033[32m{text}\033[39m'
return text return text
def error(text: str) -> str: def error(text: str) -> str:
if sys.stdout.isatty(): if sys.stdout.isatty():
text = '\033[91m' + text + '\033[39m' text = f'\033[91m{text}\033[39m'
return text return text
@ -118,7 +118,7 @@ def pkg_version(package: str) -> Tuple[int, int]:
def at_least_version(package: str, major: int, minor: int = 0) -> None: def at_least_version(package: str, major: int, minor: int = 0) -> None:
q = f'{major}.{minor}' q = f'{major}.{minor}'
if subprocess.run([PKGCONFIG, package, '--atleast-version=' + q] if subprocess.run([PKGCONFIG, package, f'--atleast-version={q}']
).returncode != 0: ).returncode != 0:
qmajor = qminor = 0 qmajor = qminor = 0
try: try:
@ -176,7 +176,7 @@ def get_python_include_paths() -> List[str]:
def get_python_flags(cflags: List[str]) -> List[str]: def get_python_flags(cflags: List[str]) -> List[str]:
cflags.extend('-I' + x for x in get_python_include_paths()) cflags.extend(f'-I{x}' for x in get_python_include_paths())
libs: List[str] = [] libs: List[str] = []
libs += (sysconfig.get_config_var('LIBS') or '').split() libs += (sysconfig.get_config_var('LIBS') or '').split()
libs += (sysconfig.get_config_var('SYSLIBS') or '').split() libs += (sysconfig.get_config_var('SYSLIBS') or '').split()
@ -199,10 +199,10 @@ def get_python_flags(cflags: List[str]) -> List[str]:
else: else:
ldlib = sysconfig.get_config_var('LIBDIR') ldlib = sysconfig.get_config_var('LIBDIR')
if ldlib: if ldlib:
libs += ['-L' + ldlib] libs += [f'-L{ldlib}']
ldlib = sysconfig.get_config_var('VERSION') ldlib = sysconfig.get_config_var('VERSION')
if ldlib: if ldlib:
libs += ['-lpython' + ldlib + sys.abiflags] libs += [f'-lpython{ldlib}{sys.abiflags}']
libs += (sysconfig.get_config_var('LINKFORSHARED') or '').split() libs += (sysconfig.get_config_var('LINKFORSHARED') or '').split()
return libs return libs
@ -369,16 +369,16 @@ def init_env(
if egl_library is not None: if egl_library is not None:
assert('"' not in egl_library) assert('"' not in egl_library)
library_paths['glfw/egl_context.c'] = ['_GLFW_EGL_LIBRARY="' + egl_library + '"'] library_paths['glfw/egl_context.c'] = [f'_GLFW_EGL_LIBRARY="{egl_library}"']
desktop_libs = [] desktop_libs = []
if startup_notification_library is not None: if startup_notification_library is not None:
assert('"' not in startup_notification_library) assert('"' not in startup_notification_library)
desktop_libs = ['_KITTY_STARTUP_NOTIFICATION_LIBRARY="' + startup_notification_library + '"'] desktop_libs = [f'_KITTY_STARTUP_NOTIFICATION_LIBRARY="{startup_notification_library}"']
if canberra_library is not None: if canberra_library is not None:
assert('"' not in canberra_library) assert('"' not in canberra_library)
desktop_libs += ['_KITTY_CANBERRA_LIBRARY="' + canberra_library + '"'] desktop_libs += [f'_KITTY_CANBERRA_LIBRARY="{canberra_library}"']
if desktop_libs != []: if desktop_libs != []:
library_paths['kitty/desktop.c'] = desktop_libs library_paths['kitty/desktop.c'] = desktop_libs
@ -454,7 +454,7 @@ def kitty_env() -> Env:
def define(x: str) -> str: def define(x: str) -> str:
return '-D' + x return f'-D{x}'
def run_tool(cmd: Union[str, List[str]], desc: Optional[str] = None) -> None: def run_tool(cmd: Union[str, List[str]], desc: Optional[str] = None) -> None:
@ -681,7 +681,7 @@ def compile_c_extension(
) -> None: ) -> None:
prefix = os.path.basename(module) prefix = os.path.basename(module)
objects = [ objects = [
os.path.join(build_dir, prefix + '-' + os.path.basename(src) + '.o') os.path.join(build_dir, f'{prefix}-{os.path.basename(src)}.o')
for src in sources for src in sources
] ]
@ -700,8 +700,8 @@ def compile_c_extension(
key = CompileKey(original_src, os.path.basename(dest)) key = CompileKey(original_src, os.path.basename(dest))
desc = f'Compiling {emphasis(desc_prefix + src)} ...' desc = f'Compiling {emphasis(desc_prefix + src)} ...'
compilation_database.add_command(desc, cmd, partial(newer, dest, *dependecies_for(src, dest, headers)), key=key, keyfile=src) compilation_database.add_command(desc, cmd, partial(newer, dest, *dependecies_for(src, dest, headers)), key=key, keyfile=src)
dest = os.path.join(build_dir, module + '.so') dest = os.path.join(build_dir, f'{module}.so')
real_dest = module + '.so' real_dest = f'{module}.so'
os.makedirs(os.path.dirname(dest), exist_ok=True) os.makedirs(os.path.dirname(dest), exist_ok=True)
desc = f'Linking {emphasis(desc_prefix + module)} ...' desc = f'Linking {emphasis(desc_prefix + module)} ...'
# Old versions of clang don't like -pthread being passed to the linker # Old versions of clang don't like -pthread being passed to the linker
@ -756,7 +756,7 @@ def compile_glfw(compilation_database: CompilationDatabase) -> None:
print(error('Disabling building of wayland backend'), file=sys.stderr) print(error('Disabling building of wayland backend'), file=sys.stderr)
continue continue
compile_c_extension( compile_c_extension(
genv, 'kitty/glfw-' + module, compilation_database, genv, f'kitty/glfw-{module}', compilation_database,
sources, all_headers, desc_prefix=f'[{module}] ') sources, all_headers, desc_prefix=f'[{module}] ')
@ -975,7 +975,7 @@ Categories=System;TerminalEmulator;
) )
base = Path(ddir) base = Path(ddir)
in_src_launcher = base / (libdir_name + '/kitty/kitty/launcher/kitty') in_src_launcher = base / (f'{libdir_name}/kitty/kitty/launcher/kitty')
launcher = base / 'bin/kitty' launcher = base / 'bin/kitty'
if os.path.exists(in_src_launcher): if os.path.exists(in_src_launcher):
os.remove(in_src_launcher) os.remove(in_src_launcher)
@ -994,7 +994,7 @@ def macos_info_plist() -> bytes:
{ {
'CFBundleTypeName': 'Terminal scripts', 'CFBundleTypeName': 'Terminal scripts',
'CFBundleTypeExtensions': ['command', 'sh', 'zsh', 'bash', 'fish', 'tool'], 'CFBundleTypeExtensions': ['command', 'sh', 'zsh', 'bash', 'fish', 'tool'],
'CFBundleTypeIconFile': appname + '.icns', 'CFBundleTypeIconFile': f'{appname}.icns',
'CFBundleTypeRole': 'Editor', 'CFBundleTypeRole': 'Editor',
}, },
{ {
@ -1035,7 +1035,7 @@ def macos_info_plist() -> bytes:
CFBundleName=appname, CFBundleName=appname,
CFBundleDisplayName=appname, CFBundleDisplayName=appname,
# Identification # Identification
CFBundleIdentifier='net.kovidgoyal.' + appname, CFBundleIdentifier=f'net.kovidgoyal.{appname}',
# Bundle Version Info # Bundle Version Info
CFBundleVersion=VERSION, CFBundleVersion=VERSION,
CFBundleShortVersionString=VERSION, CFBundleShortVersionString=VERSION,
@ -1060,7 +1060,7 @@ def macos_info_plist() -> bytes:
CFBundleAllowMixedLocalizations=True, CFBundleAllowMixedLocalizations=True,
TICapsLockLanguageSwitchCapable=True, TICapsLockLanguageSwitchCapable=True,
# User Interface and Graphics # User Interface and Graphics
CFBundleIconFile=appname + '.icns', CFBundleIconFile=f'{appname}.icns',
NSHighResolutionCapable=True, NSHighResolutionCapable=True,
NSSupportsAutomaticGraphicsSwitching=True, NSSupportsAutomaticGraphicsSwitching=True,
# Needed for dark mode in Mojave when linking against older SDKs # Needed for dark mode in Mojave when linking against older SDKs
@ -1068,13 +1068,13 @@ def macos_info_plist() -> bytes:
# Services # Services
NSServices=[ NSServices=[
{ {
'NSMenuItem': {'default': 'New ' + appname + ' Tab Here'}, 'NSMenuItem': {'default': f'New {appname} Tab Here'},
'NSMessage': 'openTab', 'NSMessage': 'openTab',
'NSRequiredContext': {'NSTextContent': 'FilePath'}, 'NSRequiredContext': {'NSTextContent': 'FilePath'},
'NSSendTypes': ['NSFilenamesPboardType', 'public.plain-text'], 'NSSendTypes': ['NSFilenamesPboardType', 'public.plain-text'],
}, },
{ {
'NSMenuItem': {'default': 'New ' + appname + ' Window Here'}, 'NSMenuItem': {'default': f'New {appname} Window Here'},
'NSMessage': 'openOSWindow', 'NSMessage': 'openOSWindow',
'NSRequiredContext': {'NSTextContent': 'FilePath'}, 'NSRequiredContext': {'NSTextContent': 'FilePath'},
'NSSendTypes': ['NSFilenamesPboardType', 'public.plain-text'], 'NSSendTypes': ['NSFilenamesPboardType', 'public.plain-text'],
@ -1109,8 +1109,8 @@ def macos_info_plist() -> bytes:
def create_macos_app_icon(where: str = 'Resources') -> None: def create_macos_app_icon(where: str = 'Resources') -> None:
iconset_dir = os.path.abspath(os.path.join('logo', appname + '.iconset')) iconset_dir = os.path.abspath(os.path.join('logo', f'{appname}.iconset'))
icns_dir = os.path.join(where, appname + '.icns') icns_dir = os.path.join(where, f'{appname}.icns')
try: try:
subprocess.check_call([ subprocess.check_call([
'iconutil', '-c', 'icns', iconset_dir, '-o', icns_dir 'iconutil', '-c', 'icns', iconset_dir, '-o', icns_dir