Search code examples
pythonjsonconfigurationcement

How to load JSON config file in cement python app?


I have just started using cement as a python framework. It seems that the default config of the app is not JSON.

It seems that Cement has JsonConfigHandler() class that could load JSON configuration into app. I have used the code below in my app:

ins = JsonConfigHandler()
ins.parse_file('/etc/luna/luna.conf')

But it gives the error below:

    return self._parse_file(file_path)
  File "/Library/Python/2.7/site-packages/cement/ext/ext_json.py", line 243, in _parse_file
    self.merge(self._json.load(open(file_path)))
AttributeError: 'NoneType' object has no attribute 'load'

Hows should I load JSON config file in cement app?

By default I load config using app.config.parse_file('/etc/my_app/app.conf') with no problem and config file contains:

[connection]
host=172.16.131.12

Solution

  • Apologies for not being on top of this, but here is the official response (primary developer):

    You simply need to include the json extension, and then set the CementApp.Meta.config_handler to json:

    from cement.core.foundation import CementApp
    
    class MyApp(CementApp):
        class Meta:
            label = 'myapp'
            extensions = ['json']
            config_handler = 'json'
            config_extension = '.json'
    

    This will keep the default config_files list intact, but use .json instead of .conf for the files it looks for.