Skip to content

Commit

Permalink
Merge pull request #86 from twisted/wip-1
Browse files Browse the repository at this point in the history
fix RPN demo to actually work
  • Loading branch information
glyph committed Mar 5, 2020
2 parents 40ca904 + a888f14 commit eb92445
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 37 deletions.
17 changes: 17 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[run]
branch = True
parallel = True
include =
tubes/*
*/site-packages/tubes/*

[paths]
source =
tubes
.tox/*/lib/python*/site-packages/tubes
.tox/*/Lib/site-packages/tubes
.tox/pypy*/site-packages/tubes

[report]
precision = 2
ignore_errors = True
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ matrix:
- env: "TOX_ENV=docs-linkcheck"
include:
- env: TOX_ENV=lint
python: 2.7
python: 3.6
- env: TOX_ENV=py27
python: 2.7
- env: TOX_ENV=py36
python: 3.6
- env: TOX_ENV=pypy
python: 3.6
- env: TOX_ENV=docs
python: 3.6
- env: TOX_ENV=apidocs
python: 2.7
- env: TOX_ENV=docs-spellcheck
python: 3.6
- env: TOX_ENV=docs-linkcheck
python: 3.6
# - PUSH_DOCS=true

install:
Expand All @@ -30,7 +35,7 @@ script:


after_success:
- if [[ "${TOX_ENV:0:2}" == 'py' ]]; then coverage combine; coveralls; fi
- if [[ "${TOX_ENV:0:2}" == 'py' ]]; then tox -e coveralls-push; fi

notifications:
irc:
Expand Down
21 changes: 15 additions & 6 deletions docs/listings/rpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@ def push(self, number):
self.stack.append(number)

def do(self, operator):
if len(self.stack) < 2:
return "UNDERFLOW"
left = self.stack.pop()
right = self.stack.pop()
result = operator(left, right)
self.push(result)
return result

err = object()

@receiver(inputType=IFrame)
def linesToNumbersOrOperators(line):
from operator import add, mul
try:
yield int(line)
except ValueError:
if line == '+':
if line == b'+':
yield add
elif line == '*':
elif line == b'*':
yield mul
else:
yield err

@tube
class CalculatingTube(object):
Expand All @@ -39,6 +45,8 @@ def __init__(self, calculator):
def received(self, value):
if isinstance(value, int):
self.calculator.push(value)
elif value is err:
yield "SYNTAX"
else:
yield self.calculator.do(value)

Expand All @@ -48,12 +56,13 @@ def numbersToLines(value):

@tube
class Prompter(object):
inputType = ISegment
outputType = ISegment
def started(self):
yield "> "
yield b"> "
def received(self, item):
yield "> "
yield b"> "
def stopped(self, failure):
yield b"BYE"

def promptingCalculatorSeries():
from tubes.fan import Thru
Expand Down Expand Up @@ -81,7 +90,7 @@ def calculatorSeries():
)

def mathFlow(flow):
processor = calculatorSeries()
processor = promptingCalculatorSeries()
nextDrain = flow.fount.flowTo(processor)
nextDrain.flowTo(flow.drain)

Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
packages=find_packages(exclude=[]),
package_dir={'tubes': 'tubes'},
install_requires=[
"characteristic",
"six",
"Twisted",
],
Expand Down
24 changes: 17 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
envlist = py27, py36, pypy, docs, lint, apidocs, docs-spellcheck

[testenv]
passenv = *
deps =
coverage
commands =
coverage run -p --source=./tubes {envbindir}/trial --rterrors {posargs:tubes}
coverage run -p {envbindir}/trial --rterrors {posargs:tubes}

[testenv:docs]
deps =
doc8
pygments
sphinx!=1.6.1
sphinx_rtd_theme
basepython = python2.7
basepython = python3.6
commands =
sphinx-build -W -b html -d {envtmpdir}/doctrees docs docs/_build/html
sphinx-build -W -b latex -d {envtmpdir}/doctrees docs docs/_build/latex
Expand All @@ -35,24 +36,33 @@ deps =
{[testenv:docs]deps}
pyenchant
sphinxcontrib-spelling
basepython = python2.7
basepython = python3.6
commands =
sphinx-build -W -b spelling docs docs/_build/html

[testenv:docs-linkcheck]
deps =
{[testenv:docs]deps}
basepython = python2.7
basepython = python3.6
commands =
sphinx-build -W -b linkcheck docs docs/_build/html

[testenv:lint]
deps =
twistedchecker==0.6.0
basepython = python2.7
twistedchecker==0.7.2
basepython = python3.6
commands =
# pep257 --ignore=D400,D401,D200,D203,D204,D205 ./tubes
python .failonoutput.py "twistedchecker -f parseable ./tubes"
python .failonoutput.py "twistedchecker --msg-template=\{path\}:\{line\}:\{column\}:\ [\{msg_id\}\(\{symbol\}\),\ \{obj\}]\ \{msg\} ./tubes"

[testenv:coveralls-push]
deps =
coveralls
coverage
commands =
coverage combine
coverage report
coveralls

[flake8]
exclude = docs,.tox,*.egg,*.pyc,.git,__pycache
Expand Down
1 change: 0 additions & 1 deletion tubes/_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,3 @@ def _registryAdapting(*fromToAdapterTuples):
for _from, to, adapter in fromToAdapterTuples:
result.register([_from], to, '', adapter)
return result

3 changes: 0 additions & 3 deletions tubes/_siphon.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,3 @@ def _tube2drain(tube):
_tubeRegistry = _registryAdapting(
(ITube, IDrain, _tube2drain),
)



16 changes: 12 additions & 4 deletions tubes/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ def flowingFrom(self, fount):
@return: C{None}; this is a terminal drain and not a data-processing
drain.
"""
if fount is self.fount:
return
beginFlowingFrom(self, fount)
if self._presentPause is not None:
p, self._presentPause = self._presentPause, None
p.unpause()
if self._in.fount._isPaused:
self._presentPause = fount.pauseFlow()
return None
Expand Down Expand Up @@ -95,7 +100,8 @@ def doPause():
def doResume():
self._isPaused = False
for drain in self._in._drains:
drain._presentPause.unpause()
drain._presentPause, currentPause = None, drain._presentPause
currentPause.unpause()
self._pauser = Pauser(doPause, doResume)
self._pauseBecauseNoDrain = OncePause(self._pauser)
self._pauseBecauseNoDrain.pauseOnce()
Expand Down Expand Up @@ -132,8 +138,11 @@ def stopFlow(self):
"""
Stop the flow of all founts flowing into L{_InDrain}s for this L{In}.
"""
for drain in self._in._drains:
drain.fount.stopFlow()
while self._in._drains:
it = self._in._drains[0]
it.fount.stopFlow()
if it in self._in._drains:
self._in._drains.remove(it)



Expand Down Expand Up @@ -462,4 +471,3 @@ def flowingFrom(self, fount):
if nextDrain is None:
return nextFount
return nextFount.flowTo(nextDrain)

25 changes: 21 additions & 4 deletions tubes/kit.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ def pause(self):
return _Pause(self)


def __repr__(self):
return (
"<Pauser pauses={!r} actuallyPause={!r} actuallyResume={!r}>"
.format(self._pauses, self._actuallyPause, self._actuallyResume)
)



def beginFlowingTo(fount, drain):
"""
Expand All @@ -96,8 +103,10 @@ def beginFlowingTo(fount, drain):
"""
oldDrain = fount.drain
fount.drain = drain
if ( (oldDrain is not None) and (oldDrain is not drain) and
(oldDrain.fount is fount) ):
if (
(oldDrain is not None) and (oldDrain is not drain) and
(oldDrain.fount is fount)
):
oldDrain.flowingFrom(None)
if drain is None:
return
Expand Down Expand Up @@ -128,8 +137,10 @@ def beginFlowingFrom(drain, fount):
fount=fount, drain=drain))
oldFount = drain.fount
drain.fount = fount
if ( (oldFount is not None) and (oldFount is not fount) and
(oldFount.drain is drain) ):
if (
(oldFount is not None) and (oldFount is not fount) and
(oldFount.drain is drain)
):
oldFount.flowTo(None)


Expand Down Expand Up @@ -159,6 +170,12 @@ def __init__(self, pauser):
self._currentlyPaused = False


def __repr__(self):
return "<OncePause pauser={!r} paused={!r}>".format(
self._pauser, self._currentlyPaused
)


def pauseOnce(self):
"""
If this L{OncePause} is not currently paused, pause its pauser.
Expand Down
3 changes: 0 additions & 3 deletions tubes/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,3 @@ def iteratorFount(iterable):
@return: a fount which will deliver the given iterable to its drain.
"""
return _NotQuiteNull().flowTo(series(_IteratorTube(iterable)))



5 changes: 5 additions & 0 deletions tubes/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ class _TransportFount(object):
drain = None
outputType = ISegment

def __repr__(self):
return "<TransportFount for {} paused={}>".format(self._transport,
self._pauser)


def __init__(self, transport):
self._transport = transport
self._pauser = Pauser(self._transport.pauseProducing,
Expand Down
9 changes: 6 additions & 3 deletions tubes/test/test_fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,14 @@ def timesThree(input):
ff = FakeFount()
fd = FakeDrain()

ff.flowTo(Thru([series(timesTwo),
series(timesThree)])).flowTo(fd)
t = Thru([series(timesTwo), series(timesThree)])
sct = series(t)
nf = ff.flowTo(sct)
self.assertEqual(ff.flowIsPaused, True)
nf.flowTo(fd)
self.assertEqual(ff.flowIsPaused, False)
ff.drain.receive(1)
ff.drain.receive(2)
ff.drain.receive(3)
self.assertEqual(fd.received,
[1*2, 1*3, 2*2, 2*3, 3*2, 3*3])

1 change: 0 additions & 1 deletion tubes/test/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,3 @@ def test_providedByNone(self):
route = router.newRoute()
self.assertEqual(False, Routed().providedBy(object()))
self.assertEqual(True, Routed().providedBy(to(route, object())))

2 changes: 0 additions & 2 deletions tubes/test/test_tube.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,5 +975,3 @@ def test_reassembleRaises(self):
If L{IDivertable.reassemble} raises an exception, then...
"""
self.fail()


4 changes: 4 additions & 0 deletions tubes/test/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ def bufferUp(self, item):
def flowTo(self, drain):
"""
Flush buffered items to the given drain as long as we're not paused.
@param drain: The drain to flush to.
@return: The result of flowing to the given drain.
"""
result = super(FakeFountWithBuffer, self).flowTo(drain)
self._go()
Expand Down

0 comments on commit eb92445

Please sign in to comment.