parse_subst |
index /home/pfeiffer/project-shared/publicize/bii_scripts-top/bii_scripts/lib/python/bii_scripts3/parse_subst.py |
===========
parse_subst
===========
Preface
=======
This module contains a parser function for epics substitution-files. The
contents of the db-file are returned in a python dictionary or list structure
that can then be used for further evaluation.
Implemented Functions
---------------------
parse
+++++
Definition::
parse(data, mode= "dict", errmsg_prefix= None)
This function parses a given string variable that must contain a complete
substitution-file. It returns a data structure where the parsed data is
stored.
The new "global" statement in substitution files is also supported. Globals are
resolved, meaning that definitions of global values are merged in the local
per-file definitions. Applications that use parse_subst do not have to be
aware of the global statement.
parameters:
- data : The string containing the substitution data.
- mode : Either "dict" or "list". This determines the format of the
returned data structure, see examples below.
- errmsg_prefix : This message is prepended to possible parse error messages
parse_file
++++++++++
Definition::
parse_file(filename, mode= "dict", encoding= None)
This function parses the contents of a file specified by a filename. If the
parameter "filename" is "-", it reads from STDIN. It returns a data structure
where the parsed data is stored.
parameters:
- filename : The name of the file.
- mode : Either "dict" or "list". This determines the format of the
returned data structure, see examples below.
- encoding : The encoding of the file, the default is your system's encoding
default, which usually is UTF-8. A list of valid encodings can be
found at:
https://docs.python.org/3/library/codecs.html#standard-encodings.
json_str
++++++++
Definition::
json_str(var, ensure_ascii= True)
This function converts a python data structure to a JSON string. It uses the
standard JSON module from python with some useful defaults.
parameters:
- var : The data structure.
- ensure_ascii : If True, ensure that the JSON string contains only ASCII
characters, all other characters are converted to JSON escape
codes. If False, leave non-ASCII characters in the string.
json_print
++++++++++
Definition::
json_print(var, ensure_ascii= True)
This function converts a python data structure to a JSON string and prints it
to the console. It uses the standard JSON module from python with some useful
defaults.
parameters:
- var : The data structure.
- ensure_ascii : If True, ensure that the JSON string contains only ASCII
characters, all other characters are converted to JSON escape
codes. If False, leave non-ASCII characters in the string.
data structures
---------------
In all the examples below, this is the substitution data parsed::
file adimogbl.template
{
{
GBASE="U3IV:",
TRIG1="U3IV:AdiMoVGblTrg.PROC",
}
}
file adimovhgbl.template
{
{
GBASE="U3IV:",
DRV="V",
AdiMopVer="9",
TRIG1="U3IV:AdiVGblPvr.PROC",
}
{
GBASE="U3IV:",
DRV="H",
AdiMopVer="9",
TRIG1="U3IV:AdiHGblPvr.PROC",
}
}
dict structure
++++++++++++++
When the "mode" parameter of the parse function is not given or set to 'dict',
the parse function returns a dict structure.
Each template-name is a key in the dictionary. It's value is a list that
contains the data for that template.
The list contains dictionary for each instantiation of that template.
Each instantiation dictionary contains a key-value pair for each field name
value. Note that undefined fields-values are empty strings ("").
Example of a dictionary that parse() returns::
{
'adimovhgbl.template' : [
{
'TRIG1' : 'U3IV:AdiVGblPvr.PROC',
'DRV' : 'V',
'GBASE' : 'U3IV:',
'AdiMopVer' : '9'
},
{
'TRIG1' : 'U3IV:AdiHGblPvr.PROC',
'DRV' : 'H',
'GBASE' : 'U3IV:',
'AdiMopVer' : '9'
}
],
'adimogbl.template' : [
{
'TRIG1' : 'U3IV:AdiMoVGblTrg.PROC',
'GBASE' : 'U3IV:'
}
]
}
list structure
++++++++++++++
When the "mode" parameter of the parse function is set to 'list', the parse
function returns a list structure.
The list contains a list for each template-name. Within these lists, the first
element is the template-name, all following elements are dictionaries, one for
each instantiation of that template.
Each instantiation dictionary contains a key-value pair for each field name
value. Note that undefined fields-values are empty strings ("").
Example of a dictionary that parse() returns::
[
[
'adimogbl.template',
{
'TRIG1' : 'U3IV:AdiMoVGblTrg.PROC',
'GBASE' : 'U3IV:'
}
],
[
'adimovhgbl.template',
{
'TRIG1' : 'U3IV:AdiVGblPvr.PROC',
'DRV' : 'V',
'GBASE' : 'U3IV:',
'AdiMopVer' : '9'
},
{
'TRIG1' : 'U3IV:AdiHGblPvr.PROC',
'DRV' : 'H',
'GBASE' : 'U3IV:',
'AdiMopVer' : '9'
}
]
]
Modules | ||||||
|
Classes | ||||||||||||||||||
|
Functions | ||
|
Data | ||
LINESEP = '\n' LINESEP_LEN = 1 MODES = {'dict', 'list'} PERMISSIVE = True SYS_DEFAULT_ENCODING = 'UTF-8' WARNINGS = True rx_bracket1 = re.compile('(\\s*(?:\\s*(?:|\\#[^\\r\\n]*)[\\r\\n]+)*\\s*)\\{', re.MULTILINE) rx_bracket2 = re.compile('(\\s*(?:\\s*(?:|\\#[^\\r\\n]*)[\\r\\n]+)*\\s*)\\}', re.MULTILINE) rx_comma = re.compile('\\s*,', re.MULTILINE) rx_complete_unquoted_value = re.compile('(?:[^"\\s\\{\\},]+)$') rx_complete_unquoted_word = re.compile('(?:\\w+)$') rx_def = re.compile('(\\s*(?:\\s*(?:|\\#[^\\r\\n]*)[\\r\\...\\s*)(,?)(\\s*(?:\\s*(?:|\\#[^\\r\, re.MULTILINE) rx_file_head = re.compile('(\\s*(?:\\s*(?:|\\#[^\\r\\n]*)[\\r\\...:|\\#[^\\r\\n]*)[\\r\\n]+)*\\s*){', re.MULTILINE) rx_pattern = re.compile('(\\s*(?:\\s*(?:|\\#[^\\r\\n]*)[\\r\\n]+)*\\s*)pattern', re.MULTILINE) rx_top = re.compile('(\\s*(?:\\s*(?:|\\#[^\\r\\n]*)[\\r\\n]+)*\\s*)(file|global|)', re.MULTILINE) rx_unquoted_filename = re.compile('(?:[^\\/\\s\\{\\}]+)', re.MULTILINE) rx_val = re.compile('(\\s*(?:\\s*(?:|\\#[^\\r\\n]*)[\\r\\..."|(?:[^"\\s\\{\\},]+))\\s*(,|\\})', re.MULTILINE) st_bracket1 = r'(\s*(?:\s*(?:|\#[^\r\n]*)[\r\n]+)*\s*)\{' st_bracket2 = r'(\s*(?:\s*(?:|\#[^\r\n]*)[\r\n]+)*\s*)\}' st_comma = r'\s*,' st_def = r'(\s*(?:\s*(?:|\#[^\r\n]*)[\r\n]+)*\s*)(\"(?:\w+)...*)(,?)(\s*(?:\s*(?:|\#[^\r\n]*)[\r\n]+)*\s*)(\}?)' st_file_head = r'(\s*(?:\s*(?:|\#[^\r\n]*)[\r\n]+)*\s*)(\"(?:.*?)...\s\{\}]+))(\s*(?:\s*(?:|\#[^\r\n]*)[\r\n]+)*\s*){' st_pattern = r'(\s*(?:\s*(?:|\#[^\r\n]*)[\r\n]+)*\s*)pattern' st_quoted = r'\"(?:.*?)(?<!\\)\"' st_quoted_word = r'\"(?:\w+)\"' st_space_or_comment = r'\s*(?:\s*(?:|\#[^\r\n]*)[\r\n]+)*\s*' st_top = r'(\s*(?:\s*(?:|\#[^\r\n]*)[\r\n]+)*\s*)(file|global|)' st_unquoted_filename = r'(?:[^\/\s\{\}]+)' st_unquoted_value = r'(?:[^"\s\{\},]+)' st_unquoted_word = r'(?:\w+)' st_val = r'(\s*(?:\s*(?:|\#[^\r\n]*)[\r\n]+)*\s*)(\"(?:.*?)(?<!\\)\"|(?:[^"\s\{\},]+))\s*(,|\})' |