HWRF  trunk@4391
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
produtil.atparse.ATParser Class Reference

Takes input files or other data, and replaces certain strings with variables or functions. More...

Detailed Description

Takes input files or other data, and replaces certain strings with variables or functions.

The calling convention is quite simple:

1 ap=ATParser(varhash={"NAME":"Katrina", "STID":"12L"})
2 ap.parse_file("input-file.txt")
3 lines="line 1\nline 2\nline 3 of @[NAME]"
4 ap.parse_lines(lines,"(string-data)")
5 ap.parse_stream(sys.stdin,"(stdin)")

Inputs are general strings with @[...] and @** escape sequences which follow familiar shell syntax (but with @[...] instead of ${...}):

1 My storm is @[NAME] and the RSMC is @[RSMC:-${center:-unknown}].

In this case, it would print:

1 My storm is Katrina and the RSMC is unknown.

since NAME is set, but RSMC and center are unset.

There are also block if statements:

1 @** if NAME==BILLY
2 storm is billy
3 @** elseif name==KATRINA
4 storm is katrina
5 @** else
6 another storm
7 @** endif

and a variety of other things:

1 @[<anotherfile.txt] # read another file
2 @[var=value] # assign a variable
3 @[var:=value] # assign a variable and insert the value in the output stream
4 @[var2:?] # abort if var2 is not assigned, otherwise insert var2's contents
5 @[var3==BLAH?thencondition:elsecondition] # if-then-else substitution
6 @[var3!=BLAH?thencondition:elsecondition] # same, but with a "not equal"
7 @[var4:-substitution] # insert var4, or this substitution if var4 is unset
8 @[var5:+text] # insert text if var5 is set

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.)

1 @[var1.uc] # uppercase value of var1
2 @[var1.lc] # lowercase value of var1
3 @[var1.len] # length of var1
4 @[var1.trim] # var1 with leading and trailing whitespace removed

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...
 

Constructor & Destructor Documentation

def produtil.atparse.ATParser.__init__ (   self,
  stream = sys.stdout,
  varhash = None,
  logger = None,
  max_lines = 1000000 
)

ATParser constructor.

Parameters
streamthe output stream
varhasha dict of variables. All values must be strings. If this is unspecified, os.environ will be used.
loggerthe logging.Logger to read.
max_linesthe maximum number of lines to read

Definition at line 156 of file atparse.py.

Member Function Documentation

def produtil.atparse.ATParser.applyfun (   self,
  val,
  fun1,
  morefun 
)
protected

Applies a function to text.

Parameters
valthe text
fun1the function to apply
morefunmore 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().

def produtil.atparse.ATParser.from_var (   self,
  varname,
  optional 
)
protected

Return the value of a variable with functions applied.

Parameters
varnamethe variable name, including functions
optionalif 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().

def produtil.atparse.ATParser.optional_var (   self,
  varname 
)
protected

Return the value of a variable with functions applied, or '' if the variable is unset.

Parameters
varnamethe 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.

Parameters
filenamethe 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.

Parameters
linethe line of text.
filenamethe name of the source file, for error messages
linenothe 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.

Parameters
linesthe multi-line string
filenamethe 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.

Parameters
streamthe stream (an opened file)
streamnamea name for this stream for error messages

Definition at line 261 of file atparse.py.

def produtil.atparse.ATParser.replace_state (   self,
  state 
)
protected

Replace the top parser state.

Parameters
statethe new parser state

Definition at line 447 of file atparse.py.

Referenced by produtil.atparse.ATParser.parse_line().

def produtil.atparse.ATParser.replace_vars (   self,
  text 
)
protected

Expand @[...] blocks in a string.

Parameters
textthe string
Returns
a new string with expansions performed

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().

def produtil.atparse.ATParser.require_data (   self,
  data 
)
protected

Expand text within an @[...] block.

Parameters
datathe contents of the block

Definition at line 373 of file atparse.py.

Referenced by produtil.atparse.ATParser.parse_line().

def produtil.atparse.ATParser.require_file (   self,
  filename_pattern 
)
protected

Read the contents of a file and return it.

Parameters
filename_patterna 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().

def produtil.atparse.ATParser.require_var (   self,
  varname 
)
protected

Return the value of a variable with functions applied, raising an exception if the variable is unset.

Parameters
varnamethe 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.

def produtil.atparse.ATParser.top_state (   self,
  what = None 
)
protected

Return the top parser state without removing it.

Parameters
whatwhy 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().

def produtil.atparse.ATParser.var_or_command (   self,
  data 
)
protected

Expand one ${...} or @[...] block.

Parameters
datathe 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().

def produtil.atparse.ATParser.warn (   self,
  text 
)
protected

Print a warning to the logger, if we have a logger.

Parameters
textthe warning text.

Definition at line 177 of file atparse.py.

Referenced by produtil.atparse.ATParser.applyfun(), and produtil.atparse.ATParser.parse_line().

Member Data Documentation

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().


The documentation for this class was generated from the following file: