Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_items_can_be_appended_to_and_removed_from_an_inline_table():
string = """table = {}
"""
parser = Parser(string)
_, table = parser._parse_item()
assert isinstance(table, InlineTable)
assert "{}" == table.as_string()
table.append(Key("foo"), String(StringType.SLB, "bar", "bar", Trivia(trail="")))
assert '{foo = "bar"}' == table.as_string()
table.append(Key("baz"), Integer(34, Trivia(trail=""), "34"))
assert '{foo = "bar", baz = 34}' == table.as_string()
table.remove(Key("baz"))
assert '{foo = "bar"}' == table.as_string()
table.remove(Key("foo"))
assert "{}" == table.as_string()
with pytest.raises(NonExistentKey):
table.remove(Key("foo"))
def test_key_value():
k, i = tomlkit.key_value("foo = 12")
assert isinstance(k, Key)
assert isinstance(i, Integer)
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):
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):
def _new(self, result):
raw = str(result)
if self._sign:
sign = "+" if result >= 0 else "-"
raw = sign + raw
return Integer(result, self._trivia, raw)
def __radd__(self, other):
result = super(Integer, self).__radd__(other)
if isinstance(other, Integer):
return self._new(result)
return result
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):
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):
base = 8
elif raw.startswith("0x"):
digits = "[0-9a-f]"
base = 16
# Underscores should be surrounded by digits
clean = re.sub("(?i)(?<={})_(?={})".format(digits, digits), "", raw)
if "_" in clean:
return
if clean.endswith("."):
return
try:
return Integer(int(sign + clean, base), trivia, sign + raw)
except ValueError:
try:
return Float(float(sign + clean), trivia, sign + raw)
except ValueError:
return