def handle_if(self, span, cond):
'''Called when parser encounters an if directive.
It is a dummy method and should be overriden for actual use.
Args:
span (tuple of int): Start and end line of the directive.
cond (str): String representation of the branching condition.
'''
self._log_event('if', span, condition=cond)
python类str()的实例源码
def handle_elif(self, span, cond):
'''Called when parser encounters an elif directive.
It is a dummy method and should be overriden for actual use.
Args:
span (tuple of int): Start and end line of the directive.
cond (str): String representation of the branching condition.
'''
self._log_event('elif', span, condition=cond)
def handle_call(self, span, name, argexpr):
'''Called when parser encounters a call directive.
It is a dummy method and should be overriden for actual use.
Args:
span (tuple of int): Start and end line of the directive.
name (str): Name of the callable to call
argexpr (str or None): Argument expression containing additional
arguments for the call.
'''
self._log_event('call', span, name=name, argexpr=argexpr)
def handle_nextarg(self, span, name):
'''Called when parser encounters a nextarg directive.
It is a dummy method and should be overriden for actual use.
Args:
span (tuple of int): Start and end line of the directive.
name (str or None): Name of the argument following next or
None if it should be the next positional argument.
'''
self._log_event('nextarg', span, name=name)
def handle_endcall(self, span, name):
'''Called when parser encounters an endcall directive.
It is a dummy method and should be overriden for actual use.
Args:
span (tuple of int): Start and end line of the directive.
name (str): Name found after the endcall directive.
'''
self._log_event('endcall', span, name=name)
def handle_eval(self, span, expr):
'''Called when parser encounters an eval directive.
It is a dummy method and should be overriden for actual use.
Args:
span (tuple of int): Start and end line of the directive.
expr (str): String representation of the Python expression to
be evaluated.
'''
self._log_event('eval', span, expression=expr)
def handle_global(self, span, name):
'''Called when parser encounters a global directive.
It is a dummy method and should be overriden for actual use.
Args:
span (tuple of int): Start and end line of the directive.
name (str): Name of the variable which should be made global.
'''
self._log_event('global', span, name=name)
def handle_stop(self, span, msg):
'''Called when parser finds an stop directive.
It is a dummy method and should be overriden for actual use.
Args:
span (tuple of int): Start and end line of the directive.
msg (str): Stop message.
'''
self._log_event('stop', span, msg=msg)
def handle_include(self, span, fname):
'''Should be called to signalize change to new file.
Args:
span (tuple of int): Start and end line of the include directive
or None if called the first time for the main input.
fname (str): Name of the file to be included.
'''
self._path.append(self._curnode)
self._curnode = []
self._open_blocks.append(
('include', self._curfile, [span], fname, None))
self._curfile = fname
self._nr_prev_blocks.append(len(self._open_blocks))
def handle_endinclude(self, span, fname):
'''Should be called when processing of a file finished.
Args:
span (tuple of int): Start and end line of the include directive
or None if called the first time for the main input.
fname (str): Name of the file which has been included.
'''
nprev_blocks = self._nr_prev_blocks.pop(-1)
if len(self._open_blocks) > nprev_blocks:
directive, fname, spans = self._open_blocks[-1][0:3]
msg = '{0} directive still unclosed when reaching end of file'\
.format(directive)
raise FyppFatalError(msg, self._curfile, spans[0])
block = self._open_blocks.pop(-1)
directive, blockfname, spans = block[0:3]
if directive != 'include':
msg = 'internal error: last open block is not \'include\' when '\
'closing file \'{0}\''.format(fname)
raise FyppFatalError(msg)
if span != spans[0]:
msg = 'internal error: span for include and endinclude differ ('\
'{0} vs {1}'.format(span, spans[0])
raise FyppFatalError(msg)
oldfname, _ = block[3:5]
if fname != oldfname:
msg = 'internal error: mismatching file name in close_file event'\
" (expected: '{0}', got: '{1}')".format(oldfname, fname)
raise FyppFatalError(msg, fname)
block = directive, blockfname, spans, fname, self._curnode
self._curnode = self._path.pop(-1)
self._curnode.append(block)
self._curfile = blockfname
def handle_if(self, span, cond):
'''Should be called to signalize an if directive.
Args:
span (tuple of int): Start and end line of the directive.
param (str): String representation of the branching condition.
'''
self._path.append(self._curnode)
self._curnode = []
self._open_blocks.append(('if', self._curfile, [span], [cond], []))
def handle_def(self, span, name, argexpr):
'''Should be called to signalize a def directive.
Args:
span (tuple of int): Start and end line of the directive.
name (str): Name of the macro to be defined.
argexpr (str): Macro argument definition or None
'''
self._path.append(self._curnode)
self._curnode = []
defblock = ('def', self._curfile, [span], name, argexpr, None)
self._open_blocks.append(defblock)
def handle_call(self, span, name, argexpr):
'''Should be called to signalize a call directive.
Args:
span (tuple of int): Start and end line of the directive.
name (str): Name of the callable to call
argexpr (str or None): Argument expression containing additional
arguments for the call.
'''
self._path.append(self._curnode)
self._curnode = []
self._open_blocks.append(
('call', self._curfile, [span, span], name, argexpr, [], []))
def handle_endcall(self, span, name):
'''Should be called to signalize an endcall directive.
Args:
span (tuple of int): Start and end line of the directive.
name (str): Name of the endcall statement. Could be None, if endcall
was specified without name.
'''
self._check_for_open_block(span, 'endcall')
block = self._open_blocks.pop(-1)
directive, fname, spans = block[0:3]
self._check_if_matches_last(directive, 'call', spans[0], span,
'endcall')
callname, callargexpr, args, argnames = block[3:7]
if name is not None and name != callname:
msg = "wrong name in endcall directive "\
"(expected '{0}', got '{1}')".format(callname, name)
raise FyppFatalError(msg, fname, span)
args.append(self._curnode)
# If nextarg or endcall immediately followed call, then first argument
# is empty and should be removed (to allow for calls without arguments
# and named first argument in calls)
if args and not args[0]:
if len(argnames) == len(args):
del argnames[0]
del args[0]
del spans[1]
spans.append(span)
block = (directive, fname, spans, callname, callargexpr, args, argnames)
self._curnode = self._path.pop(-1)
self._curnode.append(block)
def handle_global(self, span, name):
'''Should be called to signalize a global directive.
Args:
span (tuple of int): Start and end line of the directive.
name (str): Name of the variable(s) to make global.
'''
self._curnode.append(('global', self._curfile, span, name))
def handle_del(self, span, name):
'''Should be called to signalize a del directive.
Args:
span (tuple of int): Start and end line of the directive.
name (str): Name of the variable(s) to delete.
'''
self._curnode.append(('del', self._curfile, span, name))
def handle_eval(self, span, expr):
'''Should be called to signalize an eval directive.
Args:
span (tuple of int): Start and end line of the directive.
expr (str): String representation of the Python expression to
be evaluated.
'''
self._curnode.append(('eval', self._curfile, span, expr))
def handle_text(self, span, txt):
'''Should be called to pass text which goes to output unaltered.
Args:
span (tuple of int): Start and end line of the text.
txt (str): Text.
'''
self._curnode.append(('txt', self._curfile, span, txt))
def render(self, tree, divert=False, fixposition=False):
'''Renders a tree.
Args:
tree (fypp-tree): Tree to render.
divert (bool): Whether output will be diverted and sent for further
processing, so that no line numbering directives and
postprocessing are needed at this stage. (Default: False)
fixposition (bool): Whether file name and line position (variables
_FILE_ and _LINE_) should be kept at their current values or
should be updated continuously. (Default: False).
Returns: str: Rendered string.
'''
diverted = self._diverted
self._diverted = divert
fixedposition = self._fixedposition
self._fixedposition = fixposition
output, eval_inds, eval_pos = self._render(tree)
if not self._diverted and eval_inds:
self._postprocess_eval_lines(output, eval_inds, eval_pos)
self._diverted = diverted
self._fixedposition = fixedposition
txt = ''.join(output)
return txt
def _handle_stop(self, fname, span, msgstr):
try:
msg = str(self._evaluate(msgstr, fname, span[0]))
except Exception as exc:
msg = "exception occured when evaluating stop message '{0}'"\
.format(msgstr)
raise FyppFatalError(msg, fname, span, exc)
raise FyppStopRequest(msg, fname, span)