Commit 2c929ad0 by Gabriel Falcao

compiling some regexes once and reusing later

parent 4a78b0b5
...@@ -34,6 +34,18 @@ from lettuce.exceptions import LettuceSyntaxError ...@@ -34,6 +34,18 @@ from lettuce.exceptions import LettuceSyntaxError
fs = FileSystem() fs = FileSystem()
class REP(object):
"RegEx Pattern"
first_of = re.compile(ur'^first_of_')
last_of = re.compile(ur'^last_of_')
language = re.compile(u"language:[ ]*([^\s]+)")
within_double_quotes = re.compile(r'("[^"]+")')
within_single_quotes = re.compile(r"('[^']+')")
only_whitespace = re.compile('^\s*$')
tag_extraction_regex = re.compile(r'(?:(?:^|\s+)[@]([^@\s]+))')
tag_strip_regex = re.compile(ur'(?:(?:^\s*|\s+)[@]\S+\s*)+$', re.DOTALL)
class HashList(list): class HashList(list):
__base_msg = 'The step "%s" have no table defined, so ' \ __base_msg = 'The step "%s" have no table defined, so ' \
'that you can\'t use step.hashes.%s' 'that you can\'t use step.hashes.%s'
...@@ -86,9 +98,9 @@ class Language(object): ...@@ -86,9 +98,9 @@ class Language(object):
return '<Language "%s">' % self.code return '<Language "%s">' % self.code
def __getattr__(self, attr): def __getattr__(self, attr):
for pattern in [ur'^first_of_', ur'^last_of']: for pattern in [REP.first_of, REP.last_of]:
if re.match(pattern, attr): if pattern.match(attr):
name = re.sub(pattern, u'', attr) name = pattern.sub(u'', attr)
return unicode(getattr(self, name, u'').split(u"|")[0]) return unicode(getattr(self, name, u'').split(u"|")[0])
return super(Language, self).__getattribute__(attr) return super(Language, self).__getattribute__(attr)
...@@ -99,7 +111,7 @@ class Language(object): ...@@ -99,7 +111,7 @@ class Language(object):
@classmethod @classmethod
def guess_from_string(cls, string): def guess_from_string(cls, string):
match = re.search(u"language:[ ]*([^\s]+)", string) match = re.search(REP.language, string)
if match: if match:
instance = cls(match.group(1)) instance = cls(match.group(1))
else: else:
...@@ -209,8 +221,8 @@ class Step(object): ...@@ -209,8 +221,8 @@ class Step(object):
method_name = sentence method_name = sentence
groups = [ groups = [
('"', re.compile(r'("[^"]+")'), r'"([^"]*)"'), ('"', REP.within_double_quotes, r'"([^"]*)"'),
("'", re.compile(r"('[^']+')"), r"\'([^\']*)\'"), ("'", REP.within_single_quotes, r"\'([^\']*)\'"),
] ]
attribute_names = [] attribute_names = []
...@@ -457,8 +469,7 @@ class Step(object): ...@@ -457,8 +469,7 @@ class Step(object):
invalid_first_line_error % (lines[0], 'multiline')) invalid_first_line_error % (lines[0], 'multiline'))
# Select only lines that aren't end-to-end whitespace # Select only lines that aren't end-to-end whitespace
only_whitspace = re.compile('^\s*$') lines = filter(lambda x: not REP.only_whitespace.match(x), lines)
lines = filter(lambda x: not only_whitspace.match(x), lines)
step_strings = [] step_strings = []
in_multiline = False in_multiline = False
...@@ -724,9 +735,7 @@ class Scenario(object): ...@@ -724,9 +735,7 @@ class Scenario(object):
return [] return []
def _extract_tag(self, item): def _extract_tag(self, item):
regex = re.compile(r'(?:(?:^|\s+)[@]([^@\s]+))') return REP.tag_extraction_regex.findall(item)
found = regex.findall(item)
return found
def _resolve_steps(self, steps, outlines, with_file, original_string): def _resolve_steps(self, steps, outlines, with_file, original_string):
for outline in outlines: for outline in outlines:
...@@ -937,9 +946,7 @@ class Feature(object): ...@@ -937,9 +946,7 @@ class Feature(object):
self.described_at = definition self.described_at = definition
def _strip_next_scenario_tags(self, string): def _strip_next_scenario_tags(self, string):
regex = re.compile(ur'(?:(?:^\s*|\s+)[@]\S+\s*)+$', re.DOTALL) stripped = REP.tag_strip_regex.sub('', string)
stripped = regex.sub('', string)
return stripped return stripped
def _parse_remaining_lines(self, lines, original_string, with_file=None): def _parse_remaining_lines(self, lines, original_string, with_file=None):
......
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