Selfie
Loading...
Searching...
No Matches
selfie_lib.EscapeLeadingWhitespace.EscapeLeadingWhitespace Class Reference
Inheritance diagram for selfie_lib.EscapeLeadingWhitespace.EscapeLeadingWhitespace:

Public Member Functions

str escape_line (self, str line, str space, str tab)
 
"EscapeLeadingWhitespace" appropriate_for (cls, str file_content)
 

Static Public Attributes

 ALWAYS = auto()
 
 NEVER = auto()
 
 ONLY_ON_SPACE = auto()
 
 ONLY_ON_TAB = auto()
 

Detailed Description

Definition at line 4 of file EscapeLeadingWhitespace.py.

Member Function Documentation

◆ appropriate_for()

"EscapeLeadingWhitespace" selfie_lib.EscapeLeadingWhitespace.EscapeLeadingWhitespace.appropriate_for (   cls,
str  file_content 
)

Definition at line 31 of file EscapeLeadingWhitespace.py.

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

◆ escape_line()

str selfie_lib.EscapeLeadingWhitespace.EscapeLeadingWhitespace.escape_line (   self,
str  line,
str  space,
str  tab 
)

Definition at line 10 of file EscapeLeadingWhitespace.py.

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
29

Member Data Documentation

◆ ALWAYS

selfie_lib.EscapeLeadingWhitespace.EscapeLeadingWhitespace.ALWAYS = auto()
static

Definition at line 5 of file EscapeLeadingWhitespace.py.

◆ NEVER

selfie_lib.EscapeLeadingWhitespace.EscapeLeadingWhitespace.NEVER = auto()
static

Definition at line 6 of file EscapeLeadingWhitespace.py.

◆ ONLY_ON_SPACE

selfie_lib.EscapeLeadingWhitespace.EscapeLeadingWhitespace.ONLY_ON_SPACE = auto()
static

Definition at line 7 of file EscapeLeadingWhitespace.py.

◆ ONLY_ON_TAB

selfie_lib.EscapeLeadingWhitespace.EscapeLeadingWhitespace.ONLY_ON_TAB = auto()
static

Definition at line 8 of file EscapeLeadingWhitespace.py.


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