rads.xml.utility module

XML tools for reading the RADS’s configuration files.

rads.xml.utility.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.utility.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.utility.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.utility.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.utility.rootless_fixer(text: str, preserve_empty: bool = False) → str[source]

Fix rootless XML files.

Give this as the fixer argument in parse(), fromstring(), or fromstringlist() 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 this functools.partial() should be used.

Returns

The given text with <__ROOTLESS__> tags added (after beginning processing instructions).

rads.xml.utility.is_empty(text: str) → bool[source]

Determine if XML string is empty.

The XML string is considered empty if it only contains processing instructions and comments.

Parameters

text – XML text to check for being empty.

Returns

True if the given XML text is empty.

rads.xml.utility.strip_comments(text: str) → str[source]

Remove XML comments from a string.

Note

This will not remove lines that had comments, it only removes the text from “<!–” to “–>”.

Parameters

text – XML text to strip comments from.

Returns

The given text without XML comments.

rads.xml.utility.strip_processing_instructions(text: str) → str[source]

Remove XML processing instructions from a string.

Note

This will not remove lines that had processing instructions, it only removes the text from “<?” to “?>”.

Parameters

text – XML text to strip processing instructions from.

Returns

The given text without XML processing instructions.

rads.xml.utility.strip_blanklines(text: str) → str[source]

Remove blank lines from a string.

Lines containing only whitespace characters are considered blank.

Parameters

text – String to remove blank lines from.

Returns

String without blank lines.