Skip to content

Commit 6a70f8f

Browse files
authored
py: leading whitespace and literal fixes (#386)
2 parents e19279c + 1f2374c commit 6a70f8f

File tree

4 files changed

+58
-30
lines changed

4 files changed

+58
-30
lines changed

python/selfie-lib/selfie_lib/EscapeLeadingWhitespace.py

+50-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,56 @@
22

33

44
class EscapeLeadingWhitespace(Enum):
5+
ALWAYS = auto()
56
NEVER = auto()
7+
ONLY_ON_SPACE = auto()
8+
ONLY_ON_TAB = auto()
69

7-
def escape_line(self, line: str, space: str, tab: str) -> str: # noqa: ARG002
8-
return line
10+
def escape_line(self, line: str, space: str, tab: str) -> str:
11+
if line.startswith(" "):
12+
if (
13+
self == EscapeLeadingWhitespace.ALWAYS
14+
or self == EscapeLeadingWhitespace.ONLY_ON_SPACE
15+
):
16+
return f"{space}{line[1:]}"
17+
else:
18+
return line
19+
elif line.startswith("\t"):
20+
if (
21+
self == EscapeLeadingWhitespace.ALWAYS
22+
or self == EscapeLeadingWhitespace.ONLY_ON_TAB
23+
):
24+
return f"{tab}{line[1:]}"
25+
else:
26+
return line
27+
else:
28+
return line
929

10-
@staticmethod
11-
def appropriate_for(file_content: str) -> "EscapeLeadingWhitespace": # noqa: ARG004
12-
return EscapeLeadingWhitespace.NEVER
30+
@classmethod
31+
def appropriate_for(cls, file_content: str) -> "EscapeLeadingWhitespace":
32+
MIXED = "m"
33+
common_whitespace = None
34+
35+
for line in file_content.splitlines():
36+
whitespace = "".join(c for c in line if c.isspace())
37+
if not whitespace:
38+
continue
39+
elif all(c == " " for c in whitespace):
40+
whitespace = " "
41+
elif all(c == "\t" for c in whitespace):
42+
whitespace = "\t"
43+
else:
44+
whitespace = MIXED
45+
46+
if common_whitespace is None:
47+
common_whitespace = whitespace
48+
elif common_whitespace != whitespace:
49+
common_whitespace = MIXED
50+
break
51+
52+
if common_whitespace == " ":
53+
return cls.ONLY_ON_TAB
54+
elif common_whitespace == "\t":
55+
return cls.ONLY_ON_SPACE
56+
else:
57+
return cls.ALWAYS

python/selfie-lib/selfie_lib/Literals.py

+3-20
Original file line numberDiff line numberDiff line change
@@ -212,27 +212,10 @@ def _unescape_python(self, source: str) -> str:
212212
return value.getvalue()
213213

214214
def parseMultiPython(self, source_with_quotes: str) -> str:
215-
assert source_with_quotes.startswith(TRIPLE_QUOTE + "\n")
215+
assert source_with_quotes.startswith(TRIPLE_QUOTE)
216216
assert source_with_quotes.endswith(TRIPLE_QUOTE)
217-
218-
source = source_with_quotes[len(TRIPLE_QUOTE) + 1 : -len(TRIPLE_QUOTE)]
219-
lines = source.split("\n")
220-
221-
common_prefix = min(
222-
(line[: len(line) - len(line.lstrip())] for line in lines if line.strip()),
223-
default="",
224-
)
225-
226-
def remove_common_prefix(line: str) -> str:
227-
return line[len(common_prefix) :] if common_prefix else line
228-
229-
def handle_escape_sequences(line: str) -> str:
230-
return self._unescape_python(line.rstrip())
231-
232-
return "\n".join(
233-
handle_escape_sequences(remove_common_prefix(line))
234-
for line in lines
235-
if line.strip()
217+
return self._unescape_python(
218+
source_with_quotes[len(TRIPLE_QUOTE) : -len(TRIPLE_QUOTE)]
236219
)
237220

238221

python/selfie-lib/selfie_lib/SourceFile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def _parse_code(
208208
# If all parentheses are closed, return the current index
209209
if parenthesis_count == 0:
210210
end_paren = i
211-
end_arg = i - 1
211+
end_arg = i
212212
return (end_paren, end_arg)
213213
# else ...
214214
raise AssertionError(

python/selfie-lib/tests/LiteralString_test.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ def test_parse_single(self, value, expected):
5050
@pytest.mark.parametrize(
5151
("value", "expected"),
5252
[
53-
("\n123\nabc", "123\nabc"),
54-
("\n 123\n abc", "123\nabc"),
55-
("\n 123 \n abc\t", "123\nabc"),
56-
("\n 123 \\s\n abc\t\\s", "123 \nabc\t "),
53+
("\n123\nabc", "\n123\nabc"),
54+
("\n 123\n abc", "\n 123\n abc"),
55+
("\n 123 \n abc\t", "\n 123 \n abc\t"),
56+
(" 123 \n abc\t", " 123 \n abc\t"),
5757
],
5858
)
5959
def test_parse_multi(self, value, expected):

0 commit comments

Comments
 (0)