Skip to content

Support custom formats#28

Open
jvrsantacruz wants to merge 4 commits intomasterfrom
support_custom_formats
Open

Support custom formats#28
jvrsantacruz wants to merge 4 commits intomasterfrom
support_custom_formats

Conversation

@jvrsantacruz
Copy link
Copy Markdown
Contributor

Allows to register custom formats fixes #26 by also changing the file loader API to control how the stream is created.

Other formats can be registered by name providing a function with the loader signature loader(path): dict and registering using the register_format function.

from confight import register_loader, register_extension

def load_assign(path):
    """Parse files with KEY=VALUE lines """
    with open(path, 'r') as stream:
        return dict(line.split('=', 1) for line in stream]

register_loader('equal', load_assign)

Extensions can also be associated to previously registered formats by adding them to the extension
register as alias so it automatically detects with .eq extension:

register_extension('eq', format='equal')

@jvrsantacruz jvrsantacruz force-pushed the support_custom_formats branch from 224fa1d to 7f5792b Compare May 28, 2024 07:02
- Changes loader API to take paths rather than open streams
- Makes loader API public through the `register_format` function
  so library users can define their own custom loaders.
@jvrsantacruz jvrsantacruz force-pushed the support_custom_formats branch from 7f5792b to abf446e Compare May 28, 2024 07:03
Copy link
Copy Markdown
Contributor

@frank-lenormand frank-lenormand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding PreCommit hooks produced a lot of noise, maybe we should create a cosmetics PR later, to address some minor things (typing annotations, not using mutable objects as default values in functions…).

Comment thread confight.py
'json': lambda *args: json.load(*args, object_pairs_hook=OrderedDict),
'toml': lambda *args: toml.load(*args, _dict=OrderedDict),
'ini': load_ini
"js": "json",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Is that really a ubiquitous abbreviation for the .json extension?

I think .js means JavaScript. Plus a JavaScript file can contain a JSON object, but they’re not necessarily cross-compatible.

I suggest removing this alias.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Giv full controll of the stream to the loaders

2 participants