Skip to main content

Automatically conversion of .ini/.cfg files to TOML equivalents

Project description

Built Status ReadTheDocs Coveralls PyPI-Server Project generated with PyScaffold

ini2toml

Automatically translates .ini/.cfg files into TOML

Description

The original purpose of this project is to help migrating setup.cfg files to PEP 621, but by extension it can also be used to convert any compatible .ini/.cfg file to TOML.

Please notice, the provided .ini/.cfg files should follow the same syntax supported by Python’s ConfigParser library (here referred to as INI syntax) and more specifically abide by ConfigUpdater restrictions (e.g., no interpolation or repeated fields).

Usage

ini2toml comes in two flavours: “lite” and “full”. The “lite” flavour will create a TOML document that does not contain any of the comments from the original .ini/.cfg file. On the other hand, the “full” flavour will make an extra effort to translate these comments into a TOML-equivalent (please notice sometimes this translation is not perfect, so it is always good to check the TOML document afterwards).

To get started, you need to install the package, which can be easily done using pipx:

$ pipx install 'ini2toml[lite]'
# OR
$ pipx install 'ini2toml[full]'

Now you can use ini2toml as a command line tool:

# in you terminal
$ ini2toml --help
$ ini2toml path/to/ini/or/cfg/file

You can also use ini2toml in your Python scripts or projects:

# in your python code
from ini2toml.api import Translator

profile_name = "setup.cfg"
toml_str = Translator().translate(original_contents_str, profile_name)

To do so, don’t forget to add it to your virtual environment or specify it as a project dependency.

Note that the class Translator will try to guess which flavour to use based on the available installed dependencies. If you need something more deterministic, consider using LiteTranslator and FullTranslator, or explicitly specifying the ini_loads_fn and toml_dumps_fn keyword arguments in the constructor.

More details about ini2toml and its Python API can be found in our docs.

Limitations

ini2toml will try its best to create good quality translations from .ini/.cfg into .toml files. However the tool comes with a set of well known limitations:

  • Although ini2toml attempts to keep the same order/sequence as the original information was written, sometimes that is not compatible with the TOML syntax, and things end up moving around a bit.

  • ini2toml uses ConfigParser + tomli-w for implementing the “lite” flavour and ConfigUpdater + tomlkit for implementing the “full” flavour. Therefore it inherits the limitations from those libraries (please check their documentation for more information). For example:

    • ConfigUpdater, will have trouble to parse interpolations and the related escaping sequence (%%) (in this respect, it behaves more similarly to RawConfigParser than ConfigParser).

    • tomli-w is not very flexible regarding formatting and will have trouble with multi-line strings.

  • ini2toml expects the input to be valid and will not perform extensive checks on the provided document. If something in the output is not working as you would expect, it might be a good idea to check the input.

  • .ini/.cfg files are used in a plethora of use cases and it is impossible to cover all of them in a single code base. Even when considering setup.cfg, there are many packages that define different sections in the document in addition to the basic definition by setuptools. Because of that ini2toml, adopts a “best-effort” approach, that might not correspond to what you expect. If that is the case please consider contributing or creating your own plugin.

  • The translation procedure analyse only the given input. If the original .ini/.cfg file contains references to other files, or behaves differently depending on the existence/presence of other files and directories, the translation will not take that into consideration.

Therefore it is recommended to double check the output and fix any problems before using the .toml files in production.

Can ini2toml also translate setup.py into pyproject.toml?

Working with .py files is not in the scope of the ini2toml project, and therefore this feature is not implemented.

However, you can probably find some tools on PyPI to translate from setup.py into setup.cfg, like setup-py-upgrade and setuptools-py2cfg [1].

Once you have setup.cfg, you can use ini2toml [2].

Note

This project was initially created in the context of PyScaffold, with the purpose of helping migrating existing projects to PEP 621-style configuration when it is made available on setuptools. For details and usage information on PyScaffold see https://pyscaffold.org/.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ini2toml-0.14.tar.gz (108.3 kB view details)

Uploaded Source

Built Distribution

ini2toml-0.14-py3-none-any.whl (55.7 kB view details)

Uploaded Python 3

File details

Details for the file ini2toml-0.14.tar.gz.

File metadata

  • Download URL: ini2toml-0.14.tar.gz
  • Upload date:
  • Size: 108.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for ini2toml-0.14.tar.gz
Algorithm Hash digest
SHA256 bc53259cc2c6af79cd768bc23f5e10f06dd40225770b9bf5b4d37601fedd411e
MD5 f31b5a9a5b53854619c518899b43b86f
BLAKE2b-256 04cf4823573a86772c3c0b968f4458ed287910fcd73824340d0c4f1222c824bf

See more details on using hashes here.

File details

Details for the file ini2toml-0.14-py3-none-any.whl.

File metadata

  • Download URL: ini2toml-0.14-py3-none-any.whl
  • Upload date:
  • Size: 55.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for ini2toml-0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 d833518ea45cad6f7b2cc656bac45bb662eb81051349ff55e6297286736cf401
MD5 c278713203416378f53a6a5e69692ef3
BLAKE2b-256 f7b7c1958fb2afa614c13b054c0f3eeb765873f98af05ed48a949554bb410644

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page