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_table():
t = tomlkit.table()
assert isinstance(t, Table)
def test_items_can_be_appended_to_and_removed_from_a_table():
string = """[table]
"""
parser = Parser(string)
_, table = parser._parse_table()
assert isinstance(table, Table)
assert "" == table.as_string()
table.append(Key("foo"), String(StringType.SLB, "bar", "bar", Trivia(trail="\n")))
assert 'foo = "bar"\n' == table.as_string()
table.append(
Key("baz"),
Integer(34, Trivia(comment_ws=" ", comment="# Integer", trail=""), "34"),
)
assert 'foo = "bar"\nbaz = 34 # Integer' == table.as_string()
table.remove(Key("baz"))
assert 'foo = "bar"\n' == table.as_string()
table = Table(
values,
Trivia(indent, cws, comment, trail),
is_aot,
name=name,
display_name=name,
)
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),
def table(): # type: () -> Table
return Table(Container(), Trivia(), False)
else:
container.append(name, table)
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]
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,
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:
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):
table = Table(Container(), Trivia(), True)
for k, _v in sorted(
v.items(), key=lambda i: (isinstance(i[1], dict), i[0])
from pip_shims.shims import Link
from vistir.compat import Path
_T = TypeVar("_T")
TMarker = Union[Marker, PkgResourcesMarker]
TVariable = TypeVar("TVariable", PkgResourcesVariable, Variable)
TValue = TypeVar("TValue", PkgResourcesValue, Value)
TOp = TypeVar("TOp", PkgResourcesOp, Op)
MarkerTuple = Tuple[TVariable, TOp, TValue]
TRequirement = Union[PackagingRequirement, PkgResourcesRequirement]
STRING_TYPE = Union[bytes, str, Text]
TOML_DICT_TYPES = Union[Container, Package, PackageCollection, Table, InlineTable]
S = TypeVar("S", bytes, str, Text)
TOML_DICT_OBJECTS = (Container, Package, Table, InlineTable, PackageCollection)
TOML_DICT_NAMES = [o.__class__.__name__ for o in TOML_DICT_OBJECTS]
HASH_STRING = " --hash={0}"
ALPHA_NUMERIC = r"[{0}{1}]".format(string.ascii_letters, string.digits)
PUNCTUATION = r"[\-_\.]"
ALPHANUM_PUNCTUATION = r"[{0}{1}\-_\.]".format(string.ascii_letters, string.digits)
NAME = r"{0}+{1}*{2}".format(ALPHANUM_PUNCTUATION, PUNCTUATION, ALPHA_NUMERIC)
REF = r"[{0}{1}\-\_\./]".format(string.ascii_letters, string.digits)
EXTRAS = r"(?P\[{0}(?:,{0})*\])".format(NAME)
NAME_WITH_EXTRAS = r"(?P{0}){1}?".format(NAME, EXTRAS)
NAME_RE = re.compile(NAME_WITH_EXTRAS)
SUBDIR_RE = r"(?:[&#]subdirectory=(?P.*))"
URL_NAME = r"(?:#egg={0})".format(NAME_WITH_EXTRAS)
REF_RE = r"(?:@(?P{0}+)?)".format(REF)
PATH_RE = r"(?P[:/])(?P