Skip to content

Commit 2767a52

Browse files
committed
fix PipeState
It was incorrect to replace the various state['something'] = True with a single-value thing. More than one needs to be able to be present at once, so make it a set instead.
1 parent 72cf265 commit 2767a52

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

src/pyff/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def process_handler(request: Request) -> Response:
289289
try:
290290
for p in request.registry.plumbings:
291291
state = PipeState(
292-
entry_name=entry,
292+
conditions={entry},
293293
headers={'Content-Type': None},
294294
accept=accept,
295295
url=request.current_route_url(),

src/pyff/builtins.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from copy import deepcopy
1414
from datetime import datetime
1515
from distutils.util import strtobool
16-
from typing import Dict, Optional
16+
from typing import Any, Dict, List, Optional
1717

1818
import ipaddr
1919
import six
@@ -25,7 +25,7 @@
2525
from pyff.decorators import deprecated
2626
from pyff.exceptions import MetadataException
2727
from pyff.logs import get_log
28-
from pyff.pipes import PipeException, PipeState, PipelineCallback, Plumbing, pipe, registry
28+
from pyff.pipes import PipeException, PipelineCallback, Plumbing, pipe, registry
2929
from pyff.samlmd import (
3030
annotate_entity,
3131
discojson_t,
@@ -383,13 +383,20 @@ def when(req: Plumbing.Request, condition: str, *values):
383383
The condition operates on the state: if 'foo' is present in the state (with any value), then the something branch is
384384
followed. If 'bar' is present in the state with the value 'bill' then the other branch is followed.
385385
"""
386-
if req.state.entry_name is None:
387-
log.debug(f'Condition {repr(condition)} not present in state {req.state}')
388-
if req.state.entry_name is not None and (not values or _any(values, req.state.entry_name)):
386+
log.debug(f'"when" called for condition "{condition}", values {values}, state {req.state}')
387+
c: Any = None
388+
if condition in req.state.conditions:
389+
c = True
390+
if condition == 'accept':
391+
c = req.state.accept
392+
if c is not None and (not values or _any(values, c)):
389393
if not isinstance(req.args, list):
390394
raise ValueError('Non-list arguments to "when" not allowed')
391395

392-
return Plumbing(pipeline=req.args, pid=f'{req.plumbing.id}.when').iprocess(req)
396+
_pid = f'{req.plumbing.id}.when'
397+
log.debug(f'Creating new plumbing: {_pid}, pipeline {req.args}')
398+
return Plumbing(pipeline=req.args, pid=_pid).iprocess(req)
399+
log.debug(f'Continuing on plumbing {req.id}')
393400
return req.t
394401

395402

src/pyff/md.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import traceback
99

1010
from pyff.constants import config, parse_options
11-
from pyff.pipes import plumbing
11+
from pyff.pipes import PipeState, plumbing
1212
from pyff.repo import MDRepository
1313

1414

@@ -30,7 +30,7 @@ def main():
3030
try:
3131
md = MDRepository()
3232
for p in args:
33-
plumbing(p).process(md, state={'batch': True, 'stats': {}})
33+
plumbing(p).process(md, state=PipeState(conditions={'batch'}))
3434
sys.exit(0)
3535
except Exception as ex:
3636
logging.debug(traceback.format_exc())

src/pyff/pipes.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import functools
88
import os
99
import traceback
10-
from typing import Any, Callable, Dict, Iterable, List, Optional, TYPE_CHECKING, Tuple, Type
11-
from typing import Union
10+
from typing import Any, Callable, Dict, Iterable, List, Optional, Set, TYPE_CHECKING, Tuple, Type, Union
1211

1312
import yaml
1413
from apscheduler.schedulers.background import BackgroundScheduler
@@ -177,7 +176,7 @@ def __call__(self, t: ElementTree, state: Optional[PipeState] = None) -> Any:
177176
if not isinstance(state, PipeState):
178177
raise ValueError(f'PipelineCallback called with invalid state ({type(state)}')
179178
try:
180-
state.entry_name = self.entry_point
179+
state.conditions.add(self.entry_point)
181180
log.debug("state: {}".format(state))
182181
return self.plumbing.process(self.req.md, store=self.store, state=state, t=t)
183182
except Exception as ex:
@@ -187,14 +186,13 @@ def __call__(self, t: ElementTree, state: Optional[PipeState] = None) -> Any:
187186

188187

189188
class PipeState(BaseModel):
190-
batch: bool = False
191-
entry_name: Optional[str] = None
189+
conditions: Set[str] = Field(set())
192190
headers: Dict[str, Any] = Field({})
193191
accept: Any = None # TODO: Re-arrange classes so that type 'MediaAccept' works
194192
url: str = ''
195-
select: str = ''
196-
match: str = ''
197-
path: str = ''
193+
select: Optional[str] = None
194+
match: Optional[str] = None
195+
path: Optional[str] = None
198196
stats: Dict[str, Any] = Field({})
199197
cache: int = 0 # cache_ttl
200198

src/pyff/test/test_pipeline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def run_pipeline(self, pl_name, ctx=None, md=None):
6161
template = templates.get_template(pl_name)
6262
with open(pipeline, "w") as fd:
6363
fd.write(template.render(ctx=ctx))
64-
res = plumbing(pipeline).process(md, PipeState(entry_name='batch'))
64+
res = plumbing(pipeline).process(md, PipeState(conditions={'batch'}))
6565
os.unlink(pipeline)
6666
return res, md, ctx
6767

@@ -70,7 +70,7 @@ def exec_pipeline(self, pstr):
7070
p = yaml.safe_load(six.StringIO(pstr))
7171
print("\n{}".format(yaml.dump(p)))
7272
pl = Plumbing(p, pid="test")
73-
res = pl.process(md, PipeState(entry_name='batch'))
73+
res = pl.process(md, PipeState(conditions={'batch'}))
7474
return res, md
7575

7676
@classmethod

0 commit comments

Comments
 (0)