Skip to content

Commit 19fada5

Browse files
docs: 🏗️ add view() Python interface design (#105)
# Description This adds the `view()` function to the interface design docs. Closes #81 Needs a thorough review. ## Checklist - [x] Formatted Markdown - [x] Ran `just run-all` --------- Co-authored-by: Joel Ostblom <joelostblom@users.noreply.github.com>
1 parent 776ea51 commit 19fada5

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

docs/design/interface/python.qmd

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,83 @@ def build(
144144
cli_message(output_files) #?
145145
```
146146
:::
147+
148+
### {{< var planned >}} `view()`
149+
150+
As mentioned in the CLI design page, this function has two parameters that
151+
are both the same as those in the `build()` function: `uri` and `style`.
152+
The only difference with the `style` parameter is that it can only accept built-in terminal styles
153+
and does not allow a custom style.
154+
155+
The internal flow of this function is shown in the diagram below.
156+
157+
```{mermaid}
158+
%%| label: fig-python-view-flow
159+
%%| fig-cap: "Diagram of the internal flow of functions and objects in the `view()` CLI function."
160+
flowchart TD
161+
uri:::input --> resolve_uri{{"resolve_uri()"}} --> path
162+
style_cfg[style]:::input --> Config
163+
path --> read_dp{{"read_properties()"}} --> PackageProperties
164+
PackageProperties & Config --> build_sections{{"build_sections()"}} --> output["list[BuiltSection]"]
165+
output --> pretty_print{{"pretty_print()"}}
166+
167+
classDef input fill:#FFF
168+
```
169+
170+
::: {.callout-tip collapse="true" icon="false"}
171+
## Pseudocode: `view()`
172+
173+
``` {.python filename="src/seedcase_flower/cli.py"}
174+
from typing import Optional
175+
from seedcase_flower.config import Style
176+
from seedcase_sprout import read_properties, PackageProperties
177+
from pathlib import Path
178+
from enum import Enum
179+
180+
# Allows for strict checking of built-in styles, as this is a sum type.
181+
# The specific terminal style needs to also exist in the Style enum.
182+
class TerminalStyle(Enum):
183+
"""Built-in styles for outputting to the terminal."""
184+
# Can add more styles later here if needed.
185+
terminal_default = "terminal-default"
186+
187+
def view(uri: str = "datapackage.json", style: TerminalStyle = TerminalStyle.terminal_default):
188+
"""Display the `datapackage.json` properties in a human-friendly way in the terminal.
189+
190+
Args:
191+
uri: The URI to a datapackage.json file. Defaults to
192+
`datapackage.json` in the current working directory.
193+
style: The style of output to use. Must be one of built-in terminal
194+
styles in `TerminalStyle` or None.
195+
"""
196+
# Potential implementation steps:
197+
198+
# Output maybe str? Path?
199+
# Use `match` inside for strictness on URI types?
200+
path: str = resolve_uri(uri)
201+
202+
# Match works well when paired with enums for strictness and checking.
203+
match style:
204+
# Might be a more concise way of doing this when there are more styles.
205+
case _ if style in TerminalStyle:
206+
config: Config = Config(style=TerminalStyle(style))
207+
208+
# This matches all other cases not explicitly handled above.
209+
case _:
210+
# Or however we implement error handling here.
211+
error("Style not supported for terminal output. Should be one of ...")
212+
213+
# Able to read from URI, e.g. `https` or `file` or `gh`
214+
properties: PackageProperties = read_properties(path)
215+
216+
# One item per section, rendered from template.
217+
# Internally uses Jinja2 to render templates with metadata.
218+
output: list[BuiltSection] = build_sections(
219+
properties,
220+
config
221+
)
222+
223+
# Pretty printing here.
224+
return print(output)
225+
```
226+
:::

0 commit comments

Comments
 (0)