From 1563b112482d4e55831f207ddfab4f2950d393e8 Mon Sep 17 00:00:00 2001 From: DenisBiryukov91 <155981813+DenisBiryukov91@users.noreply.github.com> Date: Wed, 11 Dec 2024 12:32:46 +0100 Subject: [PATCH] add --no-multicast-scouting and --cfg args to examples (#413) * add --no-multicast-scouting flag to examples * add --cfg arg to examples --- examples/common/__init__.py | 1 + examples/common/common.py | 83 ++++++++++++++++++++++++++++++++++++ examples/z_delete.py | 50 +++------------------- examples/z_get.py | 50 +++------------------- examples/z_get_liveliness.py | 49 ++------------------- examples/z_info.py | 49 ++------------------- examples/z_liveliness.py | 49 ++------------------- examples/z_ping.py | 56 +++++------------------- examples/z_pong.py | 49 ++------------------- examples/z_pub.py | 49 ++------------------- examples/z_pub_thr.py | 49 ++------------------- examples/z_pull.py | 49 ++------------------- examples/z_put.py | 49 ++------------------- examples/z_put_float.py | 49 ++------------------- examples/z_querier.py | 50 +++------------------- examples/z_queryable.py | 49 ++------------------- examples/z_storage.py | 49 ++------------------- examples/z_sub.py | 49 ++------------------- examples/z_sub_liveliness.py | 49 ++------------------- examples/z_sub_queued.py | 49 ++------------------- examples/z_sub_thr.py | 49 ++------------------- 21 files changed, 170 insertions(+), 855 deletions(-) create mode 100644 examples/common/__init__.py create mode 100644 examples/common/common.py diff --git a/examples/common/__init__.py b/examples/common/__init__.py new file mode 100644 index 0000000..55e5f84 --- /dev/null +++ b/examples/common/__init__.py @@ -0,0 +1 @@ +from .common import * diff --git a/examples/common/common.py b/examples/common/common.py new file mode 100644 index 0000000..43a8642 --- /dev/null +++ b/examples/common/common.py @@ -0,0 +1,83 @@ +import argparse +import json + +import zenoh + + +def add_config_arguments(parser: argparse.ArgumentParser): + parser.add_argument( + "--mode", + "-m", + dest="mode", + choices=["peer", "client"], + type=str, + help="The zenoh session mode.", + ) + parser.add_argument( + "--connect", + "-e", + dest="connect", + metavar="ENDPOINT", + action="append", + type=str, + help="Endpoints to connect to.", + ) + parser.add_argument( + "--listen", + "-l", + dest="listen", + metavar="ENDPOINT", + action="append", + type=str, + help="Endpoints to listen on.", + ) + parser.add_argument( + "--config", + "-c", + dest="config", + metavar="FILE", + type=str, + help="A configuration file.", + ) + parser.add_argument( + "--no-multicast-scouting", + dest="no_multicast_scouting", + default=False, + action="store_true", + help="Disable multicast scouting.", + ) + parser.add_argument( + "--cfg", + dest="cfg", + metavar="CFG", + default=[], + action="append", + type=str, + help="Allows arbitrary configuration changes as column-separated KEY:VALUE pairs. Where KEY must be a valid config path and VALUE must be a valid JSON5 string that can be deserialized to the expected type for the KEY field. Example: --cfg='transport/unicast/max_links:2'.", + ) + + +def get_config_from_args(args) -> zenoh.Config: + conf = ( + zenoh.Config.from_file(args.config) + if args.config is not None + else zenoh.Config() + ) + if args.mode is not None: + conf.insert_json5("mode", json.dumps(args.mode)) + if args.connect is not None: + conf.insert_json5("connect/endpoints", json.dumps(args.connect)) + if args.listen is not None: + conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + if args.no_multicast_scouting: + conf.insert_json5("scouting/multicast/enabled", json.dumps(False)) + + for c in args.cfg: + try: + [key, value] = c.split(":") + except: + print(f"`--cfg` argument: expected KEY:VALUE pair, got {c}") + raise + conf.insert_json5(key, value) + + return conf diff --git a/examples/z_delete.py b/examples/z_delete.py index 1c22690..411d3d8 100644 --- a/examples/z_delete.py +++ b/examples/z_delete.py @@ -29,33 +29,10 @@ def main(conf: zenoh.Config, key: str): import argparse import json + import common + parser = argparse.ArgumentParser(prog="z_delete", description="zenoh put example") - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--key", "-k", @@ -64,27 +41,10 @@ def main(conf: zenoh.Config, key: str): type=str, help="The key expression matching resources to delete.", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) + key = args.key main(conf, key) diff --git a/examples/z_get.py b/examples/z_get.py index 6ba63f0..4149a88 100644 --- a/examples/z_get.py +++ b/examples/z_get.py @@ -42,33 +42,10 @@ def main( import argparse import json + import common + parser = argparse.ArgumentParser(prog="z_get", description="zenoh get example") - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--selector", "-s", @@ -101,27 +78,10 @@ def main( type=float, help="The query timeout", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) + target = { "ALL": zenoh.QueryTarget.ALL, "BEST_MATCHING": zenoh.QueryTarget.BEST_MATCHING, diff --git a/examples/z_get_liveliness.py b/examples/z_get_liveliness.py index 520864a..08b8522 100644 --- a/examples/z_get_liveliness.py +++ b/examples/z_get_liveliness.py @@ -35,35 +35,12 @@ def main(conf: zenoh.Config, key: str, timeout: float): import argparse import json + import common + parser = argparse.ArgumentParser( prog="z_get_liveliness", description="zenoh put example" ) - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--key", "-k", @@ -80,26 +57,8 @@ def main(conf: zenoh.Config, key: str, timeout: float): type=float, help="The query timeout.", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.key, args.timeout) diff --git a/examples/z_info.py b/examples/z_info.py index a84c718..4b3edf2 100644 --- a/examples/z_info.py +++ b/examples/z_info.py @@ -31,53 +31,12 @@ def main(conf: zenoh.Config): import argparse import json + import common + parser = argparse.ArgumentParser(prog="z_info", description="zenoh info example") - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) + common.add_config_arguments(parser) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf) diff --git a/examples/z_liveliness.py b/examples/z_liveliness.py index 8f25a6f..6e2cf8f 100644 --- a/examples/z_liveliness.py +++ b/examples/z_liveliness.py @@ -36,35 +36,12 @@ def main(conf: zenoh.Config, key: str): import argparse import json + import common + parser = argparse.ArgumentParser( prog="z_liveliness", description="zenoh put example" ) - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--key", "-k", @@ -73,26 +50,8 @@ def main(conf: zenoh.Config, key: str): type=str, help="The key expression to write.", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.key) diff --git a/examples/z_ping.py b/examples/z_ping.py index 23e3688..604c863 100644 --- a/examples/z_ping.py +++ b/examples/z_ping.py @@ -51,41 +51,10 @@ def main(conf: zenoh.Config, payload_size: int, warmup: int, samples: int): import argparse import json + import common + parser = argparse.ArgumentParser(prog="z_ping", description="zenoh get example") - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) + common.add_config_arguments(parser) parser.add_argument( "--warmup", "-w", @@ -110,18 +79,15 @@ def main(conf: zenoh.Config, payload_size: int, warmup: int, samples: int): type=int, help="Sets the size of the payload to publish.", ) + parser.add_argument( + "--no-multicast-scouting", + dest="no_multicast_scouting", + default=False, + action="store_true", + help="Disable multicast scouting.", + ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.payload_size, args.warmup, args.samples) diff --git a/examples/z_pong.py b/examples/z_pong.py index 5547816..0a2f3ac 100644 --- a/examples/z_pong.py +++ b/examples/z_pong.py @@ -39,41 +39,10 @@ def main(conf: zenoh.Config, express: bool): import argparse import json + import common + parser = argparse.ArgumentParser(prog="z_pong", description="zenoh get example") - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) + common.add_config_arguments(parser) parser.add_argument( "--express", dest="express", @@ -84,16 +53,6 @@ def main(conf: zenoh.Config, express: bool): ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.express) diff --git a/examples/z_pub.py b/examples/z_pub.py index 5917771..001ca8d 100644 --- a/examples/z_pub.py +++ b/examples/z_pub.py @@ -43,33 +43,10 @@ def main( import itertools import json + import common + parser = argparse.ArgumentParser(prog="z_pub", description="zenoh pub example") - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--key", "-k", @@ -96,26 +73,8 @@ def main( default=1.0, help="Interval between each put", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.key, args.payload, args.iter, args.interval) diff --git a/examples/z_pub_thr.py b/examples/z_pub_thr.py index 68b7f96..82cb671 100644 --- a/examples/z_pub_thr.py +++ b/examples/z_pub_thr.py @@ -39,58 +39,17 @@ def main(conf: zenoh.Config, payload_size: int): import argparse import json + import common + parser = argparse.ArgumentParser( prog="z_pub_thr", description="zenoh throughput pub example" ) - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "payload_size", type=int, help="Sets the size of the payload to publish." ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.payload_size) diff --git a/examples/z_pull.py b/examples/z_pull.py index ad70315..4743a26 100644 --- a/examples/z_pull.py +++ b/examples/z_pull.py @@ -45,33 +45,10 @@ def main(conf: zenoh.Config, key: str, size: int, interval: int): import argparse import json + import common + parser = argparse.ArgumentParser(prog="z_pull", description="zenoh pull example") - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--key", "-k", @@ -80,14 +57,6 @@ def main(conf: zenoh.Config, key: str, size: int, interval: int): type=str, help="The key expression matching resources to pull.", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) parser.add_argument( "--size", dest="size", default=3, type=int, help="The size of the ringbuffer" ) @@ -100,16 +69,6 @@ def main(conf: zenoh.Config, key: str, size: int, interval: int): ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.key, args.size, args.interval) diff --git a/examples/z_put.py b/examples/z_put.py index 645164a..e3b830c 100644 --- a/examples/z_put.py +++ b/examples/z_put.py @@ -31,33 +31,10 @@ def main(conf: zenoh.Config, key: str, payload: str): import argparse import json + import common + parser = argparse.ArgumentParser(prog="z_put", description="zenoh put example") - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--key", "-k", @@ -74,26 +51,8 @@ def main(conf: zenoh.Config, key: str, payload: str): type=str, help="The payload to write.", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.key, args.payload) diff --git a/examples/z_put_float.py b/examples/z_put_float.py index c621c43..c49f795 100644 --- a/examples/z_put_float.py +++ b/examples/z_put_float.py @@ -32,35 +32,12 @@ def main(conf: zenoh.Config, key: str, payload: float): import argparse import json + import common + parser = argparse.ArgumentParser( prog="z_put_float", description="zenoh put example" ) - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--key", "-k", @@ -77,26 +54,8 @@ def main(conf: zenoh.Config, key: str, payload: float): type=float, help="The payload to write.", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.key, args.payload) diff --git a/examples/z_querier.py b/examples/z_querier.py index a71ec0d..5983494 100644 --- a/examples/z_querier.py +++ b/examples/z_querier.py @@ -58,35 +58,12 @@ def main( import argparse import json + import common + parser = argparse.ArgumentParser( prog="z_querier", description="zenoh querier example" ) - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--selector", "-s", @@ -119,30 +96,13 @@ def main( type=float, help="The query timeout", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) parser.add_argument( "--iter", dest="iter", type=int, help="How many gets to perform" ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) + target = { "ALL": zenoh.QueryTarget.ALL, "BEST_MATCHING": zenoh.QueryTarget.BEST_MATCHING, diff --git a/examples/z_queryable.py b/examples/z_queryable.py index ef0d50c..2456531 100644 --- a/examples/z_queryable.py +++ b/examples/z_queryable.py @@ -50,35 +50,12 @@ def queryable_callback(query): import argparse import json + import common + parser = argparse.ArgumentParser( prog="z_queryable", description="zenoh queryable example" ) - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--key", "-k", @@ -102,26 +79,8 @@ def queryable_callback(query): action="store_true", help="Declare the queryable as complete w.r.t. the key expression.", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.key, args.payload, args.complete) diff --git a/examples/z_storage.py b/examples/z_storage.py index f34f80e..2b22ca0 100644 --- a/examples/z_storage.py +++ b/examples/z_storage.py @@ -64,35 +64,12 @@ def main(conf: zenoh.Config, key: str, complete: bool): import argparse import json + import common + parser = argparse.ArgumentParser( prog="z_storage", description="zenoh storage example" ) - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--key", "-k", @@ -108,26 +85,8 @@ def main(conf: zenoh.Config, key: str, complete: bool): action="store_true", help="Declare the storage as complete w.r.t. the key expression.", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.key, args.complete) diff --git a/examples/z_sub.py b/examples/z_sub.py index 52de869..31e24e9 100644 --- a/examples/z_sub.py +++ b/examples/z_sub.py @@ -42,33 +42,10 @@ def listener(sample: zenoh.Sample): import argparse import json + import common + parser = argparse.ArgumentParser(prog="z_sub", description="zenoh sub example") - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--key", "-k", @@ -77,26 +54,8 @@ def listener(sample: zenoh.Sample): type=str, help="The key expression to subscribe to.", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.key) diff --git a/examples/z_sub_liveliness.py b/examples/z_sub_liveliness.py index e5f9fd0..23e85e6 100644 --- a/examples/z_sub_liveliness.py +++ b/examples/z_sub_liveliness.py @@ -39,35 +39,12 @@ def main(conf: zenoh.Config, key: str, history: bool): import argparse import json + import common + parser = argparse.ArgumentParser( prog="z_sub_liveliness", description="zenoh sub example" ) - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--key", "-k", @@ -83,26 +60,8 @@ def main(conf: zenoh.Config, key: str, history: bool): type=bool, help="Get historical liveliness tokens.", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.key, args.history) diff --git a/examples/z_sub_queued.py b/examples/z_sub_queued.py index c664859..d78d248 100644 --- a/examples/z_sub_queued.py +++ b/examples/z_sub_queued.py @@ -34,35 +34,12 @@ def main(conf: zenoh.Config, key: str): import argparse import json + import common + parser = argparse.ArgumentParser( prog="z_sub_queued", description="zenoh sub example" ) - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--key", "-k", @@ -71,26 +48,8 @@ def main(conf: zenoh.Config, key: str): type=str, help="The key expression to subscribe to.", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.key) diff --git a/examples/z_sub_thr.py b/examples/z_sub_thr.py index 51f1530..ae7aa13 100644 --- a/examples/z_sub_thr.py +++ b/examples/z_sub_thr.py @@ -63,35 +63,12 @@ def report(): import argparse import json + import common + parser = argparse.ArgumentParser( prog="z_sub_thr", description="zenoh throughput sub example" ) - parser.add_argument( - "--mode", - "-m", - dest="mode", - choices=["peer", "client"], - type=str, - help="The zenoh session mode.", - ) - parser.add_argument( - "--connect", - "-e", - dest="connect", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to connect to.", - ) - parser.add_argument( - "--listen", - "-l", - dest="listen", - metavar="ENDPOINT", - action="append", - type=str, - help="Endpoints to listen on.", - ) + common.add_config_arguments(parser) parser.add_argument( "--number", "-n", @@ -101,26 +78,8 @@ def report(): type=int, help="Number of messages in each throughput measurements.", ) - parser.add_argument( - "--config", - "-c", - dest="config", - metavar="FILE", - type=str, - help="A configuration file.", - ) args = parser.parse_args() - conf = ( - zenoh.Config.from_file(args.config) - if args.config is not None - else zenoh.Config() - ) - if args.mode is not None: - conf.insert_json5("mode", json.dumps(args.mode)) - if args.connect is not None: - conf.insert_json5("connect/endpoints", json.dumps(args.connect)) - if args.listen is not None: - conf.insert_json5("listen/endpoints", json.dumps(args.listen)) + conf = common.get_config_from_args(args) main(conf, args.number)