rads.xml package¶
Module contents¶
XML library, specifically for reading the RADS’s configuration files.
This includes Element which allows easy traversal of the XML tree new
versions of the parse(), fromstring() and fromstringlist()
functions for parsing and XML document that return Element. These
functions also support XML documents without a root element, such as the RADS
v4 configuration file.
-
class
rads.xml.Element(element: lxml.etree._Element, *, file: Optional[str] = None)[source]¶ Bases:
rads.xml.base.ElementXML element that encapsulates an element from lxml.
Supports line number examination.
- Param
XML element from the lxml library.
- Parameters
file – Optional filename/path the element is from.
-
next() → rads.xml.lxml.Element[source]¶ Get the next sibling element.
- Returns
Next XML sibling element.
- Raises
StopIteration – If there is no next sibling element.
-
prev() → rads.xml.lxml.Element[source]¶ Get the previous sibling element.
- Returns
Previous XML sibling element.
- Raises
StopIteration – If there is no previous sibling element.
-
up() → rads.xml.lxml.Element[source]¶ Get the parent of this element.
- Returns
Parent XML element.
- Raises
StopIteration – If there is no parent element.
-
down() → rads.xml.lxml.Element[source]¶ Get the first child of this element.
- Returns
First child XML element.
- Raises
StopIteration – If this element does not have any children.
-
property
file¶ Get the name of the XML file containing this element.
- Returns
Name of the file containing this element, or None.
-
property
opening_line¶ Get the opening line of the XML element.
- Returns
Opening line number, or None.
-
num_lines() → int[source]¶ Get the number of lines making up the XML element.
- Returns
Number of lines in XML element, or None.
-
closing_line() → int[source]¶ Get the closing line of the XML element.
- Returns
Closing line number, or None.
-
property
tag¶ Tag name of the element.
-
property
text¶ Internal text of the element.
-
property
attributes¶ The attributes of the element, as a dictionary.
-
rads.xml.ParseError¶
-
rads.xml.fromstring(text: str, *, parser: Optional[lxml.etree.XMLParser] = None, fixer: Optional[Callable[[str], str]] = None, file: Optional[str] = None) → rads.xml.lxml.Element[source]¶ Parse an XML document or section from a string constant.
- Parameters
text – XML text to parse.
parser – XML parser to use, defaults to the standard XMLParser, which is ElementTree compatible regardless of backend.
fixer – A function to pre-process the XML string. This can be used to fix files during load.
file – Optional filename to associate with the returned
xml.Element.
- Returns
The root XML element (of the section given in text). If rootless is True this will be the added <rootless> element.
-
rads.xml.fromstringlist(sequence: Sequence[str], parser: Optional[lxml.etree.XMLParser] = None, fixer: Optional[Callable[[str], str]] = None, file: Optional[str] = None) → rads.xml.lxml.Element[source]¶ Parse an XML document or section from a sequence of string fragments.
- Parameters
sequence – String fragments containing the XML text to parse.
parser – XML parser to use, defaults to the standard XMLParser, which is ElementTree compatible regardless of backend.
fixer – A function to pre-process the XML string. This can be used to fix files during load. This will not be a string list but the full string with newlines.
file – Optional filename to associate with the returned
xml.Element.
- Returns
The root XML element (of the section given in text). If rootless is True this will be the added <rootless> element.
-
rads.xml.parse(source: Union[str, os.PathLike, IO[Any]], parser: Optional[lxml.etree.XMLParser] = None, fixer: Optional[Callable[[str], str]] = None) → rads.xml.lxml.Element[source]¶ Parse an XML document from a file or file-like object.
- Parameters
source – File or file-like object containing the XML data.
parser – XML parser to use, defaults to the standard XMLParser, which is ElementTree compatible regardless of backend.
fixer – A function to pre-process the XML string. This can be used to fix files during load.
- Returns
The root XML element. If rootless is True this will be the added <rootless> element
-
rads.xml.rads_fixer(text: str) → str[source]¶ Fix XML problems with the upstream RADS XML configuration.
This fixer is for problems that will not be fixed upstream or for which the fix has been delayed. It is primary for making up the difference between the official RADS parser which is very lenient and the PyRADS parser which is very strict.
Currently, this fixes the following bugs with the RADS config.
The RADS XML file does not have a root as dictated by the XML 1.0 standard. This is fixed by adding <__ROOTLESS__> tags around the entire file. This is the only fix that is considered part of the RADS standard (that is RADS lies about it being XML 1.0).
The RADS MXL file has some instances of int3 used in the <compress> tag. This is an invalid type (there is no 3 byte integer) and in the official RADS implementation all invalid types default to dble (double). However, The intended type here is int4. This fix corrects this.
- Parameters
text – RADS XML string to fix.
- Returns
Repaired RADS XML string.
-
rads.xml.rootless_fixer(text: str, preserve_empty: bool = False) → str[source]¶ Fix rootless XML files.
Give this as the fixer argument in
parse(),fromstring(), orfromstringlist()to load XML files that do not have a root tag. This is done by adding a <__ROOTLESS__> block around the entire document.- Parameters
text – XML text to wrap <__ROOTLESS__> tags around.
preserve_empty – Set to False to skip adding <__ROOTLESS__> tags to an empty XML file. See
is_empty()for the definition of empty. In order to set thisfunctools.partial()should be used.
- Returns
The given text with <__ROOTLESS__> tags added (after beginning processing instructions).