Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.inc()
cws, comment, trail = self._parse_comment_trail()
result = Null()
if len(name_parts) > 1:
if missing_table:
# Missing super table
# i.e. a table initialized like this: [foo.bar]
# without initializing [foo]
#
# So we have to create the parent tables
table = Table(
Container(True),
Trivia(indent, cws, comment, trail),
is_aot and name_parts[0].key in self._aot_stack,
is_super_table=True,
name=name_parts[0].key,
)
result = table
key = name_parts[0]
for i, _name in enumerate(name_parts[1:]):
if _name in table:
child = table[_name]
else:
child = Table(
Container(True),
Trivia(indent, cws, comment, trail),
is_aot and i == len(name_parts[1:]) - 1,
def inline_table(): # type: () -> InlineTable
return InlineTable(Container(), Trivia())
elems.add(key, val)
# consume trailing whitespace
mark = self._idx
self.consume(TOMLChar.SPACES)
raw = self._src[mark : self._idx]
if raw:
elems.add(Whitespace(raw))
# consume trailing comma
trailing_comma = self._current == ","
if trailing_comma:
# consume closing bracket, EOF here is an issue (middle of inline table)
self.inc(exception=UnexpectedEofError)
return InlineTable(elems, Trivia())
def remove(self, key): # type: (Union[Key, str]) -> Container
if not isinstance(key, Key):
key = Key(key)
idx = self._map.pop(key, None)
if idx is None:
raise NonExistentKey(key)
if isinstance(idx, tuple):
for i in idx:
self._body[i] = (None, Null())
else:
old_data = self._body[idx][1]
trivia = getattr(old_data, "trivia", None)
if trivia and trivia.comment:
self._body[idx] = (None, Comment(Trivia(comment_ws="", comment=trivia.comment)))
else:
self._body[idx] = (None, Null())
super(Container, self).__delitem__(key.key)
return self
for i, _name in enumerate(names[1:]):
if i == len(names) - 2:
_name.sep = key.sep
table.append(_name, value)
else:
_name._dotted = True
if _name in table.value:
table = table.value.item(_name)
else:
table.append(
_name,
Table(
Container(True),
Trivia(),
False,
is_super_table=i < len(names) - 2,
),
)
table = table[_name]
def item(value, _parent=None):
from .container import Container
if isinstance(value, Item):
return value
if isinstance(value, bool):
return Bool(value, Trivia())
elif isinstance(value, int):
return Integer(value, Trivia(), str(value))
elif isinstance(value, float):
return Float(value, Trivia(), str(value))
elif isinstance(value, dict):
if isinstance(value, InlineTableDict):
val = InlineTable(Container(), Trivia())
else:
val = Table(Container(), Trivia(), False)
for k, v in sorted(value.items(), key=lambda i: (isinstance(i[1], dict), i[0])):
val[k] = item(v, _parent=val)
return val
elif isinstance(value, list):
if value and isinstance(value[0], dict):
a = AoT([])
else:
a = Array([], Trivia())
for v in value:
if isinstance(v, dict):
table = Table(Container(), Trivia(), True)
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, value, escaped, Trivia())
elif isinstance(value, datetime):
return DateTime(value, Trivia(), value.isoformat().replace("+00:00", "Z"))
elif isinstance(value, date):
return Date(value, Trivia(), value.isoformat())
elif isinstance(value, time):
return Time(value, Trivia(), value.isoformat())
raise ValueError("Invalid type {}".format(type(value)))
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:
Trivia(indent, cws, comment, trail),
is_aot and name_parts[0].key in self._aot_stack,
is_super_table=True,
name=name_parts[0].key,
)
result = table
key = name_parts[0]
for i, _name in enumerate(name_parts[1:]):
if _name in table:
child = table[_name]
else:
child = Table(
Container(True),
Trivia(indent, cws, comment, trail),
is_aot and i == len(name_parts[1:]) - 1,
is_super_table=i < len(name_parts[1:]) - 1,
name=_name.key,
display_name=name if i == len(name_parts[1:]) - 1 else None,
)
if is_aot and i == len(name_parts[1:]) - 1:
table.append(_name, AoT([child], name=table.name, parsed=True))
else:
table.append(_name, child)
table = child
values = table.value
else:
if name_parts:
key = name_parts[0]
from .container import Container
if isinstance(value, Item):
return value
if isinstance(value, bool):
return Bool(value, Trivia())
elif isinstance(value, int):
return Integer(value, Trivia(), str(value))
elif isinstance(value, float):
return Float(value, Trivia(), str(value))
elif isinstance(value, dict):
if isinstance(value, InlineTableDict):
val = InlineTable(Container(), Trivia())
else:
val = Table(Container(), Trivia(), False)
for k, v in sorted(value.items(), key=lambda i: (isinstance(i[1], dict), i[0])):
val[k] = item(v, _parent=val)
return val
elif isinstance(value, list):
if value and isinstance(value[0], dict):
a = AoT([])
else:
a = Array([], Trivia())
for v in value:
if isinstance(v, dict):
table = Table(Container(), Trivia(), True)
for k, _v in sorted(
v.items(), key=lambda i: (isinstance(i[1], dict), i[0])