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