def __next__(self): """Parse and return next record from input stream. :return: Record. If the record cannot be parsed and `ignore_bad` is True, then an empty Record will be returned. :raises: ValueError on bad record, unless the class attribute `ignore_bad` is True. StopIteration at end of data """ # process initial metadata text = None while 1: try: text = next(self._in) except TypeError: text = self._in.next() if not isinstance(text, str): text = get_str(text) if not text.startswith(META_LINE_MARKER): break self._process_meta(text[len(META_LINE_MARKER):]) if self._is_json: result = json.loads(text) else: result = self._parse_kvp(text) if not result and not self.ignore_bad: raise ValueError( "No key/value pairs in record: '{}'".format(text.strip())) return Record(result)
def handle_read(self): """Read data from connection, break it into records, and invoke the call """ data = self.recv(self.block_size) if len(data) == 0: return if not isinstance(data, str): data = get_str(data) self._buf += data offs = self._extract_records() # Shorten buffer to unsent portion if offs > 0: self._buf = self._buf[offs:] self.notify(offs)
def run_command(command, env=None): """ Runs the command for the specified task :param env: :param command: the command line execution :return: the command output :rtype: str """ prog = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, env=env, shell=True) stdout, stderr = prog.communicate() if len(stderr) > 0: raise TigresException("EXECUTABLE '{}' failed with error:{} output:{}" .format(command, stderr, stdout)) return get_str(stdout).rstrip('\n')