Commit 7a058601 authored by Sebastien Robin's avatar Sebastien Robin

Add the possibility to mix code using sequence with code not

using sequences

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@31163 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent ba31ea0f
...@@ -97,19 +97,23 @@ class Step: ...@@ -97,19 +97,23 @@ class Step:
class Sequence: class Sequence:
def __init__(self): def __init__(self, context=None):
self._step_list = [] self._step_list = []
self._dict = {} self._dict = {}
self._played_index = 0
self._context = context
def play(self, context, sequence=None, sequence_number=0, quiet=0): def play(self, context, sequence=None, sequence_number=0, quiet=0):
if not quiet: if not quiet:
if self._played_index == 0:
ZopeTestCase._print('\nStarting New Sequence %i... ' % sequence_number) ZopeTestCase._print('\nStarting New Sequence %i... ' % sequence_number)
LOG('Sequence.play', 0, 'Starting New Sequence %i... ' % sequence_number) LOG('Sequence.play', 0, 'Starting New Sequence %i... ' % sequence_number)
if sequence is None: if sequence is None:
for idx, step in enumerate(self._step_list): for idx, step in enumerate(self._step_list[self._played_index:]):
step.play(context, sequence=self, quiet=quiet) step.play(context, sequence=self, quiet=quiet)
# commit transaction after each step # commit transaction after each step
transaction.commit() transaction.commit()
self._played_index = len(self._step_list)
def addStep(self,method_name,required=1,max_replay=1): def addStep(self,method_name,required=1,max_replay=1):
new_step = Step(method_name=method_name, new_step = Step(method_name=method_name,
...@@ -128,6 +132,32 @@ class Sequence: ...@@ -128,6 +132,32 @@ class Sequence:
def setdefault(self, key, default=None): def setdefault(self, key, default=None):
return self._dict.setdefault(key, default) return self._dict.setdefault(key, default)
def __call__(self, sequence_string, sequence_number=0, quiet=0):
"""
add some steps and directly runs them, this allows to easily write
such code when sequence are unable to handle too complex cases :
sequence('CreateFoo Tic')
[some code not using sequences]
sequence('CreateBar Tic')
"""
self.setSequenceString(sequence_string)
if self._context is None:
raise ValueError('context must be initialized when sequence directly called')
self.play(self._context, sequence_number=sequence_number, quiet=quiet)
def setSequenceString(self, sequence_string):
sequence_string = re.subn("#.*\n", "\n", sequence_string)[0]
step_list = sequence_string.split()
self.setSequenceStringList(step_list)
def setSequenceStringList(self, step_list):
for step in step_list:
if step != '':
if step.startswith('step'):
step = step[4:]
self.addStep(step)
class SequenceList: class SequenceList:
def __init__(self): def __init__(self):
...@@ -144,18 +174,14 @@ class SequenceList: ...@@ -144,18 +174,14 @@ class SequenceList:
returns the sequence for those steps. returns the sequence for those steps.
""" """
# remove comments in sequence strings # remove comments in sequence strings
sequence_string = re.subn("#.*\n", "\n", sequence_string)[0] sequence = Sequence()
step_list = sequence_string.split() sequence.setSequenceString(sequence_string)
return self.addSequenceStringList(step_list) self.addSequence(sequence)
return sequence
def addSequenceStringList(self, step_list): def addSequenceStringList(self, step_list):
step_list
sequence = Sequence() sequence = Sequence()
for step in step_list: sequence.setSequenceStringList(step_list)
if step != '':
if step.startswith('step'):
step = step[4:]
sequence.addStep(step)
self.addSequence(sequence) self.addSequence(sequence)
return sequence return sequence
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment