Skip to content

Commit

Permalink
allow unknown targets
Browse files Browse the repository at this point in the history
Allow unknown targets by always converting them to JSON and formatting
them with a common context.

Signed-off-by: Simon de Vlieger <[email protected]>
  • Loading branch information
supakeen committed May 28, 2024
1 parent 0a96d2c commit 78441e8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/otk/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .context import registry as context_registry
from .document import Omnifest
from .help.log import JSONSequenceHandler
from .target import CommonTarget
from .target import registry as target_registry
from .transform import resolve

Expand Down Expand Up @@ -151,8 +152,8 @@ def compile(

kind = name.split(".")[0]

context = context_registry[kind](ctx)
target = target_registry[kind]()
context = context_registry.get(kind, CommonContext)(ctx)
target = target_registry.get(kind, CommonTarget)()

# This time we resolve with a kind
tree = resolve(context, tree)
Expand Down
12 changes: 11 additions & 1 deletion src/otk/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import ABC, abstractmethod
from typing import Any

from .context import Context, OSBuildContext
from .context import Context, CommonContext, OSBuildContext


log = logging.getLogger(__name__)
Expand All @@ -17,6 +17,16 @@ def is_valid(self, tree: Any) -> bool: ...
def as_string(self, context: Context, tree: Any, pretty: bool) -> str: ...


# NOTE this common target is a bit weird, we probably shouldn't always assume JSON but
# NOTE it makes development a tad easier until we figure out all our targets
class CommonTarget(Target):
def is_valid(self, tree: Any) -> bool:
return True

def as_string(self, context: CommonContext, tree: Any, pretty: bool = True) -> str:
return json.dumps(tree, indent=2 if pretty else None)


class OSBuildTarget(Target):
def is_valid(self, tree: Any) -> bool:
return True
Expand Down

0 comments on commit 78441e8

Please sign in to comment.