Skip to content

Commit

Permalink
Fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro committed Dec 4, 2023
1 parent 43850a8 commit 515fcb8
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 48 deletions.
12 changes: 10 additions & 2 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
]
},
Expand Down
13 changes: 11 additions & 2 deletions nbs/01_loaders.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
]
},
Expand Down
13 changes: 11 additions & 2 deletions nbs/02_codegen.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
]
},
Expand Down
13 changes: 11 additions & 2 deletions nbs/03_cli.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
]
},
Expand Down
13 changes: 11 additions & 2 deletions nbs/99_arguments.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
]
},
Expand Down
13 changes: 11 additions & 2 deletions nbs/99_frontmatter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
]
},
Expand Down
13 changes: 11 additions & 2 deletions nbs/99_templates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
]
},
Expand Down Expand Up @@ -216,7 +226,6 @@
"outputs": [],
"source": [
"# | hide\n",
"from sal.utils import files\n",
"\n",
"with files(\n",
" {\n",
Expand Down
13 changes: 11 additions & 2 deletions nbs/99_utils.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
]
},
Expand Down
4 changes: 4 additions & 0 deletions nbs/sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions sal/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]


Expand Down
10 changes: 5 additions & 5 deletions sal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
# %% 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

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


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
Expand All @@ -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()
6 changes: 3 additions & 3 deletions sal/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +20,7 @@
MissingTemplateException,
)

# %% ../nbs/02_codegen.ipynb 11
# %% ../nbs/02_codegen.ipynb 12
@dataclass
class WriteFileResult:
to: str
Expand Down Expand Up @@ -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] = {}
Expand Down
10 changes: 5 additions & 5 deletions sal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -129,15 +129,15 @@ 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"""

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]
Expand Down
4 changes: 2 additions & 2 deletions sal/frontmatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions sal/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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"]
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 515fcb8

Please sign in to comment.