Commit 72852cee authored by smurail's avatar smurail Committed by Romain Bignon

let JSVar inherits from parameters of Regexp (like nth)

parent eb335e66
......@@ -20,7 +20,7 @@
import re
from import _NO_DEFAULT, Filter, Regexp, RegexpError
from import Filter, Regexp, RegexpError
from import ParseError
......@@ -78,10 +78,12 @@ class JSVar(Regexp):
'Some "string" value'
>>> JSVar(var='test').filter("var test = false;\nsomecode()")
>>> JSVar(var='test', nth=1).filter("var test = false; test = true;\nsomecode()")
pattern_template = r"""(?x)
(?:var\s+)? # optional var keyword
\b%%s # var name
\b%%s # var name
\s*=\s* # equal sign
(?:(?P<float>[-+]?\s* # float ?
......@@ -99,8 +101,8 @@ class JSVar(Regexp):
for t, v in values.iteritems():
if v is not None:
if self.of_type and t != self.of_type:
raise ParseError('Variable %r with type %s not found' % (self.var, self.of_type))
if self.need_type and t != self.need_type:
raise ParseError('Variable %r with type %s not found' % (self.var, self.need_type))
if t in ('int', 'float'):
v = self._re_spaces.sub('', v).lower()
if t == 'int':
......@@ -117,12 +119,14 @@ class JSVar(Regexp):
return self.default
raise ParseError('Unable to parse variable %r value' % self.var)
def __init__(self, selector=None, var=None, of_type=None, default=_NO_DEFAULT):
def __init__(self, selector=None, var=None, need_type=None, **kwargs):
assert var is not None, 'Please specify a var parameter'
assert 'pattern' not in kwargs, "It would be meaningless to define a pattern, use Regexp"
assert 'template' not in kwargs, "Can't use a template, use Regexp if you have to"
self.var = var
self.of_type = of_type.__name__ if type(of_type) == type else of_type
pattern = self.pattern_template % var
super(JSVar, self).__init__(selector, pattern=pattern, template=self.to_python, default=default)
self.need_type = need_type.__name__ if type(need_type) == type else need_type
pattern = self.pattern_template % re.escape(var)
super(JSVar, self).__init__(selector, pattern=pattern, template=self.to_python, **kwargs)
def filter(self, txt):
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