Class representing a pipeline template.
| 83 | |
| 84 | |
| 85 | class Template: |
| 86 | """Class representing a pipeline template.""" |
| 87 | |
| 88 | def __init__(self): |
| 89 | """Template() returns a fresh pipeline template.""" |
| 90 | self.debugging = 0 |
| 91 | self.reset() |
| 92 | |
| 93 | def __repr__(self): |
| 94 | """t.__repr__() implements repr(t).""" |
| 95 | return '<Template instance, steps=%r>' % (self.steps,) |
| 96 | |
| 97 | def reset(self): |
| 98 | """t.reset() restores a pipeline template to its initial state.""" |
| 99 | self.steps = [] |
| 100 | |
| 101 | def clone(self): |
| 102 | """t.clone() returns a new pipeline template with identical |
| 103 | initial state as the current one.""" |
| 104 | t = Template() |
| 105 | t.steps = self.steps[:] |
| 106 | t.debugging = self.debugging |
| 107 | return t |
| 108 | |
| 109 | def debug(self, flag): |
| 110 | """t.debug(flag) turns debugging on or off.""" |
| 111 | self.debugging = flag |
| 112 | |
| 113 | def append(self, cmd, kind): |
| 114 | """t.append(cmd, kind) adds a new step at the end.""" |
| 115 | if not isinstance(cmd, str): |
| 116 | raise TypeError('Template.append: cmd must be a string') |
| 117 | if kind not in stepkinds: |
| 118 | raise ValueError('Template.append: bad kind %r' % (kind,)) |
| 119 | if kind == SOURCE: |
| 120 | raise ValueError('Template.append: SOURCE can only be prepended') |
| 121 | if self.steps and self.steps[-1][1] == SINK: |
| 122 | raise ValueError('Template.append: already ends with SINK') |
| 123 | if kind[0] == 'f' and not re.search(r'\$IN\b', cmd): |
| 124 | raise ValueError('Template.append: missing $IN in cmd') |
| 125 | if kind[1] == 'f' and not re.search(r'\$OUT\b', cmd): |
| 126 | raise ValueError('Template.append: missing $OUT in cmd') |
| 127 | self.steps.append((cmd, kind)) |
| 128 | |
| 129 | def prepend(self, cmd, kind): |
| 130 | """t.prepend(cmd, kind) adds a new step at the front.""" |
| 131 | if not isinstance(cmd, str): |
| 132 | raise TypeError('Template.prepend: cmd must be a string') |
| 133 | if kind not in stepkinds: |
| 134 | raise ValueError('Template.prepend: bad kind %r' % (kind,)) |
| 135 | if kind == SINK: |
| 136 | raise ValueError('Template.prepend: SINK can only be appended') |
| 137 | if self.steps and self.steps[0][1] == SOURCE: |
| 138 | raise ValueError('Template.prepend: already begins with SOURCE') |
| 139 | if kind[0] == 'f' and not re.search(r'\$IN\b', cmd): |
| 140 | raise ValueError('Template.prepend: missing $IN in cmd') |
| 141 | if kind[1] == 'f' and not re.search(r'\$OUT\b', cmd): |
| 142 | raise ValueError('Template.prepend: missing $OUT in cmd') |
no outgoing calls
no test coverage detected