Selfie
Loading...
Searching...
No Matches
selfie_lib.Literals.LiteralString Class Reference
Inheritance diagram for selfie_lib.Literals.LiteralString:
selfie_lib.Literals.LiteralFormat

Public Member Functions

str encode (self, str value, Language language, EscapeLeadingWhitespace encoding_policy)
 
str parse (self, str string, Language language)
 
str encodeMultiPython (self, str arg, EscapeLeadingWhitespace escape_leading_whitespace)
 
str inline_backslashes (self, str source)
 
str parseMultiPython (self, str source_with_quotes)
 

Protected Member Functions

str _encodeSinglePython (self, str value)
 
bool _is_control_char (self, str c)
 
str _parseSinglePython (self, str source_with_quotes)
 
str _unescape_python (self, str source)
 

Static Protected Attributes

 _char_literal_pattern = re.compile()
 

Detailed Description

Definition at line 68 of file Literals.py.

Member Function Documentation

◆ _encodeSinglePython()

str selfie_lib.Literals.LiteralString._encodeSinglePython (   self,
str  value 
)
protected

Definition at line 93 of file Literals.py.

93 def _encodeSinglePython(self, value: str) -> str:
94 source = io.StringIO()
95 source.write('"')
96 for char in value:
97 if char == "\b":
98 source.write("\\b")
99 elif char == "\n":
100 source.write("\\n")
101 elif char == "\r":
102 source.write("\\r")
103 elif char == "\t":
104 source.write("\\t")
105 elif char == '"':
106 source.write('\\"')
107 elif char == "\\":
108 source.write("\\\\")
109 elif self._is_control_char(char):
110 source.write("\\u" + str(ord(char)).zfill(4))
111 else:
112 source.write(char)
113 source.write('"')
114 return source.getvalue()
115

◆ _is_control_char()

bool selfie_lib.Literals.LiteralString._is_control_char (   self,
str  c 
)
protected

Definition at line 116 of file Literals.py.

116 def _is_control_char(self, c: str) -> bool:
117 return c in "\u0000\u001f" or c == "\u007f"
118

◆ _parseSinglePython()

str selfie_lib.Literals.LiteralString._parseSinglePython (   self,
str  source_with_quotes 
)
protected

Definition at line 120 of file Literals.py.

120 def _parseSinglePython(self, source_with_quotes: str) -> str:
121 assert source_with_quotes.startswith('"')
122 assert source_with_quotes.endswith('"')
123 source = source_with_quotes[1:-1]
124 to_unescape = self.inline_backslashes(source) # changed from inline_dollar
125 return self._unescape_python(to_unescape)
126

◆ _unescape_python()

str selfie_lib.Literals.LiteralString._unescape_python (   self,
str  source 
)
protected

Definition at line 179 of file Literals.py.

179 def _unescape_python(self, source: str) -> str:
180 value = io.StringIO()
181 i = 0
182 while i < len(source):
183 c = source[i]
184 if c == "\\":
185 i += 1
186 c = source[i]
187 if c == '"':
188 value.write('"')
189 elif c == "\\":
190 value.write("\\")
191 elif c == "b":
192 value.write("\b")
193 elif c == "f":
194 value.write("\f")
195 elif c == "n":
196 value.write("\n")
197 elif c == "r":
198 value.write("\r")
199 elif c == "s":
200 value.write(" ")
201 elif c == "t":
202 value.write("\t")
203 elif c == "u":
204 code = int(source[i + 1 : i + 5], 16)
205 value.write(chr(code))
206 i += 4
207 else:
208 raise ValueError(f"Unknown escape sequence {c}")
209 else:
210 value.write(c)
211 i += 1
212 return value.getvalue()
213

◆ encode()

str selfie_lib.Literals.LiteralString.encode (   self,
str  value,
Language  language,
EscapeLeadingWhitespace   encoding_policy 
)

Reimplemented from selfie_lib.Literals.LiteralFormat.

Definition at line 69 of file Literals.py.

71 ) -> str:
72 if language == Language.PYTHON:
73 if "\n" not in value:
74 return self._encodeSinglePython(value)
75 else:
76 return self.encodeMultiPython(value, encoding_policy)
77 else:
78 raise NotImplementedError(
79 f"Encoding for language {language} is not implemented."
80 )
81

◆ encodeMultiPython()

str selfie_lib.Literals.LiteralString.encodeMultiPython (   self,
str  arg,
EscapeLeadingWhitespace   escape_leading_whitespace 
)

Definition at line 127 of file Literals.py.

129 ) -> str:
130 escape_backslashes = arg.replace("\\", "\\\\")
131 escape_triple_quotes = escape_backslashes.replace(TRIPLE_QUOTE, '\\"\\"\\"')
132
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"
138 else:
139 return line
140
141 lines = escape_triple_quotes.splitlines()
142 protect_whitespace = "\n".join(
143 escape_leading_whitespace.escape_line(
144 protect_trailing_whitespace(line), "\\u0020", "\\t"
145 )
146 for line in lines
147 )
148
149 return f"{TRIPLE_QUOTE}{protect_whitespace}{TRIPLE_QUOTE}"
150

◆ inline_backslashes()

str selfie_lib.Literals.LiteralString.inline_backslashes (   self,
str  source 
)

Definition at line 153 of file Literals.py.

153 def inline_backslashes(self, source: str) -> str:
154 def replace_char(char_literal: str) -> str:
155 if len(char_literal) == 1:
156 return char_literal
157 elif len(char_literal) == 2 and char_literal[0] == "\\":
158 if char_literal[1] == "t":
159 return "\t"
160 elif char_literal[1] == "b":
161 return "\b"
162 elif char_literal[1] == "n":
163 return "\n"
164 elif char_literal[1] == "r":
165 return "\r"
166 elif char_literal[1] == "'":
167 return "'"
168 elif char_literal[1] == "\\":
169 return "\\"
170 else:
171 raise ValueError(f"Unknown character literal {char_literal}")
172 else:
173 raise ValueError(f"Unknown character literal {char_literal}")
174
175 return self._char_literal_pattern.sub(
176 lambda match: replace_char(match.group(1)), source
177 )
178

◆ parse()

str selfie_lib.Literals.LiteralString.parse (   self,
str  string,
Language  language 
)

Reimplemented from selfie_lib.Literals.LiteralFormat.

Definition at line 82 of file Literals.py.

82 def parse(self, string: str, language: Language) -> str:
83 if language == Language.PYTHON:
84 if not string.startswith(TRIPLE_QUOTE):
85 return self._parseSinglePython(string)
86 else:
87 return self.parseMultiPython(string)
88 else:
89 raise NotImplementedError(
90 f"Encoding for language {language} is not implemented."
91 )
92

◆ parseMultiPython()

str selfie_lib.Literals.LiteralString.parseMultiPython (   self,
str  source_with_quotes 
)

Definition at line 214 of file Literals.py.

214 def parseMultiPython(self, source_with_quotes: str) -> str:
215 assert source_with_quotes.startswith(TRIPLE_QUOTE)
216 assert source_with_quotes.endswith(TRIPLE_QUOTE)
217 return self._unescape_python(
218 source_with_quotes[len(TRIPLE_QUOTE) : -len(TRIPLE_QUOTE)]
219 )
220
221

Member Data Documentation

◆ _char_literal_pattern

selfie_lib.Literals.LiteralString._char_literal_pattern = re.compile()
staticprotected

Definition at line 151 of file Literals.py.


The documentation for this class was generated from the following file: