chimpler / pyhocon

HOCON parser for Python
Apache License 2.0
502 stars 118 forks source link

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

Open juskaiser opened 6 years ago

juskaiser commented 6 years ago

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!

enlightenalpha commented 6 years ago

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.

absltkaos commented 6 years ago

@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"
    }
}
enlightenalpha commented 6 years ago

@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

elbakramer commented 3 years ago

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

ArthurFaisman commented 2 years ago

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.