예제 #1
0
def main():
    parser = get_arg_parser()
    args = parser.parse_args()

    setup_logging(args)

    debug(f'{VERSION_MSG[0]}')

    if args.command is None:
        parser.print_help()
        sys.exit(0)

    if not os.path.isdir(os.path.expanduser('~/.config/rabbitholer')):
        os.makedirs(os.path.expanduser('~/.config/rabbitholer'))

    if not os.path.isfile(
            os.path.expanduser('~/.config/rabbitholer/config.py'), ):
        with open(os.path.expanduser('~/.config/rabbitholer/config.py'), 'a'):
            pass

    config_file = '~/.config/rabbitholer/config.py'
    if args.config is not None:
        config_file = args.config

    config_file = os.path.expanduser(config_file)
    if not os.path.isfile(config_file):
        print(f'Invalid config file: {config_file}')
        sys.exit(1)

    debug(f'Config file: {config_file}')

    config_spec = importlib.util.spec_from_file_location(
        'config.module',
        config_file,
    )
    config = importlib.util.module_from_spec(config_spec)
    config_spec.loader.exec_module(config)

    if not hasattr(config, 'config'):
        debug('The configuration file does not define a config dict')

    for key in vars(args):
        value = getattr(args, key)
        if value is not None or key not in config.config.keys():
            config.config[key] = value

    config.config.update(
        (k, os.environ[k]) for k in config.config.keys() & os.environ.keys())

    args = argparse.Namespace(**config.config)

    debug(f'Command called: {args.command}')
    debug(f'Arguments: {vars(args)}')

    COMMANDS[args.command](args)
예제 #2
0
 def send(self, msg, headers=None, key=None):
     try:
         props = pika.spec.BasicProperties(expiration='30000', headers=headers)
         self.channel.basic_publish(
             exchange=self.exchange if self.exchange else '',
             routing_key=self.routing_key if not key else key,
             body=msg,
             properties=props,
         )
         debug('Message send!')
     except pika.exceptions.ConnectionClosedByBroker as err:
         print(f'AMQP Connection closed by the broker: {err}')
     except pika.exceptions.AMQPChannelError as err:
         print(f'AMQP channel error: {err}, stopping...')
     except pika.exceptions.AMQPConnectionError as err:
         print(f'AMQP Connection closed: {err}')
예제 #3
0
def play(args):
    with RabbitDumper(args) as dump, MsgPickler.open_file(
            args.output, 'a', args) as file_descriptor:
        try:
            while 1:
                msg = pickle.load(file_descriptor)

                if msg.body is None:
                    prev_time = msg.timestamp
                    continue

                debug(
                    f'Sending message with key {msg.routing_key} to exchange {msg.routing_key}'
                )
                dump.send(f'{msg.body}',
                          headers=msg.props,
                          key=msg.routing_key)
                time.sleep(msg.timestamp - prev_time)
                prev_time = msg.timestamp
        except EOFError:
            pass
예제 #4
0
def pipe(args):
    path = args.pipe_name
    debug_cyan(f'Trying to open pipe on {path}')
    if os.path.exists(path) or os.path.isfile(path):
        print('The given path is already exists: {}'.format(path), )
        sys.exit(1)

    with RabbitDumper(args) as dump:
        try:
            os.mkfifo(path)
            debug('Pipe creted')
            with open(path) as pipe_fd:
                while True:
                    message = pipe_fd.readline()
                    if message:
                        dump.send(message)
                        time.sleep(0.5)
        except OSError as err:
            print('Error wile opening pipe: {}'.format(err), )
        finally:
            debug('Deliting trhe named pipe')
            os.remove(path)
예제 #5
0
    def receive(self, callback, full_msg=False):
        self.callback = callback
        self.full_msg = full_msg

        if self.queue:
            self.queue = self.channel.queue_declare(queue=self.queue, auto_delete=True).method.queue
        else:
            self.queue = self.channel.queue_declare(queue='', auto_delete=True).method.queue

        debug(f'Declared queue with name {self.queue}')

        if self.exchange is not None:
            self.channel.queue_bind(
                exchange=self.exchange,
                queue=self.queue,
                routing_key=self.routing_key,
            )
            debug(f'Queue was bound to the exchange {self.exchange} with\
routing key {self.routing_key}')

        debug_cyan(f'Starting to recieve messages from {self.queue}')

        try:
            self.channel.basic_consume(
                queue=self.queue, on_message_callback=self.new_msg,
                auto_ack=True,
            )
            self.channel.start_consuming()
        except pika.exceptions.ConnectionClosedByBroker as err:
            print(f'AMQP Connection closed by the broker: {err}.')
        except pika.exceptions.AMQPChannelError as err:
            print(f'AMQP channel error: {err}.')
        except pika.exceptions.AMQPConnectionError as err:
            print(f'AMQP Connection closed: {err}.')
        except pika.exceptions.StreamLostError as err:
            print(f'AMQP Stream lost: {err}.')
예제 #6
0
    def __init__(self, args):

        self.args = args

        self.exchange = args.exchange
        self.queue = args.queue
        self.routing_key = args.routing_key
        self.server = args.server

        self.callback = None
        self.full_msg = None

        debug_cyan(f'Trying to open connection to {args.server}')
        try:
            try:
                self.connection = pika.BlockingConnection(
                    pika.ConnectionParameters(host=args.server),
                )
            except:  # noqa: E722
                print('Establishing AMQP Connection failed! Check the server!')
                sys.exit(1)

            debug('Connection opend')

            self.channel = self.connection.channel()
            self.channel.basic_qos(prefetch_count=1)

        except pika.exceptions.ConnectionClosedByBroker as err:
            print(f'AMQP Connection closed by the broker: {err}')
            sys.exit(1)
        except pika.exceptions.AMQPChannelError as err:
            print(f'AMQP channel error: {err}, stopping...')
            sys.exit(1)
        except pika.exceptions.AMQPConnectionError as err:
            print(f'AMQP Connection closed: {err}')
            sys.exit(1)