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

osl_func utility: run any function from the command line #257

Merged
merged 1 commit into from
Jan 11, 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
1 change: 1 addition & 0 deletions osl/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .simulate import * # noqa: F401, F403
from .opm import * # noqa: F401, F403
from .package import soft_import, run_package_tests # noqa: F401, F403
from . import run_func # noqa: F401, F403

with open(os.path.join(os.path.dirname(__file__), "README.md"), 'r') as f:
__doc__ = f.read()
38 changes: 38 additions & 0 deletions osl/utils/run_func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from functools import partial
import sys

def main(argv=None):
# sys.argv[1] is the function name
# sys.argv[2:] are the arguments to the function
# e.g. python -m osl.utils.run_func my_func arg1 arg2
# will call my_func(arg1, arg2)
if argv is None:
argv = sys.argv[1:]

func_name = argv[0]
func_args = argv[1:]

# iteratively open each (sub)module
for ii, mod in enumerate(func_name.split('.')):
if ii==0:
module = __import__(mod)
else:
module = getattr(module, mod)
func = module

# do some general argument checks
for ii in range(len(func_args)):
if type(func_args[ii]) is str:
if func_args[ii]=='None':
func_args[ii] = None
elif func_args[ii]=='True':
func_args[ii] = True
elif func_args[ii]=='False':
func_args[ii] = False

# run the function
func(*func_args)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
'console_scripts': [
'osl_maxfilter = osl.maxfilter.maxfilter:main',
'osl_preproc = osl.preprocessing.batch:main',
'osl_func = osl.utils.run_func:main',
]},

packages=['osl', 'osl.tests', 'osl.report', 'osl.maxfilter',
Expand Down
Loading