Conditions | 31 |
Total Lines | 213 |
Code Lines | 153 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like build.rsudp.client.run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import sys, os |
||
149 | def run(settings, debug): |
||
150 | ''' |
||
151 | Main setup function. Takes configuration values and passes them to |
||
152 | the appropriate threads and functions. |
||
153 | |||
154 | :param dict settings: settings dictionary (see :ref:`defaults` for guidance) |
||
155 | :param bool debug: whether or not to show debug output (should be turned off if starting as daemon) |
||
156 | ''' |
||
157 | global PLOTTER, SOUND |
||
158 | # handler for the exit signal |
||
159 | signal.signal(signal.SIGINT, handler) |
||
160 | |||
161 | if TESTING: |
||
162 | global TESTQUEUE |
||
163 | # initialize the test data to read information from file and put it on the port |
||
164 | TESTQUEUE = Queue() # separate from client library because this is not downstream of the producer |
||
165 | tdata = TestData(q=TESTQUEUE, data_file=TESTFILE, port=settings['settings']['port']) |
||
166 | tdata.start() |
||
167 | |||
168 | # initialize the central library |
||
169 | rs.initRSlib(dport=settings['settings']['port'], |
||
170 | rsstn=settings['settings']['station']) |
||
171 | |||
172 | if TESTING: |
||
173 | t.TEST['n_port'][1] = True # port has been opened |
||
174 | print_stats() |
||
175 | if rs.sps == 0: |
||
176 | printE('There is already a Raspberry Shake sending data to this port.', sender=SENDER) |
||
177 | printE('For testing, please change the port in your settings file to an unused one.', |
||
178 | sender=SENDER, spaces=True) |
||
179 | _xit(1) |
||
180 | |||
181 | |||
182 | output_dir = settings['settings']['output_dir'] |
||
183 | |||
184 | |||
185 | if settings['printdata']['enabled']: |
||
186 | # set up queue and process |
||
187 | q = mk_q() |
||
188 | prnt = PrintRaw(q) |
||
189 | mk_p(prnt) |
||
190 | |||
191 | if settings['write']['enabled']: |
||
192 | # set up queue and process |
||
193 | cha = settings['write']['channels'] |
||
194 | q = mk_q() |
||
195 | writer = Write(q=q, cha=cha) |
||
196 | mk_p(writer) |
||
197 | |||
198 | if settings['plot']['enabled'] and MPL: |
||
199 | while True: |
||
200 | if rs.numchns == 0: |
||
201 | time.sleep(0.01) |
||
202 | continue |
||
203 | else: |
||
204 | break |
||
205 | cha = settings['plot']['channels'] |
||
206 | sec = settings['plot']['duration'] |
||
207 | spec = settings['plot']['spectrogram'] |
||
208 | full = settings['plot']['fullscreen'] |
||
209 | kiosk = settings['plot']['kiosk'] |
||
210 | screencap = settings['plot']['eq_screenshots'] |
||
211 | alert = settings['alert']['enabled'] |
||
212 | if settings['plot']['deconvolve']: |
||
213 | if settings['plot']['units'].upper() in rs.UNITS: |
||
214 | deconv = settings['plot']['units'].upper() |
||
215 | else: |
||
216 | deconv = 'CHAN' |
||
217 | else: |
||
218 | deconv = False |
||
219 | pq = mk_q() |
||
220 | PLOTTER = Plot(cha=cha, seconds=sec, spectrogram=spec, |
||
221 | fullscreen=full, kiosk=kiosk, deconv=deconv, q=pq, |
||
222 | screencap=screencap, alert=alert) |
||
223 | # no mk_p() here because the plotter must be controlled by the main thread (this one) |
||
224 | |||
225 | if settings['forward']['enabled']: |
||
226 | # put settings in namespace |
||
227 | addr = settings['forward']['address'] |
||
228 | port = settings['forward']['port'] |
||
229 | cha = settings['forward']['channels'] |
||
230 | # set up queue and process |
||
231 | q = mk_q() |
||
232 | forward = Forward(addr=addr, port=port, cha=cha, q=q) |
||
233 | mk_p(forward) |
||
234 | |||
235 | if settings['alert']['enabled']: |
||
236 | # put settings in namespace |
||
237 | sta = settings['alert']['sta'] |
||
238 | lta = settings['alert']['lta'] |
||
239 | thresh = settings['alert']['threshold'] |
||
240 | reset = settings['alert']['reset'] |
||
241 | bp = [settings['alert']['highpass'], settings['alert']['lowpass']] |
||
242 | cha = settings['alert']['channel'] |
||
243 | if settings['alert']['deconvolve']: |
||
244 | if settings['alert']['units'].upper() in rs.UNITS: |
||
245 | deconv = settings['alert']['units'].upper() |
||
246 | else: |
||
247 | deconv = 'CHAN' |
||
248 | else: |
||
249 | deconv = False |
||
250 | |||
251 | # set up queue and process |
||
252 | q = mk_q() |
||
253 | alrt = Alert(sta=sta, lta=lta, thresh=thresh, reset=reset, bp=bp, |
||
254 | cha=cha, debug=debug, q=q, |
||
255 | deconv=deconv) |
||
256 | mk_p(alrt) |
||
257 | |||
258 | if settings['alertsound']['enabled']: |
||
259 | sender = 'AlertSound' |
||
260 | SOUND = False |
||
261 | soundloc = False |
||
262 | if PYDUB_EXISTS: |
||
263 | soundloc = os.path.expanduser(os.path.expanduser(settings['alertsound']['mp3file'])) |
||
264 | if soundloc in ['doorbell', 'alarm', 'beeps', 'sonar']: |
||
265 | soundloc = pr.resource_filename('rsudp', os.path.join('rs_sounds', '%s.mp3' % soundloc)) |
||
266 | if os.path.exists(soundloc): |
||
267 | try: |
||
268 | SOUND = AudioSegment.from_file(soundloc, format="mp3") |
||
269 | printM('Loaded %.2f sec alert sound from %s' % (len(SOUND)/1000., soundloc), sender='AlertSound') |
||
270 | except FileNotFoundError as e: |
||
271 | printW("You have chosen to play a sound, but don't have ffmpeg or libav installed.", sender='AlertSound') |
||
272 | printW('Sound playback requires one of these dependencies.', sender='AlertSound', spaces=True) |
||
273 | printW("To install either dependency, follow the instructions at:", sender='AlertSound', spaces=True) |
||
274 | printW('https://github.com/jiaaro/pydub#playback', sender='AlertSound', spaces=True) |
||
275 | printW('The program will now continue without sound playback.', sender='AlertSound', spaces=True) |
||
276 | SOUND = False |
||
277 | else: |
||
278 | printW("The file %s could not be found." % (soundloc), sender='AlertSound') |
||
279 | printW('The program will now continue without sound playback.', sender='AlertSound', spaces=True) |
||
280 | else: |
||
281 | printW("You don't have pydub installed, so no sound will play.", sender='AlertSound') |
||
282 | printW('To install pydub, follow the instructions at:', sender='AlertSound', spaces=True) |
||
283 | printW('https://github.com/jiaaro/pydub#installation', sender='AlertSound', spaces=True) |
||
284 | printW('Sound playback also requires you to install either ffmpeg or libav.', sender='AlertSound', spaces=True) |
||
285 | |||
286 | q = mk_q() |
||
287 | alsnd = AlertSound(q=q, sound=SOUND, soundloc=soundloc) |
||
288 | mk_p(alsnd) |
||
289 | |||
290 | runcustom = False |
||
291 | try: |
||
292 | f = False |
||
293 | win_ovr = False |
||
294 | if settings['custom']['enabled']: |
||
295 | # put settings in namespace |
||
296 | f = settings['custom']['codefile'] |
||
297 | win_ovr = settings['custom']['win_override'] |
||
298 | if f == 'n/a': |
||
299 | f = False |
||
300 | runcustom = True |
||
301 | except KeyError as e: |
||
302 | if settings['alert']['exec'] != 'eqAlert': |
||
303 | printW('the custom code function has moved to its own module (rsudp.c_custom)', sender='Custom') |
||
304 | f = settings['alert']['exec'] |
||
305 | win_ovr = settings['alert']['win_override'] |
||
306 | runcustom = True |
||
307 | else: |
||
308 | raise KeyError(e) |
||
309 | if runcustom: |
||
310 | # set up queue and process |
||
311 | q = mk_q() |
||
312 | cstm = Custom(q=q, codefile=f, win_ovr=win_ovr) |
||
313 | mk_p(cstm) |
||
314 | |||
315 | |||
316 | if settings['tweets']['enabled']: |
||
317 | consumer_key = settings['tweets']['api_key'] |
||
318 | consumer_secret = settings['tweets']['api_secret'] |
||
319 | access_token = settings['tweets']['access_token'] |
||
320 | access_token_secret = settings['tweets']['access_secret'] |
||
321 | tweet_images = settings['tweets']['tweet_images'] |
||
322 | |||
323 | q = mk_q() |
||
324 | tweet = Tweeter(q=q, consumer_key=consumer_key, consumer_secret=consumer_secret, |
||
325 | access_token=access_token, access_token_secret=access_token_secret, |
||
326 | tweet_images=tweet_images) |
||
327 | mk_p(tweet) |
||
328 | |||
329 | if settings['telegram']['enabled']: |
||
330 | token = settings['telegram']['token'] |
||
331 | chat_id = settings['telegram']['chat_id'] |
||
332 | send_images = settings['telegram']['send_images'] |
||
333 | |||
334 | q = mk_q() |
||
335 | telegram = Telegrammer(q=q, token=token, chat_id=chat_id, |
||
336 | send_images=send_images) |
||
337 | mk_p(telegram) |
||
338 | |||
339 | # start additional modules here! |
||
340 | ################################ |
||
341 | |||
342 | |||
343 | ################################ |
||
344 | |||
345 | if TESTING: |
||
346 | # initialize test consumer |
||
347 | q = mk_q() |
||
348 | test = Testing(q=q) |
||
349 | mk_p(test) |
||
350 | |||
351 | |||
352 | # start the producer, consumer, and activated modules |
||
353 | start() |
||
354 | |||
355 | PLOTTER = False |
||
356 | if not TESTING: |
||
357 | _xit() |
||
358 | else: |
||
359 | printW('Client has exited, ending tests...', sender=SENDER, announce=False) |
||
360 | if SOUND: |
||
361 | t.TEST['d_pydub'][1] = True |
||
362 | |||
712 |