Skip to content

Commit 62d8fa2

Browse files
committed
Add ability to track visits thru the magic variable $visits_{node}
1 parent 8daaf1e commit 62d8fa2

5 files changed

Lines changed: 66 additions & 1 deletion

File tree

examples/visits.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
id,text,file,node,lineNumber
2+
visits-loop-0,This is the loop node.,visits,loop,8
3+
visits-end-1,This is the end.,visits,end,19

examples/visits.yarn

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
title: Start
2+
---
3+
[[loop]]
4+
===
5+
title: loop
6+
---
7+
8+
This is the loop node.
9+
10+
<<if $visits_loop > 3>>
11+
[[end]]
12+
<<endif>>
13+
14+
[[loop]]
15+
===
16+
title: end
17+
---
18+
19+
This is the end.
20+
===

examples/visits.yarnc

299 Bytes
Binary file not shown.

tests/test_visits.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
from .context import YarnRunner
3+
4+
compiled_yarn_f = open(os.path.join(os.path.dirname(
5+
__file__), '../examples/visits.yarnc'), 'rb')
6+
names_csv_f = open(os.path.join(os.path.dirname(
7+
__file__), '../examples/visits.csv'), 'r')
8+
9+
runner = YarnRunner(compiled_yarn_f, names_csv_f)
10+
11+
12+
def test_visits():
13+
assert "This is the loop node." == runner.get_line()
14+
assert runner.has_line()
15+
assert "This is the loop node." == runner.get_line()
16+
assert runner.has_line()
17+
assert "This is the loop node." == runner.get_line()
18+
assert runner.has_line()
19+
assert "This is the loop node." == runner.get_line()
20+
assert runner.has_line()
21+
assert "This is the end." == runner.get_line()
22+
assert not runner.has_line()
23+
assert runner.finished
24+
assert runner.visits['loop'] == 4
25+
assert runner.visits['Start'] == 1
26+
assert runner.visits['end'] == 1

yarnrunner_python/runner.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import csv
2+
import re
23
from warnings import warn
34
from .yarn_spinner_pb2 import Program as YarnProgram, Instruction
45
from .vm_std_lib import functions as std_lib_functions
@@ -210,8 +211,23 @@ def __call_func(self, instruction):
210211
self._vm_data_stack.insert(0, ret)
211212

212213
def __push_variable(self, instruction):
214+
variable_name = instruction.operands[0].string_value
215+
216+
match = re.search(r"\$visits_([a-zA-Z\_0-9]+)", variable_name)
217+
if match:
218+
node_name = match.group(1)
219+
if node_name not in self.visits:
220+
visits = 0
221+
else:
222+
visits = self.visits[node_name]
223+
self._vm_data_stack.insert(0, visits)
224+
return
225+
226+
if variable_name not in self.variables:
227+
raise Exception(f"Variable {variable_name} has not been set.")
228+
213229
self._vm_data_stack.insert(
214-
0, self.variables[instruction.operands[0].string_value])
230+
0, self.variables[variable_name])
215231

216232
def __store_variable(self, instruction):
217233
self.variables[instruction.operands[0].string_value] = self._vm_data_stack[0]

0 commit comments

Comments
 (0)