From a0d6445dda1a466b6be7916d2264c55a2960e13c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 5 May 2020 08:22:08 +0530 Subject: [PATCH] Move expandvars to utils --- kitty/config.py | 20 +++----------------- kitty/main.py | 6 +++--- kitty/utils.py | 18 ++++++++++++++++-- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/kitty/config.py b/kitty/config.py index 2faa08b64..53f308eb2 100644 --- a/kitty/config.py +++ b/kitty/config.py @@ -9,8 +9,8 @@ import sys from contextlib import contextmanager, suppress from functools import partial from typing import ( - Any, Callable, Dict, Generator, Iterable, List, Match, NamedTuple, - Optional, Sequence, Set, Tuple, Type, Union + Any, Callable, Dict, Generator, Iterable, List, NamedTuple, Optional, + Sequence, Set, Tuple, Type, Union ) from . import fast_data_types as defines @@ -24,7 +24,7 @@ from .constants import cache_dir, defconf, is_macos from .key_names import get_key_name_lookup, key_name_aliases from .options_stub import Options as OptionsStub from .typing import EdgeLiteral, TypedDict -from .utils import log_error +from .utils import expandvars, log_error KeySpec = Tuple[int, bool, int] KeyMap = Dict[KeySpec, 'KeyAction'] @@ -585,20 +585,6 @@ def handle_deprecated_macos_show_window_title_in_menubar_alias(key: str, val: st ans['macos_show_window_title_in'] = macos_show_window_title_in -def expandvars(val: str, env: Dict[str, str] = {}) -> str: - - def sub(m: Match) -> str: - key = m.group(1) - result = env.get(key) - if result is None: - result = os.environ.get(key) - if result is None: - result = m.group() - return result - - return re.sub(r'\$\{(\S+?)\}', sub, val) - - @special_handler def handle_env(key: str, val: str, ans: Dict[str, Any]) -> None: key, val = val.partition('=')[::2] diff --git a/kitty/main.py b/kitty/main.py index 7e9a62441..93b8640b4 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -7,7 +7,7 @@ import os import shutil import sys from contextlib import contextmanager, suppress -from typing import Generator, List, Mapping, Optional, Tuple, Sequence +from typing import Generator, List, Mapping, Optional, Sequence, Tuple from .borders import load_borders_program from .boss import Boss @@ -15,7 +15,7 @@ from .child import set_default_env from .cli import create_opts, parse_args from .cli_stub import CLIOptions from .conf.utils import BadLine -from .config import cached_values_for, initial_window_size_func, expandvars +from .config import cached_values_for, initial_window_size_func from .constants import ( appname, beam_cursor_data_file, config_dir, glfw_path, is_macos, is_wayland, kitty_exe, logo_data_file, running_in_kitty @@ -29,7 +29,7 @@ from .fonts.box_drawing import set_scale from .fonts.render import set_font_family from .options_stub import Options as OptionsStub from .utils import ( - detach, log_error, read_shell_environment, single_instance, + detach, expandvars, log_error, read_shell_environment, single_instance, startup_notification_handler, unix_socket_paths ) from .window import load_shader_programs diff --git a/kitty/utils.py b/kitty/utils.py index 2a8e2447a..30aff65e6 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -14,8 +14,8 @@ from contextlib import suppress from functools import lru_cache from time import monotonic from typing import ( - Any, Callable, Dict, Generator, Iterable, List, NamedTuple, Optional, - Tuple, Union, cast + Any, Callable, Dict, Generator, Iterable, List, Match, NamedTuple, + Optional, Tuple, Union, cast ) from .constants import ( @@ -28,6 +28,20 @@ from .typing import AddressFamily, PopenType, Socket, StartupCtx BASE = os.path.dirname(os.path.abspath(__file__)) +def expandvars(val: str, env: Dict[str, str] = {}) -> str: + + def sub(m: Match) -> str: + key = m.group(1) + result = env.get(key) + if result is None: + result = os.environ.get(key) + if result is None: + result = m.group() + return result + + return re.sub(r'\$\{(\S+?)\}', sub, val) + + def load_shaders(name: str) -> Tuple[str, str]: from .fast_data_types import GLSL_VERSION with open(os.path.join(BASE, '{}_vertex.glsl'.format(name))) as f: