def save_to_about_filesystem(name, text): url = "about://%s" % name fh = opener.open(url, "wb") if hasattr(text, 'create_bitmap'): fhb = opener.open(text.absolute_path, "rb") text = fhb.read() fhb.close() fh.write(text) fh.close() log.debug("Created %s" % url)
def init_about_filesystem(): opener.add(AboutOpener) for name, text in about.iteritems(): url = "about://%s" % name fh = opener.open(url, "wb") if hasattr(text, "create_bitmap"): fhb = opener.open(text.absolute_path, "rb") text = fhb.read() fhb.close() fh.write(text) fh.close() log.debug("Created %s" % url)
def setcontents(self, source, name, chunk_size=65536): """Create a new file from a string or file-like object. :note: All paths has to be absolute or specified in full URI format. :param data: . :param name: File URI or filename generator taking `self` as argument. """ if isinstance(source, six.string_types): self['source'] = source f = opener.open(source, 'rb') else: f = source if callable(name): name = name(self) else: name = fs.path.abspath(name) signals.document_before_content_set.send(self, name=name) if self.get('source', '') != name: data = f.read() _fs, filename = opener.parse(name) _fs.setcontents(filename, data, chunk_size) _fs.close() signals.document_after_content_set.send(self, name=name) if hasattr(f, 'close'): f.close() self['uri'] = name self.commit()
def get_packaged_file(cls, name): source = get_template(name) if source is not None: uri = "about://%s" % os.path.basename(name) fh = opener.open(uri, "wb") fh.write(source) fh.close() return cls(uri)
def test_readline(tmppath): """Tests for readline().""" fd = get_mltl_file(tmppath) fb = get_copy_file(fd) fp, fc = fd['full_path'], fd['contents'] fp2 = fb['full_path'] xfile, pfile = XRootDPyFile(mkurl(fp), 'r'), opener.open(fp2, 'r') assert xfile.readline() == pfile.readline().encode() assert xfile.readline() == pfile.readline().encode() assert xfile.readline() == pfile.readline().encode() xfile.close(), pfile.close() xfile, pfile = XRootDPyFile(mkurl(fp), 'r'), opener.open(fp2, 'r') assert xfile.readline() == pfile.readline().encode() xfile.seek(0), pfile.seek(0) assert xfile.readline() == pfile.readline().encode() assert xfile.tell(), pfile.tell() xfile.close(), pfile.close() xfile = XRootDPyFile(mkurl(fp), 'w+') str1 = 'hello\n' str2 = 'bye\n' xfile.write(str1 + str2) xfile.seek(0) assert xfile.readline() == str1.encode() assert xfile.readline() == str2.encode() assert xfile.readline() == b'' assert xfile.readline() == b'' xfile.seek(100) assert xfile.readline() == b'' xfile.close() xfile = XRootDPyFile(mkurl(fp), 'w+') xfile.write(str2) xfile.seek(len(str2) + 1) xfile.write(str2) xfile.seek(0) _value = u'\x00' + str2 assert xfile.readline() == str2.encode() assert xfile.readline() == _value.encode()
def test_readline(tmppath): """Tests for readline().""" fd = get_mltl_file(tmppath) fb = get_copy_file(fd) fp, fc = fd["full_path"], fd["contents"] fp2 = fb["full_path"] xfile, pfile = XRootDPyFile(mkurl(fp), "r"), opener.open(fp2, "r") assert xfile.readline() == pfile.readline() assert xfile.readline() == pfile.readline() assert xfile.readline() == pfile.readline() xfile.close(), pfile.close() xfile, pfile = XRootDPyFile(mkurl(fp), "r"), opener.open(fp2, "r") assert xfile.readline() == pfile.readline() xfile.seek(0), pfile.seek(0) assert xfile.readline() == pfile.readline() assert xfile.tell(), pfile.tell() xfile.close(), pfile.close() xfile = XRootDPyFile(mkurl(fp), "w+") str1 = "hello\n" str2 = "bye\n" xfile.write(str1 + str2) xfile.seek(0) assert xfile.readline() == str1 assert xfile.readline() == str2 assert xfile.readline() == "" assert xfile.readline() == "" xfile.seek(100) assert xfile.readline() == "" xfile.close() xfile = XRootDPyFile(mkurl(fp), "w+") xfile.write(str2) xfile.seek(len(str2) + 1) xfile.write(str2) xfile.seek(0) assert xfile.readline() == str2 assert xfile.readline() == u"\x00" + str2
def test_readline(tmppath): """Tests for readline().""" fd = get_mltl_file(tmppath) fb = get_copy_file(fd) fp, fc = fd['full_path'], fd['contents'] fp2 = fb['full_path'] xfile, pfile = XRootDPyFile(mkurl(fp), 'r'), opener.open(fp2, 'r') assert xfile.readline() == pfile.readline() assert xfile.readline() == pfile.readline() assert xfile.readline() == pfile.readline() xfile.close(), pfile.close() xfile, pfile = XRootDPyFile(mkurl(fp), 'r'), opener.open(fp2, 'r') assert xfile.readline() == pfile.readline() xfile.seek(0), pfile.seek(0) assert xfile.readline() == pfile.readline() assert xfile.tell(), pfile.tell() xfile.close(), pfile.close() xfile = XRootDPyFile(mkurl(fp), 'w+') str1 = 'hello\n' str2 = 'bye\n' xfile.write(str1+str2) xfile.seek(0) assert xfile.readline() == str1 assert xfile.readline() == str2 assert xfile.readline() == '' assert xfile.readline() == '' xfile.seek(100) assert xfile.readline() == '' xfile.close() xfile = XRootDPyFile(mkurl(fp), 'w+') xfile.write(str2) xfile.seek(len(str2)+1) xfile.write(str2) xfile.seek(0) assert xfile.readline() == str2 assert xfile.readline() == u'\x00'+str2
def compute_checksum(self, chunk_size=None, progress_callback=None): """Compute checksum of file.""" fp = opener.open(self.file.uri, mode='rb') try: value = self._compute_checksum( fp, progress_callback=progress_callback) except StorageError: raise finally: fp.close() return value
def read_pyfs_chunks(url, chunksize=2097152, n=100): """Read a file in chunks.""" t1 = time.time() i = 0 while i < n: fsfile = opener.open(url, 'rb-') while True: data = fsfile.read(chunksize) if not data: break i += 1 t2 = time.time() return (t2-t1)/n
def read_pyfs_chunks(url, chunksize=2097152, n=100): """Read a file in chunks.""" t1 = time.time() i = 0 while i < n: fsfile = opener.open(url, 'rb-') while True: data = fsfile.read(chunksize) if not data: break i += 1 t2 = time.time() return (t2 - t1) / n
def OpenFile(self, fs, location): # For some reason, the actual path shows up as the right location # rather than left, and it includes the leading slashes. path = self.GetRightLocation(location).lstrip("/") wxfs = wx.FileSystem() fsfile = wxfs.OpenFile("memory:%s" % path) if fsfile is None: try: fh = opener.open(location, "rb") except FSError, e: print str(e) return None log.debug("Created %s in wxMemoryFS" % path) wx.MemoryFSHandler.AddFile(path, fh.read()) fsfile = wxfs.OpenFile("memory:%s" % path)
def OpenFile(self, fs, location): # For some reason, the actual path shows up as the right location # rather than left, and it includes the leading slashes. path = self.GetRightLocation(location).lstrip("/") wxfs = wx.FileSystem() fsfile = wxfs.OpenFile("memory:%s" % path) if fsfile is None: try: fh = opener.open(location, "rb") except errors.FSError, e: log.error(str(e)) return None data = np.fromstring(fh.read(), dtype=np.uint8) log.debug("Created %s in wxMemoryFS" % path) wx.MemoryFSHandler.AddFileWithMimeType(path, data, "image/png") fsfile = wxfs.OpenFile("memory:%s" % path)
def setcontents(self, source, **kwargs): """Create a new file from a string or file-like object.""" if isinstance(source, six.string_types): _file = opener.open(source, 'rb') else: _file = source # signals.document_before_content_set.send(self) data = _file.read() _fs, filename = opener.parse(self.uri) _fs.setcontents(filename, data, **kwargs) _fs.close() # signals.document_after_content_set.send(self) if isinstance(source, six.string_types) and hasattr(_file, 'close'): _file.close()
def open(self): """Open file. The caller is responsible for closing the file. """ return opener.open(self.file.uri, mode='rb')
def get_file(fn, fp, tmppath): fpp = join(tmppath, fp, fn) with opener.open(fpp) as f: fc = f.read() return {"filename": fn, "dir": fp, "contents": fc, "full_path": fpp}
def get_file(fn, fp, tmppath): fpp = join(tmppath, fp, fn) with opener.open(fpp) as f: fc = f.read() return {'filename': fn, 'dir': fp, 'contents': fc, 'full_path': fpp}
def testOpen(self): filename = path.join(self.temp_dir, 'foo.txt') file_object = opener.open(filename, 'wb') file_object.close() self.assertTrue(file_object.closed)