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

karabo-bridge-serve-run command #458

Merged
merged 13 commits into from
Nov 6, 2023
81 changes: 81 additions & 0 deletions extra_data/cli/serve_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from argparse import ArgumentParser
import sys

from .. import open_run

IMPORT_FAILED_MSG = """\
{}

karabo-bridge-serve-run requires additional dependencies:
pip install karabo-bridge psutil
"""

def main(argv=None):
ap = ArgumentParser(prog="karabo-bridge-serve-run")
ap.add_argument("proposal", help="Proposal number")
ap.add_argument("run", help="Run number")
ap.add_argument("port", help="TCP port or ZMQ endpoint to send data on")
ap.add_argument(
"--include", help="Select matching sources (and optionally keys) to "
"include in streamed data",
action='append'
)
ap.add_argument(
"--append-detector-modules", help="combine multiple module sources"
" into one (will only work for AGIPD data currently).",
action='store_true'
)
ap.add_argument(
"--dummy-timestamps", help="create dummy timestamps if the meta-data"
" lacks proper timestamps",
action='store_true'
)
ap.add_argument(
"--use-infiniband", help="Use infiniband interface if available "
"(if a TCP port is specified)",
action='store_true'
)
ap.add_argument(
"-z", "--socket-type", help="ZeroMQ socket type",
choices=['PUB', 'PUSH', 'REP'], default='REP'
)
args = ap.parse_args(argv)

try:
from ..export import serve_data
except ImportError as e:
sys.exit(IMPORT_FAILED_MSG.format(e))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason to do this import here within the function and not at the top of the file? (except of course the fail message constant needs to be defined first)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd usually try to avoid side-effects (like sys.exit()) when loading a module, although it doesn't matter so much for a module defining a CLI like this. It's also handy that --help still works even without the extra dependencies.

We could still have the import at the top like this:

# Top of file
try:
    from ..export import serve_data
except ImportError:
    serve_data = None

# In the function
if serve_data is None:
    sys.exit(msg)

But that looks less neat to me.

run = open_run(args.proposal, args.run, data='all')

if not args.include:
print("Available sources:")
for s in sorted(run.all_sources):
print(f" {s}")
sys.exit("Please select at least one source with --include")

include = []
for pat in args.include:
if '[' in pat:
if not pat.endswith(']'):
sys.exit(f"Missing final ] in {pat!r}")
src_pat, key_pat = pat[:-1].split('[', 1)
include.append((src_pat, key_pat))
else:
# Source pattern only
include.append(pat)

sel = run.select(include)

try:
serve_data(
sel, source_glob=args.source, key_glob=args.key,
append_detector_modules=args.append_detector_modules,
dummy_timestamps=args.dummy_timestamps,
use_infiniband=args.use_infiniband, sock=args.socket_type
)
except KeyboardInterrupt:
print('\nStopped.')

if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def find_version(*parts):
"console_scripts": [
"lsxfel = extra_data.lsxfel:main",
"karabo-bridge-serve-files = extra_data.cli.serve_files:main",
"karabo-bridge-serve-run = extra_data.cli.serve_run:main",
"extra-data-validate = extra_data.validation:main",
"extra-data-make-virtual-cxi = extra_data.cli.make_virtual_cxi:main",
"extra-data-locality = extra_data.locality:main",
Expand Down