Skip to content

Commit

Permalink
Move load_file to utils module to make it usable in user code
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Feb 12, 2024
1 parent 9d171cd commit 1ae8aa0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
7 changes: 3 additions & 4 deletions k4FWCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ install(TARGETS k4FWCore k4FWCorePlugins
# Copy python parsing file to genConfDir in Gaudi
add_custom_command(
TARGET k4FWCore POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${PROJECT_SOURCE_DIR}/k4FWCore/python/k4FWCore/parseArgs.py
${CMAKE_CURRENT_BINARY_DIR}/genConfDir/k4FWCore/parseArgs.py)

COMMAND ${CMAKE_COMMAND} -E copy_directory
${PROJECT_SOURCE_DIR}/k4FWCore/python/k4FWCore
${CMAKE_CURRENT_BINARY_DIR}/genConfDir/k4FWCore)
30 changes: 30 additions & 0 deletions k4FWCore/python/k4FWCore/utils.py
Original file line number Diff line number Diff line change
@@ -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())
6 changes: 2 additions & 4 deletions k4FWCore/scripts/k4run
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1ae8aa0

Please sign in to comment.