Skip to content

Commit

Permalink
FEATURE : Partial implementation of given rewriting.
Browse files Browse the repository at this point in the history
  • Loading branch information
crdoconnor committed Jan 8, 2024
1 parent 41ec347 commit afbc2b8
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 61 deletions.
2 changes: 1 addition & 1 deletion hitch/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Engine(BaseEngine):
)

info_definition = InfoDefinition(
status=InfoProperty(schema=Enum(["experimental", "stable"])),
status=InfoProperty(schema=Enum(["experimental", "stable", "unimplemented"])),
category=InfoProperty(schema=Enum(["behavior", "runner", "inheritance", "parameterization", "documentation",])),
docs=InfoProperty(schema=Str()),
)
Expand Down
2 changes: 2 additions & 0 deletions hitch/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def regression():
# toolkit.lint(exclude=["__init__.py"])
StoryCollection(
pathquery(DIR.key).ext("story"), Engine(DIR, python_path=_devenv().python_path)
).filter(
lambda story: story.info.get("status") != "unimplemented"
).only_uninherited().ordered_by_name().play()


Expand Down
8 changes: 4 additions & 4 deletions hitch/story/invalid-steps.story
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ Invalid story:
/src/hitchstory/given.py
56 : return self._preconditions[slug]
57 : else:
--> 58 : raise KeyError(
59 : (
61 : return self._preconditions[slug]
62 : else:
--> 63 : raise KeyError(
64 : (
Expand Down
8 changes: 4 additions & 4 deletions hitch/story/rewrite-bugs.story
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ Rewrite single argument incorrect:
/src/hitchstory/story_file.py
72 : step_to_update[self._step.name] = text
73 : else:
--> 74 : raise exceptions.RewriteFailure(
75 : f"'{key_to_update}' doesn't exist, only '{single_argument_name}' exists."
86 : step_to_update[self._given_or_step.name] = text
87 : else:
--> 88 : raise exceptions.RewriteFailure(
89 : (
Expand Down
1 change: 1 addition & 0 deletions hitch/story/rewrite-given.story
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Story that rewrites given preconditions:
status: unimplemented
docs: engine/rewrite-given
about: |
These examples show how to build stories that rewrite themselves
Expand Down
9 changes: 7 additions & 2 deletions hitchstory/given.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, given):
for name, precondition in self._given.items():
self._properties[name] = GivenProperty(
precondition,
self._given.story,
jinja2.Template(self._given._document_templates[name]).render(
**{name: precondition}
),
Expand All @@ -36,17 +37,21 @@ def items(self):

class Given(object):
def __init__(
self, preconditions, child_preconditions=None, document_templates=None
self, preconditions, story, child_preconditions=None, document_templates=None
):
self._preconditions = preconditions
self.story = story

if child_preconditions is not None:
self.child = Given(child_preconditions)
self.child = Given(child_preconditions, story)
else:
self.child = self

self._document_templates = document_templates

def rewrite(self, *args):
return self.story.rewriter(self, args)

def get(self, key, default=None):
return self._preconditions.get(utils.underscore_slugify(key), default)

Expand Down
5 changes: 3 additions & 2 deletions hitchstory/story.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def initialize(self):
preconditions=self._parameterized_preconditions(
self._unparameterized_preconditions()
),
story=self,
child_preconditions=self._parameterized_preconditions(
self._unparameterized_preconditions(child=True),
),
Expand All @@ -133,8 +134,8 @@ def initialize(self):
for index, parsed_step in enumerate(self._yaml_steps)
]

def rewriter(self, step, args):
return self._story_file.rewriter(self, step, args)
def rewriter(self, given_or_step, args):
return self._story_file.rewriter(self, given_or_step, args)

def update(self, step, kwargs):
self._story_file.update(self, step, kwargs)
Expand Down
113 changes: 65 additions & 48 deletions hitchstory/story_file.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from strictyaml import load, Map, Str, Seq, Optional, MapPattern, Any, YAMLError
from hitchstory import exceptions, utils
from hitchstory.story import Story
from hitchstory.given import Given
from path import Path
import copy


class Rewriter(object):
def __init__(self, story_file, story, step, args):
def __init__(self, story_file, story, given_or_step, args):
self._story_file = story_file
self._story = story
self._step = step
self._given_or_step = given_or_step
self._args = args
# assert len(args) == 1

@property
def updated_yaml(self):
Expand All @@ -26,62 +26,79 @@ def to(self, text):
if self._story.variation:
variations = self.updated_yaml[self._story.based_on]["variations"]

if self._step.child_index >= 0:
yaml_story = variations[self._story.child_name]
if isinstance(self._given_or_step, Given):
raise NotImplementedError
else:
if self._given_or_step.child_index >= 0:
yaml_story = variations[self._story.child_name]

step_type = _step_type(yaml_story)
step_type = _step_type(yaml_story)

if self._step.arguments.single_argument:
yaml_story[step_type][self._step.child_index][
self._step.name
] = text
else:
yaml_step = yaml_story[step_type][self._step.child_index][
self._step.name
]
if self._given_or_step.arguments.single_argument:
yaml_story[step_type][self._given_or_step.child_index][
self._given_or_step.name
] = text
else:
yaml_step = yaml_story[step_type][
self._given_or_step.child_index
][self._given_or_step.name]

for subkey in self._args[:-1]:
yaml_step = yaml_step[subkey]
for subkey in self._args[:-1]:
yaml_step = yaml_step[subkey]

yaml_step[key_to_update] = text
else:
yaml_story = variations[self._story.child_name]
step_type = _step_type(yaml_story)

if self._step.arguments.single_argument:
yaml_story[step_type][self._step.index][self._step.name] = text
yaml_step[key_to_update] = text
else:
yaml_step = yaml_story[step_type][self._step.index][self._step.name]

for subkey in self._args[:-1]:
yaml_step = yaml_step[subkey]

yaml_step[key_to_update] = text
yaml_story = variations[self._story.child_name]
step_type = _step_type(yaml_story)

if self._given_or_step.arguments.single_argument:
yaml_story[step_type][self._given_or_step.index][
self._given_or_step.name
] = text
else:
yaml_step = yaml_story[step_type][self._given_or_step.index][
self._given_or_step.name
]

for subkey in self._args[:-1]:
yaml_step = yaml_step[subkey]

yaml_step[key_to_update] = text
else:
yaml_story = self.updated_yaml[self._story.name]

if "following_steps" in yaml_story:
step_to_update = yaml_story[_step_type(yaml_story)][
self._step.child_index
]
if isinstance(self._given_or_step, Given):
raise NotImplementedError
else:
step_to_update = yaml_story[_step_type(yaml_story)][self._step.index]

if self._step.arguments.single_argument:
single_argument_name = self._step.step_method_obj.single_argument_name
if key_to_update == single_argument_name:
step_to_update[self._step.name] = text
if "following_steps" in yaml_story:
step_to_update = yaml_story[_step_type(yaml_story)][
self._given_or_step.child_index
]
else:
raise exceptions.RewriteFailure(
f"'{key_to_update}' doesn't exist, only '{single_argument_name}' exists."
step_to_update = yaml_story[_step_type(yaml_story)][
self._given_or_step.index
]

if self._given_or_step.arguments.single_argument:
single_argument_name = (
self._given_or_step.step_method_obj.single_argument_name
)
else:
yaml_step = step_to_update[self._step.name]
if key_to_update == single_argument_name:
step_to_update[self._given_or_step.name] = text
else:
raise exceptions.RewriteFailure(
(
f"'{key_to_update}' doesn't exist, "
f"only '{single_argument_name}' exists."
)
)
else:
yaml_step = step_to_update[self._given_or_step.name]

for subkey in self._args[:-1]:
yaml_step = yaml_step[subkey]
for subkey in self._args[:-1]:
yaml_step = yaml_step[subkey]

yaml_step[key_to_update] = text
yaml_step[key_to_update] = text


def _step_type(yaml_story):
Expand Down Expand Up @@ -162,11 +179,11 @@ def rewrite(self):
if self._updated_yaml is not None:
self.path.write_text(self._updated_yaml.as_yaml())

def rewriter(self, story, step, args):
def rewriter(self, story, given_or_step, args):
return Rewriter(
self,
story,
step,
given_or_step,
args,
)

Expand Down

0 comments on commit afbc2b8

Please sign in to comment.