From ee5c030cbfac2ff7176aa4a8dac100da5a1fb2cb Mon Sep 17 00:00:00 2001 From: Gabriel Pettier Date: Sun, 22 Sep 2019 12:57:46 +0200 Subject: [PATCH] codestyle fixes --- oscpy/cli.py | 21 +++++++++++++-------- oscpy/parser.py | 8 ++++++-- oscpy/server.py | 10 ++++++---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/oscpy/cli.py b/oscpy/cli.py index 011ef4a..99b55b1 100644 --- a/oscpy/cli.py +++ b/oscpy/cli.py @@ -15,7 +15,7 @@ def _send(options): def _parse(s): try: return literal_eval(s) - except: + except (ValueError, SyntaxError): return s stats = Stats() @@ -59,7 +59,7 @@ def dump(address, *values): return osc -def _dump(options): # pragma: no cover +def _dump(options): # pragma: no cover osc = __dump(options) try: while True: @@ -82,7 +82,8 @@ def init_parser(): help='port to send message to.') send.add_argument('--encoding', '-e', action='store', default='utf-8', help='how to encode the strings') - send.add_argument('--encoding_errors', '-E', action='store', default='replace', + send.add_argument('--encoding_errors', '-E', action='store', + default='replace', help='how to treat string encoding issues') send.add_argument('--safer', '-s', action='store_true', help='wait a little after sending message') @@ -92,9 +93,10 @@ def init_parser(): send.add_argument('address', action='store', help='OSC address to send the message to.') send.add_argument('message', nargs='*', - help='content of the message, separated by spaces.') + help='content of the message, separated by spaces.') - dump = subparser.add_parser('dump', help='listen for messages and print them') + dump = subparser.add_parser('dump', + help='listen for messages and print them') dump.set_defaults(func=_dump) dump.add_argument('--host', '-H', action='store', default='localhost', help='host (ip or name) to send message to.') @@ -102,14 +104,17 @@ def init_parser(): help='port to send message to.') dump.add_argument('--encoding', '-e', action='store', default='utf-8', help='how to encode the strings') - dump.add_argument('--encoding_errors', '-E', action='store', default='replace', + dump.add_argument('--encoding_errors', '-E', action='store', + default='replace', help='how to treat string encoding issues') - # bridge = parser.add_parser('bridge', help='listen for messages and redirect them to a server') + # TODO + # bridge = parser.add_parser('bridge', help='listen for messages and + # redirect them to a server') return parser -def main(): # pragma: no cover +def main(): # pragma: no cover parser = init_parser() options = parser.parse_args() exit(options.func(options)) diff --git a/oscpy/parser.py b/oscpy/parser.py index 4817e83..e58544e 100644 --- a/oscpy/parser.py +++ b/oscpy/parser.py @@ -46,6 +46,7 @@ MidiTuple = namedtuple('MidiTuple', 'port_id status_byte data1 data2') + def padded(l, n=4): """Return the size to pad a thing to. @@ -237,7 +238,9 @@ def format_message(address, values, encoding='', encoding_errors='strict'): if cls_or_value == UNICODE: if not encoding: - raise TypeError(u"Can't format unicode string without encoding") + raise TypeError( + u"Can't format unicode string without encoding" + ) cls_or_value = bytes value = ( @@ -274,7 +277,8 @@ def format_message(address, values, encoding='', encoding_errors='strict'): tags, *( ( - encode_cache.get(v) + NULL if isinstance(v, UNICODE) and encoding + encode_cache.get(v) + NULL + if isinstance(v, UNICODE) and encoding else (v + NULL) if t in (b's', b'b') else format_midi(v) if isinstance(v, MidiTuple) else v diff --git a/oscpy/server.py b/oscpy/server.py index 25104cb..ac72b39 100644 --- a/oscpy/server.py +++ b/oscpy/server.py @@ -62,7 +62,7 @@ def __init__( """Create an OSCThreadServer. - `timeout` is a number of seconds used as a time limit for - select() calls in the listening thread, optiomal, defaults to + select() calls in the listening thread, optional, defaults to 0.01. - `drop_late_bundles` instruct the server not to dispatch calls from bundles that arrived after their timetag value. @@ -327,7 +327,7 @@ def _listen(self): continue else: try: - read, write, error = select(self.sockets, [], [], self.timeout) + read, _, _ = select(self.sockets, [], [], self.timeout) except (ValueError, socket.error): continue @@ -441,7 +441,8 @@ def send_bundle( return stats def get_sender(self): - """Return the socket, ip and port of the message that is currently being managed. + """Return the socket, ip and port of the message that is + currently being managed. Warning:: this method should only be called from inside the handling @@ -568,8 +569,9 @@ def bind_meta_routes(self, sock=None): """ self.bind(b'/_oscpy/version', self._get_version, sock=sock) self.bind(b'/_oscpy/routes', self._get_routes, sock=sock) - self.bind(b'/_oscpy/stats/received', self._get_stats_received, sock=sock) self.bind(b'/_oscpy/stats/sent', self._get_stats_sent, sock=sock) + self.bind(b'/_oscpy/stats/received', + self._get_stats_received, sock=sock) def _get_version(self, port, *args): self.answer(