Move expandvars to utils

This commit is contained in:
Kovid Goyal
2020-05-05 08:22:08 +05:30
parent e74797037c
commit a0d6445dda
3 changed files with 22 additions and 22 deletions
+16 -2
View File
@@ -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: