11#!/usr/bin/env python
22
3- import socket , re , sys
3+ import socket
44import nrepl .bencode as bencode
55import threading
66try :
77 from urlparse import urlparse , ParseResult
88except 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-
0 commit comments