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