DEV Community

Cover image for HOCON - secret behind .conf files
sriram sridhar
sriram sridhar

Posted on

HOCON - secret behind .conf files

HOCON (Human-Optimized Config Object Notation) is a flexible and user-friendly configuration format often used in .conf files. It builds on JSON but introduces several enhancements that make it more human-readable and easier to work with.

Key Features:

  • Syntax Simplicity: HOCON allows for relaxed syntax: Commas between fields are optional. Quotes are not always required around keys or strings.
  • Inheritance: It supports configuration merging and inheritance, enabling overrides or extensions of base configurations.
  • Comments: Unlike JSON, HOCON supports comments (# or //), making files easier to document.
  • Substitutions: It allows variable substitution using placeholders (${}) for dynamic configurations.
  • Conciseness: Supports multi-line strings, unquoted keys, and compact object definitions.
  • Extensibility: Easily integrates with tools like the Typesafe Config library, often used in Scala and Java applications, especially with frameworks like Akka and Play.

To Interact with .conf files in python - you need pyhocon library

pip install pyhocon

Below is the sample code to create config files on runtime

from pyhocon import ConfigFactory, HOCONConverter
import json

# Create a configuration object
config = ConfigFactory.parse_string("""
app {
    name = "MyApp"
    version = "1.0.0"
    features = {
        enable_feature_x = true
        enable_feature_y = false
    }
    database {
        url = "jdbc:postgresql://localhost:5432/mydb"
        user = "db_user"
        password = "db_password"
    }
}
""")

# Save the configuration to a file
with open('config.conf', 'w') as file:
    file.write(HOCONConverter.convert(config, 'hocon'))
print("HOCON file created: config.conf")
Enter fullscreen mode Exit fullscreen mode

you can read the created files as below

from pyhocon import ConfigFactory

# Load the configuration file
config = ConfigFactory.parse_file('config.conf')

# Access configuration values
app_name = config.get('app.name')
db_url = config.get('app.database.url')
enable_feature_x = config.get('app.features.enable_feature_x')

# Print configuration values
print(f"App Name: {app_name}")
print(f"Database URL: {db_url}")
print(f"Is Feature X Enabled? {enable_feature_x}")
Enter fullscreen mode Exit fullscreen mode

So the output will be as below

HOCON file created: config.conf
App Name: MyApp
Database URL: jdbc:postgresql://localhost:5432/mydb
Is Feature X Enabled? True
Enter fullscreen mode Exit fullscreen mode
  • You can flag features with the help of configuration management.
  • You can refresh the configuration over periodic intervals or expose an API endpoint to refresh the configuration of the running application.

Library link : https://github.com/chimpler/pyhocon

If you found this helpful, let me know by leaving a 👍 or a comment!, or if you think this post could help someone, feel free to share it! Thank you very much! 😃

Top comments (0)