def open(path, mode='r'): if 'w' in mode or 'a' in mode: raise IOError(_errno.EINVAL, path, "Write access not supported") elif 'r+' in mode: raise IOError(_errno.EINVAL, path, "Write access not supported") full_path = path path, rest = _locate(path) if not rest: return _open(path, mode) else: try: zf = _zipfile.ZipFile(path, 'r') except _zipfile.error: raise IOError(_errno.ENOENT, full_path, "No such file or directory") try: data = zf.read(rest) except (_zipfile.error, KeyError): zf.close() raise IOError(_errno.ENOENT, full_path, "No such file or directory") zf.close() if mode == 'rb': return _BytesIO(data) else: if _sys.version_info[0] == 3: data = data.decode('ascii') return _StringIO(data)
def __enter__(self): if self.old_stdout is not None: raise RuntimeError("Nested pagers") self.buffer = _StringIO() self.old_stdout = _sys.stdout _sys.stdout = self.buffer
def _extract_echo_retval_from_csv(self, response): strio = _StringIO(response.text) reader = _csv.reader(strio) year_str, month_str, day_str, hour_str, minute_str, second_str, \ microsecond_str, tz_str = \ reader.next() with self.assertRaises(StopIteration): reader.next() year = int(year_str) month = int(month_str) day = int(day_str) hour = int(hour_str or 0) minute = int(minute_str or 0) second = int(second_str or 0) microsecond = int(microsecond_str or 0) tz_match = _re.match(r'(?P<tz_sign>[-+])(?P<tz_hours>\d\d)' r'(?P<tz_minutes>\d\d)?', tz_str) if tz_match: tz_sign = tz_match.group('tz_sign') tz_hours = int(tz_sign + tz_match.group('tz_hours')) tz_minutes = int(tz_sign + (tz_match.group('tz_minutes') or '0')) tz_minutes += tz_hours * 60 tzinfo = _tz.FixedOffset(tz_minutes) else: tzinfo = _tz.UTC return _datetime(year, month, day, hour, minute, second, microsecond, tzinfo)
def get_disk_devs(vm_): doc = minidom.parse(_StringIO(get_xml(vm_))) disks = [] for elem in doc.getElementsByTagName('disk'): targets = elem.getElementsByTagName('target') target = targets[0] disks.append(target.getAttribute('dev')) return disks
def read(self, pathfilename): with _codecs.open(pathfilename, 'r', encoding='utf-8') as input_file: config_pairs = input_file.read() with _closing(_StringIO(u"[{0}]{1}{2}".format(self._default_section, _os.linesep, config_pairs))) \ as default_section: _RawConfigParser.readfp(self, default_section)
def read(self, pathfilename): with codecs.open(pathfilename, 'r', encoding='utf-8') as input_file: config_pairs = input_file.read() with _closing(_StringIO(u"[{0}]{1}{2}".format(self._default_section, os.linesep, config_pairs))) \ as default_section: _RawConfigParser.readfp(self, default_section)
def fromBox(self, name, strings, objects, proto): value = _StringIO() value.write(strings.get(name)) for counter in _count(2): chunk = strings.get("%s.%d" % (name, counter)) if chunk is None: break value.write(chunk) objects[name] = self.build_value(value.getvalue())
def __init__(self, max_size=0, mode='w+b', bufsize=-1, suffix="", prefix=template, dir=None): self._file = _StringIO() self._max_size = max_size self._rolled = False self._TemporaryFileArgs = (mode, bufsize, suffix, prefix, dir) # MP self._readers = []
def Pandas_read(data_string, *args, **kwargs): # Strict: don't use 'isinstance', only allow buildin str # objects if type(data_string) is not str: raise _ZopeGuardsUnauthorized( "Parsing object '%s' of type '%s' is prohibited!" % (data_string, type(data_string))) string_io = _StringIO(data_string) return original_function(string_io, *args, **kwargs)
def get_reader(self): file = self._file if hasattr(file, 'name'): r = open(file.name, 'r') else: r = _StringIO(file.getvalue()) self._readers.append(r) return r
def send_string(src, filename, type=None, **kwargs): """ Convert data written to StringIO into the format expected by the Download component. :param src: a string or a writer that can write to StringIO :param filename: the name of the file :param type: type of the file (optional, passed to Blob in the javascript layer) :return: dict of data frame content (NOT base64 encoded) and meta data used by the Download component """ content = src if isinstance(src, str) else _io_to_str(_StringIO(), src, **kwargs) return dict(content=content, filename=filename, type=type, base64=False)
def read(self, pathfilename): #seb: expand path to allow using homedir and relative paths pathfilename = os.path.realpath(os.path.expanduser(pathfilename)) with codecs.open(pathfilename, 'r', encoding=ServiceDefault.CHAR_CODEC) as input_file: config_pairs = input_file.read() with _closing(_StringIO(u"[{0}]{1}{2}".format(self._default_section, os.linesep, config_pairs))) \ as default_section: _RawConfigParser.readfp(self, default_section)
def toBox(self, name, strings, objects, proto): value = _StringIO(self.from_value(objects[name])) first_chunk = value.read(CHUNK_MAX) strings[name] = first_chunk counter = 2 while True: next_chunk = value.read(CHUNK_MAX) if not next_chunk: break strings["%s.%d" % (name, counter)] = next_chunk counter += 1
def get(self, echo_dt, dt): dt_native = echo_dt.native() strio = _StringIO() csv_writer = _csv.writer(strio) csv_writer.writerow((dt_native.year, dt_native.month, dt_native.day, dt_native.hour, dt_native.minute, dt_native.second, dt_native.microsecond, dt_native.strftime('%z'), )) return strio.getvalue()
def get_macs(vm_): ''' Return a list off MAC addresses from the named vm CLI Example:: salt '*' virt.get_macs <vm name> ''' macs = [] doc = minidom.parse(_StringIO(get_xml(vm_))) for node in doc.getElementsByTagName('devices'): i_nodes = node.getElementsByTagName('interface') for i_node in i_nodes: for v_node in i_node.getElementsByTagName('mac'): macs.append(v_node.getAttribute('address')) return macs
def sendAsBuffer(req, text, content_type, force=0, allow_cross_origin=False): stringio = _StringIO(text) try: file_length = len(stringio.buf) except OSError: error(req, 404) return ims = get_header_match(IF_MODIFIED_SINCE, req.header) length_match = 1 if ims: length = ims.group(4) if length: with _suppress(Exception, warn=False): length = _string.atoi(length) if length != file_length: length_match = 0 ims_date = 0 if ims: ims_date = parse_http_date(ims.group(1)) try: import time mtime = _time.time() # _os.stat (path)[stat.ST_MTIME] except: error(req, 404) return if length_match and ims_date: if mtime <= ims_date and not force: req.reply_code = 304 return try: file = stringio except IOError: error(req, 404) return req.reply_headers['Last-Modified'] = build_http_date(mtime) req.reply_headers['Content-Length'] = file_length req.reply_headers['Content-Type'] = content_type if allow_cross_origin: req.reply_headers['Access-Control-Allow-Origin'] = '*' if req.command == 'GET': req.push(_athana.file_producer(file)) return
def _cat_config_file(self, commands): """ Update command with cat of configuration file. Args: commands (list of str): Commands """ if not self._init_config: return config = (self._config if self._init_config is True else self._init_config) # Write default configuration file stream = _StringIO() _cfg.create_configuration(config).write(stream) stream.seek(0) commands += [ "cat << EOF > %s/accelerator.conf" % self._HOME, stream.read(), "EOF\n" ]
def get_nics(vm_): ''' Return info about the network interfaces of a named vm CLI Example:: salt '*' virt.get_nics <vm name> ''' nics = {} doc = minidom.parse(_StringIO(get_xml(vm_))) for node in doc.getElementsByTagName('devices'): i_nodes = node.getElementsByTagName('interface') for i_node in i_nodes: nic = {} nic['type'] = i_node.getAttribute('type') for v_node in i_node.getElementsByTagName('*'): if v_node.tagName == 'mac': nic['mac'] = v_node.getAttribute('address') if v_node.tagName == 'model': nic['model'] = v_node.getAttribute('type') if v_node.tagName == 'target': nic['target'] = v_node.getAttribute('dev') # driver, source, and match can all have optional attributes if re.match('(driver|source|address)', v_node.tagName): temp = {} for key in v_node.attributes.keys(): temp[key] = v_node.getAttribute(key) nic[str(v_node.tagName)] = temp # virtualport needs to be handled separately, to pick up the # type attribute of the virtualport itself if v_node.tagName == 'virtualport': temp = {} temp['type'] = v_node.getAttribute('type') for key in v_node.attributes.keys(): temp[key] = v_node.getAttribute(key) nic['virtualport'] = temp if 'mac' not in nic: continue nics[nic['mac']] = nic return nics
def open(path, mode='r'): if 'w' in mode or 'a' in mode: raise IOError( _errno.EINVAL, path, "Write access not supported") elif 'r+' in mode: raise IOError( _errno.EINVAL, path, "Write access not supported") full_path = path path, rest = _locate(path) if not rest: return _open(path, mode) else: try: zf = _zipfile.ZipFile(path, 'r') except _zipfile.error: raise IOError( _errno.ENOENT, full_path, "No such file or directory") try: data = zf.read(rest) except (_zipfile.error, KeyError): zf.close() raise IOError( _errno.ENOENT, full_path, "No such file or directory") zf.close() if mode == 'rb': return _BytesIO(data) else: if _sys.version_info[0] == 3: data = data.decode('ascii') return _StringIO(data)
def __bootstrap(self): try: self.__started = True _active_limbo_lock.acquire() _active[_get_ident()] = self del _limbo[self] _active_limbo_lock.release() if __debug__: self._note("%s.__bootstrap(): thread started", self) if _trace_hook: self._note("%s.__bootstrap(): registering trace hook", self) _sys.settrace(_trace_hook) if _profile_hook: self._note("%s.__bootstrap(): registering profile hook", self) _sys.setprofile(_profile_hook) try: self.run() except SystemExit: if __debug__: self._note("%s.__bootstrap(): raised SystemExit", self) except: if __debug__: self._note("%s.__bootstrap(): unhandled exception", self) s = _StringIO() _print_exc(file=s) _sys.stderr.write("Exception in thread %s:\n%s\n" % (self.getName(), s.getvalue())) else: if __debug__: self._note("%s.__bootstrap(): normal return", self) finally: self.__stop() try: self.__delete() except: pass
def get_graphics(vm_): ''' Returns the information on vnc for a given vm CLI Example:: salt '*' virt.get_graphics <vm name> ''' out = { 'autoport': 'None', 'keymap': 'None', 'listen': 'None', 'port': 'None', 'type': 'vnc' } xml = get_xml(vm_) ssock = _StringIO(xml) doc = minidom.parse(ssock) for node in doc.getElementsByTagName('domain'): g_nodes = node.getElementsByTagName('graphics') for g_node in g_nodes: for key in g_node.attributes.keys(): out[key] = g_node.getAttribute(key) return out
def error(self, error_message): with _closing(_StringIO()) as usage: self.print_usage(usage) message = EBSCliAttr.ErrorMsg.format(error_message, usage.getvalue(), self.prog) raise ArgumentError(message)
def get_reader(self): r = _StringIO(self._data) self._readers.append(r) return r
def collection_to_string(collection, depth=3): with _closing(_StringIO()) as item_list: _itr_printer(collection, depth=depth, stream=item_list) return item_list.getvalue()
def get_disks(vm_): ''' Return the disks of a named vm CLI Example:: salt '*' virt.get_disks <vm name> ''' disks = collections.OrderedDict() doc = minidom.parse(_StringIO(get_xml(vm_))) for elem in doc.getElementsByTagName('disk'): sources = elem.getElementsByTagName('source') targets = elem.getElementsByTagName('target') if len(sources) > 0: source = sources[0] else: continue if len(targets) > 0: target = targets[0] else: continue if target.hasAttribute('dev'): qemu_target = '' if source.hasAttribute('file'): qemu_target = source.getAttribute('file') elif source.hasAttribute('dev'): qemu_target = source.getAttribute('dev') elif source.hasAttribute('protocol') and \ source.hasAttribute('name'): # For rbd network qemu_target = '%s:%s' % (source.getAttribute('protocol'), source.getAttribute('name')) if qemu_target: disks[target.getAttribute('dev')] = {\ 'file': qemu_target} for dev in disks: try: if not os.path.exists(disks[dev]['file']): continue output = [] try: qemu_output = runCmdRaiseException('qemu-img info -U %s' % disks[dev]['file']) except: qemu_output = runCmdRaiseException('qemu-img info %s' % disks[dev]['file']) snapshots = False columns = None lines = qemu_output.strip().split('\n') for line in lines: if line.startswith('Snapshot list:'): snapshots = True continue elif snapshots: if line.startswith('ID'): # Do not parse table headers line = line.replace('VM SIZE', 'VMSIZE') line = line.replace('VM CLOCK', 'TIME VMCLOCK') columns = re.split('\s+', line) columns = [c.lower() for c in columns] output.append('snapshots:') continue fields = re.split('\s+', line) for i, field in enumerate(fields): sep = ' ' if i == 0: sep = '-' output.append('{0} {1}: "{2}"'.format( sep, columns[i], field)) continue output.append(line) output = '\n'.join(output) disks[dev].update(yaml.safe_load(output)) except TypeError: disks[dev].update(yaml.safe_load('image: Does not exist')) return disks
def _safe_repr(object, context, maxlevels, level): typ = _type(object) if typ is str: if 'locale' not in _sys.modules: return repr(object), True, False if "'" in object and '"' not in object: closure = '"' quotes = {'"': '\\"'} else: closure = "'" quotes = {"'": "\\'"} qget = quotes.get sio = _StringIO() write = sio.write for char in object: if char.isalpha(): write(char) else: write(qget(char, repr(char)[1:-1])) return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False r = getattr(typ, "__repr__", None) if issubclass(typ, dict) and r is dict.__repr__: if not object: return "{}", True, False objid = _id(object) if maxlevels and level > maxlevels: return "{...}", False, objid in context if objid in context: return _recursion(object), False, True context[objid] = 1 readable = True recursive = False components = [] append = components.append level += 1 saferepr = _safe_repr for k, v in sorted(object.items()): krepr, kreadable, krecur = saferepr(k, context, maxlevels, level) vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level) append("%s: %s" % (krepr, vrepr)) readable = readable and kreadable and vreadable if krecur or vrecur: recursive = True del context[objid] return "{%s}" % _commajoin(components), readable, recursive if (issubclass(typ, list) and r is list.__repr__) or \ (issubclass(typ, tuple) and r is tuple.__repr__): if issubclass(typ, list): if not object: return "[]", True, False format = "[%s]" elif _len(object) == 1: format = "(%s,)" else: if not object: return "()", True, False format = "(%s)" objid = _id(object) if maxlevels and level > maxlevels: return format % "...", False, objid in context if objid in context: return _recursion(object), False, True context[objid] = 1 readable = True recursive = False components = [] append = components.append level += 1 for o in object: orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level) append(orepr) if not oreadable: readable = False if orecur: recursive = True del context[objid] return format % _commajoin(components), readable, recursive rep = repr(object) if (issubclass(typ, float)): rep = float_format % object if (issubclass(typ, complex)): rep = '(%s+%sj)' % (float_format, float_format) % \ (object.real, object.imag) return rep, (rep and not rep.startswith('<')), False
def pformat(self, object): sio = _StringIO() self._format(object, sio, 0, 0, {}, 0) return sio.getvalue()
def _process_subtree(lines): """Read the subtree section into a networkx object.""" return read_tree(_StringIO("".join(lines)))
def __init__(self, delegate, verbosity=2): self._captured = _StringIO() self.delegate = delegate self.verbosity = verbosity
def collection_to_string(collection, depth=3): with _closing(_StringIO()) as item_list: _itr_printer(collection, depth = depth, stream = item_list) return item_list.getvalue()