0

In my cookiecutter template, I have the following question: "Do you want to use an internal repository? ["Yes", "No"].

If answered "Yes", I would like to add another table to my pyproject.toml configuration file.

[[tool.poetry.source]]
name = "internal_repo"
url = "https://internal_repo/simple"
default = true

What's the best way to do it?

EDIT: Include parts of the template and parts of the resulting file.

Template:

# pyproject.toml template

...

[[tool.poetry.source]]
name = "internal_repo1"
url = "https://internal_repo1/simple"
default = true

{% if cookiecutter.use_bloomberg == "Yes" %}
[[tool.poetry.source]]
name = "internal_repo2"
url = "https://internal_repo2/simple"
{% endif %}

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

Output if cookiecutter.use_bloomerg == "Yes"

# pyproject.toml

...

[[tool.poetry.source]]
name = "internal_repo1"
url = "https://internal_repo1/simple"
default = true


[[tool.poetry.source]]
name = "internal_repo2"
url = "https://internal_repo2/simple"




[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

Output if cookiecutter.use_bloomerg == "No"

# pyproject.toml

...

[[tool.poetry.source]]
name = "internal_repo1"
url = "https://internal_repo1/simple"
default = true





[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

I would like to have 1 blank line between the different sections in the pyproject.toml file.

Andi
  • 3,196
  • 2
  • 24
  • 44

1 Answers1

1

You should not use spaces in your questions, as those are used as attributes of the cookiecutter object afterwards.

So, what you want will look similar to this:

// cookiecutter.json
{
  "use_internal_repository": ["Yes", "No"]
}
# The file where you need to check
{% if cookiecutter.use_internal_repository == "Yes" %}
Yes, I want an internal repo!
{% else %}
No, thank you! I'm good.
{% endif %}

Keep in mind, that those template tags may lead to unwanted newlines. If you want to control that, you should use minus signs here and there. You could learn more about this here.

wankata
  • 855
  • 4
  • 12
  • This works. The only question left open is the fact that all the rows containing {% ... %} will lead to empty roads in the final ``pyproject.toml`` file. This doesn't influence the correct functioning of the file, it's just a formatting issue. – Andi Aug 26 '22 at 12:29
  • Yes, you should click on the link in my comment to learn how to control the white spaces. Basically you should find the right combination of minus signs in front of the %s. – wankata Aug 29 '22 at 06:10
  • I don't understand it. It doesn't matter where I place minus signs (front/end), there's no difference in my ``pyproject.toml`` file. The blank lines are still present. – Andi Aug 31 '22 at 10:42
  • If it is OK, could you please add your template and the result to the question, so I can inspect it and help with it? – wankata Aug 31 '22 at 11:09