iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📑

Centralize Your Configuration Files with pyconfviewer!

に公開

Introduction

When building various things yourself, you probably find that regardless of the programming language, you need various configuration files, including .env, when creating applications.

Management is easy if it's just a single file, but when it's spread across multiple files, I often feel like there isn't anything that lets me view all the settings at once.

I also wanted to see the differences between development and production configurations, where the files are the same but the contents are different.

To solve this, I created a Python library that outputs configuration file contents to HTML so they can be easily browsed.

That is pyconfviewer!

What is pyconfviewer?

Everything is documented in the GitHub repository below, but I'll describe it here as well.
https://github.com/pkaiy81/pyconfviewer

It is a Python library that does exactly what I described in the Introduction.

Key Features

  • Support for loading configurations in multiple formats: Generates HTML reports to easily visualize YAML, JSON, INI, .env, and TOML settings.
  • Compare configurations between two directories and highlight differences in HTML format.
  • Easy to install and run with simple command examples.

Installation

Since it's a Python library, installation is just one command!

pip install pyconfviewer

Let's actually try using it!

The directory structure can be anything, but for this example, let's place each configuration file in config_a/config_b as shown below.
Each file is available here, so please use them.

── tests
    ├── mock_configs
    │   ├── config_a
    │   │   ├── file1.yaml
    │   │   ├── file2.json
    │   │   ├── settings.ini
    │   │   └── settings.toml
    │   └── config_b
    │       ├── file1.yaml
    │       ├── file2.json
    │       ├── settings.ini
    │       └── settings.toml

Once the configuration files are in place, all that's left is to write a script that uses the library.
Create demo.py in the same directory as the tests directory.

As shown in the README.md, you can verify it with the following code:

demo.py
import os
from pyconfviewer.config_generator import ConfigGenerator

# Define the configuration directory
config_dir = "tests/mock_configs/config_a"
output_dir = "output"
config_html = os.path.join(output_dir, "config_report.html")

# Create an instance of ConfigGenerator and generate the HTML report
config_generator = ConfigGenerator()
configs = config_generator.load_configs(config_dir)
config_generator.generate_config_html(configs, output_dir=output_dir, output_html_path=config_html)

print(f"Configuration HTML generated at {config_html}")


from pyconfviewer.diff_generator import DiffGenerator

# Define the configuration directories to compare
config_a_dir = "tests/mock_configs/config_a"
config_b_dir = "tests/mock_configs/config_b"
diff_html = os.path.join(output_dir, "diff_report.html")

# Create an instance of DiffGenerator and generate the HTML diff report
diff_generator = DiffGenerator()
configs_a = config_generator.load_configs(config_a_dir)
configs_b = config_generator.load_configs(config_b_dir)
diffs = diff_generator.generate_diff(configs_a, configs_b)
diff_generator.generate_diff_html(diffs, output_dir=output_dir, output_html_path=diff_html)

print(f"Diff HTML generated at {diff_html}")

Execution

Please execute it using the following command:

python3 demo.py

Once the execution is complete, config_report.html and diff_report.html should be created under the output directory.

Verifying the Output

The created config_report.html and diff_report.html can be viewed by opening them in a browser, such as by double-clicking them.

If it looks like the following, it's a success!
config_report.html

diff_report.html

Conclusion

What do you think?
I think this allows you to view configuration files collectively, even in environments where there are many of them.
Since it was just recently created, there aren't many supported file formats yet.

Please let me know in the comments if there are any other file formats you would like supported!!!

Discussion