Skip to content

Commit ea902ee

Browse files
author
Brendan Whitfield
committed
made async properties private
1 parent cd4c6e3 commit ea902ee

1 file changed

Lines changed: 47 additions & 42 deletions

File tree

obd/async.py

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ class Async(OBD):
4343

4444
def __init__(self, portstr=None, baudrate=38400):
4545
super(Async, self).__init__(portstr, baudrate)
46-
self.commands = {} # key = OBDCommand, value = Response
47-
self.callbacks = {} # key = OBDCommand, value = list of Functions
48-
self.thread = None
49-
self.running = False
50-
self.was_running = False # used with pause() and resume()
46+
self.__commands = {} # key = OBDCommand, value = Response
47+
self.__callbacks = {} # key = OBDCommand, value = list of Functions
48+
self.__thread = None
49+
self.__running = False
50+
self.__was_running = False # used with __enter__() and __exit__()
51+
52+
53+
@property
54+
def running(self):
55+
return self.__running
5156

5257

5358
def start(self):
@@ -56,25 +61,25 @@ def start(self):
5661
debug("Async thread not started because no connection was made")
5762
return
5863

59-
if len(self.commands) == 0:
64+
if len(self.__commands) == 0:
6065
debug("Async thread not started because no commands were registered")
6166
return
6267

63-
if self.thread is None:
68+
if self.__thread is None:
6469
debug("Starting async thread")
65-
self.running = True
66-
self.thread = threading.Thread(target=self.run)
67-
self.thread.daemon = True
68-
self.thread.start()
70+
self.__running = True
71+
self.__thread = threading.Thread(target=self.run)
72+
self.__thread.daemon = True
73+
self.__thread.start()
6974

7075

7176
def stop(self):
7277
""" Stops the async update loop """
73-
if self.thread is not None:
78+
if self.__thread is not None:
7479
debug("Stopping async thread...")
75-
self.running = False
76-
self.thread.join()
77-
self.thread = None
80+
self.__running = False
81+
self.__thread.join()
82+
self.__thread = None
7883
debug("Async thread stopped")
7984

8085

@@ -94,17 +99,17 @@ def __enter__(self):
9499
pauses the async loop,
95100
while recording the old state
96101
"""
97-
self.was_running = self.running
102+
self.__was_running = self.__running
98103
self.stop()
99-
return self.was_running
104+
return self.__was_running
100105

101106

102107
def __exit__(self, exc_type, exc_value, traceback):
103108
"""
104109
resumes the update loop if it was running
105110
when __enter__ was called
106111
"""
107-
if not self.running and self.was_running:
112+
if not self.__running and self.__was_running:
108113
self.start()
109114

110115
return False # don't suppress any exceptions
@@ -124,7 +129,7 @@ def watch(self, c, callback=None, force=False):
124129
"""
125130

126131
# the dict shouldn't be changed while the daemon thread is iterating
127-
if self.running:
132+
if self.__running:
128133
debug("Can't watch() while running, please use stop()", True)
129134
else:
130135

@@ -133,15 +138,15 @@ def watch(self, c, callback=None, force=False):
133138
return
134139

135140
# new command being watched, store the command
136-
if c not in self.commands:
141+
if c not in self.__commands:
137142
debug("Watching command: %s" % str(c))
138-
self.commands[c] = Response() # give it an initial value
139-
self.callbacks[c] = [] # create an empty list
143+
self.__commands[c] = Response() # give it an initial value
144+
self.__callbacks[c] = [] # create an empty list
140145

141146
# if a callback was given, push it
142-
if hasattr(callback, "__call__") and (callback not in self.callbacks[c]):
147+
if hasattr(callback, "__call__") and (callback not in self.__callbacks[c]):
143148
debug("subscribing callback for command: %s" % str(c))
144-
self.callbacks[c].append(callback)
149+
self.__callbacks[c].append(callback)
145150

146151

147152
def unwatch(self, c, callback=None):
@@ -152,35 +157,35 @@ def unwatch(self, c, callback=None):
152157
"""
153158

154159
# the dict shouldn't be changed while the daemon thread is iterating
155-
if self.running:
160+
if self.__running:
156161
debug("Can't unwatch() while running, please use stop()", True)
157162
else:
158163
debug("Unwatching command: %s" % str(c))
159164

160-
if c in self.commands:
165+
if c in self.__commands:
161166
# if a callback was specified, only remove the callback
162-
if hasattr(callback, "__call__") and (callback in self.callbacks[c]):
163-
self.callbacks[c].remove(callback)
167+
if hasattr(callback, "__call__") and (callback in self.__callbacks[c]):
168+
self.__callbacks[c].remove(callback)
164169

165170
# if no more callbacks are left, remove the command entirely
166-
if len(self.callbacks[c]) == 0:
167-
self.commands.pop(c, None)
171+
if len(self.__callbacks[c]) == 0:
172+
self.__commands.pop(c, None)
168173
else:
169174
# no callback was specified, pop everything
170-
self.callbacks.pop(c, None)
171-
self.commands.pop(c, None)
175+
self.__callbacks.pop(c, None)
176+
self.__commands.pop(c, None)
172177

173178

174179
def unwatch_all(self):
175180
""" Unsubscribes all commands and callbacks from being updated """
176181

177182
# the dict shouldn't be changed while the daemon thread is iterating
178-
if self.running:
183+
if self.__running:
179184
debug("Can't unwatch_all() while running, please use stop()", True)
180185
else:
181186
debug("Unwatching all")
182-
self.commands = {}
183-
self.callbacks = {}
187+
self.__commands = {}
188+
self.__callbacks = {}
184189

185190

186191
def query(self, c):
@@ -189,8 +194,8 @@ def query(self, c):
189194
Only commands that have been watch()ed will return valid responses
190195
"""
191196

192-
if c in self.commands:
193-
return self.commands[c]
197+
if c in self.__commands:
198+
return self.__commands[c]
194199
else:
195200
return Response()
196201

@@ -199,20 +204,20 @@ def run(self):
199204
""" Daemon thread """
200205

201206
# loop until the stop signal is recieved
202-
while self.running:
207+
while self.__running:
203208

204-
if len(self.commands) > 0:
209+
if len(self.__commands) > 0:
205210
# loop over the requested commands, send, and collect the response
206-
for c in self.commands:
211+
for c in self.__commands:
207212

208213
# force, since commands are checked for support in watch()
209214
r = super(Async, self).query(c, force=True)
210215

211216
# store the response
212-
self.commands[c] = r
217+
self.__commands[c] = r
213218

214219
# fire the callbacks, if there are any
215-
for callback in self.callbacks[c]:
220+
for callback in self.__callbacks[c]:
216221
callback(r)
217222

218223
else:

0 commit comments

Comments
 (0)