Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom formats #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

jvrsantacruz
Copy link
Collaborator

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
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…).

'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
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