Skip to content

Commit 39aca6f

Browse files
authored
Merge pull request #166 from SNEWS2/publish-default-env-detector-name
Use environment detector name when publishing
2 parents 46ea140 + e141943 commit 39aca6f

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

docs/user/command_line_interface.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ Sent message to kafka://kafka.scimma.org/snews.experiments-firedrill
149149
--------------------------------
150150
```
151151

152+
**Note**: For authentication purposes, the detector name is read from the environment file. If the detector name in the JSON file does not match the detector name in the environment file, a warning is printed and the detector name from the environment file is used.
153+
152154
## Retraction Messages
153155

154156
It can happen that user publishes a message by accident or with wrong input. In these cases `snews_pt` allows for retraction messages.

snews_pt/__main__.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ def main(ctx, env):
5353
show_default="True",
5454
help="Whether to use firedrill brokers or default ones",
5555
)
56+
@click.option(
57+
"--force",
58+
is_flag=True,
59+
default=False,
60+
show_default="False",
61+
help="Force json file to overwrite env variables",
62+
)
5663
@click.option(
5764
"--verbose",
5865
"-v",
@@ -62,7 +69,7 @@ def main(ctx, env):
6269
)
6370
@click.argument("file", nargs=-1)
6471
@click.pass_context
65-
def publish(ctx, file, firedrill, verbose):
72+
def publish(ctx, file, firedrill, force, verbose):
6673
"""Publish a message using snews_pub, multiple files are allowed
6774
6875
$: snews_pt publish my_json_message.json
@@ -73,11 +80,40 @@ def publish(ctx, file, firedrill, verbose):
7380
If no file is given it can still submit dummy messages with default values
7481
"""
7582

83+
def _check_detector_name(json_detector_name, env_detector_name, force):
84+
"""Check if detector name in json file matches the environment detector name.
85+
Defaults to env name. The json file name can be used with the --force flag
86+
"""
87+
88+
if force:
89+
click.secho(f"Forcing detector name from json file: "
90+
f"{click.style(json_detector_name, bold = True)}")
91+
return json_detector_name
92+
93+
if json_detector_name != env_detector_name:
94+
click.secho(
95+
f"{click.style('Warning:',bg = 'red')} "
96+
f"Detector name in JSON file ({click.style(
97+
json_detector_name, bold = True)}) "
98+
f"does not match the environment detector name "
99+
f"{click.style(env_detector_name, bold = True)}",
100+
fg="red", color="black"
101+
)
102+
click.secho(
103+
f"{click.style('Warning:',bg = 'red')} "
104+
f"Using environment detector name "
105+
f"{click.style(env_detector_name, bold = True)} "
106+
f"for this message. "
107+
f"To change the detector name, use the `snews_pt set-name` command.",
108+
)
109+
return env_detector_name
110+
76111
if firedrill:
77112
publisher = Publisher(kafka_topic=os.getenv("FIREDRILL_OBSERVATION_TOPIC"))
78113
else:
79114
publisher = Publisher(kafka_topic=os.getenv("OBSERVATION_TOPIC"))
80-
115+
116+
env_detector_name=ctx.obj["DETECTOR_NAME"]
81117
for filename in file:
82118
if filename.endswith(".json"):
83119
try:
@@ -87,8 +123,12 @@ def publish(ctx, file, firedrill, verbose):
87123
)
88124
snews_messages = messages.create_messages(**json_data)
89125
for message in snews_messages:
126+
_detector_name = _check_detector_name(
127+
message.detector_name, env_detector_name, force)
128+
message.detector_name = _detector_name
90129
publisher.add_message(message)
91130
publisher.send(verbose=verbose)
131+
92132
except FileNotFoundError:
93133
click.echo(f"Error: File not found: {filename}")
94134
except json.JSONDecodeError as e:

0 commit comments

Comments
 (0)