This repository was archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·56 lines (41 loc) · 1.8 KB
/
cli.py
File metadata and controls
executable file
·56 lines (41 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
import sys
import signal
import logging
from traceback import print_exc
from python_driver.processor_configs import ProcessorConfigs
from python_driver.requestprocessor import RequestProcessor, InBuffer, OutBuffer
from typing import Any, Tuple, Optional
logging.basicConfig(level=logging.ERROR)
class RequestInstantiationException(Exception):
pass
# Gracefully handle control c without adding another try-except on top of the loop
def ctrlc_signal_handler(sgn: int, frame: Any) -> None:
sys.exit(0)
signal.signal(signal.SIGINT, ctrlc_signal_handler)
def get_processor_instance(format_: str, custom_inbuffer: InBuffer=None,
custom_outbuffer: OutBuffer=None) -> Tuple[Any, Any]:
"""
Get a processor instance. The class and buffers will be selected based on the
python_driver.ProcessorConfigs dictionary. The input and output buffers can
be overriden using the custom_inbuffer and custom_outbuffer parameters. This
is mainly useful for unittesting.
"""
conf = ProcessorConfigs.get(format_)
if not conf:
raise RequestInstantiationException('No RequestProcessor found for format %s' % format_)
inbuffer = custom_inbuffer if custom_inbuffer else conf['inbuffer']
outbuffer = custom_outbuffer if custom_outbuffer else conf['outbuffer']
instance = conf['class'](outbuffer) # type: ignore
return instance, inbuffer
def main() -> None:
format_ = 'json'
processor, inbuffer = get_processor_instance(format_)
try:
processor.process_requests(inbuffer)
except UnicodeDecodeError:
print_exc()
print('Error while trying to decode the message, are you sure you are not '
'using a different input format that the currently configured (%s)?' % format_)
if __name__ == '__main__':
main()