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_toml_document_set_super_table_element():
content = """[site.user]
name = "John"
"""
doc = parse(content)
doc["site"]["user"] = "Tom"
assert (
doc.as_string()
== """[site]
user = "Tom"
def test_pyproject_toml(cookies, context):
"""Ensures that all variables are replaced inside project files."""
baked_project = cookies.bake(extra_context=context)
path = os.path.join(str(baked_project.project), 'pyproject.toml')
with open(path) as pyproject:
poetry = tomlkit.parse(pyproject.read())['tool']['poetry']
assert poetry['name'] == context['project_name']
assert poetry['description'] == context['project_description']
assert poetry['repository'] == 'https://github.com/{0}/{1}'.format(
context['organization'],
context['project_name'],
)
def test_dump(requirements_path):
converter = PoetryLockConverter()
resolver = converter.load_resolver(requirements_path / 'poetry.lock.toml')
reqs = Requirement.from_graph(graph=resolver.graph, lock=False)
assert len(reqs) > 2
content = converter.dumps(reqs=reqs, project=resolver.graph.metainfo)
assert 'name = "enum34"' in content
assert 'Python 3.4 Enum backported' in content
parsed = tomlkit.parse(content)['package']
parsed = {dep['name']: dep for dep in parsed}
content = dedent("""
[tool.poetry]
name = "test"
version = "1.2.3"
[tool.poetry.dependencies]
python = "*"
[[tool.poetry.source]]
name = "pypi"
url = "https://pypi.org/pypi"
""")
converter = PoetryConverter()
root = converter.loads(content)
new_content = converter.dumps(reqs=[], project=root)
parsed = tomlkit.parse(content)['tool']['poetry']
new_parsed = tomlkit.parse(new_content)['tool']['poetry']
assert parsed['source'] == new_parsed['source']
assert parsed == new_parsed
def pyproject() -> TOMLDocument:
with Path(__file__).parent.parent.joinpath("pyproject.toml").open() as f:
pyproject = f.read()
return tomlkit.parse(pyproject)
def _apply_version(version: str, config: Mapping, pyproject_path: Path) -> None:
pyproject = tomlkit.parse(pyproject_path.read_text())
if pyproject["tool"]["poetry"]["version"] != version:
pyproject["tool"]["poetry"]["version"] = version
pyproject_path.write_text(tomlkit.dumps(pyproject))
_substitute_version(
pyproject_path.parent,
config["substitution"]["files"],
config["substitution"]["patterns"],
version,
)
def nodes_or_groups_from_dir(self, directory):
path = join(self.path, directory)
if not isdir(path):
return
for root_dir, _dirs, files in walk(path):
for filename in files:
filepath = join(root_dir, filename)
if not filename.endswith(".toml") or \
not isfile(filepath) or \
filename.startswith("_"):
continue
infodict = dict(parse_toml(get_file_contents(filepath)))
infodict['file_path'] = filepath
yield filename[:-5], infodict
def __call__(self) -> bool:
if self.args.config:
path = Path(self.args.config)
else:
path = Path(self.config['project']) / 'pyproject.toml'
if not path.exists():
self.logger.error('cannot generate file without config')
return False
with path.open('r', encoding='utf8') as stream:
config = tomlkit.parse(stream.read())
config = dict(config['tool']['dephell'])
project_path = Path(self.config['project'])
text = make_contributing(config=config, project_path=project_path)
(project_path / self.file_name).write_text(text, encoding='utf8')
self.logger.info('generated', extra=dict(file=self.file_name))
return True
def get_version_from_pyproject_toml(pyproject_toml_path: Path = None) -> str:
"""
Return the version information from the [tool.poetry] section of the
pyproject.toml file. The version may be in non standardized form.
"""
if not pyproject_toml_path:
path = Path(__file__)
pyproject_toml_path = path.parent.parent / 'pyproject.toml'
if not pyproject_toml_path.exists():
raise RuntimeError('pyproject.toml file not found.')
pyproject_toml = tomlkit.parse(pyproject_toml_path.read_text())
if (
'tool' in pyproject_toml
and 'poetry' in pyproject_toml['tool']
and 'version' in pyproject_toml['tool']['poetry']
):
return pyproject_toml['tool']['poetry']['version']
raise RuntimeError('Version information not found in pyproject.toml file.')