Commit 42856dfb by willmcgugan

Renamed fs.py to base.py after all. Added copydir and movedir functions to utils

parent 1fe2212c
......@@ -7,7 +7,7 @@ __version__ = "0.1.0"
__author__ = "Will McGugan (will@willmcgugan.com)"
from fs import *
from base import *
from helpers import *
__all__ = ['memoryfs',
'mountfs',
......
......@@ -11,7 +11,7 @@ Requires wxPython.
import wx
import wx.gizmos
import fs
import base as fs
class InfoFrame(wx.Frame):
......
......@@ -2,6 +2,19 @@
from itertools import chain
def _iteratepath(path, numsplits=None):
path = resolvepath(path)
if not path:
return []
if numsplits == None:
return filter(lambda p:bool(p), path.split('/'))
else:
return filter(lambda p:bool(p), path.split('/', numsplits))
def isabsolutepath(path):
"""Returns True if a given path is absolute.
......
......@@ -6,8 +6,8 @@ A filesystem that exists only in memory, which obviously makes it very fast.
import os
import datetime
from fs import _iteratepath
from fs import *
from helpers import _iteratepath
from base import *
try:
from cStringIO import StringIO
......
#!/usr/bin/env python
from fs import *
from base import *
from objecttree import ObjectTree
from memoryfs import MemoryFS
......@@ -261,6 +261,7 @@ class MountFS(FS):
self.mount_tree[path] = MountFS.DirMount(path, fs)
finally:
self._lock.release()
mount = mountdir
def mountfile(self, path, open_callable=None, info_callable=None):
self._lock.acquire()
......
#!/usr/in/env python
from fs import FS, FSError
from base import FS, FSError
from helpers import *
class MultiFS(FS):
......
#!/usr/bin/env python
from fs import _iteratepath, pathsplit
from helpers import _iteratepath, pathsplit
class _ObjectDict(dict):
pass
......
#!/usr/bin/env python
from fs import *
from base import *
from helpers import *
class OSFS(FS):
......
#!/usr/bin/env python
import unittest
import fs
import base as fs
from helpers import *
from helpers import _iteratepath
import shutil
class TestHelpers(unittest.TestCase):
......@@ -77,11 +78,11 @@ class TestHelpers(unittest.TestCase):
for path, results in tests:
print repr(path), results
for path_component, expected in zip(fs._iteratepath(path), results):
for path_component, expected in zip(_iteratepath(path), results):
self.assertEqual(path_component, expected)
self.assertEqual(list(fs._iteratepath("a/b/c/d", 1)), ["a", "b/c/d"])
self.assertEqual(list(fs._iteratepath("a/b/c/d", 2)), ["a", "b", "c/d"])
self.assertEqual(list(_iteratepath("a/b/c/d", 1)), ["a", "b/c/d"])
self.assertEqual(list(_iteratepath("a/b/c/d", 2)), ["a", "b", "c/d"])
def test_pathsplit(self):
tests = [ ("a/b", ("a", "b")),
......
"""Contains a number of high level utility functions for working with FS objects."""
import shutil
from mountfs import MountFS
def copy_file(src_fs, src_path, dst_fs, dst_path, chunk_size=1024*16):
def copyfile(src_fs, src_path, dst_fs, dst_path, chunk_size=1024*16):
"""Copy a file from one filesystem to another. Will use system copyfile, if both files have a syspath.
Otherwise file will be copied a chunk at a time.
......@@ -42,9 +43,94 @@ def copy_file(src_fs, src_path, dst_fs, dst_path, chunk_size=1024*16):
if dst is not None:
dst.close()
def get_total_data(count_fs):
"""Returns the total number of bytes contained within files.
def movefile(src_fs, src_path, dst_fs, dst_path, chunk_size=1024*16):
"""Move a file from one filesystem to another. Will use system copyfile, if both files have a syspath.
Otherwise file will be copied a chunk at a time.
src_fs -- Source filesystem object
src_path -- Source path
dst_fs -- Destination filesystem object
dst_path -- Destination filesystem object
chunk_size -- Size of chunks to move if system copyfile is not available (default 16K)
"""
src_syspath = src_fs.getsyspath(src_path, default="")
dst_syspath = dst_fs.getsyspath(dst_path, default="")
# System copy if there are two sys paths
if src_syspath and dst_syspath:
shutil.movefile(src_syspath, dst_syspath)
return
src, dst = None
try:
# Chunk copy
src = src_fs.open(src_path, 'rb')
dst = dst_fs.open(dst_path, 'wb')
while True:
chunk = src.read(chunk_size)
if not chunk:
break
dst.write(chunk)
src_fs.remove(src)
finally:
if src is not None:
src.close()
if dst is not None:
dst.close()
def movedir(fs1, fs2, ignore_errors=False, chunk_size=16384):
"""Moves contents of a directory from one filesystem to another.
fs1 -- Source filesystem, or a tuple of (<filesystem>, <directory path>)
fs2 -- Destination filesystem, or a tuple of (<filesystem>, <directory path>)
ignore_errors -- If True, exceptions from file moves are ignored
chunk_size -- Size of chunks to move if a simple copy is used
"""
if isinstance(fs1, tuple):
fs1, dir1 = fs1
fs1 = fs1.opendir(dir1)
if isinstance(fs2, tuple):
fs2, dir2 = fs2
fs2 = fs1.opendir(dir2)
mount_fs = MountFS()
mount_fs.mount('dir1', fs1)
mount_fs.mount('dir2', fs2)
mount_fs.movedir('dir1', 'dir2', ignore_errors=ignore_errors, chunk_size=chunk_size)
def copydir(fs1, fs2, ignore_errors=False, chunk_size=16384):
"""Copies contents of a directory from one filesystem to another.
fs1 -- Source filesystem, or a tuple of (<filesystem>, <directory path>)
fs2 -- Destination filesystem, or a tuple of (<filesystem>, <directory path>)
ignore_errors -- If True, exceptions from file moves are ignored
chunk_size -- Size of chunks to move if a simple copy is used
"""
if isinstance(fs1, tuple):
fs1, dir1 = fs1
fs1 = fs1.opendir(dir1)
if isinstance(fs2, tuple):
fs2, dir2 = fs2
fs2 = fs1.opendir(dir2)
mount_fs = MountFS()
mount_fs.mount('dir1', fs1)
mount_fs.mount('dir2', fs2)
mount_fs.movedir('dir1', 'dir2', ignore_errors=ignore_errors, chunk_size=chunk_size)
def countbytes(count_fs):
"""Returns the total number of bytes contained within files in a filesystem.
count_fs -- A filesystem object
......
#!/usr/bin/env python
from fs import *
from base import *
from helpers import *
from zipfile import ZipFile, ZIP_DEFLATED, ZIP_STORED
from memoryfs import MemoryFS
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment