From a952ea60a0da87692203946f250dfc17b3d6fedd Mon Sep 17 00:00:00 2001 From: tmadlener Date: Mon, 12 Feb 2024 15:11:02 +0100 Subject: [PATCH] Move load_file to utils module to make it usable in user code --- k4FWCore/python/k4FWCore/utils.py | 30 ++++++++++++++++++++++++++++++ k4FWCore/scripts/k4run | 6 ++---- 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 k4FWCore/python/k4FWCore/utils.py diff --git a/k4FWCore/python/k4FWCore/utils.py b/k4FWCore/python/k4FWCore/utils.py new file mode 100644 index 00000000..df03b754 --- /dev/null +++ b/k4FWCore/python/k4FWCore/utils.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +# +# Copyright (c) 2014-2024 Key4hep-Project. +# +# This file is part of Key4hep. +# See https://key4hep.github.io/key4hep-doc/ for further info. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +from io import TextIOWrapper +from typing import Union + + +def load_file(opt_file: Union[TextIOWrapper, os.PathLike]) -> None: + """Load the file content and run it in the current interpreter session""" + if isinstance(opt_file, os.PathLike): + opt_file = open(opt_file, "r") + code = compile(opt_file.read(), opt_file.name, "exec") + exec(code, globals()) diff --git a/k4FWCore/scripts/k4run b/k4FWCore/scripts/k4run index 3e1049d7..40e8df7c 100755 --- a/k4FWCore/scripts/k4run +++ b/k4FWCore/scripts/k4run @@ -6,6 +6,8 @@ import argparse from multiprocessing import cpu_count import logging +from k4FWCore.utils import load_file + # these default properties are filtered as otherwise they will clutter the argument list FILTER_GAUDI_PROPS = [ "ContextService", @@ -55,10 +57,6 @@ seen_files = set() option_db = {} -def load_file(file): - exec(file.read(), {}) - - def add_arguments(parser, app_mgr): # length increases when properties of an algorithm with tools are inspected # see https://github.com/key4hep/k4FWCore/pull/138