lazy parse scripts
This commit is contained in:
parent
80b65ae7e7
commit
9281bdb61b
1 changed files with 20 additions and 9 deletions
|
@ -284,19 +284,30 @@ class Template:
|
||||||
|
|
||||||
class Script:
|
class Script:
|
||||||
|
|
||||||
__slots__ = 'source', 'template', 'values'
|
__slots__ = 'source', '_template', '_values', '_template_hint'
|
||||||
|
|
||||||
templates: List[Template] = []
|
templates: List[Template] = []
|
||||||
|
|
||||||
def __init__(self, source=None, template=None, values=None, template_hint=None):
|
def __init__(self, source=None, template=None, values=None, template_hint=None):
|
||||||
self.source = source
|
self.source = source
|
||||||
self.template = template
|
self._template = template
|
||||||
self.values = values
|
self._values = values
|
||||||
if source:
|
self._template_hint = template_hint
|
||||||
self.parse(template_hint)
|
if source is None and template and values:
|
||||||
elif template and values:
|
|
||||||
self.generate()
|
self.generate()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def template(self):
|
||||||
|
if self._template is None:
|
||||||
|
self.parse(self._template_hint)
|
||||||
|
return self._template
|
||||||
|
|
||||||
|
@property
|
||||||
|
def values(self):
|
||||||
|
if self._values is None:
|
||||||
|
self.parse(self._template_hint)
|
||||||
|
return self._values
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def tokens(self):
|
def tokens(self):
|
||||||
return tokenize(BCDataStream(self.source))
|
return tokenize(BCDataStream(self.source))
|
||||||
|
@ -311,15 +322,15 @@ class Script:
|
||||||
if not template:
|
if not template:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
self.values = template.parse(tokens)
|
self._values = template.parse(tokens)
|
||||||
self.template = template
|
self._template = template
|
||||||
return
|
return
|
||||||
except ParseError:
|
except ParseError:
|
||||||
continue
|
continue
|
||||||
raise ValueError('No matching templates for source: {}'.format(hexlify(self.source)))
|
raise ValueError('No matching templates for source: {}'.format(hexlify(self.source)))
|
||||||
|
|
||||||
def generate(self):
|
def generate(self):
|
||||||
self.source = self.template.generate(self.values)
|
self.source = self.template.generate(self._values)
|
||||||
|
|
||||||
|
|
||||||
class BaseInputScript(Script):
|
class BaseInputScript(Script):
|
||||||
|
|
Loading…
Reference in a new issue