Skip to content

Combine functions, add logic that checks xml type and converts to xmldoc if path-like.

Luke Davis requested to merge refactor/implicit_xml_type_conversion into dev

I think a lot of these xml load functions can be combined, with the argument that if a wrapper function returns the output of the inner-function without further modification, then the wrapper logic can just be inserted to the start of the inner-function conditional on the input type.

I've also tried to make the names we use a bit more consistent.
So we have:

filepath: str | Path == PathLike
fileobj: file-object (from open()) | BinaryIO (from BytesIO)
file: filepath | fileobj
xmldoc: ligo.lw.ligolw.Document

To stop these common arguments from cluttering up all our functions, i've just put them through as kwargs until they hit load_ligolw_xmldoc:

ilwdchar_compat: bool = True,
legacy_postcoh_compat: bool = True,
nullable: bool = True,
contenthandler: Optional[ligo.lw.ligolw.LIGOLWContentHandler] = None,

e.g.

def get_params_from_xml(
    file: Union[PathLike, BinaryIO, ligo.lw.ligolw.Document],
    params: Optional[Union[Sequence[str], str]] = None,
    **kwargs)
   xmldoc = load_ligolw_xmldoc(file, **kwargs)
   ...

To determine if an object is a filepath or a fileobj I check for the existence of .seek():
if hasattr(file, 'seek'):

Edited by Luke Davis

Merge request reports