summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/pyssg/__init__.py2
-rw-r--r--src/pyssg/yaml_parser.py7
-rw-r--r--tests/conftest.py6
-rw-r--r--tests/io_files/simple.yaml28
-rw-r--r--tests/test_yaml_parser.py19
5 files changed, 59 insertions, 3 deletions
diff --git a/src/pyssg/__init__.py b/src/pyssg/__init__.py
index 5d112f1..46e2323 100644
--- a/src/pyssg/__init__.py
+++ b/src/pyssg/__init__.py
@@ -1,7 +1,9 @@
from .pyssg import main
from .custom_logger import setup_logger
+from .yaml_parser import setup_custom_yaml
setup_logger()
+setup_custom_yaml()
# not meant to be used as a package, so just give main
__all__ = ['main']
diff --git a/src/pyssg/yaml_parser.py b/src/pyssg/yaml_parser.py
index fb19a23..2e1548b 100644
--- a/src/pyssg/yaml_parser.py
+++ b/src/pyssg/yaml_parser.py
@@ -13,6 +13,10 @@ def __join_constructor(loader: SafeLoader, node: SequenceNode) -> str:
return ''.join([str(i) for i in seq])
+def setup_custom_yaml() -> None:
+ SafeLoader.add_constructor('!join', __join_constructor)
+
+
def __read_raw_yaml(path: str) -> list[dict]:
all_docs: list[dict] = []
with open(path, 'r') as f:
@@ -28,6 +32,3 @@ def get_parsed_yaml(resource: str, package: str = '') -> list[dict]:
log.debug('parsing yaml; reading "%s.%s"', package, resource)
with rpath(package, resource) as p:
return __read_raw_yaml(str(p))
-
-
-SafeLoader.add_constructor('!join', __join_constructor)
diff --git a/tests/conftest.py b/tests/conftest.py
index 58416ea..1beffc6 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,3 +1,4 @@
+import os
import sys
from typing import Callable
import pytest
@@ -7,6 +8,11 @@ from pyssg.arg_parser import get_parser
from pyssg.custom_logger import setup_logger
+@pytest.fixture
+def test_dir():
+ return os.path.dirname(os.path.abspath(__file__))
+
+
@pytest.fixture(scope='session')
def arg_parser():
return get_parser()
diff --git a/tests/io_files/simple.yaml b/tests/io_files/simple.yaml
new file mode 100644
index 0000000..0b722a6
--- /dev/null
+++ b/tests/io_files/simple.yaml
@@ -0,0 +1,28 @@
+%YAML 1.2
+---
+define: &root "$HOME/pyssg/site_example/"
+
+title: "Example site"
+path:
+ src: !join [*root, "src"]
+ dst: !join [*root, "dst"]
+ plt: !join [*root, "plt"]
+ db: !join [*root, ".files"]
+url:
+ main: "https://example.com"
+ static: "https://static.example.com"
+ default_image: "images/default.png"
+fmt:
+ date: "%a, %b %d, %Y @ %H:%M %Z"
+ list_date: "%b %d"
+ list_sep_date: "%B %Y"
+dirs:
+ /:
+ cfg:
+ plt: "page.html"
+ tags: False
+ index: False
+ rss: False
+ sitemap: False
+ exclude_dirs: []
+... \ No newline at end of file
diff --git a/tests/test_yaml_parser.py b/tests/test_yaml_parser.py
new file mode 100644
index 0000000..9a1b3c3
--- /dev/null
+++ b/tests/test_yaml_parser.py
@@ -0,0 +1,19 @@
+from importlib.resources import path as rpath
+from pyssg.yaml_parser import get_parsed_yaml
+
+
+def test_yaml_resource_read() -> None:
+ yaml: list[dict] = get_parsed_yaml('simple.yaml', 'tests.io_files')
+ assert len(yaml) == 1
+
+
+def test_yaml_path_read(test_dir: str) -> None:
+ yaml: list[dict] = get_parsed_yaml(f'{test_dir}/io_files/simple.yaml')
+ assert len(yaml) == 1
+
+
+def test_yaml_join() -> None:
+ yaml: dict = get_parsed_yaml('simple.yaml', 'tests.io_files')[0]
+ define_str: str = '$HOME/pyssg/site_example/'
+ assert yaml['define'] == define_str
+ assert yaml['path']['src'] == f'{define_str}src'