Skip to content

Commit

Permalink
chore: restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Nov 10, 2024
1 parent 55b9cb6 commit fb03669
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 13 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,31 @@ loader_cls = get_loader(
# Use custom loader
data = yaml.load(content, Loader=loader_cls)
```

## Universal load interface

Yamling provides a universal load function that can handle YAML, JSON, TOML, and INI files.
Apart from yaml, only stdlib modules are used, so no additional dependencies are required.
Here's a simple example:

```python
import yamling

# Load files based on their extension
config = yamling.load_file("config.yaml") # YAML
settings = yamling.load_file("settings.json") # JSON
params = yamling.load_file("params.toml") # TOML

# Or explicitly specify the format
data = yamling.load_file("config.txt", mode="yaml")

# Load directly from strings
yaml_text = """
name: John
age: 30
"""
data = yamling.load(yaml_text, mode="yaml")
```

> **Note**
> If [orjson](https://github.com/ijl/orjson) is installed, the loader will automatically use it for JSON parsing, offering significantly better performance compared to the standard `json` module.
13 changes: 11 additions & 2 deletions src/yamling/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
__version__ = "0.0.3"

import yaml
from yamling.load import load_yaml, load_yaml_file, get_loader
from yamling.yaml_loaders import load_yaml, load_yaml_file, get_loader
from yamling.load_universal import load, load_file
from yamling.dump import dump_yaml


YAMLError = yaml.YAMLError # Reference for external libs that need to catch this error


__all__ = ["load_yaml", "dump_yaml", "YAMLError", "load_yaml_file", "get_loader"]
__all__ = [
"load_yaml",
"dump_yaml",
"YAMLError",
"load_yaml_file",
"get_loader",
"load",
"load_file",
]
2 changes: 1 addition & 1 deletion src/yamling/load_universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from yaml import YAMLError

from yamling.yaml import load_yaml
from yamling.yaml_loaders import load_yaml


logger = logging.getLogger(__name__)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/test_jinja_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import yaml
from yaml.constructor import ConstructorError

from yamling.load import load_yaml, load_yaml_file
from yamling.yaml_loaders import load_yaml, load_yaml_file


# Test Constants
Expand Down
2 changes: 1 addition & 1 deletion tests/test_load.py → tests/test_load_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import pytest

from yamling.load import load_yaml_file
from yamling.yaml_loaders import load_yaml_file


if TYPE_CHECKING:
Expand Down
16 changes: 8 additions & 8 deletions tests/test_yaml_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import yaml_include

import yamling
from yamling import load
from yamling import yaml_loaders


if TYPE_CHECKING:
Expand Down Expand Up @@ -60,32 +60,32 @@ def test_empty_yaml():


def test_safe_loader():
loader = load.get_safe_loader(yaml.SafeLoader)
loader = yaml_loaders.get_safe_loader(yaml.SafeLoader)
assert loader.yaml_constructors["!relative"] is not None


def test_get_include_constructor():
"""Test get_include_constructor with different filesystem types."""
constructor = load.get_include_constructor()
constructor = yaml_loaders.get_include_constructor()
assert isinstance(constructor, yaml_include.Constructor)

constructor = load.get_include_constructor(fs="file")
constructor = yaml_loaders.get_include_constructor(fs="file")
assert isinstance(constructor, yaml_include.Constructor)

with pytest.raises(TypeError):
load.get_include_constructor(fs=123) # Invalid type
yaml_loaders.get_include_constructor(fs=123) # Invalid type


def test_get_loader():
"""Test get_loader with different configurations."""
loader = load.get_loader(yaml.SafeLoader)
loader = yaml_loaders.get_loader(yaml.SafeLoader)
assert loader.yaml_constructors["!include"] is not None
assert loader.yaml_constructors["!ENV"] is not None

loader = load.get_loader(yaml.SafeLoader, enable_include=False)
loader = yaml_loaders.get_loader(yaml.SafeLoader, enable_include=False)
assert "!include" not in loader.yaml_constructors

loader = load.get_loader(yaml.SafeLoader, enable_env=False)
loader = yaml_loaders.get_loader(yaml.SafeLoader, enable_env=False)
assert "!ENV" not in loader.yaml_constructors


Expand Down

0 comments on commit fb03669

Please sign in to comment.