Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if self._current != delim.unit:
raise self.parse_error(
InternalParserError,
"Invalid character for string type {}".format(delim),
)
# consume the opening/first delim, EOF here is an issue
# (middle of string or middle of delim)
self.inc(exception=UnexpectedEofError)
if self._current == delim.unit:
# consume the closing/second delim, we do not care if EOF occurs as
# that would simply imply an empty single line string
if not self.inc() or self._current != delim.unit:
# Empty string
return String(delim, "", "", Trivia())
# consume the third delim, EOF here is an issue (middle of string)
self.inc(exception=UnexpectedEofError)
delim = delim.toggle() # convert delim to multi delim
self.mark() # to extract the original string with whitespace and all
value = ""
# A newline immediately following the opening delimiter will be trimmed.
if delim.is_multiline() and self._current == "\n":
# consume the newline, EOF here is an issue (middle of string)
self.inc(exception=UnexpectedEofError)
escaped = False # whether the previous key was ESCAPE
while True:
):
i = item(_v)
if isinstance(table, InlineTable):
i.trivia.trail = ""
table[k] = item(i)
v = table
a.append(v)
return a
elif isinstance(value, (str, unicode)):
escaped = escape_string(value)
return String(StringType.SLB, decode(value), escaped, Trivia())
elif isinstance(value, datetime):
return DateTime(
value.year,
value.month,
value.day,
value.hour,
value.minute,
value.second,
value.microsecond,
value.tzinfo,
Trivia(),
value.isoformat().replace("+00:00", "Z"),
)
elif isinstance(value, date):
return Date(value.year, value.month, value.day, Trivia(), value.isoformat())
elif isinstance(value, time):
def tomlkit_value_to_python(toml_value):
# type: (Union[Array, AoT, TOML_DICT_TYPES, Item]) -> Union[List, Dict]
value_type = type(toml_value).__name__
if (
isinstance(toml_value, TOML_DICT_OBJECTS + (dict,))
or value_type in TOML_DICT_NAMES
):
return tomlkit_dict_to_python(toml_value)
elif isinstance(toml_value, AoT) or value_type == "AoT":
return [tomlkit_value_to_python(val) for val in toml_value._body]
elif isinstance(toml_value, Array) or value_type == "Array":
return [tomlkit_value_to_python(val) for val in list(toml_value)]
elif isinstance(toml_value, String) or value_type == "String":
return "{0!s}".format(toml_value)
elif isinstance(toml_value, Bool) or value_type == "Bool":
return toml_value.value
elif isinstance(toml_value, Item):
return toml_value.value
return toml_value
def _new(self, result):
return String(self._t, result, result, self._trivia)
break
close += delim.unit
# consume this delim, EOF here is only an issue if this
# is not the third (last) delim character
self.inc(exception=UnexpectedEofError if not last else None)
if not close: # if there is no close characters, keep parsing
continue
else:
# consume the closing delim, we do not care if EOF occurs as
# that would simply imply the end of self._src
self.inc()
return String(delim, value, original, Trivia())
elif delim.is_basic() and escaped:
# attempt to parse the current char as an escaped value, an exception
# is raised if this fails
value += self._parse_escaped_char(delim.is_multiline())
# no longer escaped
escaped = False
elif delim.is_basic() and self._current == "\\":
# the next char is being escaped
escaped = True
# consume this char, EOF here is an issue (middle of string)
self.inc(exception=UnexpectedEofError)
else:
# this is either a literal string where we keep everything as is,
# or this is not a special escaped char in a basic string
):
i = item(_v)
if isinstance(table, InlineTable):
i.trivia.trail = ""
table[k] = item(i)
v = table
a.append(v)
return a
elif isinstance(value, (str, unicode)):
escaped = escape_string(value)
return String(StringType.SLB, decode(value), escaped, Trivia())
elif isinstance(value, datetime):
return DateTime(
value.year,
value.month,
value.day,
value.hour,
value.minute,
value.second,
value.microsecond,
value.tzinfo,
Trivia(),
value.isoformat().replace("+00:00", "Z"),
)
elif isinstance(value, date):
return Date(value.year, value.month, value.day, Trivia(), value.isoformat())
elif isinstance(value, time):
def tomlkit_value_to_python(toml_value):
# type: (Union[Array, AoT, TOML_DICT_TYPES, Item]) -> Union[List, Dict]
value_type = type(toml_value).__name__
if (
isinstance(toml_value, TOML_DICT_OBJECTS + (dict,))
or value_type in TOML_DICT_NAMES
):
return tomlkit_dict_to_python(toml_value)
elif isinstance(toml_value, AoT) or value_type == "AoT":
return [tomlkit_value_to_python(val) for val in toml_value._body]
elif isinstance(toml_value, Array) or value_type == "Array":
return [tomlkit_value_to_python(val) for val in list(toml_value)]
elif isinstance(toml_value, String) or value_type == "String":
return "{0!s}".format(toml_value)
elif isinstance(toml_value, Bool) or value_type == "Bool":
return toml_value.value
elif isinstance(toml_value, Item):
return toml_value.value
return toml_value