Skip to content

Commit

Permalink
Merge branch 'develop' for release.
Browse files Browse the repository at this point in the history
  • Loading branch information
StyXman committed Oct 17, 2017
2 parents 2ba5871 + 3e88f47 commit e900aad
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ doesn't need much maintenance. Still, any issues will be treated with due
diligency.

`dinant` is an attempt, like may others, to make regular expressions more
readable, and, like many other, fails miserably... but we try anyways.
readable and, like many others, fails miserably... but we try anyways.

You can find many examples in the source file, which includes unit tests to make
sure we don't make things worse. Because it's implementation is currently very,
Expand Down
46 changes: 40 additions & 6 deletions dinant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import sys
import copy
from functools import partial

class Dinant:
# TODO: *others, should help fixing either()
Expand Down Expand Up @@ -75,12 +76,33 @@ def __getitem__(self, index):
return self.expression[index]


def match(self, s):
def match(self, s, debug=False):
"""For compatibility with the `re` module."""
if self.compiled is None:
self.compiled = re.compile(str(self))
if not debug:
if self.compiled is None:
self.compiled = re.compile(str(self))

return self.compiled.match(s)
return self.compiled.match(s)
else:
so_far = ''
syntax_error = None

for string in self.strings:
so_far += string
try:
compiled = re.compile(so_far)
except re.error as e:
syntax_error = e
else:
syntax_error = None
if not compiled.match(s):
return so_far

if syntax_error is not None:
raise syntax_error
else:
# it matched
return True


def __eq__(self, other):
Expand Down Expand Up @@ -174,8 +196,8 @@ def maybe(s, greedy=True):

return result

def then(s):
return Dinant(s)
then = Dinant
text = Dinant

bol = Dinant('^', escape=False)
eol = Dinant('$', escape=False)
Expand Down Expand Up @@ -205,6 +227,9 @@ def between(m, n, s, greedy=True):
hex = one_or_more(any_of('0-9A-Fa-f'))
hexa = hex

# fallback
regexp = partial(Dinant, escape=False)

# NOTE: none of these regexps do any value checking (%H between 00-23, etc)
__dt_format_to_re = {
'%a': one_or_more(anything, greedy=False), # TODO: this is not really specific
Expand Down Expand Up @@ -392,6 +417,15 @@ def call_id_re(capt=True, name=None):

test(call_re, line, ('[Apr 27 07:01:27] VERBOSE[4023][C-0005da36] chan_sip.c: [Apr 27 07:01:27] ', 'Apr 27 07:01:27'))

identifier_re = one_or_more(any_of('A-Za-z0-9-'))
line = """36569.12ms (cpu 35251.71ms) | rendering style for layer: 'terrain-small' and style 'terrain-small'"""
render_time_re = ( bol + capture(float, name='wall_time') + 'ms ' +
'(cpu' + capture(float, name='cpu_time') + 'ms)' + one_or_more(' ') + '| ' +
"rendering style for layer: '" + capture(identifier_re, name='layer') + "' " +
"and style '" + capture(identifier_re, name='style') + "'" + eol )
render_time_partial_match_re = ( bol + capture(float, name='wall_time') + 'ms ' +
'(cpu' + capture(float, name='cpu_time') )
ass(render_time_re.match(line, debug=True), str(render_time_partial_match_re))

print('A-OK!')

Expand Down

0 comments on commit e900aad

Please sign in to comment.