snakeoil.bash module¶
Functionality for reading bash like files
Please note that while this functionality can do variable interpolation, it strictly treats the source as non-executable code. It cannot parse subshells, variable additions, etc.
Its primary usage is for reading things like gentoo make.conf’s, or libtool .la files that are bash compatible, but non-executable.
- exception snakeoil.bash.BashParseError(filename, line, errmsg=None)[source]¶
Bases:
ExceptionException thrown when a handle being parsed isn’t valid bash.
- snakeoil.bash.iter_read_bash(*a, **kw)¶
- snakeoil.bash.read_bash(bash_source, allow_inline_comments=True, allow_line_cont=False, enum_line=False)[source]¶
Iterate over a file honoring bash commenting rules and line continuations.
Note that it’s considered good behaviour to close filehandles, as such, either iterate fully through this, or use read_bash instead. Once the file object is no longer referenced the handle will be closed, but be proactive instead of relying on the garbage collector.
- Parameters:
bash_source – either a file to read from or a string holding the filename to open.
allow_inline_comments – whether or not to prune characters after a # that isn’t at the start of a line.
allow_line_cont – whether or not to respect line continuations
- Returns:
yields lines w/ commenting stripped out
- snakeoil.bash.read_bash_dict(bash_source, vars_dict=None, sourcing_command=None)[source]¶
Read bash source, yielding a dict of vars.
- Parameters:
bash_source – either a file to read from or a string holding the filename to open
vars_dict (dict or None) – initial ‘env’ for the sourcing. Is protected from modification.
sourcing_command (callable) – controls whether a source command exists. If one does and is encountered, then this func is called.
- Raises:
BashParseError – thrown if invalid syntax is encountered.
- Returns:
dict representing the resultant env if bash executed the source.
- snakeoil.bash.read_dict(bash_source, splitter='=', source_isiter=False, allow_inline_comments=True, strip=False, filename=None, ignore_errors=False)[source]¶
Read key value pairs from a file, ignoring bash-style comments.
- Parameters:
splitter – the string to split on. Can be None to default to str.split’s default
bash_source – either a file to read from, or a string holding the filename to open.
allow_inline_comments – whether or not to prune characters after a # that isn’t at the start of a line.
ignore_errors – parse errors are logged instead of raised
- Raise:
BashParseErrorif there are parse errors found.