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