Skip to content

Commit

Permalink
Fix the build and setup the flake8 config
Browse files Browse the repository at this point in the history
  • Loading branch information
palikar committed Aug 16, 2020
1 parent f0dc236 commit 927d1bb
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 120
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ max-branches=20

[FORMAT]
max-line-length = 120
disable=locally-disabled,locally-enabled,missing-docstring,duplicate-code,fixme,cyclic-import,redefined-variable-type,stop-iteration-return
disable=locally-disabled,locally-enabled,missing-docstring,duplicate-code,fixme,cyclic-import,redefined-variable-type,stop-iteration-return, too-many-instance-attributes, too-few-public-methods, bare-except, bad-except-order

[TYPECHECK]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ FILTER ?= .
CWD = $(shell pwd)


TEST_PATHS = $(shell find ./rabbitholer -mindepth 1 -maxdepth 1 ! -name '__pycache__')
TEST_PATHS = $(shell find ./rabbitholer -mindepth 1 -maxdepth 1 ! -name '__pycache__' ! -name .mypy_cache)

TEST_FILES = $(shell find tests -mindepth 1 -maxdepth 1 -type f -not -name '__*')

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ config['exchange'] = 'amq.topic'
# the default routing key to be used for sending and receiving mesages
config['routing_key'] = 'home'

# during recording of mesasges, the number of messages after which
# during recording of mesasges, the number of messages after which
# the messages will be synchronized with the file on the filesystem
config['pickler_cache_size'] = 50
```
6 changes: 2 additions & 4 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ echo '<msg>' > ./rabbithole
#+BEGIN_SRC sh
rabbitholer.py record [...] -o ~/car_msgs.p
#+END_SRC
This will save the recorded messages in the file =~/car_msgs.p=. The messages are recorded through [[https://docs.python.org/3/library/pickle.html][pickling]]. Some meta information about the messages is saved (exchange, routing key, timestamps...) so that they can be alter replayed exactly as they originally have been. The file can also be compressed with the =-c= flag. The used compression is [[https://en.wikipedia.org/wiki/Bzip2][bzip2]].
This will save the recorded messages in the file =~/car_msgs.p=. The messages are recorded through [[https://docs.python.org/3/library/pickle.html][pickling]]. Some meta information about the messages is saved (exchange, routing key, timestamps...) so that they can be alter replayed exactly as they originally have been. The file can also be compressed with the =-c= flag. The used compression is [[https://en.wikipedia.org/wiki/Bzip2][bzip2]].

*play*: Replay messages that have been previously saved through the *record* command. As previously said, a timestamp of the messages is saved, so they will be replayed in a temporarily equivalent manner as they have been recorded (i.e. the relative time between two consecutive replayed messages will be same as it was during the recording of those messages). The command can be used like:
#+BEGIN_SRC sh
Expand Down Expand Up @@ -196,12 +196,10 @@ config['exchange'] = 'amq.topic'
# the default routing key to be used for sending and receiving mesages
config['routing_key'] = 'home'

# during recording of mesasges, the number of messages after which
# during recording of mesasges, the number of messages after which
# the messages will be synchronized with the file on the filesystem
config['pickler_cache_size'] = 50
#+END_SRC


# LocalWords: Rabbitholer Todos


10 changes: 3 additions & 7 deletions rabbitholer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ def get_arg_parser():
help='Format string for the printed messages.',
)


printer_parser.add_argument(
'--json', '-j', action='store_true', default=False,
required=False, dest='json',
Expand Down Expand Up @@ -183,14 +182,12 @@ def get_arg_parser():
help='Append the recorded messages to the output given file.',
)


record_parser.add_argument(
'--compress', '-c', dest='compress',
action='store_true', required=False, default=False,
help='Compress the binary file where the messages are saved',
)


play_parser = subparsers.add_parser(
'play',
help='Replay previosly recorded massages',
Expand All @@ -203,7 +200,7 @@ def get_arg_parser():
action='store', required=True, default='rabbitmq_msgs.msg',
help='The input file where the mesasges were previously stored',
)

play_parser.add_argument(
'--compress', '-c', dest='compress',
action='store_true', required=False, default=False,
Expand All @@ -221,14 +218,13 @@ def get_arg_parser():
'file', action='store',
help='The input file where the mesasges were previously stored',
)

list_parser.add_argument(
'--compress', '-c', dest='compress',
action='store_true', required=False, default=False,
help='Signify that the give mesage file is compressed',
)


return parser


Expand All @@ -237,7 +233,7 @@ def monitor(args):
printer = MessagePrinter(args)
with RabbitDumper(args) as dump:
debug_cyan('Monitoring for messages:')
dump.receive(lambda msg: printer.print_message(msg))
dump.receive(printer.print_message)
except KeyboardInterrupt:
print('')
sys.exit(0)
Expand Down
8 changes: 3 additions & 5 deletions rabbitholer/msg_printer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import json


class MessagePrinter:

def __init__(self, args):
self.args = args
self.format = args.format


def format_msg(self, msg):
string = self.format

Expand All @@ -19,14 +19,13 @@ def format_msg(self, msg):

print(string)


def json_msg(self, msg):
@staticmethod
def json_msg(msg):
try:
json_formated_msg = json.dumps(json.loads(msg), indent=2)
print(json_formated_msg)
except json.JSONDecodeError:
print(msg)


def print_message(self, msg):

Expand All @@ -36,4 +35,3 @@ def print_message(self, msg):
self.json_msg(msg.body)
else:
print(msg.body)

4 changes: 1 addition & 3 deletions rabbitholer/rabbit_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ def __init__(self, body, props, exchange, routing_key):
self.exchange = exchange
self.routing_key = routing_key

def __repr__(self):
return self.body


class RabbitDumper:

Expand All @@ -30,6 +27,7 @@ def __init__(self, args):
self.server = args.server

self.callback = None
self.full_msg = None

debug_cyan(f'Trying to open connection to {args.server}')
try:
Expand Down
29 changes: 15 additions & 14 deletions rabbitholer/record_play.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import bz2
import pickle
import sys
import time
import bz2
import os

from rabbitholer.logger import debug
from rabbitholer.logger import debug_cyan
Expand All @@ -25,6 +24,8 @@ def __init__(self, args):
self.cahce_dirty = True
self.cache_size = args.pickler_cache_size if args.pickler_cache_size else 20

self.file_descriptor = None

if not self.append:
with open(self.output, 'wb'):
pass
Expand All @@ -44,31 +45,31 @@ def flush(self):
return
debug_cyan(f'Flushing messages in {self.output}')
for msg in self.cache:
pickle.dump(msg, self.fd)
self.cache.clear()
self.cahce_dirty = False
pickle.dump(msg, self.file_descriptor)
self.cache.clear()
self.cahce_dirty = False

def __enter__(self):
self.fd = MsgPickler.open_file(self.output, 'a', self.args)
self.file_descriptor = MsgPickler.open_file(self.output, 'a', self.args)
return self

def __exit__(self, *_):
self.flush()
self.fd.close()
self.file_descriptor.close()

@staticmethod
def open_file(file, mode, args):
if args.compress:
return bz2.BZ2File(file, mode)
else:
return open(file, mode + 'b')

return open(file, mode + 'b')


def play(args):
with RabbitDumper(args) as dump, MsgPickler.open_file(args.output, 'a', args) as fd:
with RabbitDumper(args) as dump, MsgPickler.open_file(args.output, 'a', args) as file_descriptor:
try:
while 1:
msg = pickle.load(fd)
msg = pickle.load(file_descriptor)

if msg.body is None:
prev_time = msg.timestamp
Expand Down Expand Up @@ -100,10 +101,10 @@ def record(args):

def list_messges(args):
try:
with MsgPickler.open_file(args.file, 'r', args) as fd:
with MsgPickler.open_file(args.file, 'r', args) as file_descriptor:
printer = MessagePrinter(args)
while 1:
msg = pickle.load(fd)
msg = pickle.load(file_descriptor)
if msg.body is None:
continue
printer.print_message(msg)
Expand Down
1 change: 1 addition & 0 deletions rabbitholer/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os


def sanitize_input_variable(var):
var = os.path.expanduser(var)
var = os.path.expandvars(var)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
from distutils.core import setup

import setuptools

setup(
Expand Down
6 changes: 3 additions & 3 deletions tests/args_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_exchange(self):
def test_very_verbose(self):
parser = get_arg_parser()

args = ['-vvv']
args = ['-vv']
parsed = parser.parse_args(args)
self.assertTrue(parsed.very_verbose)

Expand All @@ -95,7 +95,7 @@ def test_very_verbose(self):
def test_verbose(self):
parser = get_arg_parser()

args = ['-vv']
args = ['-v']
parsed = parser.parse_args(args)
self.assertTrue(parsed.verbose)

Expand All @@ -106,7 +106,7 @@ def test_verbose(self):
def test_version(self):
parser = get_arg_parser()
with self.assertRaises(SystemExit):
args = ['-v']
args = ['-V']
parser.parse_args(args)
with self.assertRaises(SystemExit):
args = ['--version']
Expand Down
33 changes: 0 additions & 33 deletions tests/main_functions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from io import StringIO
from unittest import mock

from rabbitholer.main import monitor
from rabbitholer.main import read
from rabbitholer.main import send

Expand Down Expand Up @@ -74,38 +73,6 @@ def test_read(self, RabbitDumper):
.return_value.send.assert_any_call('msg2')
)

@mock.patch(
'rabbitholer.main.RabbitDumper',
autospec=True,
)
def test_monitor(self, RabbitDumper):
RabbitDumper.return_value.__enter__.return_value = mock.Mock()

args = mock.Mock()
args.queue = 'general'
args.exchange = 'general'
args.routing_key = 'general'
args.server = 'general'

monitor(args)

(
RabbitDumper.return_value.__enter__
.return_value.receive.assert_called_once()
)

call = (
RabbitDumper.return_value.__enter__
.return_value.receive.call_args_list[0]
)
callback, kwargs = call

with captured_output() as (out, err):
callback[0]('general')

line = out.getvalue()
self.assertEqual('general\n', line)


if __name__ == '__main__':
unittest.main()

0 comments on commit 927d1bb

Please sign in to comment.