3from abc
import abstractmethod
4from enum
import Enum, auto
5from typing
import Any, Optional, Protocol, TypeVar
7from .EscapeLeadingWhitespace
import EscapeLeadingWhitespace
17 extension = filename.rsplit(
".", 1)[-1]
21 raise ValueError(f
"Unknown language for file {filename}")
25 def __init__(self, expected: Optional[T], actual: T, fmt:
"LiteralFormat") ->
None:
34 self, value: T, language: Language, encoding_policy:
"EscapeLeadingWhitespace"
36 raise NotImplementedError(
"Subclasses must implement the encode method")
39 def parse(self, string: str, language: Language) -> T:
40 raise NotImplementedError(
"Subclasses must implement the parse method")
44PADDING_SIZE = len(str(MAX_RAW_NUMBER)) - 1
48 if value >= MAX_RAW_NUMBER:
49 mod = value % MAX_RAW_NUMBER
50 left_padding = PADDING_SIZE - len(str(mod))
53 buffer.write(
"0" * left_padding)
54 buffer.write(str(mod))
55 return buffer.getvalue()
59 return buffer.getvalue()
61 buffer.write(str(value))
62 return buffer.getvalue()
70 self, value: str, language: Language, encoding_policy: EscapeLeadingWhitespace
72 if language == Language.PYTHON:
78 raise NotImplementedError(
79 f
"Encoding for language {language} is not implemented."
82 def parse(self, string: str, language: Language) -> str:
83 if language == Language.PYTHON:
84 if not string.startswith(TRIPLE_QUOTE):
89 raise NotImplementedError(
90 f
"Encoding for language {language} is not implemented."
94 source = io.StringIO()
110 source.write(
"\\u" + str(ord(char)).zfill(4))
114 return source.getvalue()
117 return c
in "\u0000\u001f" or c ==
"\u007f"
121 assert source_with_quotes.startswith(
'"')
122 assert source_with_quotes.endswith(
'"')
123 source = source_with_quotes[1:-1]
128 self, arg: str, escape_leading_whitespace: EscapeLeadingWhitespace
130 escape_backslashes = arg.replace(
"\\",
"\\\\")
131 escape_triple_quotes = escape_backslashes.replace(TRIPLE_QUOTE,
'\\"\\"\\"')
133 def protect_trailing_whitespace(line: str) -> str:
134 if line.endswith(
" "):
135 return line[:-1] +
"\\u0020"
136 elif line.endswith(
"\t"):
137 return line[:-1] +
"\\t"
141 lines = escape_triple_quotes.splitlines()
142 protect_whitespace =
"\n".join(
143 escape_leading_whitespace.escape_line(
144 protect_trailing_whitespace(line),
"\\u0020",
"\\t"
149 return f
"{TRIPLE_QUOTE}{protect_whitespace}{TRIPLE_QUOTE}"
151 _char_literal_pattern = re.compile(
r"""\{'(\\?.)'\}""")
154 def replace_char(char_literal: str) -> str:
155 if len(char_literal) == 1:
157 elif len(char_literal) == 2
and char_literal[0] ==
"\\":
158 if char_literal[1] ==
"t":
160 elif char_literal[1] ==
"b":
162 elif char_literal[1] ==
"n":
164 elif char_literal[1] ==
"r":
166 elif char_literal[1] ==
"'":
168 elif char_literal[1] ==
"\\":
171 raise ValueError(f
"Unknown character literal {char_literal}")
173 raise ValueError(f
"Unknown character literal {char_literal}")
176 lambda match: replace_char(match.group(1)), source
180 value = io.StringIO()
182 while i < len(source):
204 code = int(source[i + 1 : i + 5], 16)
205 value.write(chr(code))
208 raise ValueError(f
"Unknown escape sequence {c}")
212 return value.getvalue()
215 assert source_with_quotes.startswith(TRIPLE_QUOTE)
216 assert source_with_quotes.endswith(TRIPLE_QUOTE)
218 source_with_quotes[len(TRIPLE_QUOTE) : -len(TRIPLE_QUOTE)]
227 encoding_policy: EscapeLeadingWhitespace,
229 if isinstance(value, int):
234 def parse(self, string: str, language: Language) -> Any:
235 raise NotImplementedError
239 to_match_disk = auto()
251 encoding_policy: EscapeLeadingWhitespace,
253 raise NotImplementedError
255 def parse(self, string: str, language: Language) -> TodoStub:
256 raise NotImplementedError
"Language" from_filename(cls, str filename)
Any parse(self, str string, Language language)
str encode(self, Any value, Language language, EscapeLeadingWhitespace encoding_policy)
str _parseSinglePython(self, str source_with_quotes)
str parseMultiPython(self, str source_with_quotes)
str inline_backslashes(self, str source)
str encodeMultiPython(self, str arg, EscapeLeadingWhitespace escape_leading_whitespace)
str _unescape_python(self, str source)
bool _is_control_char(self, str c)
str _encodeSinglePython(self, str value)
str parse(self, str string, Language language)
str encode(self, str value, Language language, EscapeLeadingWhitespace encoding_policy)
str encode(self, TodoStub value, Language language, EscapeLeadingWhitespace encoding_policy)
TodoStub parse(self, str string, Language language)
None __init__(self, Optional[T] expected, T actual, "LiteralFormat" fmt)
str _encode_int_underscores(io.StringIO buffer, int value)