Selfie
Loading...
Searching...
No Matches
selfie_lib.SourceFile.SourceFile.ToBeLiteral Class Reference

Public Member Functions

None __init__ (self, "SourceFile" parent, str dot_fun_open_paren, Slice function_call_plus_arg, Slice arg, Language language, EscapeLeadingWhitespace escape_leading_whitespace)
 
int set_literal_and_get_newline_delta (self, LiteralValue literal_value)
 
Any parse_literal (self, LiteralFormat literal_format)
 

Protected Member Functions

 _get_function_call_plus_arg (self)
 
 _get_arg (self)
 

Detailed Description

Definition at line 56 of file SourceFile.py.

Constructor & Destructor Documentation

◆ __init__()

None selfie_lib.SourceFile.SourceFile.ToBeLiteral.__init__ (   self,
"SourceFile"  parent,
str  dot_fun_open_paren,
Slice  function_call_plus_arg,
Slice  arg,
Language  language,
EscapeLeadingWhitespace  escape_leading_whitespace 
)

Definition at line 57 of file SourceFile.py.

65 ) -> None:
66 self.__parent = parent
67 self.__dot_fun_open_paren = dot_fun_open_paren
68 self.__function_call_plus_arg = function_call_plus_arg
69 self.__arg = arg
70 self.__language = language
71 self.__escape_leading_whitespace = escape_leading_whitespace
72

Member Function Documentation

◆ _get_arg()

selfie_lib.SourceFile.SourceFile.ToBeLiteral._get_arg (   self)
protected

Definition at line 76 of file SourceFile.py.

76 def _get_arg(self):
77 return self.__arg
78

◆ _get_function_call_plus_arg()

selfie_lib.SourceFile.SourceFile.ToBeLiteral._get_function_call_plus_arg (   self)
protected

Definition at line 73 of file SourceFile.py.

73 def _get_function_call_plus_arg(self):
74 return self.__function_call_plus_arg
75

◆ parse_literal()

Any selfie_lib.SourceFile.SourceFile.ToBeLiteral.parse_literal (   self,
LiteralFormat  literal_format 
)

Definition at line 110 of file SourceFile.py.

110 def parse_literal(self, literal_format: LiteralFormat) -> Any:
111 return literal_format.parse(self.__arg.__str__(), self.__language)
112
113 def find_on_line(self, to_find: str, line_one_indexed: int) -> Slice:

◆ set_literal_and_get_newline_delta()

int selfie_lib.SourceFile.SourceFile.ToBeLiteral.set_literal_and_get_newline_delta (   self,
LiteralValue  literal_value 
)

Definition at line 79 of file SourceFile.py.

79 def set_literal_and_get_newline_delta(self, literal_value: LiteralValue) -> int:
80 encoded = literal_value.format.encode(
81 literal_value.actual,
82 self.__language,
83 self.__escape_leading_whitespace,
84 )
85 if not isinstance(literal_value.format, LiteralRepr):
86 # we don't roundtrip LiteralRepr because `eval` is dangerous
87 round_tripped = literal_value.format.parse(encoded, self.__language)
88 if round_tripped != literal_value.actual:
89 raise ValueError(
90 f"There is an error in {literal_value.format.__class__.__name__}, "
91 "the following value isn't round tripping.\n"
92 f"Please report this error and the data below at "
93 "https://github.com/diffplug/selfie/issues/new\n"
94 f"```\n"
95 f"ORIGINAL\n{literal_value.actual}\n"
96 f"ROUNDTRIPPED\n{round_tripped}\n"
97 f"ENCODED ORIGINAL\n{encoded}\n"
98 f"```\n"
99 )
100 existing_newlines = self.__function_call_plus_arg.count("\n")
101 new_newlines = encoded.count("\n")
102 self.__parent._content_slice = Slice( # noqa: SLF001
103 self.__function_call_plus_arg.replaceSelfWith(
104 f"{self.__dot_fun_open_paren}{encoded})"
105 )
106 )
107
108 return new_newlines - existing_newlines
109

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