Skip to content

Commit 8a47f57

Browse files
committed
remove unused imports; PEP 8 compliance
1 parent 0072859 commit 8a47f57

File tree

2 files changed

+85
-57
lines changed

2 files changed

+85
-57
lines changed

nrepl/__init__.py

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
#!/usr/bin/env python
22

3-
import socket, re, sys
3+
import socket
44
import nrepl.bencode as bencode
55
import threading
66
try:
77
from urlparse import urlparse, ParseResult
88
except ImportError:
99
from urllib.parse import urlparse, ParseResult
1010

11-
def _bencode_connect (uri):
11+
12+
def _bencode_connect(uri):
1213
s = socket.create_connection(uri.netloc.split(":"))
1314
# TODO I don't think .close() will propagate to the socket automatically...
1415
f = s.makefile('rw')
1516
return bencode.BencodeIO(f)
1617

17-
def _match_criteria (criteria, msg):
18+
19+
def _match_criteria(criteria, msg):
1820
for k, v in criteria.items():
1921
mv = msg.get(k, None)
2022
if isinstance(v, set):
@@ -26,59 +28,67 @@ def _match_criteria (criteria, msg):
2628
return False
2729
return True
2830

29-
class WatchableConnection (object):
30-
def __init__ (self, IO):
31-
"""Create a new WatchableConnection with an nREPL message transport
31+
32+
class WatchableConnection(object):
33+
def __init__(self, IO):
34+
"""
35+
Create a new WatchableConnection with an nREPL message transport
3236
supporting `read()` and `write()` methods that return and accept nREPL
33-
messages, e.g. bencode.BencodeIO."""
37+
messages, e.g. bencode.BencodeIO.
38+
"""
3439
self._IO = IO
3540
self._watches = {}
3641
self._watches_lock = threading.RLock()
37-
class Monitor (threading.Thread):
38-
def run (_):
42+
43+
class Monitor(threading.Thread):
44+
def run(_):
3945
watches = None
4046
for incoming in self._IO:
4147
with self._watches_lock:
4248
watches = dict(self._watches)
4349
for key, (pred, callback) in watches.items():
44-
if pred(incoming):
50+
if pred(incoming):
4551
callback(incoming, self, key)
4652
self._thread = Monitor()
4753
self._thread.daemon = True
4854
self._thread.start()
4955

50-
def close (self):
56+
def close(self):
5157
self._IO.close()
5258

53-
def send (self, message):
59+
def send(self, message):
5460
"Send an nREPL message."
5561
self._IO.write(message)
5662

57-
def unwatch (self, key):
63+
def unwatch(self, key):
5864
"Removes the watch previously registered with [key]."
5965
with self._watches_lock:
6066
self._watches.pop(key, None)
6167

62-
def watch (self, key, criteria, callback):
63-
"""Registers a new watch under [key] (which can be used with `unwatch()`
68+
def watch(self, key, criteria, callback):
69+
"""
70+
Registers a new watch under [key] (which can be used with `unwatch()`
6471
to remove the watch) that filters messages using [criteria] (may be a
6572
predicate or a 'criteria dict' [see the README for more info there]).
6673
Matching messages are passed to [callback], which must accept three
6774
arguments: the matched incoming message, this instance of
6875
`WatchableConnection`, and the key under which the watch was
69-
registered."""
76+
registered.
77+
"""
7078
if hasattr(criteria, '__call__'):
7179
pred = criteria
7280
else:
73-
pred = lambda incoming: _match_criteria(criteria, incoming)
81+
pred = lambda incoming: _match_criteria(criteria, incoming)
7482
with self._watches_lock:
7583
self._watches[key] = (pred, callback)
7684

7785
# others can add in implementations here
7886
_connect_fns = {"nrepl": _bencode_connect}
7987

80-
def connect (uri):
81-
"""Connects to an nREPL endpoint identified by the given URL/URI. Valid
88+
89+
def connect(uri):
90+
"""
91+
Connects to an nREPL endpoint identified by the given URL/URI. Valid
8292
examples include:
8393
8494
nrepl://192.168.0.12:7889
@@ -87,15 +97,16 @@ def connect (uri):
8797
8898
This fn delegates to another looked up in that dispatches on the scheme of
8999
the URI provided (which can be a string or java.net.URI). By default, only
90-
`nrepl` (corresponding to using the default bencode transport) is supported.
91-
Alternative implementations may add support for other schemes, such as
92-
http/https, JMX, various message queues, etc."""
100+
`nrepl` (corresponding to using the default bencode transport) is
101+
supported. Alternative implementations may add support for other schemes,
102+
such as http/https, JMX, various message queues, etc.
103+
"""
93104
#
94105
uri = uri if isinstance(uri, ParseResult) else urlparse(uri)
95106
if not uri.scheme:
96107
raise ValueError("uri has no scheme: " + uri)
97108
f = _connect_fns.get(uri.scheme.lower(), None)
98109
if not f:
99-
raise Exception("No connect function registered for scheme `%s`" % uri.scheme)
110+
err = "No connect function registered for scheme `%s`" % uri.scheme
111+
raise Exception(err)
100112
return f(uri)
101-

nrepl/bencode.py

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,25 @@
55
except ImportError:
66
from io import StringIO
77

8-
import socket, sys
9-
108
import sys
119

1210
# Some code so we can use different features without worrying about versions.
1311
PY2 = sys.version_info[0] == 2
1412
if not PY2:
1513
text_type = str
16-
string_types = (str,bytes)
14+
string_types = (str, bytes)
1715
unichr = chr
1816
else:
1917
text_type = unicode
2018
string_types = (str, unicode)
2119
unichr = unichr
2220

2321

24-
def _read_byte (s):
22+
def _read_byte(s):
2523
return s.read(1)
2624

27-
def _read_int (s, terminator=None, init_data=None):
25+
26+
def _read_int(s, terminator=None, init_data=None):
2827
int_chrs = init_data or []
2928
while True:
3029
c = _read_byte(s)
@@ -34,12 +33,14 @@ def _read_int (s, terminator=None, init_data=None):
3433
int_chrs.append(c)
3534
return int(''.join(int_chrs))
3635

37-
def _read_bytes (s, n):
36+
37+
def _read_bytes(s, n):
3838
data = StringIO()
3939
cnt = 0
4040
while cnt < n:
4141
m = s.read(n - cnt)
42-
if not m: raise Exception("Invalid bytestring, unexpected end of input.")
42+
if not m:
43+
raise Exception("Invalid bytestring, unexpected end of input.")
4344
data.write(m)
4445
cnt += len(m)
4546
data.flush()
@@ -50,40 +51,48 @@ def _read_bytes (s, n):
5051
ret = data.getvalue()
5152
return ret
5253

53-
def _read_delimiter (s):
54+
55+
def _read_delimiter(s):
5456
d = _read_byte(s)
55-
if d.isdigit(): d = _read_int(s, ":", [d])
57+
if d.isdigit():
58+
d = _read_int(s, ":", [d])
5659
return d
5760

58-
def _read_list (s):
61+
62+
def _read_list(s):
5963
data = []
6064
while True:
6165
datum = _read_datum(s)
62-
if not datum: break
66+
if not datum:
67+
break
6368
data.append(datum)
6469
return data
6570

66-
def _read_map (s):
71+
72+
def _read_map(s):
6773
i = iter(_read_list(s))
6874
return dict(zip(i, i))
6975

76+
7077
_read_fns = {"i": _read_int,
7178
"l": _read_list,
7279
"d": _read_map,
7380
"e": lambda _: None,
7481
# EOF
7582
None: lambda _: None}
7683

77-
def _read_datum (s):
84+
85+
def _read_datum(s):
7886
delim = _read_delimiter(s)
7987
if delim:
8088
return _read_fns.get(delim, lambda s: _read_bytes(s, delim))(s)
8189

82-
def _write_datum (x, out):
90+
91+
def _write_datum(x, out):
8392
if isinstance(x, string_types):
84-
#x = x.encode("UTF-8")
85-
# TODO revisit encodings, this is surely not right. Python (2.x, anyway)
86-
# conflates bytes and strings, but 3.x does not...
93+
# x = x.encode("UTF-8")
94+
# TODO revisit encodings, this is surely not right. Python
95+
# (2.x, anyway) conflates bytes and strings, but 3.x does not...
8796
out.write(str(len(x)))
8897
out.write(":")
8998
out.write(x)
@@ -93,7 +102,8 @@ def _write_datum (x, out):
93102
out.write("e")
94103
elif isinstance(x, (list, tuple)):
95104
out.write("l")
96-
for v in x: _write_datum(v, out)
105+
for v in x:
106+
_write_datum(v, out)
97107
out.write("e")
98108
elif isinstance(x, dict):
99109
out.write("d")
@@ -103,49 +113,56 @@ def _write_datum (x, out):
103113
out.write("e")
104114
out.flush()
105115

106-
def encode (v):
116+
117+
def encode(v):
107118
"bencodes the given value, may be a string, integer, list, or dict."
108119
s = StringIO()
109120
_write_datum(v, s)
110121
return s.getvalue()
111122

112-
def decode_file (file):
123+
124+
def decode_file(file):
113125
while True:
114126
x = _read_datum(file)
115-
if not x: break
127+
if not x:
128+
break
116129
yield x
117130

118-
def decode (string):
131+
132+
def decode(string):
119133
"Generator that yields decoded values from the input string."
120134
return decode_file(StringIO(string))
121135

122-
class BencodeIO (object):
123-
def __init__ (self, file):
136+
137+
class BencodeIO(object):
138+
def __init__(self, file):
124139
self._file = file
125140

126-
def read (self):
141+
def read(self):
127142
return _read_datum(self._file)
128143

129-
def __iter__ (self):
144+
def __iter__(self):
130145
return self
131146

132147
def next(self):
133148
v = self.read()
134-
if not v: raise StopIteration
149+
if not v:
150+
raise StopIteration
135151
return v
136152

137153
def __next__(self):
138154
# In Python3, __next__ it is an own special class.
139155
v = self.read()
140-
if not v: raise StopIteration
156+
if not v:
157+
raise StopIteration
141158
return v
142159

143-
def write (self, v):
160+
def write(self, v):
144161
return _write_datum(v, self._file)
145162

146-
def flush (self):
147-
if self._file.flush: self._file.flush()
163+
def flush(self):
164+
if self._file.flush:
165+
self._file.flush()
148166

149-
def close (self):
167+
def close(self):
150168
self._file.close()
151-

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy