def findProjectURL(cls, url): # Check to see if we already know what the path is, and if we think we # do, make sure the project path still exists if url in cls.buffer_url_to_project_dir: path = cls.buffer_url_to_project_dir[url] if vfs.is_folder(path): return path del cls.buffer_url_to_project_dir[url] # Look for a new project path starting from the specified URL. if vfs.is_folder(url): last = vfs.normalize(url) else: last = vfs.normalize(vfs.get_dirname(url)) cls.dprint(str(last.path)) while not last.path.is_relative() and True: path = last.resolve2("%s" % (cls.classprefs.project_directory)) cls.dprint(path.path) if vfs.is_folder(path): cls.buffer_url_to_project_dir[url] = path return path path = vfs.get_dirname(path.resolve2('..')) if path == last: cls.dprint("Done!") break last = path return None
def findProjectURL(cls, url): # Check to see if we already know what the path is, and if we think we # do, make sure the project path still exists if url in cls.buffer_url_to_project_dir: path = cls.buffer_url_to_project_dir[url] if vfs.is_folder(path): return path del cls.buffer_url_to_project_dir[url] # Look for a new project path starting from the specified URL. if vfs.is_folder(url): last = vfs.normalize(url) else: last = vfs.normalize(vfs.get_dirname(url)) cls.dprint(str(last.path)) while not last.path.is_relative() and True: path = last.resolve2("%s" % (cls.classprefs.project_directory)) cls.dprint(path.path) if vfs.is_folder(path): cls.buffer_url_to_project_dir[url] = path return path path = vfs.get_dirname(path.resolve2("..")) if path == last: cls.dprint("Done!") break last = path return None
def test01_type_checking(self): is_file = vfs.is_file(self.root + 'README') self.assertEqual(is_file, True) is_file = vfs.is_file(self.root) self.assertEqual(is_file, False) is_folder = vfs.is_folder(self.root) self.assertEqual(is_folder, True) is_folder = vfs.is_folder(self.root + 'README') self.assertEqual(is_folder, False)
def test01_type_checking(self): self.assertEqual(True, vfs.is_file('file:vfs/sample.tar.bz2')) self.assertEqual(False, vfs.is_file('tar:vfs/sample.tar.bz2')) self.assertEqual(True, vfs.is_folder('tar:vfs/sample.tar.bz2')) self.assertEqual(True, vfs.is_folder('tar:vfs/sample.tar.bz2/')) self.assertEqual(True, vfs.is_file('tar:vfs/sample.tar.bz2/small.py')) self.assertEqual(True, vfs.is_folder('tar:vfs/sample.tar.bz2/dir1/')) self.assertEqual(True, vfs.is_file('tar:vfs/sample.tar.bz2/dir1/hello1.txt')) self.assertEqual(True, vfs.is_file('tar:vfs/sample.tar.gz/dir1/hello1.txt')) self.assertEqual(True, vfs.is_folder('tar:vfs/sample.tar.bz2/dir1/dir2'))
def test01_type_checking(self): is_file = vfs.is_file(self.root + "tmp/blah.txt") self.assertEqual(is_file, True) is_file = vfs.is_file(self.root + "tmp") self.assertEqual(is_file, False) is_folder = vfs.is_folder(self.root + "tmp") self.assertEqual(is_folder, True) is_folder = vfs.is_folder(self.root + "tmp/blah.txt") self.assertEqual(is_folder, False) mimetype = vfs.get_mimetype(self.root + "tmp/blah.txt") self.assertEqual(mimetype, "text/plain")
def test01_type_checking(self): is_file = vfs.is_file('mem:tmp/blah.txt') self.assertEqual(is_file, True) is_file = vfs.is_file('mem:tmp') self.assertEqual(is_file, False) is_folder = vfs.is_folder('mem:tmp') self.assertEqual(is_folder, True) is_folder = vfs.is_folder('mem:tmp/blah.txt') self.assertEqual(is_folder, False) mimetype = vfs.get_mimetype('mem:tmp/blah.txt') self.assertEqual(mimetype, 'text/plain')
def test01_type_checking(self): is_file = vfs.is_file(self.root + 'tmp/blah.txt') self.assertEqual(is_file, True) is_file = vfs.is_file(self.root + 'tmp') self.assertEqual(is_file, False) is_folder = vfs.is_folder(self.root + 'tmp') self.assertEqual(is_folder, True) is_folder = vfs.is_folder(self.root + 'tmp/blah.txt') self.assertEqual(is_folder, False) mimetype = vfs.get_mimetype(self.root + 'tmp/blah.txt') self.assertEqual(mimetype, 'text/plain')
def createProject(cls, topdir): url = vfs.normalize(topdir) if url in cls.url_to_project_mapping: raise TypeError("Project already exists.") proj_dir = url.resolve2(cls.classprefs.project_directory) if not vfs.is_folder(proj_dir): if not vfs.exists(proj_dir): vfs.make_folder(proj_dir) else: raise TypeError("Can't create directory %s -- seems already exist as a file" % proj_dir) info = cls.registerProject(None, proj_dir) info.savePrefs() cls.dprint(info) buffers = BufferList.getBuffers() for buffer in buffers: if buffer.url.scheme != "file": continue cls.dprint(u"prefix=%s topdir=%s" % (buffer.url.path.get_prefix(url.path), url.path)) if buffer.url.path.get_prefix(url.path) == url.path: cls.dprint(u"belongs in project! %s" % buffer.url.path) for mode in buffer.iterViewers(): mode.project_info = info else: cls.dprint(u"not in project: %s" % buffer.url.path) return info
def test10_creation(self): file = vfs.make_file(self.root + 'testfile.txt') file.write("one\n") file.close() self.assertEqual(vfs.is_file(self.root + 'testfile.txt'), True) url = self.root + 'tmp/dir' vfs.make_folder(url) self.assertEqual(vfs.is_folder(url), True) url = self.root + 'tmp/dir/file1' fh = vfs.make_file(url) fh.write("this is file1") fh.close() self.assertEqual(vfs.is_file(url), True) url = self.root + 'tmp/dir/zero' fh = vfs.make_file(url) fh.close() self.assertEqual(vfs.is_file(url), True) # this should raise an OSError because it's trying to make a file out # of an existing folder url = self.root + 'tmp/dir' self.assertRaises(OSError, vfs.make_file, url) # this should raise an OSError because it's trying to make a file in # another file url = self.root + 'tmp/dir/file1/file2' self.assertRaises(OSError, vfs.make_file, url)
def createProject(cls, topdir): url = vfs.normalize(topdir) if url in cls.url_to_project_mapping: raise TypeError("Project already exists.") proj_dir = url.resolve2(cls.classprefs.project_directory) if not vfs.is_folder(proj_dir): if not vfs.exists(proj_dir): vfs.make_folder(proj_dir) else: raise TypeError( "Can't create directory %s -- seems already exist as a file" % proj_dir) info = cls.registerProject(None, proj_dir) info.savePrefs() cls.dprint(info) buffers = BufferList.getBuffers() for buffer in buffers: if buffer.url.scheme != "file": continue cls.dprint(u"prefix=%s topdir=%s" % (buffer.url.path.get_prefix(url.path), url.path)) if buffer.url.path.get_prefix(url.path) == url.path: cls.dprint(u"belongs in project! %s" % buffer.url.path) for mode in buffer.iterViewers(): mode.project_info = info else: cls.dprint(u"not in project: %s" % buffer.url.path) return info
def getKey(self, base_url, name): if isinstance(name, unicode): name = name.encode("utf-8") # Force the URL to remove any special meaning of the ? and # characters import urllib name = urllib.quote(name) url = base_url.resolve2(name) mode = [] if vfs.is_folder(url): url.path.endswith_slash = True mode.append("d") else: url.path.endswith_slash = False mode.append("-") if vfs.can_read(url): mode.append("r") else: mode.append("-") if vfs.can_write(url): mode.append("w") else: mode.append("-") return url, "".join(mode)
def cwd(self, use_vfs=False): """Find the current working directory of the buffer. Can be used in two ways based on use_vfs: use_vfs == True: uses the vfs to return the directory in the same scheme as the buffer use_vfs == False (the default): find the current working directory on the local filesystem. Some schemes, like tar, for instance, are overlays on the current filesystem and the cwd of those schemes with this sense of use_vfs will report the overlayed directory. """ if self.pending_url is not None: url = self.pending_url else: url = self.url if use_vfs: if vfs.is_folder(url): path = vfs.normalize(url) else: path = vfs.get_dirname(url) return path else: path = self._cwd(url) if (not path or path == '/') and self.created_from_url: path = self._cwd(self.created_from_url) if path == '/': path = wx.StandardPaths.Get().GetDocumentsDir() return path
def completePath(self, text, uri, path): paths = [] if "/" in path: if path.endswith("/"): # It's already a directory uridir = uri pattern = "" else: # The stuff after the last slash is the pattern to match uridir, pattern = path.rsplit("/", 1) uridir = vfs.get_dirname(uri) elif path == ".": # Needed to handle protocols that don't use leading slashes uridir = uri pattern = "" else: uridir = vfs.get_dirname(uri) pattern = path self.dprint("dir=%s pattern=%s" % (uridir, pattern)) try: for name in vfs.get_names(uridir): if not name.startswith(pattern): self.dprint("skipping %s because it doesn't start with %s" % (name, pattern)) continue uri = uridir.resolve2(name) path = str(uri) if vfs.is_folder(uri) and not path.endswith("/"): path += "/" self.dprint(path) paths.append(path) except vfs.AuthenticationCancelled: pass return paths
def match(cls, buffer, magic_size=None, url=None, header=None): if url is None: url = buffer.raw_url if vfs.is_folder(url): mode = cls.matchFolder(buffer, url) else: mode = cls.matchFile(buffer, magic_size, url, header) return mode
def findProjectConfigFileHandle(cls, mode, subdir): """Get a file handle to data in a sub-directory of the project's configuration directory. Uses L{getConfigFileHandle} to return a file-like object inside a subdirectory of the project's config directory. """ cls.dprint(mode) if mode.project_info: url = mode.project_info.getSettingsRelativeURL(subdir) cls.dprint(unicode(url)) if vfs.is_folder(url): fh = cls.getConfigFileHandle(url, mode, mode.buffer.url) return fh return None
def test13_folder_creation(self): url = 'mem:testfile.txt/dir' self.assertEqual(vfs.is_folder(url), False) self.assertRaises(OSError, vfs.make_folder, url) # This should raise an OSError because we're trying to make a file # inside another file file = vfs.make_file('mem:blah1') file.write("blah1\n") file.close() self.assertRaises(OSError, vfs.make_folder, 'mem:blah1/bad1') # This should raise OSError because we're trying to make a file with # the same name as an existing folder url = 'mem:blah2/file2' file = vfs.make_file(url) file.write("blah2\n") file.close() self.assertEqual(True, vfs.exists(url)) self.assertRaises(OSError, vfs.make_file, 'mem:blah2')
def completePath(self, text, uri, path): paths = [] if '/' in path: if path.endswith('/'): # It's already a directory uridir = uri pattern = '' else: # The stuff after the last slash is the pattern to match uridir, pattern = path.rsplit('/', 1) uridir = vfs.get_dirname(uri) elif path == '.': # Needed to handle protocols that don't use leading slashes uridir = uri pattern = '' else: uridir = vfs.get_dirname(uri) pattern = path self.dprint('dir=%s pattern=%s' % (uridir, pattern)) try: for name in vfs.get_names(uridir): if not name.startswith(pattern): self.dprint( "skipping %s because it doesn't start with %s" % (name, pattern)) continue uri = uridir.resolve2(name) path = str(uri) if vfs.is_folder(uri) and not path.endswith('/'): path += '/' self.dprint(path) paths.append(path) except vfs.AuthenticationCancelled: pass return paths
def test07_make_folder(self): vfs.make_folder('vfs/folder') self.assertEqual(vfs.is_folder('vfs/folder'), True)
def test05_is_not_folder(self): is_folder = vfs.is_folder('vfs/hello.txt') self.assertEqual(is_folder, False)
def test04_is_folder(self): is_folder = vfs.is_folder('vfs') self.assertEqual(is_folder, True)
def handleFileDrop(self, x, y, filenames): self.dprint("%d file(s) dropped at %d,%d:" % (len(filenames), x, y)) srcdir = None tocopy = [] for src in filenames: filename = os.path.basename(src) if not srcdir: srcdir = os.path.dirname(src) srcdir += os.pathsep if os.path.isdir(src): self.dprint("dir: %s" % src) for root, dirs, files in os.walk(src): for file in files: child = os.path.join(root, file) self.dprint(" file: %s" % child) tocopy.append(child) elif os.path.isfile(src): self.dprint("file: %s" % src) tocopy.append(src) else: self.dprint("not file or dir: %s NOT COPYING" % src) self.status_info.startProgress("Copying...", len(tocopy), delay=1.0) count = 0 skipped = 0 errors = [] for src in tocopy: try: srcref = vfs.get_file_reference(src) relsrc = src[len(srcdir):] relref = vfs.get_file_reference(relsrc) dest = self.url.resolve2(relref) self.dprint("srcref=%s, relref=%s, dest=%s" % (srcref, relref, dest)) self.dprint("copying: '%s' -> '%s'" % (src, dest)) self.status_info.updateProgress(count, "Copying to %s" % str(dest)) destdir = vfs.get_dirname(dest) if not vfs.exists(destdir): # Need to recursively make directories dirs = [] parent = destdir maxdepth = len(parent.path) while not vfs.exists(parent) and maxdepth > 0: dirs.append(parent) parent = vfs.get_dirname(parent) maxdepth -= 1 dirs.reverse() for dir in dirs: self.dprint("Creating dir: %s" % dir) vfs.make_folder(dir) copy = True self.dprint("src path: %s" % srcref.path) self.dprint("dest path: %s" % dest.path) if vfs.exists(dest): self.dprint("exists: %s" % str(dest)) if vfs.is_folder(dest): errors.append("Not copying folder %s; shouldn't be specified from %s" % (dest, src)) copy = False else: dlg = CustomOkDialog(self.frame, u"File exists! Replace\n\n%s, %s, %s\n\nwith:\n\n%s, %s, %s" % (dest, vfs.get_mtime(dest), vfs.get_size(dest), src, os.path.getmtime(src), os.path.getsize(src)), "Replace", "Skip") retval=dlg.ShowModal() dlg.Destroy() copy = retval==wx.ID_OK if copy: self.dprint("Removing %s" % dest) vfs.remove(dest) if copy: vfs.copy(srcref, dest) else: skipped += 1 except Exception, e: errors.append("%s: %s" % (src, e)) count += 1