pyhocon icon indicating copy to clipboard operation
pyhocon copied to clipboard

Possible to write to a conf file in hocon format from within python script?

Open juskaiser opened this issue 7 years ago • 5 comments

I'm creating a config object within a python script using the from_dict method. Is it possible to then save (dump) that config to a .conf file? I see there is a converter to go between different file formats but I'm not seeing a way to save a .conf file in HOCON format that is generated within the script.

Thanks!

juskaiser avatar Nov 29 '17 22:11 juskaiser

That's a great question. I've been trying to use this module for the past few weeks assuming it supported conversion from other formats to HOCON - but seems it may only support conversion from HOCON to other formats.

I'm querying a Cloudera Hadoop cluster using their Python API, building a dict of configuration settings - then trying to export this to HOCON. Seems you are trying to do something similar.

For me, when trying to parse the dict using ConfigFactory.parse_string(my_dict.dumps()), it throws an exception trying to parse values in the following format:

"value" : "{"warning":500,"critical":"never"}"

I'll let you know if I figure out a way around this. Hopefully the maintainers can throw us some clues as well.

enlightenalpha avatar Dec 09 '17 17:12 enlightenalpha

@juskaiser Since HOCON is a superset of JSON, couldn't you just use JSOn for whatever project you're using that needs a HOCON formatted file?

@enlightenalpha Double check your double quotes... looks like you have some double quotes around the dict value. This works:

import json
from pyhocon import ConfigFactory
hconf = ConfigFactory.parse_string('"value" : {"warning":500,"critical":"never"}')
print(json.dumps(hconf,indent=4))
#Output
{
    "value": {
        "warning": 500, 
        "critical": "never"
    }
}

absltkaos avatar Jan 17 '18 21:01 absltkaos

@absltkaos You are correct - that example was incorrect. It should have been: "value" : "{"warning":500,"critical":"never"}"

In this particular case, the dict value needs to be quoted when passed to Cloudera as a service configuration value, otherwise the service will fail after applying the update. Cloudera exposes an API which provides the ability to dump service configs as JSON - and this is how it dumps the config, so we try to honor that. Anyway - don't want to hijack juskaiser's issue. Please see: https://github.com/chimpler/pyhocon/issues/138

enlightenalpha avatar Jan 23 '18 17:01 enlightenalpha

This issue is a bit old, but I think now we can use pyhocon.converter.HOCONConverter.to_hocon() for saving config objects into files:

https://github.com/chimpler/pyhocon/blob/091830001f2d44f91f0f8281fb119c87fd1f6660/pyhocon/converter.py#L78-L151

elbakramer avatar Aug 09 '21 22:08 elbakramer

This issue is a bit old, but I think now we can use pyhocon.converter.HOCONConverter.to_hocon() for saving config objects into files:

https://github.com/chimpler/pyhocon/blob/091830001f2d44f91f0f8281fb119c87fd1f6660/pyhocon/converter.py#L78-L151

Note that this approach will flatten the original HOCON file to remove any referenced variables. So reading and then writing out a HOCON file using this method will write out a modified file.

ArthurFaisman avatar Jun 07 '22 19:06 ArthurFaisman