Commit 4d62da2f authored by Felix Delattre's avatar Felix Delattre
Browse files

Introduced payload_argument

parent 08401e16
Pipeline #22002 passed with stage
in 1 minute and 9 seconds
......@@ -25,6 +25,7 @@ class ArgumentBase:
def __init__(self):
self.argument = None
self.rabotnik_message = None
self.payload_argument = None
self.help_text = None
def add_argument_to_parser(self, parser: ArgumentParser):
......
......@@ -26,6 +26,7 @@ class ArgumentBuildingId(ArgumentBase):
def __init__(self):
self.argument = "--building-id"
self.rabotnik_message = "building"
self.payload_argument = "building_id"
self.help_text = "Single `building_id` as integer"
async def execute(self, message_bus, argument, value):
......@@ -33,4 +34,6 @@ class ArgumentBuildingId(ArgumentBase):
logger.info(
"Dispatching `%s` with `%s` on `%s`.", argument, value, self.rabotnik_message
)
await message_bus.send(message=self.rabotnik_message, payload={argument: value})
await message_bus.send(
message=self.rabotnik_message, payload={self.payload_argument: value}
)
......@@ -27,16 +27,23 @@ class ArgumentBuildingIdsFile(ArgumentBase):
def __init__(self):
self.argument = "--building-ids-file"
self.rabotnik_message = "building"
self.payload_argument = "building_id"
self.help_text = "File with many `building_id` in every new line"
async def execute(self, message_bus, argument, filename):
with open(filename) as f:
data = f.read()
data = f.readlines()
for line in data:
value = line.strip()
if value != "":
logger.info(
"Dispatching `%s` with `%s` on `%s`.", argument, value, self.rabotnik_message
"Dispatching `%s` with `%s` on `%s`.",
argument,
value,
self.rabotnik_message,
)
await message_bus.send(
message=self.rabotnik_message, payload={self.payload_argument: value}
)
await message_bus.send(message=self.rabotnik_message, payload={argument: value})
......@@ -21,7 +21,7 @@ import sys
import asyncio
import logging
from rabotnik.bus import MessageBus, PASSWORD, USERNAME, URL
from rabotnik.bus import MessageBus
from .argument_factory import ArgumentFactory
......@@ -56,7 +56,7 @@ class RCLI:
# Find the argument to be put on the message bus
for key, value in args.items():
if key in self.arguments.arguments:
if value is not None and key in self.arguments.arguments:
rabotnik_argument = key
rabotnik_value = value
......@@ -83,6 +83,8 @@ def main(args: list = None, message_bus: MessageBus = None):
"""
logging.basicConfig(level=logging.INFO)
loop = asyncio.get_event_loop()
args = args or sys.argv[1:]
parser = argparse.ArgumentParser()
......@@ -91,20 +93,16 @@ def main(args: list = None, message_bus: MessageBus = None):
group = parser.add_argument_group("message bus")
group.add_argument(
"--hostname", type=str, default=URL, help=f"set message bus hostname. Default: {URL}"
)
group.add_argument("--hostname", type=str, help="Set message bus hostname.")
group.add_argument(
"--username",
type=str,
default=USERNAME,
help=f"set message bus username. Default: {USERNAME}",
help="Set message bus username.",
)
group.add_argument(
"--password",
type=str,
default=PASSWORD,
help=f"set message bus password. Default: {PASSWORD}",
help="Set message bus password.",
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="verbose mode", dest="verbose"
......@@ -117,8 +115,11 @@ def main(args: list = None, message_bus: MessageBus = None):
logger.setLevel(logging.DEBUG)
if message_bus is None:
message_bus = MessageBus(
url=args.hostname, username=args.username, password=args.password
message_bus = MessageBus()
loop.create_task(
message_bus.connect(
host=args.hostname, username=args.username, password=args.password
)
)
rcli.set_message_bus(message_bus)
......@@ -126,5 +127,4 @@ def main(args: list = None, message_bus: MessageBus = None):
# Parse `args` to dictionary.
args_dict = vars(args)
loop = asyncio.get_event_loop()
loop.run_until_complete(rcli.execute(args_dict))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment