Skip to content

Commit

Permalink
sistana: feat: export
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyElaina committed Sep 25, 2024
1 parent eeec0df commit dd9db0c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/arclet/alconna/sistana/model/fragment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class _Fragment:
name: str
variadic: bool = False
default: Some[Any] = None
export: bool = False

separators: str | None = None
hybrid_separators: bool = True
Expand Down
22 changes: 21 additions & 1 deletion src/arclet/alconna/sistana/model/mix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TYPE_CHECKING, Any, Iterable

from ..err import CaptureRejected, ReceivePanic, TransformPanic, ValidateRejected
from ..some import Value
from ..some import Some, Value
from .fragment import _Fragment, assert_fragments_order

if TYPE_CHECKING:
Expand Down Expand Up @@ -234,3 +234,23 @@ def complete_track(self, name: str):
def complete_all(self):
for track in self.tracks.values():
track.complete()

def export_track(self, name: str) -> dict[str, Some[Any]]:
track = self.tracks[name]
result = {}
for frag in self.preset.tracks[name].fragments:
if not frag.export:
continue

if frag.name in track.assignes:
result[frag.name] = Value(track.assignes[frag.name])
else:
result[frag.name] = None

if track.header is not None and track.header.export:
if track.header.name in track.assignes:
result[track.header.name] = Value(track.assignes[track.header.name])
else:
result[track.header.name] = None

return result
29 changes: 26 additions & 3 deletions src/arclet/alconna/sistana/model/snapshot.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import annotations

from collections import defaultdict
from typing import TYPE_CHECKING, Generic, TypeVar
from typing import TYPE_CHECKING, Any, Generic, TypeVar

from ..some import Some, Value

if TYPE_CHECKING:
from .pattern import SubcommandPattern, OptionPattern
from .pointer import Pointer
from .mix import Mix, Track
from .pattern import OptionPattern, SubcommandPattern
from .pointer import Pointer

T = TypeVar("T")

Expand Down Expand Up @@ -109,3 +109,26 @@ def determined(self):

def determine(self, endpoint: Pointer):
self.endpoint = Value(endpoint)

def _export(self):
result: dict[Pointer, dict[str, Some[Any]]] = {}

for traverse in self.traverses:
mix = traverse.mix
result[traverse.ref] = mix.export_track(traverse.trigger)
for option in traverse.subcommand.options.values():
ref = traverse.ref.option(option.keyword)

if ref in result:
continue

result[ref] = mix.export_track(option.keyword)

return result

def export(self):
a = iter(self._export().values())
r = next(a).copy()
for b in a:
r.update(b)
return r

0 comments on commit dd9db0c

Please sign in to comment.