Files
kitty-mirror/kitty/bash.py
Kovid Goyal f1d0d0949b Run pyupgrade to update the codebase to python 3.9
Gets rid of a lot of typing ugliness by using type annotations on
builtin types
2024-07-31 07:55:27 +05:30

67 lines
1.7 KiB
Python

#!/usr/bin/env python
# License: GPLv3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
from .utils import shlex_split
def decode_ansi_c_quoted_string(text: str) -> str:
return next(shlex_split(text, True))
def decode_double_quoted_string(text: str, pos: int) -> tuple[str, int]:
escapes = r'"\$`'
buf: list[str] = []
a = buf.append
while pos < len(text):
ch = text[pos]
pos += 1
if ch == '\\':
if text[pos] in escapes:
a(text[pos])
pos += 1
continue
a(ch)
elif ch == '"':
break
else:
a(ch)
return ''.join(buf), pos
def parse_modern_bash_env(text: str) -> dict[str, str]:
ans = {}
for line in text.splitlines():
idx = line.find('=')
if idx < 0:
break
key = line[:idx].rpartition(' ')[2]
val = line[idx+1:]
if val.startswith('"'):
val = decode_double_quoted_string(val, 1)[0]
else:
val = decode_ansi_c_quoted_string(val)
ans[key] = val
return ans
def parse_bash_env(text: str, bash_version: str) -> dict[str, str]:
# See https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
parts = bash_version.split('.')
bv = tuple(map(int, parts[:2]))
if bv >= (5, 2):
return parse_modern_bash_env(text)
ans = {}
pos = 0
while pos < len(text):
idx = text.find('="', pos)
if idx < 0:
break
i = text.rfind(' ', 0, idx)
if i < 0:
break
key = text[i+1:idx]
pos = idx + 2
ans[key], pos = decode_double_quoted_string(text, pos)
return ans