Skip to content

Commit

Permalink
Do not decode captured output
Browse files Browse the repository at this point in the history
Pass it as bytes to the client, he knows how to decode it
  • Loading branch information
jacquev6 committed Jul 19, 2017
1 parent 8acb128 commit c7c45d4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 30 deletions.
20 changes: 9 additions & 11 deletions ActionTree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(self, label, dependencies=[], resources_required={}, accept_failed_
"""
:param label: A string used to represent the action in :class:`GanttChart` and
:class:`DependencyGraph`. Can be retrieved by :attr:`label`.
:type label: string or None
:type label: str or None
:param list(Action) dependencies:
see :meth:`~.Action.add_dependency`
:param resources_required:
Expand Down Expand Up @@ -249,13 +249,13 @@ def action_started(self, time, action):
:param Action action: the action.
"""

def action_printed(self, time, action, text):
def action_printed(self, time, action, data):
"""
Called when an action prints something.
:param datetime.datetime time: the time at which the action printed the text.
:param datetime.datetime time: the time at which the action printed the data.
:param Action action: the action.
:param str text: the text printed.
:param str data: the data printed.
"""

def action_successful(self, time, action, return_value):
Expand Down Expand Up @@ -346,15 +346,15 @@ def _set_start_time(self, start_time):
def _set_success(self, success_time, return_value):
self.__success_time = success_time
self.__return_value = return_value
self._add_output("")
self._add_output(b"")

def _set_failure(self, failure_time, exception):
self.__failure_time = failure_time
self.__exception = exception
self._add_output("")
self._add_output(b"")

def _add_output(self, output):
self.__output = (self.__output or "") + output
self.__output = (self.__output or b"") + output

@property
def status(self):
Expand Down Expand Up @@ -958,10 +958,8 @@ def _handle_successful_event(self, action, success_time, return_value):
self._deallocate_resources(action)

def _handle_printed_event(self, action, print_time, data):
text = data.decode("utf8") # @todo DO NOT decode.
# We have no way to know the encoding used. Give bytes back to client and let them decode.
self.report.get_action_status(action)._add_output(text)
self.hooks.action_printed(print_time, action, text)
self.report.get_action_status(action)._add_output(data)
self.hooks.action_printed(print_time, action, data)

def _handle_failed_event(self, action, failure_time, exception):
self.report.get_action_status(action)._set_failure(failure_time, exception)
Expand Down
16 changes: 8 additions & 8 deletions ActionTree/tests/capture_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ def test_successful_nothing(self):
a = self._action("a")
report = execute(a)

self.assertEqual(report.get_action_status(a).output, "")
self.assertEqual(report.get_action_status(a).output, b"")

def test_failed_nothing(self):
a = self._action("a", exception=Exception())
report = execute(a, do_raise=False)

self.assertEqual(report.get_action_status(a).output, "")
self.assertEqual(report.get_action_status(a).output, b"")

def test_canceled_nothing(self):
a = self._action("a")
Expand All @@ -32,25 +32,25 @@ def test_print(self):
a = self._action("a", print_on_stdout="printed on stdout")
report = execute(a)

self.assertEqual(report.get_action_status(a).output, "printed on stdout\n")
self.assertEqual(report.get_action_status(a).output, b"printed on stdout\n")

def test_print_stderr(self):
a = self._action("a", print_on_stderr="printed on stderr")
report = execute(a)

self.assertEqual(report.get_action_status(a).output, "printed on stderr\n")
self.assertEqual(report.get_action_status(a).output, b"printed on stderr\n")

def test_echo(self):
a = self._action("a", echo_on_stdout="echoed on stdout")
report = execute(a)

self.assertEqual(report.get_action_status(a).output, "echoed on stdout\n")
self.assertEqual(report.get_action_status(a).output, b"echoed on stdout\n")

def test_puts(self):
a = self._action("a", puts_on_stdout=b"putsed on stdout")
report = execute(a)

self.assertEqual(report.get_action_status(a).output, "putsed on stdout\n")
self.assertEqual(report.get_action_status(a).output, b"putsed on stdout\n")

def test_many_print(self):
MANY = 5
Expand All @@ -61,5 +61,5 @@ def test_many_print(self):
a.add_dependency(y)
report = execute(a)

self.assertEqual(report.get_action_status(x).output, "x\n" * MANY)
self.assertEqual(report.get_action_status(y).output, "y\n" * MANY)
self.assertEqual(report.get_action_status(x).output, b"x\n" * MANY)
self.assertEqual(report.get_action_status(y).output, b"y\n" * MANY)
10 changes: 5 additions & 5 deletions ActionTree/tests/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_successful_action_print(self):
("pending", "a"),
("ready", "a"),
("started", "a"),
("printed", "a", "something\n"),
("printed", "a", b"something\n"),
("successful", "a", None),
]
)
Expand All @@ -115,7 +115,7 @@ def test_failed_action_print(self):
("pending", "a"),
("ready", "a"),
("started", "a"),
("printed", "a", "something\n"),
("printed", "a", b"something\n"),
("failed", "a", "foo"),
]
)
Expand All @@ -131,9 +131,9 @@ def test_print_several_times(self):
("pending", "a"),
("ready", "a"),
("started", "a"),
("printed", "a", "something 1\n"),
("printed", "a", "something 2\n"),
("printed", "a", "something 3\n"),
("printed", "a", b"something 1\n"),
("printed", "a", b"something 2\n"),
("printed", "a", b"something 3\n"),
("successful", "a", None),
]
)
Expand Down
10 changes: 5 additions & 5 deletions docs/reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ <h1>Reference<a class="headerlink" href="#reference" title="Permalink to this he
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>label</strong> (<a class="reference external" href="https://docs.python.org/2/library/string.html#module-string" title="(in Python v2.7)"><em>string</em></a><em> or </em><a class="reference external" href="https://docs.python.org/2/library/constants.html#None" title="(in Python v2.7)"><em>None</em></a>) – A string used to represent the action in <a class="reference internal" href="#ActionTree.GanttChart" title="ActionTree.GanttChart"><code class="xref py py-class docutils literal"><span class="pre">GanttChart</span></code></a> and
<li><strong>label</strong> (<a class="reference external" href="https://docs.python.org/2/library/functions.html#str" title="(in Python v2.7)"><em>str</em></a><em> or </em><a class="reference external" href="https://docs.python.org/2/library/constants.html#None" title="(in Python v2.7)"><em>None</em></a>) – A string used to represent the action in <a class="reference internal" href="#ActionTree.GanttChart" title="ActionTree.GanttChart"><code class="xref py py-class docutils literal"><span class="pre">GanttChart</span></code></a> and
<a class="reference internal" href="#ActionTree.DependencyGraph" title="ActionTree.DependencyGraph"><code class="xref py py-class docutils literal"><span class="pre">DependencyGraph</span></code></a>. Can be retrieved by <a class="reference internal" href="#ActionTree.Action.label" title="ActionTree.Action.label"><code class="xref py py-attr docutils literal"><span class="pre">label</span></code></a>.</li>
<li><strong>dependencies</strong> (<em>list</em><em>(</em><a class="reference internal" href="#ActionTree.Action" title="ActionTree.Action"><em>Action</em></a><em>)</em>) – see <a class="reference internal" href="#ActionTree.Action.add_dependency" title="ActionTree.Action.add_dependency"><code class="xref py py-meth docutils literal"><span class="pre">add_dependency()</span></code></a></li>
<li><strong>resources_required</strong> (<a class="reference external" href="https://docs.python.org/2/library/stdtypes.html#dict" title="(in Python v2.7)"><em>dict</em></a><em>(</em><a class="reference internal" href="#ActionTree.Resource" title="ActionTree.Resource"><em>Resource</em></a><em>, </em><a class="reference external" href="https://docs.python.org/2/library/functions.html#int" title="(in Python v2.7)"><em>int</em></a><em>)</em>) – see <a class="reference internal" href="#ActionTree.Action.require_resource" title="ActionTree.Action.require_resource"><code class="xref py py-meth docutils literal"><span class="pre">require_resource()</span></code></a></li>
Expand Down Expand Up @@ -314,16 +314,16 @@ <h1>Reference<a class="headerlink" href="#reference" title="Permalink to this he

<dl class="method">
<dt id="ActionTree.Hooks.action_printed">
<code class="descname">action_printed</code><span class="sig-paren">(</span><em>time</em>, <em>action</em>, <em>text</em><span class="sig-paren">)</span><a class="headerlink" href="#ActionTree.Hooks.action_printed" title="Permalink to this definition"></a></dt>
<code class="descname">action_printed</code><span class="sig-paren">(</span><em>time</em>, <em>action</em>, <em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#ActionTree.Hooks.action_printed" title="Permalink to this definition"></a></dt>
<dd><p>Called when an action prints something.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>time</strong> (<a class="reference external" href="https://docs.python.org/2/library/datetime.html#datetime.datetime" title="(in Python v2.7)"><em>datetime.datetime</em></a>) – the time at which the action printed the text.</li>
<li><strong>time</strong> (<a class="reference external" href="https://docs.python.org/2/library/datetime.html#datetime.datetime" title="(in Python v2.7)"><em>datetime.datetime</em></a>) – the time at which the action printed the data.</li>
<li><strong>action</strong> (<a class="reference internal" href="#ActionTree.Action" title="ActionTree.Action"><em>Action</em></a>) – the action.</li>
<li><strong>text</strong> (<a class="reference external" href="https://docs.python.org/2/library/functions.html#str" title="(in Python v2.7)"><em>str</em></a>) – the text printed.</li>
<li><strong>data</strong> (<a class="reference external" href="https://docs.python.org/2/library/functions.html#str" title="(in Python v2.7)"><em>str</em></a>) – the data printed.</li>
</ul>
</td>
</tr>
Expand Down Expand Up @@ -610,7 +610,7 @@ <h1>Reference<a class="headerlink" href="#reference" title="Permalink to this he
<dl class="method">
<dt id="ActionTree.DependencyGraph.get_graphviz_graph">
<code class="descname">get_graphviz_graph</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#ActionTree.DependencyGraph.get_graphviz_graph" title="Permalink to this definition"></a></dt>
<dd><p>Return a <a class="reference external" href="http://graphviz.readthedocs.io/en/stable/api.html#graphviz.Digraph" title="(in graphviz v0.7.1)"><code class="xref py py-class docutils literal"><span class="pre">graphviz.Digraph</span></code></a> of this dependency graph.</p>
<dd><p>Return a <a class="reference external" href="http://graphviz.readthedocs.io/en/stable/api.html#graphviz.Digraph" title="(in graphviz v0.8)"><code class="xref py py-class docutils literal"><span class="pre">graphviz.Digraph</span></code></a> of this dependency graph.</p>
<p>See also <a class="reference internal" href="#ActionTree.DependencyGraph.write_to_png" title="ActionTree.DependencyGraph.write_to_png"><code class="xref py py-meth docutils literal"><span class="pre">write_to_png()</span></code></a> for the simplest use-case.</p>
</dd></dl>

Expand Down

0 comments on commit c7c45d4

Please sign in to comment.