/usr/share/pyshared/freshen/parser.py is in python-freshen 0.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | #-*- coding: utf8 -*-
# This line ensures that frames from this file will not be shown in tracebacks
__unittest = 1
from pyparsing import *
import copy
import logging
import os
import re
import textwrap
try:
from os.path import relpath
except Exception, e:
from freshen.compat import relpath
log = logging.getLogger('freshen')
class Feature(object):
def __init__(self, use_step_defs, tags, name, description, background, scenarios):
self.use_step_defs = use_step_defs
self.tags = tags
self.name = name
self.description = description
self.scenarios = scenarios
self.background = None
if background != []:
self.background = background[0]
for sc in scenarios:
sc.feature = self
sc.background = self.background
def __repr__(self):
return '<Feature "%s": %d scenario(s)>' % (self.name, len(self.scenarios))
def has_background(self):
return self.background is not None
def iter_scenarios(self):
for sco in self.scenarios:
for sc in sco.iterate():
yield sc
class Background(object):
def __init__(self, name, steps):
self.name = name
self.steps = steps
def __repr__(self):
return '<Background "%s">' % self.name
def iter_steps(self):
for step in self.steps:
yield step
class Scenario(object):
def __init__(self, tags, name, steps):
self.tags = tags
self.name = name
self.steps = steps
self.background = None
def __repr__(self):
return '<Scenario "%s">' % self.name
def get_tags(self):
return self.tags + self.feature.tags
def iterate(self):
yield self
def iter_steps(self):
if self.background is not None:
for step in self.background.iter_steps():
yield step
for step in self.steps:
yield step
class ScenarioOutline(Scenario):
def __init__(self, tags, name, steps, examples):
self.examples = examples
super(ScenarioOutline, self).__init__(tags, name, steps)
def __repr__(self):
return '<ScenarioOutline "%s">' % self.name
def iterate(self):
for ex in self.examples:
for values in ex.table.iterrows():
new_steps = []
for step in self.steps:
new_steps.append(step.set_values(values))
sc = Scenario(self.tags, self.name, new_steps)
sc.feature = self.feature
sc.background = self.background
yield sc
class Step(object):
def __init__(self, step_type, match, arg=None):
self.step_type_native, self.step_type = step_type
self.match = match
self.arg = arg
def __repr__(self):
return '<%s "%s">' % (self.step_type, self.match)
def source_location(self, absolute=True):
p = relpath(self.src_file, os.getcwd()) if absolute else self.src_file
return '%s:%d' % (p, self.src_line)
def set_values(self, value_dict):
result = copy.deepcopy(self)
for name, value in value_dict.iteritems():
result.match = result.match.replace("<%s>" % name, value)
return result
class Examples(object):
def __init__(self, name, table):
self.name = name
self.table = table
class Table(object):
def __init__(self, headings, rows):
assert [len(r) == len(headings) for r in rows], "Malformed table"
self.headings = headings
self.rows = rows
def __repr__(self):
return "<Table: %dx%d>" % (len(self.headings), len(self.rows))
def iterrows(self):
for row in self.rows:
yield dict(zip(self.headings, row))
def grammar(fname, l, convert=True, base_line=0):
# l = language
def create_object(klass):
def untokenize(s, loc, toks):
result = []
for t in toks:
if isinstance(t, ParseResults):
t = t.asList()
result.append(t)
obj = klass(*result)
obj.src_file = fname
obj.src_line = base_line + lineno(loc, s)
return obj
return untokenize
def process_descr(s):
return [p.strip() for p in s[0].strip().split("\n")]
# This has to be an array for compatibility with Python versions which do not have "nonlocal"
last_step_type = [None]
def process_given_step(s):
last_step_type[0] = 'given'
return (s[0], 'given')
def process_when_step(s):
last_step_type[0] = 'when'
return (s[0], 'when')
def process_then_step(s):
last_step_type[0] = 'then'
return (s[0], 'then')
def process_and_but_step(orig, loc, s):
if last_step_type[0] == None:
raise ParseFatalException(orig, loc,
"'And' or 'But' steps can only come after 'Given', 'When', or 'Then'")
return (s[0], last_step_type[0])
def process_string(s):
return s[0].strip()
def process_m_string(s):
return textwrap.dedent(s[0])
def process_tag(s):
return s[0].strip("@")
def or_words(words, kind, suffix='', parse_acts=None):
elements = []
for index, native_word in enumerate(words):
for word in l.words(native_word):
element = kind(word + suffix)
if parse_acts is not None:
element.setParseAction(parse_acts[index])
elements.append(element)
return Or(elements)
empty_not_n = empty.copy().setWhitespaceChars(" \t")
tags = OneOrMore(Word("@", alphanums + "_").setParseAction(process_tag))
step_file = quotedString.setParseAction( removeQuotes )
list_of_step_files = step_file + ZeroOrMore(Suppress(',') + step_file)
use_step_defs = or_words(['use_step_defs'], Suppress, ':') + list_of_step_files
following_text = empty_not_n + restOfLine + Suppress(lineEnd)
section_header = lambda name: Suppress(name + ":") + following_text
section_name = or_words(['scenario', 'scenario_outline', 'background'], Literal)
descr_block = Group(SkipTo(section_name | tags).setParseAction(process_descr))
table_row = Group(Suppress("|") +
delimitedList(Suppress(empty_not_n) +
CharsNotIn("|\n").setParseAction(process_string) +
Suppress(empty_not_n), delim="|") +
Suppress("|"))
table = table_row + Group(OneOrMore(table_row))
m_string = (Suppress(Literal('"""') + lineEnd).setWhitespaceChars(" \t") +
SkipTo((lineEnd +
Literal('"""')).setWhitespaceChars(" \t")).setWhitespaceChars("") +
Suppress('"""'))
m_string.setParseAction(process_m_string)
step_name = or_words(['given', 'when', 'then', 'and', 'but'], Keyword,
parse_acts=[process_given_step, process_when_step, process_then_step,
process_and_but_step, process_and_but_step])
step = step_name + following_text + Optional(table | m_string)
steps = Group(ZeroOrMore(step))
example = or_words(['examples'], section_header) + table
background = or_words(['background'], section_header) + steps
scenario = Group(Optional(tags)) + or_words(['scenario'], section_header) + steps
scenario_outline = Group(Optional(tags)) + or_words(['scenario_outline'], section_header) + steps + Group(OneOrMore(example))
feature = (Group(Optional(use_step_defs)) +
Group(Optional(tags)) +
or_words(['feature'], section_header) +
descr_block +
Group(Optional(background)) +
Group(OneOrMore(scenario | scenario_outline)))
# Ignore tags for now as they are not supported
feature.ignore(pythonStyleComment)
steps.ignore(pythonStyleComment)
if convert:
table.setParseAction(create_object(Table))
step.setParseAction(create_object(Step))
background.setParseAction(create_object(Background))
scenario.setParseAction(create_object(Scenario))
scenario_outline.setParseAction(create_object(ScenarioOutline))
example.setParseAction(create_object(Examples))
feature.setParseAction(create_object(Feature))
return feature, steps
def parse_file(fname, language, convert=True):
feature, _ = grammar(fname, language, convert)
if convert:
return feature.parseFile(fname)[0]
else:
return feature.parseFile(fname)
def parse_steps(spec, fname, base_line, language, convert=True):
_, steps = grammar(fname, language, convert, base_line)
if convert:
return steps.parseString(spec)[0]
else:
return steps.parseString(spec)
|