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

add --no-multicast-scouting and --cfg args to examples #413

Merged
merged 2 commits into from
Dec 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 examples/common/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .common import *
83 changes: 83 additions & 0 deletions examples/common/common.py
Original file line number Diff line number Diff line change
@@ -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
50 changes: 5 additions & 45 deletions examples/z_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
50 changes: 5 additions & 45 deletions examples/z_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
49 changes: 4 additions & 45 deletions examples/z_get_liveliness.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
49 changes: 4 additions & 45 deletions examples/z_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading
Loading