Conditions | 35 |
Total Lines | 240 |
Code Lines | 172 |
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 |
||
137 | def run(settings, debug): |
||
138 | ''' |
||
139 | Main setup function. Takes configuration values and passes them to |
||
140 | the appropriate threads and functions. |
||
141 | |||
142 | :param dict settings: settings dictionary (see :ref:`defaults` for guidance) |
||
143 | :param bool debug: whether or not to show debug output (should be turned off if starting as daemon) |
||
144 | ''' |
||
145 | global PLOTTER, SOUND |
||
146 | # handler for the exit signal |
||
147 | signal.signal(signal.SIGINT, handler) |
||
148 | |||
149 | if TESTING: |
||
150 | global TESTQUEUE |
||
151 | # initialize the test data to read information from file and put it on the port |
||
152 | TESTQUEUE = Queue() # separate from client library because this is not downstream of the producer |
||
153 | tdata = TestData(q=TESTQUEUE, data_file=TESTFILE, port=settings['settings']['port']) |
||
154 | tdata.start() |
||
155 | |||
156 | # initialize the central library |
||
157 | rs.initRSlib(dport=settings['settings']['port'], |
||
158 | rsstn=settings['settings']['station']) |
||
159 | |||
160 | H.conn_stats(TESTING) |
||
161 | if TESTING: |
||
162 | T.TEST['n_port'][1] = True # port has been opened |
||
163 | if rs.sps == 0: |
||
164 | printE('There is already a Raspberry Shake sending data to this port.', sender=SENDER) |
||
165 | printE('For testing, please change the port in your settings file to an unused one.', |
||
166 | sender=SENDER, spaces=True) |
||
167 | _xit(1) |
||
168 | |||
169 | |||
170 | output_dir = settings['settings']['output_dir'] |
||
171 | |||
172 | |||
173 | if settings['printdata']['enabled']: |
||
174 | # set up queue and process |
||
175 | q = mk_q() |
||
176 | prnt = PrintRaw(q) |
||
177 | mk_p(prnt) |
||
178 | |||
179 | if settings['write']['enabled']: |
||
180 | # set up queue and process |
||
181 | cha = settings['write']['channels'] |
||
182 | q = mk_q() |
||
183 | writer = Write(q=q, cha=cha) |
||
184 | mk_p(writer) |
||
185 | |||
186 | if settings['plot']['enabled'] and MPL: |
||
187 | while True: |
||
188 | if rs.numchns == 0: |
||
189 | time.sleep(0.01) |
||
190 | continue |
||
191 | else: |
||
192 | break |
||
193 | cha = settings['plot']['channels'] |
||
194 | sec = settings['plot']['duration'] |
||
195 | spec = settings['plot']['spectrogram'] |
||
196 | full = settings['plot']['fullscreen'] |
||
197 | kiosk = settings['plot']['kiosk'] |
||
198 | screencap = settings['plot']['eq_screenshots'] |
||
199 | alert = settings['alert']['enabled'] |
||
200 | if settings['plot']['deconvolve']: |
||
201 | if settings['plot']['units'].upper() in rs.UNITS: |
||
202 | deconv = settings['plot']['units'].upper() |
||
203 | else: |
||
204 | deconv = 'CHAN' |
||
205 | else: |
||
206 | deconv = False |
||
207 | pq = mk_q() |
||
208 | PLOTTER = Plot(cha=cha, seconds=sec, spectrogram=spec, |
||
209 | fullscreen=full, kiosk=kiosk, deconv=deconv, q=pq, |
||
210 | screencap=screencap, alert=alert) |
||
211 | # no mk_p() here because the plotter must be controlled by the main thread (this one) |
||
212 | |||
213 | if settings['forward']['enabled']: |
||
214 | # put settings in namespace |
||
215 | addr = settings['forward']['address'] |
||
216 | port = settings['forward']['port'] |
||
217 | cha = settings['forward']['channels'] |
||
218 | # set up queue and process |
||
219 | q = mk_q() |
||
220 | forward = Forward(addr=addr, port=port, cha=cha, q=q) |
||
221 | mk_p(forward) |
||
222 | |||
223 | if settings['alert']['enabled']: |
||
224 | # put settings in namespace |
||
225 | sta = settings['alert']['sta'] |
||
226 | lta = settings['alert']['lta'] |
||
227 | thresh = settings['alert']['threshold'] |
||
228 | reset = settings['alert']['reset'] |
||
229 | bp = [settings['alert']['highpass'], settings['alert']['lowpass']] |
||
230 | cha = settings['alert']['channel'] |
||
231 | if settings['alert']['deconvolve']: |
||
232 | if settings['alert']['units'].upper() in rs.UNITS: |
||
233 | deconv = settings['alert']['units'].upper() |
||
234 | else: |
||
235 | deconv = 'CHAN' |
||
236 | else: |
||
237 | deconv = False |
||
238 | |||
239 | # set up queue and process |
||
240 | q = mk_q() |
||
241 | alrt = Alert(sta=sta, lta=lta, thresh=thresh, reset=reset, bp=bp, |
||
242 | cha=cha, debug=debug, q=q, |
||
243 | deconv=deconv) |
||
244 | mk_p(alrt) |
||
245 | |||
246 | if settings['alertsound']['enabled']: |
||
247 | sender = 'AlertSound' |
||
248 | SOUND = False |
||
249 | soundloc = False |
||
250 | if PYDUB_EXISTS: |
||
251 | soundloc = os.path.expanduser(os.path.expanduser(settings['alertsound']['mp3file'])) |
||
252 | if soundloc in ['doorbell', 'alarm', 'beeps', 'sonar']: |
||
253 | soundloc = pr.resource_filename('rsudp', os.path.join('rs_sounds', '%s.mp3' % soundloc)) |
||
254 | if os.path.exists(soundloc): |
||
255 | try: |
||
256 | SOUND = AudioSegment.from_file(soundloc, format="mp3") |
||
257 | printM('Loaded %.2f sec alert sound from %s' % (len(SOUND)/1000., soundloc), sender='AlertSound') |
||
258 | except FileNotFoundError as e: |
||
259 | printW("You have chosen to play a sound, but don't have ffmpeg or libav installed.", sender='AlertSound') |
||
260 | printW('Sound playback requires one of these dependencies.', sender='AlertSound', spaces=True) |
||
261 | printW("To install either dependency, follow the instructions at:", sender='AlertSound', spaces=True) |
||
262 | printW('https://github.com/jiaaro/pydub#playback', sender='AlertSound', spaces=True) |
||
263 | printW('The program will now continue without sound playback.', sender='AlertSound', spaces=True) |
||
264 | SOUND = False |
||
265 | else: |
||
266 | printW("The file %s could not be found." % (soundloc), sender='AlertSound') |
||
267 | printW('The program will now continue without sound playback.', sender='AlertSound', spaces=True) |
||
268 | else: |
||
269 | printW("You don't have pydub installed, so no sound will play.", sender='AlertSound') |
||
270 | printW('To install pydub, follow the instructions at:', sender='AlertSound', spaces=True) |
||
271 | printW('https://github.com/jiaaro/pydub#installation', sender='AlertSound', spaces=True) |
||
272 | printW('Sound playback also requires you to install either ffmpeg or libav.', sender='AlertSound', spaces=True) |
||
273 | |||
274 | q = mk_q() |
||
275 | alsnd = AlertSound(q=q, sound=SOUND, soundloc=soundloc) |
||
276 | mk_p(alsnd) |
||
277 | |||
278 | runcustom = False |
||
279 | try: |
||
280 | f = False |
||
281 | win_ovr = False |
||
282 | if settings['custom']['enabled']: |
||
283 | # put settings in namespace |
||
284 | f = settings['custom']['codefile'] |
||
285 | win_ovr = settings['custom']['win_override'] |
||
286 | if f == 'n/a': |
||
287 | f = False |
||
288 | runcustom = True |
||
289 | except KeyError as e: |
||
290 | if settings['alert']['exec'] != 'eqAlert': |
||
291 | printW('the custom code function has moved to its own module (rsudp.c_custom)', sender='Custom') |
||
292 | f = settings['alert']['exec'] |
||
293 | win_ovr = settings['alert']['win_override'] |
||
294 | runcustom = True |
||
295 | else: |
||
296 | raise KeyError(e) |
||
297 | if runcustom: |
||
298 | # set up queue and process |
||
299 | q = mk_q() |
||
300 | cstm = Custom(q=q, codefile=f, win_ovr=win_ovr) |
||
301 | mk_p(cstm) |
||
302 | |||
303 | |||
304 | if settings['tweets']['enabled']: |
||
305 | consumer_key = settings['tweets']['api_key'] |
||
306 | consumer_secret = settings['tweets']['api_secret'] |
||
307 | access_token = settings['tweets']['access_token'] |
||
308 | access_token_secret = settings['tweets']['access_secret'] |
||
309 | tweet_images = settings['tweets']['tweet_images'] |
||
310 | |||
311 | q = mk_q() |
||
312 | tweet = Tweeter(q=q, consumer_key=consumer_key, consumer_secret=consumer_secret, |
||
313 | access_token=access_token, access_token_secret=access_token_secret, |
||
314 | tweet_images=tweet_images) |
||
315 | mk_p(tweet) |
||
316 | |||
317 | if settings['telegram']['enabled']: |
||
318 | token = settings['telegram']['token'] |
||
319 | chat_ids = settings['telegram']['chat_id'].strip(' ').split(',') |
||
320 | send_images = settings['telegram']['send_images'] |
||
321 | |||
322 | for chat_id in chat_ids: |
||
323 | sender = "Telegram id %s" % (chat_id) |
||
324 | q = mk_q() |
||
325 | telegram = Telegrammer(q=q, token=token, chat_id=chat_id, |
||
326 | send_images=send_images, |
||
327 | sender=sender) |
||
328 | mk_p(telegram) |
||
329 | |||
330 | if settings['rsam']['enabled']: |
||
331 | # put settings in namespace |
||
332 | fwaddr = settings['rsam']['fwaddr'] |
||
333 | fwport = settings['rsam']['fwport'] |
||
334 | fwformat = settings['rsam']['fwformat'] |
||
335 | interval = settings['rsam']['interval'] |
||
336 | cha = settings['rsam']['channel'] |
||
337 | quiet = settings['rsam']['quiet'] |
||
338 | if settings['rsam']['deconvolve']: |
||
339 | if settings['rsam']['units'].upper() in rs.UNITS: |
||
340 | deconv = settings['rsam']['units'].upper() |
||
341 | else: |
||
342 | deconv = 'CHAN' |
||
343 | else: |
||
344 | deconv = False |
||
345 | |||
346 | # set up queue and process |
||
347 | q = mk_q() |
||
348 | rsam = RSAM(q=q, debug=debug, interval=interval, cha=cha, deconv=deconv, |
||
349 | fwaddr=fwaddr, fwport=fwport, fwformat=fwformat, quiet=quiet) |
||
350 | |||
351 | mk_p(rsam) |
||
352 | |||
353 | |||
354 | # start additional modules here! |
||
355 | ################################ |
||
356 | |||
357 | |||
358 | ################################ |
||
359 | |||
360 | if TESTING: |
||
361 | # initialize test consumer |
||
362 | q = mk_q() |
||
363 | test = Testing(q=q) |
||
364 | mk_p(test) |
||
365 | |||
366 | |||
367 | # start the producer, consumer, and activated modules |
||
368 | start() |
||
369 | |||
370 | PLOTTER = False |
||
371 | if not TESTING: |
||
372 | _xit() |
||
373 | else: |
||
374 | printW('Client has exited, ending tests...', sender=SENDER, announce=False) |
||
375 | if SOUND: |
||
376 | T.TEST['d_pydub'][1] = True |
||
377 | |||
611 |