Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make podio build version read from file accessible from python #659

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions python/podio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
raise

from .frame import Frame
from . import root_io, reading
from . import root_io, reading, version

try:
# We try to import the sio bindings which may fail if ROOT is not able to
Expand All @@ -21,10 +21,4 @@
except ImportError:
pass

__all__ = [
"__version__",
"Frame",
"root_io",
"sio_io",
"reading",
]
__all__ = ["__version__", "Frame", "root_io", "sio_io", "reading", "version"]
9 changes: 9 additions & 0 deletions python/podio/base_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ def get_datamodel_definition(self, edm_name):
if self._is_legacy:
return ""
return self._reader.getDatamodelDefinition(edm_name).data()

def current_file_version(self):
"""Get the podio (build) version that was used to write this file

Returns:
podio.version.Version: The build version of podio that was use to
write this file
"""
return self._reader.currentFileVersion()
26 changes: 26 additions & 0 deletions python/podio/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python3
"""Module that facilitates working with the podio::version::Version"""

import ROOT

# NOTE: It is necessary that this can be found on the ROOT_INCLUDE_PATH
#
# We check whether we can actually load the header to not break python bindings
# in environments with *ancient* podio versions
if ROOT.gInterpreter.LoadFile("podio/podioVersion.h") == 0: # noqa: E402
from ROOT import podio # noqa: E402 # pylint: disable=wrong-import-position

build_version = podio.version.build_version


def version_as_str(ver):
"""Stringify the version into the usual format

Args:
ver (podio.version.Version): A podio version

Returns:
str: A stringified version of the version, in the format
MAJOR.MINOR.PATCH
"""
return f"{ver.major}.{ver.minor}.{ver.patch}"
8 changes: 7 additions & 1 deletion tools/podio-dump
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import yaml
from tabulate import tabulate

from podio_version import __version__
from podio.version import version_as_str


def print_general_info(reader, filename):
Expand All @@ -20,11 +21,16 @@ def print_general_info(reader, filename):
reader (root_io.Reader, sio_io.Reader): An initialized reader
"""
legacy_text = " (this is a legacy file!)" if reader.is_legacy else ""
print(f"input file: {filename}{legacy_text}\n")
print(
f"input file: {filename}{legacy_text}\n"
" (written with podio version: "
f"{version_as_str(reader.current_file_version())})\n"
)
print(
"datamodel model definitions stored in this file: "
f'{", ".join(reader.datamodel_definitions)}'
)

print()
print("Frame categories in this file:")
cats = [(c, len(reader.get(c))) for c in reader.categories]
Expand Down