This script is a command-line utility for processing JSON configuration file.
This script is a command-line Python tool that simplifies and automates JSON configuration files processing. It provides functionality to read and write configuration files, with option to modify, and update specific parameters within the file.
- The script uses the argparse module for parsing command-line arguments, enabling users to specify the action (read or write), the file path of the JSON configuration file, and an optional parameter to modify when writing to the file.
- Depending on the specified action, the script reads the configuration file using
json.load()
or updates the configuration parameter using a customupdate_nested_config()
function, and then writes the updated configuration to the file usingjson.dump()
.
- Clone this repository
git clone https://github.com/torshin5ergey/python-playground.git
- Go to this project directory
cd python-playground/ungrouped/json_config_tool
- Run Python file. For example: reading configuration file.
python cfgtool.py read config.json
cfgtool.py
: command-line utility for processing JSON configuration file.config.json
: Example configuration JSON file.README.md
: This readme file.
The main file cfgtool.py
contains functions:
read_config()
: Read the configuration JSON file.write_config()
: Write the JSON configuration file.update_nested_config()
: Update the nested configuration parameter.
>>> python cfgtool.py read config.json
Configuration file content:
{
"server": {
"host": "0.0.0.0",
"port": 8080,
"debug": "true"
},
"database": {
"type": "postgresql",
"host": "localhost",
"port": 5432,
"username": "db_user",
"password": "db_password",
"database_name": "my_database"
},
"logging": {
"level": "INFO",
"file": "/var/log/myapp.log"
},
"api": {
"key": "my_api_key",
"endpoint": "https://api.example.com/v1/"
},
"email": {
"smtp_server": "smtp.example.com",
"smtp_port": 587,
"username": "email_user",
"password": "email_password",
"from_email": "[email protected]",
"to_email": "[email protected]"
},
"features": {
"enable_feature_x": true,
"enable_feature_y": false
}
}
>>> python cfgtool.py write config.json --param server.port=3000
Configuration file has been updated.
Before:
...
"server": {
"host": "0.0.0.0",
"port": "8080",
"debug": "true"
},
...
After:
...
"server": {
"host": "0.0.0.0",
"port": "3000",
"debug": "true"
},
...
>>> python cfgtool.py -h
usage: cfgtool.py [-h] [--param PARAM]
{read,write} file_path
Process JSON configuration file
positional arguments:
{read,write} Action to perform: read or
write
file_path Path to the JSON
configuration file
optional arguments:
-h, --help show this help message and
exit
--param PARAM Path and value for parameter
in format path=value (only
for write)
- The script utilizes the
json
module for working with JSON files. It uses functions such asjson.load()
to read JSON data from a file andjson.dump()
to write JSON data to a file. Additionally, thejson.dumps()
function is used for pretty-printing JSON content with indentation for improved readability. -- The script contains optional command-line argument--param
that allows users to specify the path and value for a parameter to be updated when writing to the configuration file.
Sergey Torshin @torshin5ergey