From 00abdcdb43051f9628ef1e729dae82674141af8a Mon Sep 17 00:00:00 2001 From: rfkelly0 Date: Wed, 3 Jun 2009 14:10:40 +0000 Subject: remove "if __main__" stuff from most modules git-svn-id: http://pyfilesystem.googlecode.com/svn/branches/rfk-ideas@151 67cdc799-7952-0410-af00-57a81ceafa0f --- fs/__init__.py | 14 ++---- fs/base.py | 130 +++++++++++-------------------------------------------- fs/browsewin.py | 5 ++- fs/memoryfs.py | 31 ------------- fs/mountfs.py | 40 ----------------- fs/multifs.py | 24 ---------- fs/objecttree.py | 13 ------ fs/rpcfs.py | 1 + fs/tempfs.py | 3 -- fs/utils.py | 9 ++-- fs/zipfs.py | 44 +------------------ 11 files changed, 39 insertions(+), 275 deletions(-) diff --git a/fs/__init__.py b/fs/__init__.py index fcd45e3..438c822 100644 --- a/fs/__init__.py +++ b/fs/__init__.py @@ -4,17 +4,11 @@ A filesystem abstraction. """ __version__ = "0.1.1dev" - __author__ = "Will McGugan (will@willmcgugan.com)" +# 'base' imports * from 'helpers' and 'errors' from base import * -from helpers import * -__all__ = ['memoryfs', - 'mountfs', - 'multifs', - 'osfs', - 'utils', - 'zipfs', - 'helpers', - 'tempfs'] + +import errors +import helpers diff --git a/fs/base.py b/fs/base.py index ea7647f..5400cf9 100644 --- a/fs/base.py +++ b/fs/base.py @@ -19,11 +19,6 @@ try: except ImportError: import dummy_threading as threading import dummy_threading -try: - import cPickle as pickle -except ImportError: - import pickle - from helpers import * from errors import * @@ -91,44 +86,7 @@ class NullFile(object): pass -def print_fs(fs, path="/", max_levels=5, indent=' '*2): - """Prints a filesystem listing to stdout (including sub dirs). - - Useful as a debugging aid. Be careful about printing a OSFS, or any other - large filesystem - without max_levels set, this function will traverse the - entire directory tree. - - fs -- A filesystem object - path -- Path of root to list (default "/") - max_levels -- Maximum levels of dirs to list (default 5), set to None - for no maximum - indent -- String to indent each directory level (default two spaces) - - """ - def print_dir(fs, path, level): - try: - dir_listing = [(fs.isdir(pathjoin(path,p)), p) for p in fs.listdir(path)] - except FSError, e: - print indent*level + "... unabled to retrieve directory list (reason: %s) ..." % str(e) - return - - dir_listing.sort(key = lambda (isdir, p):(not isdir, p.lower())) - - for is_dir, item in dir_listing: - - if is_dir: - print indent*level + '[%s]' % item - if max_levels is None or level < max_levels: - print_dir(fs, pathjoin(path, item), level+1) - if max_levels is not None: - if level >= max_levels: - print indent*(level+1) + "..." - else: - print indent*level + '%s' % item - print_dir(fs, path, 0) - - -def _synchronize(func): +def synchronize(func): """Decorator to synchronize a method on self._lock.""" def acquire_lock(self, *args, **kwargs): self._lock.acquire() @@ -177,7 +135,6 @@ class FS(object): thread_synconize -- If True, a lock object will be created for the object, otherwise a dummy lock will be used. - """ if thread_synchronize: self._lock = threading.RLock() @@ -218,7 +175,6 @@ class FS(object): path -- A path within the filesystem allow_none -- If True, this method should return None if there is no system path, rather than raising NoSysPathError - """ if not allow_none: raise NoSysPathError(path=path) @@ -228,7 +184,6 @@ class FS(object): """Return True if the path maps to a system path. path -- Pach to check - """ return self.getsyspath(path, None) is not None @@ -241,7 +196,6 @@ class FS(object): in 'file' and 'open' builtins kwargs -- Additional (optional) keyword parameters that may be required to open the file - """ raise UnsupportedError("open file") @@ -285,7 +239,6 @@ class FS(object): The directory contents are returned as a list of paths. If the given path is not found then ResourceNotFoundError is raised; if it exists but is not a directory, ResourceInvalidError is raised. - """ raise UnsupportedError("list directory") @@ -301,7 +254,6 @@ class FS(object): that directory, this method applies the semantics of the listdir() keyword arguments. An appropriately modified and filtered list of directory entries is returned. - """ if dirs_only and files_only: raise ValueError("dirs_only and files_only can not both be True") @@ -336,7 +288,6 @@ class FS(object): * ParentDirectoryMissingError, if a containing directory is missing and recursive is False * ResourceInvalidError, if path is an existing file - """ raise UnsupportedError("make directory") @@ -348,7 +299,6 @@ class FS(object): This method can raise the following errors: * ResourceNotFoundError, if the path does not exist * ResourceInvalidError, if the path is a directory - """ raise UnsupportedError("remove resource") @@ -364,7 +314,6 @@ class FS(object): * ResourceInvalidError, if the path is not a directory * DirectoryNotEmptyError, if the directory is not empty and force is False - """ raise UnsupportedError("remove directory") @@ -373,7 +322,6 @@ class FS(object): src -- Path to rename dst -- New name (not a path) - """ raise UnsupportedError("rename resource") @@ -381,7 +329,6 @@ class FS(object): """Returns information for a path as a dictionary. path -- A path to retrieve information for - """ raise UnsupportedError("get resource info") @@ -392,7 +339,6 @@ class FS(object): path -- A path to describe This is mainly for use as a debugging aid. - """ if not self.exists(path): return "No description available" @@ -410,7 +356,6 @@ class FS(object): """Returns the contents of a file as a string. path -- path of file to read. - """ f = None try: @@ -426,7 +371,6 @@ class FS(object): path -- Path of the file to create data -- A string containing the contents of the file - """ f = None try: @@ -441,7 +385,6 @@ class FS(object): """Opens a directory and returns a FS object representing its contents. path -- Path to directory to open - """ if not self.exists(path): raise DirectoryNotFoundError(path) @@ -451,16 +394,16 @@ class FS(object): def walk(self, path="/", wildcard=None, dir_wildcard=None, search="breadth"): """Walks a directory tree and yields the root path and contents. - Yields a tuple of the path of each directory and a list of its file contents. + Yields a tuple of the path of each directory and a list of its file + contents. path -- Root path to start walking wildcard -- If given, only return files that match this wildcard - dir_wildcard -- If given, only walk in to directories that match this wildcard - search -- A string that identifies the method used to walk the directories, - can be 'breadth' for a breadth first search, or 'depth' for a depth first - search. Use 'depth' if you plan to create / delete files as you go. - - + dir_wildcard -- If given, only walk directories that match the wildcard + search -- A string dentifying the method used to walk the directories. + Can be 'breadth' for a breadth first search, or 'depth' for a + depth first search. Use 'depth' if you plan to create or + delete files as you go. """ if search == "breadth": dirs = [path] @@ -504,13 +447,12 @@ class FS(object): path -- Root path to start walking wildcard -- If given, only return files that match this wildcard - dir_wildcard -- If given, only walk in to directories that match this wildcard - search -- A string that identifies the method used to walk the directories, - can be 'breadth' for a breadth first search, or 'depth' for a depth first - search. Use 'depth' if you plan to create / delete files as you go. - + dir_wildcard -- If given, only walk directories that match the wildcard + search -- A string dentifying the method used to walk the directories. + Can be 'breadth' for a breadth first search, or 'depth' for a + depth first search. Use 'depth' if you plan to create or + delete files as you go. """ - for path, files in self.walk(path, wildcard, dir_wildcard, search): for f in files: yield pathjoin(path, f) @@ -520,7 +462,6 @@ class FS(object): """Returns the size (in bytes) of a resource. path -- A path to the resource - """ info = self.getinfo(path) size = info.get('size', None) @@ -537,7 +478,6 @@ class FS(object): be overwritten; If False then DestinationExistsError will be raised. chunk_size -- Size of chunks to use if a simple copy is required - """ if not self.isfile(src): @@ -578,7 +518,6 @@ class FS(object): overwrite -- If True, then an existing file at the destination path will be silently overwritte; if False then an exception will be raised in this case. - """ src_syspath = self.getsyspath(src, allow_none=True) @@ -607,10 +546,12 @@ class FS(object): src -- Source directory path dst -- Destination directory path - overwrite -- If True then any existing files in the destination directory will be overwritten - ignore_errors -- If True then this method will ignore FSError exceptions when moving files - chunk_size -- Size of chunks to use when copying, if a simple copy is required - + overwrite -- If True then any existing files in the destination + directory will be overwritten + ignore_errors -- If True then this method will ignore FSError + exceptions when moving files + chunk_size -- Size of chunks to use when copying, if a simple copy + is required """ if not self.isdir(src): raise ResourceInvalidError(src, msg="Source is not a directory: %(path)s") @@ -659,10 +600,11 @@ class FS(object): src -- Source directory path dst -- Destination directory path - overwrite -- If True then any existing files in the destination directory will be overwritten + overwrite -- If True then any existing files in the destination + directory will be overwritten ignore_errors -- If True, exceptions when copying will be ignored - chunk_size -- Size of chunks to use when copying, if a simple copy is required - + chunk_size -- Size of chunks to use when copying, if a simple copy + is required """ if not self.isdir(src): raise ResourceInvalidError(src, msg="Source is not a directory: %(path)s") @@ -698,7 +640,6 @@ class FS(object): """Return True if a path contains no files. path -- Path of a directory - """ path = normpath(path) iter_dir = iter(self.listdir(path)) @@ -714,9 +655,8 @@ class SubFS(FS): """A SubFS represents a sub directory of another filesystem object. SubFS objects are returned by opendir, which effectively creates a 'sandbox' - filesystem that can only access files/dirs under a root path within its - 'parent' dir. - + 'sandbox' filesystem that can only access files/dirs under a root path + within its 'parent' dir. """ def __init__(self, parent, sub_dir): @@ -798,23 +738,3 @@ class SubFS(FS): def rename(self, src, dst): return self.parent.rename(self._delegate(src), self._delegate(dst)) - - -if __name__ == "__main__": - import osfs - import browsewin - - fs1 = osfs.OSFS('~/') - fs2 = fs1.opendir("projects").opendir('prettycharts') - - for d, f in fs1.walk('/projects/prettycharts'): - print d, f - - for f in fs1.walkfiles("/projects/prettycharts"): - print f - - #print_fs(fs2) - - - #browsewin.browse(fs1) - browsewin.browse(fs2) diff --git a/fs/browsewin.py b/fs/browsewin.py index 22ae99f..a5c8e65 100644 --- a/fs/browsewin.py +++ b/fs/browsewin.py @@ -151,15 +151,16 @@ class BrowseFrame(wx.Frame): info_frame = InfoFrame(path, self.fs.desc(path), info) info_frame.Show() -def browse(fs): +def browse(fs): app = wx.PySimpleApp() frame = BrowseFrame(fs) frame.Show() app.MainLoop() -if __name__ == "__main__": +if __name__ == "__main__": from osfs import OSFS home_fs = OSFS("~/") browse(home_fs) + diff --git a/fs/memoryfs.py b/fs/memoryfs.py index f67ab29..54be786 100644 --- a/fs/memoryfs.py +++ b/fs/memoryfs.py @@ -455,7 +455,6 @@ class MemoryFS(FS): finally: self._lock.release() - def _on_close_memory_file(self, open_file, path, value): self._lock.acquire() try: @@ -477,8 +476,6 @@ class MemoryFS(FS): finally: self._lock.release() - - def listdir(self, path="/", wildcard=None, full=False, absolute=False, dirs_only=False, files_only=False): self._lock.acquire() try: @@ -511,31 +508,3 @@ class MemoryFS(FS): self._lock.release() - -def main(): - - mem_fs = MemoryFS() - mem_fs.makedir('test/test2', recursive=True) - mem_fs.makedir('test/A', recursive=True) - mem_fs.makedir('test/A/B', recursive=True) - - mem_fs.open("test/readme.txt", 'w').write("Hello, World!") - mem_fs.open("test/readme.txt", 'wa').write("\nSecond Line") - - print mem_fs.open("test/readme.txt", 'r').read() - - f1 = mem_fs.open("/test/readme.txt", 'r') - f2 = mem_fs.open("/test/readme.txt", 'r') - print f1.read(10) - print f2.read(10) - f1.close() - f2.close() - f3 = mem_fs.open("/test/readme.txt", 'w') - print_fs(mem_fs) - - from browsewin import browse - browse(mem_fs) - - -if __name__ == "__main__": - main() diff --git a/fs/mountfs.py b/fs/mountfs.py index 09223a0..742b4d7 100644 --- a/fs/mountfs.py +++ b/fs/mountfs.py @@ -315,43 +315,3 @@ class MountFS(FS): except: self._lock.release() - -if __name__ == "__main__": - - help(MountFS) - - fs1 = MemoryFS() - fs1.makedir("Memroot/B/C/D", recursive=True) - fs1.open("test.txt", 'w').write("Hello, World!") - - #print_fs(fs1) - - mountfs = MountFS() - - mountfs.mountdir('1/2', fs1) - mountfs.mountdir('1/another', fs1) - - def testfile(*args, **kwargs): - print args, kwargs - - def testfile_info(*args, **kwargs): - print "testfile_info", args, kwargs - return {'size':100} - - mountfs.mountfile('filedir/file.txt', testfile, testfile_info) - - print mountfs.getinfo("filedir/file.txt") - - #print mountfs.listdir('1/2/Memroot/B/C') - - print mountfs.isdir("1") - - print mountfs.desc('1/2/Memroot/B') - print_fs(mountfs) - - import browsewin - browsewin.browse(mountfs) - - print mountfs.getinfo("1/2") - - #print mountfs._delegate('1/2/Memroot/B') diff --git a/fs/multifs.py b/fs/multifs.py index 0f78a06..c295e61 100644 --- a/fs/multifs.py +++ b/fs/multifs.py @@ -226,27 +226,3 @@ class MultiFS(FS): finally: self._lock.release() - -if __name__ == "__main__": - - import fs - import osfs - osfs = osfs.OSFS('~/') - import memoryfs - - mem_fs = memoryfs.MemoryFS() - mem_fs.makedir('projects/test2', recursive=True) - mem_fs.makedir('projects/A', recursive=True) - mem_fs.makedir('projects/A/B', recursive=True) - - - mem_fs.open("projects/test2/readme.txt", 'w').write("Hello, World!") - mem_fs.open("projects/A/readme.txt", 'w').write("\nSecond Line") - - multifs = MultiFS() - multifs.addfs("osfs", osfs) - multifs.addfs("mem_fs", mem_fs) - - import browsewin - - browsewin.browse(multifs) diff --git a/fs/objecttree.py b/fs/objecttree.py index b28cef6..e073298 100644 --- a/fs/objecttree.py +++ b/fs/objecttree.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python class _ObjectDict(dict): @@ -104,15 +103,3 @@ class ObjectTree(object): return self.root.iteritems() -if __name__ == "__main__": - - ot = ObjectTree() - ot['a/b/c'] = "Hai!" - - print ot['a/b/c'] - - print ot.partialget("/a/b/c/d/e/f") - - ot['a/b/c/d'] = "?" - - print ot['a/b/c'].keys() diff --git a/fs/rpcfs.py b/fs/rpcfs.py index 904bae9..d53d082 100644 --- a/fs/rpcfs.py +++ b/fs/rpcfs.py @@ -53,6 +53,7 @@ def re_raise_faults(func): def _object_by_name(name,root=None): """Look up an object by dotted-name notation.""" + print name, root bits = name.split(".") if root is None: try: diff --git a/fs/tempfs.py b/fs/tempfs.py index f886ada..993e297 100644 --- a/fs/tempfs.py +++ b/fs/tempfs.py @@ -44,7 +44,4 @@ class TempFS(OSFS): def __del__(self): self.close() -if __name__ == "__main__": - tfs = TempFS() - print tfs diff --git a/fs/utils.py b/fs/utils.py index 85fa464..073c656 100644 --- a/fs/utils.py +++ b/fs/utils.py @@ -1,6 +1,6 @@ """ - fs.utils: high level utility functions for working with FS objects. + fs.utils: high-level utility functions for working with FS objects. """ @@ -131,11 +131,12 @@ def copydir(fs1, fs2, ignore_errors=False, chunk_size=16384): mount_fs.copydir('dir1', 'dir2', ignore_errors=ignore_errors, chunk_size=chunk_size) -def countbytes(count_fs): +def countbytes(fs): """Returns the total number of bytes contained within files in a filesystem. - count_fs -- A filesystem object + fs -- A filesystem object """ - total = sum(count_fs.getsize(f) for f in count_fs.walkfiles()) + total = sum(fs.getsize(f) for f in fs.walkfiles()) return total + diff --git a/fs/zipfs.py b/fs/zipfs.py index 78d60ec..504273a 100644 --- a/fs/zipfs.py +++ b/fs/zipfs.py @@ -225,46 +225,4 @@ class ZipFS(FS): finally: self._lock.release() -if __name__ == "__main__": - def test(): - zfs = ZipFS("t.zip", "w") - zfs.createfile("t.txt", "Hello, World!") - zfs.close() - rfs = ZipFS("t.zip", 'r') - print rfs.getcontents("t.txt") - print rfs.getcontents("w.txt") - - def test2(): - zfs = ZipFS("t2.zip", "r") - print zfs.listdir("/tagging-trunk") - print zfs.listdir("/") - import browsewin - browsewin.browse(zfs) - zfs.close() - #zfs.open("t.txt") - #print zfs.listdir("/") - - test2() - - zfs = ZipFS("t3.zip", "w") - zfs.createfile("t.txt", "Hello, World!") - zfs.createfile("foo/bar/baz/t.txt", "Hello, World!") - - print zfs.getcontents('t.txt') - #print zfs.isdir("t.txt") - #print zfs.isfile("t.txt") - #print zfs.isfile("foo/bar") - zfs.close() - zfs = ZipFS("t3.zip", "r") - print "--" - print zfs.listdir("foo") - print zfs.isdir("foo/bar") - print zfs.listdir("foo/bar") - print zfs.listdir("foo/bar/baz") - print_fs(zfs) - - - #zfs = ZipFS("t3.zip", "r") - #print zfs.zf.getinfo("asd.txt") - - #zfs.close() + -- cgit v1.2.1