diff --git a/nbs/00_core.ipynb b/nbs/00_core.ipynb index 8920de3..ea033fb 100644 --- a/nbs/00_core.ipynb +++ b/nbs/00_core.ipynb @@ -23,8 +23,16 @@ "import jupyter_black\n", "import nbdev.showdoc as showdoc\n", "from fastcore.test import *\n", - "from fastcore.imports import *\n", - "\n", + "from fastcore.imports import *" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# | hide\n", "jupyter_black.load()" ] }, diff --git a/nbs/01_loaders.ipynb b/nbs/01_loaders.ipynb index 6dc9254..8554bc2 100644 --- a/nbs/01_loaders.ipynb +++ b/nbs/01_loaders.ipynb @@ -28,8 +28,17 @@ "outputs": [], "source": [ "# | hide\n", - "import jupyter_black\n", - "\n", + "import jupyter_black" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0dd798e8-addd-47c3-88f4-a55898ffa868", + "metadata": {}, + "outputs": [], + "source": [ + "# | hide\n", "jupyter_black.load()" ] }, diff --git a/nbs/02_codegen.ipynb b/nbs/02_codegen.ipynb index 5c1607d..1335947 100644 --- a/nbs/02_codegen.ipynb +++ b/nbs/02_codegen.ipynb @@ -30,8 +30,17 @@ "from textwrap import dedent\n", "\n", "from sal.loaders import xml_to_data\n", - "from sal.utils import files\n", - "\n", + "from sal.utils import files" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c207b1c5-14c6-4783-9712-23ea543ff414", + "metadata": {}, + "outputs": [], + "source": [ + "# | hide\n", "jupyter_black.load()" ] }, diff --git a/nbs/03_cli.ipynb b/nbs/03_cli.ipynb index bc5ca4b..7e8ade5 100644 --- a/nbs/03_cli.ipynb +++ b/nbs/03_cli.ipynb @@ -31,8 +31,17 @@ "import nbdev.showdoc as showdoc\n", "from fastcore.test import *\n", "\n", - "from sal.utils import files\n", - "\n", + "from sal.utils import files" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e6a55fbf-739f-48e6-a541-83517bca7309", + "metadata": {}, + "outputs": [], + "source": [ + "# | hide\n", "jupyter_black.load()" ] }, diff --git a/nbs/99_arguments.ipynb b/nbs/99_arguments.ipynb index 85dc29d..54f01c7 100644 --- a/nbs/99_arguments.ipynb +++ b/nbs/99_arguments.ipynb @@ -23,8 +23,17 @@ "\n", "import jupyter_black\n", "import nbdev.showdoc as showdoc\n", - "from fastcore.test import *\n", - "\n", + "from fastcore.test import *" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a10162a0-7753-4f47-aebf-9568042b2bbe", + "metadata": {}, + "outputs": [], + "source": [ + "# | hide\n", "jupyter_black.load()" ] }, diff --git a/nbs/99_frontmatter.ipynb b/nbs/99_frontmatter.ipynb index b3703a2..bbd567b 100644 --- a/nbs/99_frontmatter.ipynb +++ b/nbs/99_frontmatter.ipynb @@ -23,8 +23,17 @@ "\n", "import jupyter_black\n", "import nbdev.showdoc as showdoc\n", - "from fastcore.test import *\n", - "\n", + "from fastcore.test import *" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9e677a32-b974-4900-90d9-59dce8626791", + "metadata": {}, + "outputs": [], + "source": [ + "# | hide\n", "jupyter_black.load()" ] }, diff --git a/nbs/99_templates.ipynb b/nbs/99_templates.ipynb index 555e45f..63fc519 100644 --- a/nbs/99_templates.ipynb +++ b/nbs/99_templates.ipynb @@ -25,7 +25,17 @@ "import nbdev.showdoc as showdoc\n", "from fastcore.test import *\n", "from jinja2 import UndefinedError\n", - "\n", + "from sal.utils import files" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "48bd2e43-e98a-4b77-974c-2222b3fca75e", + "metadata": {}, + "outputs": [], + "source": [ + "# | hide\n", "jupyter_black.load()" ] }, @@ -216,7 +226,6 @@ "outputs": [], "source": [ "# | hide\n", - "from sal.utils import files\n", "\n", "with files(\n", " {\n", diff --git a/nbs/99_utils.ipynb b/nbs/99_utils.ipynb index 23d6031..200e9f7 100644 --- a/nbs/99_utils.ipynb +++ b/nbs/99_utils.ipynb @@ -23,8 +23,17 @@ "\n", "import jupyter_black\n", "import nbdev.showdoc as showdoc\n", - "from fastcore.test import *\n", - "\n", + "from fastcore.test import *" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5d425ddb-7923-4b2f-9fb1-c6da34f13a5e", + "metadata": {}, + "outputs": [], + "source": [ + "# | hide\n", "jupyter_black.load()" ] }, diff --git a/nbs/sidebar.yml b/nbs/sidebar.yml index 8c9c2c3..fae238c 100644 --- a/nbs/sidebar.yml +++ b/nbs/sidebar.yml @@ -6,3 +6,7 @@ website: - 01_loaders.ipynb - 02_codegen.ipynb - 03_cli.ipynb + - 99_arguments.ipynb + - 99_frontmatter.ipynb + - 99_templates.ipynb + - 99_utils.ipynb diff --git a/sal/arguments.py b/sal/arguments.py index 780538f..d8af913 100644 --- a/sal/arguments.py +++ b/sal/arguments.py @@ -3,11 +3,11 @@ # %% auto 0 __all__ = ['JsonType', 'parse_arg', 'parse_attrs'] -# %% ../nbs/99_arguments.ipynb 2 +# %% ../nbs/99_arguments.ipynb 3 import json from typing import Union -# %% ../nbs/99_arguments.ipynb 5 +# %% ../nbs/99_arguments.ipynb 6 JsonType = Union[None, int, str, bool, list["JsonType"], dict[str, "JsonType"]] diff --git a/sal/cli.py b/sal/cli.py index a75c6ae..46b93a1 100644 --- a/sal/cli.py +++ b/sal/cli.py @@ -3,7 +3,7 @@ # %% auto 0 __all__ = ['main', 'render'] -# %% ../nbs/03_cli.ipynb 3 +# %% ../nbs/03_cli.ipynb 4 import click from pathlib import Path from typing import Any @@ -11,7 +11,7 @@ from .codegen import Sal from .utils import is_notebook -# %% ../nbs/03_cli.ipynb 6 +# %% ../nbs/03_cli.ipynb 7 # TODO support filters from config file @@ -19,12 +19,12 @@ def _render(file: str, directories: list[Path]) -> str | Any: sal = Sal.from_config(template_directories=directories) return sal.process_xml_from_filename(file) -# %% ../nbs/03_cli.ipynb 9 +# %% ../nbs/03_cli.ipynb 10 @click.group() def main() -> None: pass -# %% ../nbs/03_cli.ipynb 10 +# %% ../nbs/03_cli.ipynb 11 # TODO : init command # - create : sal.xml file # - create : sal folder @@ -45,6 +45,6 @@ def render(filename: str, folder: str) -> None: # TODO snapshot # TODO check.snapshot -# %% ../nbs/03_cli.ipynb 11 +# %% ../nbs/03_cli.ipynb 12 if __name__ == "__main__" and not is_notebook(): main() diff --git a/sal/codegen.py b/sal/codegen.py index 8a10a7b..a5febfb 100644 --- a/sal/codegen.py +++ b/sal/codegen.py @@ -3,7 +3,7 @@ # %% auto 0 __all__ = ['Config', 'Sal'] -# %% ../nbs/02_codegen.ipynb 2 +# %% ../nbs/02_codegen.ipynb 3 import abc from pydantic import BaseModel from typing import Any, Callable @@ -20,7 +20,7 @@ MissingTemplateException, ) -# %% ../nbs/02_codegen.ipynb 11 +# %% ../nbs/02_codegen.ipynb 12 @dataclass class WriteFileResult: to: str @@ -64,7 +64,7 @@ class GroupAction(SalAction): def process_data(self, sal: "Sal", data: Data) -> tuple[Any, None]: return [sal.process(d) for d in data.children], None -# %% ../nbs/02_codegen.ipynb 12 +# %% ../nbs/02_codegen.ipynb 13 class Config(BaseModel): template_directories: list[Path] filters: dict[str, Callable] = {} diff --git a/sal/core.py b/sal/core.py index 2efa529..952439a 100644 --- a/sal/core.py +++ b/sal/core.py @@ -3,13 +3,13 @@ # %% auto 0 __all__ = ['WithChildrenMixin', 'Data', 'MappedData', 'map_data'] -# %% ../nbs/00_core.ipynb 2 +# %% ../nbs/00_core.ipynb 3 from copy import deepcopy from textwrap import indent from collections import ChainMap from typing import Callable, Any, Generator, Sequence, Optional -# %% ../nbs/00_core.ipynb 7 +# %% ../nbs/00_core.ipynb 8 class WithChildrenMixin: """ Adds `parent`/`children` functionality to a class. @@ -48,7 +48,7 @@ def iter_data(obj: "WithChildrenMixin", level: int | None = 0) -> Generator: return iter_data(self) -# %% ../nbs/00_core.ipynb 9 +# %% ../nbs/00_core.ipynb 10 class Data(WithChildrenMixin): """ Data holder used during code generation. Logic is kept as separate functions. @@ -129,7 +129,7 @@ def __str__(self) -> str: __repr__ = __str__ -# %% ../nbs/00_core.ipynb 34 +# %% ../nbs/00_core.ipynb 35 class MappedData(WithChildrenMixin): """Data structure used to return results from the `map_data` function""" @@ -137,7 +137,7 @@ def __init__(self, value: Any): self.value = value super().__init__() -# %% ../nbs/00_core.ipynb 35 +# %% ../nbs/00_core.ipynb 36 def map_data(obj: Data, process: Callable, level: int | None = 0) -> MappedData: """Maps over a `Data` inst returning `MappedData` instances""" child_results = [map_data(c, process, level=(level or 0) + 1) for c in obj.children] diff --git a/sal/frontmatter.py b/sal/frontmatter.py index 657cd5b..bee3334 100644 --- a/sal/frontmatter.py +++ b/sal/frontmatter.py @@ -3,13 +3,13 @@ # %% auto 0 __all__ = ['FrontMatter'] -# %% ../nbs/99_frontmatter.ipynb 2 +# %% ../nbs/99_frontmatter.ipynb 3 from typing import Any from textwrap import dedent from frontmatter.util import u # type: ignore[import-untyped] from frontmatter.default_handlers import YAMLHandler # type: ignore[import-untyped] -# %% ../nbs/99_frontmatter.ipynb 5 +# %% ../nbs/99_frontmatter.ipynb 6 class FrontMatter: def __init__(self, h: YAMLHandler | None = None): # type: ignore[no-any-unimported] if h is None: diff --git a/sal/loaders.py b/sal/loaders.py index 5674cfa..9e558e2 100644 --- a/sal/loaders.py +++ b/sal/loaders.py @@ -3,13 +3,13 @@ # %% auto 0 __all__ = ['xml_to_data', 'xml_file_to_data', 'dict_to_data', 'json_to_data'] -# %% ../nbs/01_loaders.ipynb 3 +# %% ../nbs/01_loaders.ipynb 4 from json import loads from xml.etree import ElementTree from .core import Data -# %% ../nbs/01_loaders.ipynb 5 +# %% ../nbs/01_loaders.ipynb 6 def element_tree_to_data(obj: ElementTree.Element) -> Data: """Takes an ElementTree, and loads it into a Data instance""" attributes = obj.attrib.copy() @@ -19,18 +19,18 @@ def element_tree_to_data(obj: ElementTree.Element) -> Data: data.append(c) return data -# %% ../nbs/01_loaders.ipynb 8 +# %% ../nbs/01_loaders.ipynb 9 def xml_to_data(xml: str) -> Data: """Takes a xml string, and loads it into a Data instance""" obj: ElementTree.Element = ElementTree.fromstring(xml) return element_tree_to_data(obj) -# %% ../nbs/01_loaders.ipynb 10 +# %% ../nbs/01_loaders.ipynb 11 def xml_file_to_data(path: str) -> Data: with open(path, "r") as h: return xml_to_data(h.read()) -# %% ../nbs/01_loaders.ipynb 14 +# %% ../nbs/01_loaders.ipynb 15 def dict_to_data(arg: dict) -> Data: """Takes a dict, and loads it into a Data instance""" name = arg["name"] @@ -45,7 +45,7 @@ def dict_to_data(arg: dict) -> Data: data.append(dict_to_data(c)) return data -# %% ../nbs/01_loaders.ipynb 19 +# %% ../nbs/01_loaders.ipynb 20 def json_to_data(arg: str) -> Data: """Takes a json string, and loads it into a Data instance""" j = loads(arg) diff --git a/sal/templates.py b/sal/templates.py index 82ca8f9..33a5360 100644 --- a/sal/templates.py +++ b/sal/templates.py @@ -3,7 +3,7 @@ # %% auto 0 __all__ = ['render_to_remove'] -# %% ../nbs/99_templates.ipynb 2 +# %% ../nbs/99_templates.ipynb 3 from pathlib import Path from typing import Any, Optional from jinja2 import ( @@ -20,11 +20,11 @@ from sal.core import Data from sal.frontmatter import FrontMatter -# %% ../nbs/99_templates.ipynb 4 +# %% ../nbs/99_templates.ipynb 5 def _get_env() -> Environment: return Environment(loader=BaseLoader(), undefined=StrictUndefined) -# %% ../nbs/99_templates.ipynb 6 +# %% ../nbs/99_templates.ipynb 7 def render_to_remove( template: str, # template in string form filters: Optional[dict] = None, # jinja filters @@ -41,14 +41,14 @@ def render_to_remove( return result -# %% ../nbs/99_templates.ipynb 10 +# %% ../nbs/99_templates.ipynb 11 class TemplateRenderer: def render(self, template: str | None = None, **kwargs: Any) -> str: if template is None: raise RuntimeError("Missing template") return render_to_remove(template, **kwargs) -# %% ../nbs/99_templates.ipynb 12 +# %% ../nbs/99_templates.ipynb 13 MissingTemplateException = TemplateNotFound @@ -78,7 +78,7 @@ def get_frontmatter_source(self, name: str) -> str: def from_directories(cls, directories: list[Path]) -> "TemplateLoader": return cls(folders=directories) -# %% ../nbs/99_templates.ipynb 16 +# %% ../nbs/99_templates.ipynb 17 # TODO remove "any" typings class Renderer: # if no template is passed in, we use the DEFAULT_TEMPLATE diff --git a/sal/utils.py b/sal/utils.py index a500bbc..bf22a51 100644 --- a/sal/utils.py +++ b/sal/utils.py @@ -3,13 +3,13 @@ # %% auto 0 __all__ = ['files', 'is_notebook'] -# %% ../nbs/99_utils.ipynb 2 +# %% ../nbs/99_utils.ipynb 3 from pathlib import Path from typing import Generator from IPython import get_ipython from contextlib import contextmanager -# %% ../nbs/99_utils.ipynb 4 +# %% ../nbs/99_utils.ipynb 5 @contextmanager def files(content: dict[str, str]) -> Generator: """Setup files with content. No override if file already exists.""" @@ -28,7 +28,7 @@ def files(content: dict[str, str]) -> Generator: path = Path(filepath) path.unlink() -# %% ../nbs/99_utils.ipynb 7 +# %% ../nbs/99_utils.ipynb 8 def is_notebook() -> bool: """Check if we are running code in a notebook or in a shell""" try: