HWRF
trunk@4391
|
Takes input files or other data, and replaces certain strings with variables or functions. More...
Takes input files or other data, and replaces certain strings with variables or functions.
The calling convention is quite simple:
Inputs are general strings with @[...] and @** escape sequences which follow familiar shell syntax (but with @[...] instead of ${...}):
In this case, it would print:
since NAME is set, but RSMC and center are unset.
There are also block if statements:
and a variety of other things:
There are also a small number of functions that modify text before it is sent to stdout. (The original variable is unmodified, only the output text is changed.)
Definition at line 98 of file atparse.py.
Public Member Functions | |
def | __init__ |
ATParser constructor. More... | |
def | max_lines (self) |
The maximum number of lines to read. More... | |
def | infile (self) |
The current input file name. More... | |
def | parse_stream (self, stream, streamname) |
Read a stream and parse its contents. More... | |
def | parse_file (self, filename) |
Read a file and parse its contents. More... | |
def | getvar (self, varname) |
Return the value of a variable, or None if it is unset. More... | |
def | str_state (self) |
Return a string description of the parser stack for debugging. More... | |
def | parse_lines (self, lines, filename) |
Given a multi-line string, parse the contents line-by-line. More... | |
def | parse_line (self, line, filename, lineno) |
Parses one line of text. More... | |
Public Attributes | |
varhash | |
The dict of variables. More... | |
Protected Member Functions | |
def | warn (self, text) |
Print a warning to the logger, if we have a logger. More... | |
def | applyfun (self, val, fun1, morefun) |
Applies a function to text. More... | |
def | from_var (self, varname, optional) |
Return the value of a variable with functions applied. More... | |
def | optional_var (self, varname) |
Return the value of a variable with functions applied, or '' if the variable is unset. More... | |
def | require_var (self, varname) |
Return the value of a variable with functions applied, raising an exception if the variable is unset. More... | |
def | replace_vars (self, text) |
Expand @[...] blocks in a string. More... | |
def | require_file (self, filename_pattern) |
Read the contents of a file and return it. More... | |
def | var_or_command (self, data) |
Expand one ${...} or @[...] block. More... | |
def | require_data (self, data) |
Expand text within an @[...] block. More... | |
def | active (self) |
Is the current block active? | |
def | top_state |
Return the top parser state without removing it. More... | |
def | push_state (self, state) |
Push a new state to the top of the parser state stack. | |
def | pop_state (self) |
Remove and return the top parser state. | |
def | replace_state (self, state) |
Replace the top parser state. More... | |
def produtil.atparse.ATParser.__init__ | ( | self, | |
stream = sys.stdout , |
|||
varhash = None , |
|||
logger = None , |
|||
max_lines = 1000000 |
|||
) |
ATParser constructor.
stream | the output stream |
varhash | a dict of variables. All values must be strings. If this is unspecified, os.environ will be used. |
logger | the logging.Logger to read. |
max_lines | the maximum number of lines to read |
Definition at line 156 of file atparse.py.
|
protected |
Applies a function to text.
val | the text |
fun1 | the function to apply |
morefun | more functions to apply |
Definition at line 195 of file atparse.py.
Referenced by produtil.atparse.ATParser.applyfun(), produtil.atparse.ATParser.from_var(), and produtil.atparse.ATParser.var_or_command().
|
protected |
Return the value of a variable with functions applied.
varname | the variable name, including functions |
optional | if False, raise an exception if the variable is unset. If True, return '' for unset variables. |
Definition at line 215 of file atparse.py.
Referenced by produtil.atparse.ATParser.from_var(), produtil.atparse.ATParser.optional_var(), and produtil.atparse.ATParser.require_var().
def produtil.atparse.ATParser.getvar | ( | self, | |
varname | |||
) |
Return the value of a variable, or None if it is unset.
Definition at line 287 of file atparse.py.
Referenced by produtil.atparse.ATParser.var_or_command().
def produtil.atparse.ATParser.infile | ( | self | ) |
The current input file name.
Definition at line 188 of file atparse.py.
Referenced by produtil.atparse.ATParser.from_var().
def produtil.atparse.ATParser.max_lines | ( | self | ) |
The maximum number of lines to read.
Definition at line 184 of file atparse.py.
Referenced by produtil.atparse.ATParser.parse_line().
|
protected |
Return the value of a variable with functions applied, or '' if the variable is unset.
varname | the name of the variable. |
Definition at line 233 of file atparse.py.
Referenced by produtil.atparse.ATParser.parse_line().
def produtil.atparse.ATParser.parse_file | ( | self, | |
filename | |||
) |
Read a file and parse its contents.
filename | the name of this file for error messages |
Definition at line 270 of file atparse.py.
def produtil.atparse.ATParser.parse_line | ( | self, | |
line, | |||
filename, | |||
lineno | |||
) |
Parses one line of text.
line | the line of text. |
filename | the name of the source file, for error messages |
lineno | the line number within the source file, for error messages |
Definition at line 462 of file atparse.py.
Referenced by produtil.atparse.ATParser.parse_file(), produtil.atparse.ATParser.parse_lines(), and produtil.atparse.ATParser.parse_stream().
def produtil.atparse.ATParser.parse_lines | ( | self, | |
lines, | |||
filename | |||
) |
Given a multi-line string, parse the contents line-by-line.
lines | the multi-line string |
filename | the name of the file it was from, for error messages |
Definition at line 453 of file atparse.py.
Referenced by produtil.atparse.ATParser.parse_line().
def produtil.atparse.ATParser.parse_stream | ( | self, | |
stream, | |||
streamname | |||
) |
Read a stream and parse its contents.
stream | the stream (an opened file) |
streamname | a name for this stream for error messages |
Definition at line 261 of file atparse.py.
|
protected |
Replace the top parser state.
state | the new parser state |
Definition at line 447 of file atparse.py.
Referenced by produtil.atparse.ATParser.parse_line().
|
protected |
Expand @[...] blocks in a string.
text | the string |
Definition at line 247 of file atparse.py.
Referenced by produtil.atparse.ATParser.parse_line(), produtil.atparse.ATParser.require_file(), and produtil.atparse.ATParser.var_or_command().
|
protected |
Expand text within an @[...] block.
data | the contents of the block |
Definition at line 373 of file atparse.py.
Referenced by produtil.atparse.ATParser.parse_line().
|
protected |
Read the contents of a file and return it.
filename_pattern | a filename with ${} or @[] blocks in it. |
Definition at line 279 of file atparse.py.
Referenced by produtil.atparse.ATParser.parse_line(), and produtil.atparse.ATParser.require_data().
|
protected |
Return the value of a variable with functions applied, raising an exception if the variable is unset.
varname | the name of the variable. |
Definition at line 240 of file atparse.py.
Referenced by produtil.atparse.ATParser.replace_vars(), and produtil.atparse.ATParser.var_or_command().
def produtil.atparse.ATParser.str_state | ( | self | ) |
Return a string description of the parser stack for debugging.
Definition at line 390 of file atparse.py.
|
protected |
Return the top parser state without removing it.
what | why the state is being examined. This is for error messages. |
Definition at line 423 of file atparse.py.
Referenced by produtil.atparse.ATParser.parse_line().
|
protected |
Expand one ${...} or @[...] block.
data | the contents of the block |
Definition at line 291 of file atparse.py.
Referenced by produtil.atparse.ATParser.replace_vars(), and produtil.atparse.ATParser.require_data().
|
protected |
Print a warning to the logger, if we have a logger.
text | the warning text. |
Definition at line 177 of file atparse.py.
Referenced by produtil.atparse.ATParser.applyfun(), and produtil.atparse.ATParser.parse_line().
produtil.atparse.ATParser.varhash |
The dict of variables.
This is NOT the dict sent to the constructor — a copy was made. That means it is safe to modify the variables all you want, even if os.environ was used.
Definition at line 164 of file atparse.py.
Referenced by produtil.atparse.ATParser.from_var(), produtil.atparse.ATParser.getvar(), and produtil.atparse.ATParser.var_or_command().