diff --git a/python/podio/root_io.py b/python/podio/root_io.py index 830eb62e7..95702f086 100644 --- a/python/podio/root_io.py +++ b/python/podio/root_io.py @@ -8,6 +8,7 @@ from podio.base_reader import BaseReaderMixin # pylint: disable=wrong-import-position # noqa: E402 from podio.base_writer import BaseWriterMixin # pylint: disable=wrong-import-position # noqa: E402 +from podio.utils import convert_to_str_paths # pylint: disable=wrong-import-position # noqa: E402 class Reader(BaseReaderMixin): @@ -17,11 +18,9 @@ def __init__(self, filenames): """Create a reader that reads from the passed file(s). Args: - filenames (str or list[str]): file(s) to open and read data from + filenames (str or list[str] or Path or list[Path]): file(s) to open and read data from """ - if isinstance(filenames, str): - filenames = (filenames,) - + filenames = convert_to_str_paths(filenames) self._reader = podio.ROOTReader() self._reader.openFiles(filenames) @@ -35,11 +34,9 @@ def __init__(self, filenames): """Create an RNTuple reader that reads from the passed file(s). Args: - filenames (str or list[str]): file(s) to open and read data from + filenames (str or list[str] or Path or list[Path]): file(s) to open and read data from """ - if isinstance(filenames, str): - filenames = (filenames,) - + filenames = convert_to_str_paths(filenames) self._reader = podio.RNTupleReader() self._reader.openFiles(filenames) @@ -57,11 +54,9 @@ def __init__(self, filenames): """Create a reader that reads from the passed file(s). Args: - filenames (str or list[str]): file(s) to open and read data from + filenames (str or list[str] or Path or list[Path]): file(s) to open and read data from """ - if isinstance(filenames, str): - filenames = (filenames,) - + filenames = convert_to_str_paths(filenames) self._reader = podio.ROOTLegacyReader() self._reader.openFiles(filenames) self._is_legacy = True @@ -76,8 +71,9 @@ def __init__(self, filename): """Create a writer for writing files Args: - filename (str): The name of the output file + filename (str or Path): The name of the output file """ + filename = convert_to_str_paths(filename)[0] self._writer = podio.ROOTWriter(filename) super().__init__() @@ -89,7 +85,8 @@ def __init__(self, filename): """Create a writer for writing files Args: - filename (str): The name of the output file + filename (str or Path): The name of the output file """ + filename = convert_to_str_paths(filename)[0] self._writer = podio.RNTupleWriter(filename) super().__init__() diff --git a/python/podio/sio_io.py b/python/podio/sio_io.py index 51c303f2a..9926b33ff 100644 --- a/python/podio/sio_io.py +++ b/python/podio/sio_io.py @@ -11,6 +11,7 @@ from podio.base_reader import BaseReaderMixin # pylint: disable=wrong-import-position from podio.base_writer import BaseWriterMixin # pylint: disable=wrong-import-position +from podio.utils import convert_to_str_paths # pylint: disable=wrong-import-position # noqa: E402 class Reader(BaseReaderMixin): @@ -20,8 +21,9 @@ def __init__(self, filename): """Create a reader that reads from the passed file. Args: - filename (str): File to open and read data from + filename (str or Path): File to open and read data from. """ + filename = convert_to_str_paths(filename)[0] self._reader = podio.SIOReader() self._reader.openFile(filename) @@ -39,8 +41,9 @@ def __init__(self, filename): """Create a reader that reads from the passed file. Args: - filename (str): File to open and read data from + filename (str or Path): File to open and read data from. """ + filename = convert_to_str_paths(filename)[0] self._reader = podio.SIOLegacyReader() self._reader.openFile(filename) self._is_legacy = True @@ -49,13 +52,15 @@ def __init__(self, filename): class Writer(BaseWriterMixin): - """Writer class for writing podio root files""" + """Writer class for writing podio root files.""" def __init__(self, filename): - """Create a writer for writing files + """Create a writer for writing files. Args: - filename (str): The name of the output file + filename (str or Path): The name of the output file. """ + filename = convert_to_str_paths(filename)[0] self._writer = podio.SIOWriter(filename) + super().__init__() diff --git a/python/podio/utils.py b/python/podio/utils.py new file mode 100644 index 000000000..5b7221c88 --- /dev/null +++ b/python/podio/utils.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +"""utility functionality for podio""" + +import os +from collections.abc import Iterable +from pathlib import Path + + +def convert_to_str_paths(filenames): + """Converts filenames to string paths, handling both string and pathlib.Path objects and + iterables of such objects. + + Args: + filenames (str, Path, or Iterable[str | Path]): A single filepath or an iterable of + filepaths to convert to str object(s). + + Returns: + list[str]: A list of filepaths as strings. + """ + + if isinstance(filenames, Iterable) and not isinstance(filenames, (str, Path)): + return [os.fspath(fn) for fn in filenames] + + return [os.fspath(filenames)]