Skip to main content

A configuration file parser for yaml, env, toml, xml, and json formats

Project description

Craftsperson Environment

What is it?

craftsperson_env reads configuration files in yaml, env, toml, xml, and json formats and adds them to the 'os.environ' system. It formats the keys according to naming-case-type, upper or lower, and arranges them with a specific join type in the upper-lower section. Additionally, it specifies the data type when retrieving data from 'os.environ' to access data of that type.

Where to get it

You can access the Github repository from here.

Pip installers for the latest released version are available at the Python Package Index (PyPI)

Installation

python setup.py install
pip install craftsperson-env

Import Library

from craftsperson_env import CraftsEnvConfig

Create Config Variable

config = CraftsEnvConfig()

Load File

Description

Help on method load_config_file in module craftsperson_env.main:

load_config_file(file_path: str, root_full_path: str = './', naming_case_type: str = None, naming_case_join_type: str = '', is_change_config_env_format: bool = False, config_env_replace_first_value: str = None, is_remove_xml_first_level: bool = False, extra_config_file_params: dict = {}) method of craftsperson_env.main.CraftsEnvConfig instance
    This function processes and uses a config file.
    
    Parameters
    ----------
    file_path : str
        This parameter specifies the file location path.
    root_full_path : str, optional
        This parameter retrieves the full path from the file. The default is './'.
    naming_case_type : str, optional
        This parameter specifies naming case types for env, yaml, json, xml, or toml. The default is None.
    naming_case_join_type : str, optional.
        This parameter specifies the join type, allowing values such as "", "-", or "_". The default is "".
    is_change_config_env_format: bool, optional.
        This parameter specifies whether the format of the env config file has been changed.
            The default value is False.
    config_env_replace_first_value: str, optional.
        This parameter specifies the replacement value for the first occurrence. The default is None.
    is_remove_xml_first_level : bool, optional
        This parameter determines whether to remove the first level. The default value is False.
    extra_config_file_params : dict, optional
        This parameter retrieves additional XML or TOML configuration parameters. The default value is {}.
    
    Returns
    -------
    None.

Yaml File Use Case

version: 2.0
application:
  name: MyWebApp
  version: 1.2.3
  environment: production
  base_url: "https://mywebapp.example.com"
  allowed_hosts:
    - mywebapp.example.com
    - api.mywebapp.example.com
  options:
    use_ssl: true
    ssl_cert: "/path/to/cert"
json_format: "{'test': 1}"
config.load_config_file(
    file_path="examples/yaml_config_file.yaml",
    root_full_path="examples/",
    naming_case_type="upper",
    naming_case_join_type=".",
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['VERSION',
 'APPLICATION.NAME',
 'APPLICATION.VERSION',
 'APPLICATION.ENVIRONMENT',
 'APPLICATION.BASE_URL',
 'APPLICATION.ALLOWED_HOSTS',
 'APPLICATION.OPTIONS.USE_SSL',
 'APPLICATION.OPTIONS.SSL_CERT',
 'JSON_FORMAT']

Xml File Use Case

<config>
  <version>2.0</version>
  <application>
    <name>MyWebApp</name>
    <version>1.2.3</version>
    <environment>production</environment>
    <base_url>https://mywebapp.example.com</base_url>
    <allowed_hosts>
      <host>mywebapp.example.com</host>
      <host>api.mywebapp.example.com</host>
    </allowed_hosts>
    <options>
      <use_ssl>true</use_ssl>
      <ssl_cert>/path/to/cert</ssl_cert>
    </options>
  </application>
  <json_format>
    {'test': 1}
  </json_format>
</config>

The is_remove_xml_first_level default value is False.

config.load_config_file(
    file_path="xml_config_file.xml",
    root_full_path = "./",
    naming_case_type="upper",
    naming_case_join_type = ".",
    is_remove_xml_first_level=False,
    extra_config_file_params={}
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['CONFIG.VERSION',
 'CONFIG.APPLICATION.NAME',
 'CONFIG.APPLICATION.VERSION',
 'CONFIG.APPLICATION.ENVIRONMENT',
 'CONFIG.APPLICATION.BASE_URL',
 'CONFIG.APPLICATION.ALLOWED_HOSTS.HOST',
 'CONFIG.APPLICATION.OPTIONS.USE_SSL',
 'CONFIG.APPLICATION.OPTIONS.SSL_CERT',
 'CONFIG.JSON_FORMAT']

The is_remove_xml_first_level is True, which removes the first level.

config.load_config_file(
    file_path="xml_config_file.xml",
    root_full_path = "./",
    naming_case_type="upper",
    naming_case_join_type = ".",
    is_remove_xml_first_level=True,
    extra_config_file_params={}
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['CONFIG.APPLICATION.NAME',
 'CONFIG.APPLICATION.VERSION',
 'CONFIG.APPLICATION.ENVIRONMENT',
 'CONFIG.APPLICATION.BASE_URL',
 'CONFIG.APPLICATION.ALLOWED_HOSTS.HOST',
 'CONFIG.APPLICATION.OPTIONS.USE_SSL',
 'CONFIG.APPLICATION.OPTIONS.SSL_CERT',
 'CONFIG.JSON_FORMAT',
 'APPLICATION.ALLOWED_HOSTS.HOST']

Toml File Use Case

version = "2.0"
json_format = "{'test': 1}"

[application]
name = "MyWebApp"
version = "1.2.3"
environment = "production"
base_url = "https://mywebapp.example.com"
allowed_hosts = ["mywebapp.example.com", "api.mywebapp.example.com"]

[application.options]
use_ssl = true
ssl_cert = "/path/to/cert"
config.load_config_file(
    file_path="toml_config_file.toml",
    root_full_path = "./",
    naming_case_type="upper",
    naming_case_join_type = ".",
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['VERSION',
 'JSON_FORMAT',
 'APPLICATION.NAME',
 'APPLICATION.VERSION',
 'APPLICATION.ENVIRONMENT',
 'APPLICATION.BASE_URL',
 'APPLICATION.ALLOWED_HOSTS',
 'APPLICATION.OPTIONS.USE_SSL',
 'APPLICATION.OPTIONS.SSL_CERT']

Json File Use Case

{
    "version": "2.0",
    "application": {
      "name": "MyWebApp",
      "version": "1.2.3",
      "environment": "production",
      "base_url": "https://mywebapp.example.com",
      "allowed_hosts": ["mywebapp.example.com", "api.mywebapp.example.com"],
      "options": {
        "use_ssl": true,
        "ssl_cert": "/path/to/cert"
      }
    },
    "json_format": "{'test': 1}"
}
config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="upper",
    naming_case_join_type = ".",
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['VERSION',
 'APPLICATION.NAME',
 'APPLICATION.VERSION',
 'APPLICATION.ENVIRONMENT',
 'APPLICATION.BASE_URL',
 'APPLICATION.ALLOWED_HOSTS',
 'APPLICATION.OPTIONS.USE_SSL',
 'APPLICATION.OPTIONS.SSL_CERT',
 'JSON_FORMAT']

Env File Use Case

VERSION=2.0
APPLICATION_NAME=MyWebApp
APPLICATION_VERSION=1.2.3
APPLICATION_ENVIRONMENT=production
APPLICATION_BASE_URL=https://mywebapp.example.com
APPLICATION_ALLOWED_HOSTS=mywebapp.example.com,api.mywebapp.example.com
APPLICATION_OPTIONS_USE_SSL=true
APPLICATION_OPTIONS_SSL_CERT=/path/to/cert
JSON_FORMAT="{'test': 1}"

The is_change_config_env_format default value is False.

config.load_config_file(
    file_path="env_config_file.env",
    root_full_path = "./",
    naming_case_type="upper",
    is_change_config_env_format=False,
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['VERSION',
 'APPLICATION_NAME',
 'APPLICATION_VERSION',
 'APPLICATION_ENVIRONMENT',
 'APPLICATION_BASE_URL',
 'APPLICATION_ALLOWED_HOSTS',
 'APPLICATION_OPTIONS_USE_SSL',
 'APPLICATION_OPTIONS_SSL_CERT',
 'JSON_FORMAT']

The is_change_config_env_format is True, which changes env format.

The config_env_replace_first_value is "_", which replacement value for the first occurrence.

config.load_config_file(
    file_path="env_config_file.env",
    root_full_path = "./",
    naming_case_type="upper",
    naming_case_join_type=".",
    is_change_config_env_format=True,
    config_env_replace_first_value="_"
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['VERSION',
 'APPLICATION.NAME',
 'APPLICATION.VERSION',
 'APPLICATION.ENVIRONMENT',
 'APPLICATION.BASE.URL',
 'APPLICATION.ALLOWED.HOSTS',
 'APPLICATION.OPTIONS.USE.SSL',
 'APPLICATION.OPTIONS.SSL.CERT',
 'JSON.FORMAT']

Naming Case Type Use Case

Pascal Case

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="pascal"
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['Version',
 'ApplicationName',
 'ApplicationVersion',
 'ApplicationEnvironment',
 'ApplicationBase_url',
 'ApplicationAllowed_hosts',
 'ApplicationOptionsUse_ssl',
 'ApplicationOptionsSsl_cert',
 'Json_format']

Camel Case

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="camel"
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['version',
 'applicationName',
 'applicationVersion',
 'applicationEnvironment',
 'applicationBase_url',
 'applicationAllowed_hosts',
 'applicationOptionsUse_ssl',
 'applicationOptionsSsl_cert',
 'json_format']

Snake Case

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="snake"
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['applicationOptionsSsl_cert',
 'json_format',
 'application_name',
 'application_version',
 'application_environment',
 'application_base_url',
 'application_allowed_hosts',
 'application_options_use_ssl',
 'application_options_ssl_cert']

Kebab Case

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="kebab"
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['version',
 'application-name',
 'application-version',
 'application-environment',
 'application-base_url',
 'application-allowed_hosts',
 'application-options-use_ssl',
 'application-options-ssl_cert',
 'json_format']

Flat Case

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="flat"
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['version',
 'applicationname',
 'applicationversion',
 'applicationenvironment',
 'applicationbase_url',
 'applicationallowed_hosts',
 'applicationoptionsuse_ssl',
 'applicationoptionsssl_cert',
 'json_format']

Upper-Flat Case

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="upper-flat"
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['VERSION',
 'APPLICATIONNAME',
 'APPLICATIONVERSION',
 'APPLICATIONENVIRONMENT',
 'APPLICATIONBASE_URL',
 'APPLICATIONALLOWED_HOSTS',
 'APPLICATIONOPTIONSUSE_SSL',
 'APPLICATIONOPTIONSSSL_CERT',
 'JSON_FORMAT']

Pascal-Snake Case

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="pascal-snake"
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['Version',
 'Application_Name',
 'Application_Version',
 'Application_Environment',
 'Application_Base_url',
 'Application_Allowed_hosts',
 'Application_Options_Use_ssl',
 'Application_Options_Ssl_cert',
 'Json_format']

Camel-Snake Case

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="camel-snake"
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['version',
 'application_Name',
 'application_Version',
 'application_Environment',
 'application_Base_url',
 'application_Allowed_hosts',
 'application_Options_Use_ssl',
 'application_Options_Ssl_cert',
 'json_format']

Screaming-Snake Case

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="screaming-snake"
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['VERSION',
 'APPLICATION_NAME',
 'APPLICATION_VERSION',
 'APPLICATION_ENVIRONMENT',
 'APPLICATION_BASE_URL',
 'APPLICATION_ALLOWED_HOSTS',
 'APPLICATION_OPTIONS_USE_SSL',
 'APPLICATION_OPTIONS_SSL_CERT',
 'JSON_FORMAT']

Train Case

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="train"
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['Version',
 'Application-Name',
 'Application-Version',
 'Application-Environment',
 'Application-Base_url',
 'Application-Allowed_hosts',
 'Application-Options-Use_ssl',
 'Application-Options-Ssl_cert',
 'Json_format']

Cobol Case

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="cobol"
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['VERSION',
 'APPLICATION-NAME',
 'APPLICATION-VERSION',
 'APPLICATION-ENVIRONMENT',
 'APPLICATION-BASE_URL',
 'APPLICATION-ALLOWED_HOSTS',
 'APPLICATION-OPTIONS-USE_SSL',
 'APPLICATION-OPTIONS-SSL_CERT',
 'JSON_FORMAT']

Other Case: Upper

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="upper",
    naming_case_join_type = "."
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['VERSION',
 'APPLICATION.NAME',
 'APPLICATION.VERSION',
 'APPLICATION.ENVIRONMENT',
 'APPLICATION.BASE_URL',
 'APPLICATION.ALLOWED_HOSTS',
 'APPLICATION.OPTIONS.USE_SSL',
 'APPLICATION.OPTIONS.SSL_CERT',
 'JSON_FORMAT']

Other Case: Lower

config.load_config_file(
    file_path="json_config_file.json",
    root_full_path = "./",
    naming_case_type="lower",
    naming_case_join_type = "."
)
import os
key_list = list(os.environ.keys())[-9:]
key_list
['version',
 'application.name',
 'application.version',
 'application.environment',
 'application.base_url',
 'application.allowed_hosts',
 'application.options.use_ssl',
 'application.options.ssl_cert',
 'json_format']

Get the Value From the 'os.environ' System

Description

Help on function get in module craftsperson_env.__main__:

get(key: str, value_type: Any = <class 'str'>, default: Any = None) -> str
    This function retrieves the value associated with the key from the 'os.environ' system.
    
    Parameters
    ----------
    key: str
        This parameter retrieves the key from the 'os.environ' system.
    value_type: str, optional.
        This parameter accepts value types such as int, str, list, dict, and others. The default value is str.
    default: Any, optional.
        This parameter retrieves the default value if the key is not found in any environment variables.
            The default is None.
    
    Returns
    -------
    value:
        This returns the value of the given environment variable key.

Additionally, it specifies the data type when retrieving data from 'os.environ' to access data of that type. For example, value_type gets int, float, str, dict and other types.

print("boolean:", config.get(key="application.options.use_ssl", value_type=bool, default=None))
print("float:", config.get(key="version", value_type=float, default=None))
print("str:", config.get(key="application.name", value_type=str, default=None))
print("json:", config.get(key="json_format", value_type=dict, default=None))
boolean: True
float: 2.0
str: MyWebApp
json: {'test': 1}

Set and Update the Value in the 'os.environ' System.

Description

Help on function set in module craftsperson_env.__main__:

set(key: str, value: str) -> None
    This function sets a key-value pair in the 'os.environ' system.
    
    Parameters
    ----------
    key: str
        This parameter specifies the key to be set as an environment variable.
    value: str
        This parameter specifies the value that will be stored in
            the environment variable identified by the given key.
    
    Returns
    -------
    None.
config.set(key="int_value", value="1923")
print("int:", config.get(key="int_value", value_type=int, default=None))
int: 1923

Author's Social Media

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

craftsperson_env-1.0.6.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

craftsperson_env-1.0.6-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file craftsperson_env-1.0.6.tar.gz.

File metadata

  • Download URL: craftsperson_env-1.0.6.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.8

File hashes

Hashes for craftsperson_env-1.0.6.tar.gz
Algorithm Hash digest
SHA256 0a0b99cc6e3c4a76124a02557275d9503467eb0644b1799a7c97ce058ed96cbd
MD5 ed77f419b09933bb36ea73523287bc04
BLAKE2b-256 5a71c1d47c33e9670fee798bc06412fde35fd3e7a5d5effa68d40cb649206be3

See more details on using hashes here.

File details

Details for the file craftsperson_env-1.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for craftsperson_env-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 9d37041f90e4065cbfcda1e557de82a44d6cecdfd7cf6ccb88a9c588d3a00119
MD5 2e054c6b7f42922ead6244424fc3fae5
BLAKE2b-256 934b507b5cc3f95153dc1102ae873f5c09fb595057c07d049e5f70e948db72f7

See more details on using hashes here.

Supported by

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