This post explains How to read and write HOCON conf file format in python

You can use pyhocon library to read and write HOCON file format.

What is Pyhocon?

Pyhocon is a python library to parse the hocon file format content. It supports most of the features of a hocon such as substittion including configuration in other files. It also provides the functionality to convert hocon configuration into YAML, JSON formats.

First, Install pyhocon library in project terminal usinbg below command

pip install pyhocon

Collecting pyhocon
  Downloading pyhocon-0.3.60.tar.gz (158 kB)
     |██                              | 10 kB 11.3 MB/s 
     |████▏                           | 20 kB 13.5 MB/s 
     |██████▏                         | 30 kB 15.2 MB/s 
     |████████▎                       | 40 kB 16.7 MB/s 
     |██████████▎                     | 51 kB 19.2 MB/s 
     |████████████▍                   | 61 kB 21.2 MB/s 
     |██████████████▌                 | 71 kB 22.9 MB/s 
     |████████████████▌               | 81 kB 25.2 MB/s 
     |██████████████████▋             | 92 kB 27.6 MB/s 
     |████████████████████▋           | 102 kB 12.7 MB/s
     |██████████████████████▊         | 112 kB 12.7 MB/s
     |████████████████████████▊       | 122 kB 12.7 MB/s
     |██████████████████████████▉     | 133 kB 12.7 MB/s
     |█████████████████████████████   | 143 kB 12.7 MB/s
     |███████████████████████████████ | 153 kB 12.7 MB/s
     |████████████████████████████████| 158 kB 12.7 MB/s 
Collecting pyparsing<4,>=2
  Downloading pyparsing-3.1.1-py3-none-any.whl (103 kB)
     |███▏                            | 10 kB 10.9 MB/s 
     |██████▍                         | 20 kB 16.5 MB/s 
     |█████████▌                      | 30 kB 21.1 MB/s 
     |████████████▊                   | 40 kB 24.1 MB/s 
     |███████████████▉                | 51 kB 27.9 MB/s 
     |███████████████████             | 61 kB 31.3 MB/s 
     |██████████████████████▎         | 71 kB 34.5 MB/s 
     |█████████████████████████▍      | 81 kB 37.2 MB/s 
     |████████████████████████████▋   | 92 kB 38.6 MB/s 
     |███████████████████████████████▊| 102 kB 40.8 MB/s
     |████████████████████████████████| 103 kB 40.8 MB/s 
Using legacy 'setup.py install' for pyhocon, since package 'wheel' is not installed.
Installing collected packages: pyparsing, pyhocon
    Running setup.py install for pyhocon ... done
Successfully installed pyhocon pyparsing

pyhocon provides following functions

  • ConfigFactory.parse_file() It parses hocon configuraiton file and returns an object.
  • ConfigFactory.parse_string() : It converts the string object and convert to hocon config object
  • ConfigFactory.from_dict() : It takes dictionary object and returns config object.
  • ConfigFactory.parse_URL

How to read hocon configuration file format in Python

Following are steps to parse hocon file

  • Import pyhocon module into code
  • use pyhocon.ConfigFactory.parse_file() to read hocon(conf) file. handle exceptions using try and except if it throws an I/O error.
  • Returns an object of pyhocon.config_tree.ConfigTree, contains tree of nested elements.
  • Use getter methods on ConfigTree object
    • get_string(key) returns the string value,
    • get(key) returns the generic value,
    • another way using square bracket syntax conf[key] or conf[parentkey][childkey]
    • use get_conf(key) to retrieve parent object
    • get(key, defaultvalue), If key not found, returns default value.
import pyhocon
try:

  conf = pyhocon.ConfigFactory.parse_file('data.conf')
  url1 = conf.get_string('database.url')
  print(url1)

  url2 = conf.get('database.url')
  print(url2)

  url3 = conf['database.url']
  print(url3)

  url4 = conf['database']['url']
  print(url4)
  port = conf['server.port']
  print(port)

  username = conf['database']['username']
  print(username)

  server = conf.get_config('server')
  print(server)
  ssl = conf.get('databases.ssl', 'true') #  use default value if key not found
  print(ssl)
except:
  print("Failed to read a data.conf file")

After running the above file, It output

jdbc:mysql://localhost:3306/employee
jdbc:mysql://localhost:3306/employee
jdbc:mysql://localhost:3306/employee
jdbc:mysql://localhost:3306/employee
8080
root
ConfigTree([('host', 'localhost'), ('port', 8080)])
true

How to write to HOCON configuraton file in Python