Pyjoyment.Plugin.JSONConfig - JSON configuration plugin

# myapp.json (it's just JSON with embedded Python)
{
  %# Just a value
  "foo": "bar",

  %# Nested data structures are fine too
  "baz": ["♥"],

  %# You have full access to the application
  % import Pyjo.JSON
  "music_dir": <%= Pyjo.JSON.to_json(app.home.child('music').to_str()) %>
}

# Pyjoyment
config = app.plugin('JSONConfig')
print(config['foo'])

# Pyjoyment.Lite
config = plugin('JSONConfig')
print(config['foo'])

# foo.html.ep
%= config()['foo']

# The configuration is available application-wide
config = app.config()
print(config['foo'])

# Everything can be customized with options
config = plugin('JSONConfig', file='/etc/myapp.conf')

Pyjoyment.Plugin.JSONConfig is a JSON configuration plugin that preprocesses its input with Pyjo.Template.

The application object can be accessed via app. A default configuration filename in the application home directory will be generated from the value of Pyjoyment.Pyjoyment_App.moniker ($moniker.json). You can extend the normal configuration file $moniker.json with mode specific ones like $moniker.$mode.json, which will be detected automatically.

A value of the application goes into the JSON through Pyjo.JSON.to_json(), which escapes what a string of JSON cannot hold as it is, as the backslashes of a path of Windows.

These configuration values are currently reserved:

config_override

If this configuration value has been set in Pyjoyment.Pyjoyment_App.config() when this plugin is loaded, it will not do anything besides loading deployment specific plugins.

plugins

"plugins": [{"SetUserGroup": {"user": "sri", "group": "staff"}}]

One or more deployment specific plugins that should be loaded right after
this plugin has been loaded.

The code of this plugin is a good example for learning to build new plugins, you’re welcome to fork it.

See Pyjoyment.Plugins for a list of plugins that are available by default.

Options

Pyjoyment.Plugin.JSONConfig inherits all options from Pyjoyment.Plugin.Config and supports the following new ones.

template

# Pyjoyment.Lite
plugin('JSONConfig', template={'line_start': '.'})

Attribute values passed to Pyjo.Template object used to preprocess configuration files.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojolicious::Plugin::JSONConfig of Mojolicious 9.49.

Classes

class Pyjoyment.Plugin.JSONConfig.Pyjoyment_Plugin_JSONConfig[source]

Pyjoyment.Plugin.JSONConfig inherits all methods from Pyjoyment.Plugin.Config and implements the following new ones.

parse(content: str, file: str, conf: dict[str, Any], app: Pyjoyment_App) → dict[str, Any][source]
plugin.parse(content, file, conf, app)

Process content with render() and parse it with Pyjo.JSON.

def parse(self, content, file, conf, app):
    ...
    content = self.render(content, file, conf, app)
    ...
    return config
register(app: Pyjoyment_App, conf: dict[str, Any] | None = None) → dict[str, Any][source]
config = plugin.register(Pyjoyment.new())
config = plugin.register(Pyjoyment.new(), {'file': '/etc/foo.conf'})

Register plugin in Pyjoyment application and merge configuration.

render(content: str, file: str, conf: dict[str, Any], app: Pyjoyment_App) → str[source]
plugin.render(content, file, conf, app)

Process configuration file with Pyjo.Template.

def render(self, content, file, conf, app):
    ...
    return content