def run(self): """Runs the Fetch Screenshot Command Summary: Fetches the screenshot and writes it to file """ scn = None try: scn = self.instrument.fetch_screenshot() except NotInitializedException as e: logger.warning("instrument not initialized for screenshot") except IOError as e: logger.warning(e) except OSError as e: logger.warning(e) except Exception as e: logger.error("Unexpected error: {}".format(e), exc_info=True) if scn: response = self.save_to_file(scn) else: response = "Unable to fetch screenshot!" logger.warning(response) self.response = response logger.info("FetchScreenshotCommand response: {}".format(response)) return self.response
def handle_scpi_file(self, filepath=SCPI_FILEPATH): """Checks file location for scpi script Description: Checks for a scpi file at the filepath parameter. This method is a wrapper for the read_scpi_file method, which gets called as long as a file exists at the filepath. This will build the G1Script that can be run later by calling the run_script method. Exceptions raised by the read_scpi_file method are logged here. Parameters: filepath: the location to check with os.path.exists to see if a scpi file exists. Returns: None. This command does not return a value """ if not os.path.exists(filepath): logger.error("No SCPI file at {}".format(filepath)) return self.script = None try: self.script = self.read_scpi_file(filepath) except IOError as e: logger.warning(e) except OSError as e: logger.warning(e) except Exception as e: logger.error("Unexpected error: {}".format(e), exc_info=True)
def write(self, file='', mode='r', content=''): try: with open(file, mode) as f: f.write(content) except IOError as e: # not able to read the file logger.warning("IOError write(): {}".format(e)) except TypeError as e: # bad data in the file logger.warning("TypeError write(): {}".format(e)) except Exception as e: logger.error("Unexpected error in write(): {}".format(e))
def _parse_loop_cmd(self, cmdstr): """Parses the loop command string This will either create a new loop or it will end a loop and add the loop to the list of commands. """ if cmdstr.upper().startswith('G1:STARTLOOP'): self.loops.append(self._create_loop(cmdstr)) elif cmdstr.upper() == 'G1:ENDLOOP': loopcmd = self.loops.pop() self.commands.append(loopcmd) else: logger.error("Unexpected loop string: {}".format(cmdstr))
def _read_trace_data(self, filepath=''): if not filepath: filepath = settings.WAVEFORM_FILE data = None with open(filepath, 'rb') as f: try: data = json.loads(f.read()) except IOError as e: logger.warning(e) except OSError as e: logger.warning(e) except Exception as e: logger.error("Unexpected error: {}".format(e), exc_info=True) if not data: data = {} return data
def save_to_file(self, data, filepath=''): """saves data to filepath designated in settings.py""" if not filepath: filepath = settings.WAVEFORM_FILE response = 'No waveform saved' # default msg until success try: with open(filepath, 'wb') as f: f.write(json.dumps(data)) except IOError as e: logger.warning(e) except OSError as e: logger.warning(e) except Exception as e: logger.error("Unexpected error: {}".format(e), exc_info=True) else: # success! response = "Waveform saved to: {}".format(filepath) return response
def save_to_file(self, data, filepath=''): """saves data to filepath designated in settings.py""" if not filepath: filepath = SCREENSHOT_FILE response = 'No screenshot saved' # default msg until success try: with open(SCREENSHOT_FILE, 'wb') as f: f.write((data)) except IOError as e: logger.warning(e) except OSError as e: logger.warning(e) except Exception as e: logger.error("Unexpected error: {}".format(e), exc_info=True) else: # success! response = "Screenshot saved to: {}".format(filepath) return response
def wrapper(*args, **kwargs): origResult = TimeoutError( 'function [{}] timeout [{} seconds] exceeded!'.format( func.__name__, timeout)) res = [origResult] def newFunc(): try: result = func(*args, **kwargs) except Exception as e: result = e res[0] = result t = Thread(target=newFunc) t.daemon = True try: t.start() t.join(timeout) except Exception as e: logger.error('error starting thread') raise e result = res[0] # get result from thread if isinstance(result, TimeoutError): logger.error( 'function [{}] timeout [{} seconds] exceeded!'.format( func.__name__, timeout)) raise result elif isinstance(result, BaseException): logger.error("Unexpected exception in {}".format( func.__name__)) raise result return result
def run_script(self): if self.script: self.script.run() else: logger.error("No script to run")