Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pyfs
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pyfs
Commits
20640d1a
Commit
20640d1a
authored
Aug 19, 2008
by
willmcgugan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Work in progress
parent
109c7f5d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
82 additions
and
39 deletions
+82
-39
fs/fs.py
+42
-16
fs/osfs.py
+1
-1
fs/tests.py
+11
-11
fs/utils.py
+28
-11
No files found.
fs/fs.py
View file @
20640d1a
...
...
@@ -140,14 +140,27 @@ class NullFile(object):
def
isabsolutepath
(
path
):
"""Returns True if a given path is absolute."""
"""Returns True if a given path is absolute.
>>> isabsolutepath("a/b/c")
False
>>> isabsolutepath("/foo/bar")
True
"""
if
path
:
return
path
[
0
]
in
'
\\
/'
return
False
def
normpath
(
path
):
"""Normalizes a path to be in the formated expected by FS objects.
Returns a new path string."""
Returns a new path string.
>>> normpath(r"foo
\\
bar
\\
baz")
'foo/bar/baz'
"""
return
path
.
replace
(
'
\\
'
,
'/'
)
...
...
@@ -156,6 +169,12 @@ def pathjoin(*paths):
paths -- An iterable of path strings
>>> pathjoin('foo', 'bar', 'baz')
'foo/bar/baz'
>>> pathjoin('foo/bar', '../baz')
'foo/baz'
"""
absolute
=
False
...
...
@@ -193,7 +212,7 @@ def pathsplit(path):
('foo', 'bar')
>>> pathsplit("foo/bar/baz")
('foo/bar', 'ba
r
')
('foo/bar', 'ba
z
')
"""
...
...
@@ -203,9 +222,25 @@ def pathsplit(path):
return
tuple
(
split
)
def
resolvepath
(
path
):
"""Normalises the path and removes any relative path components.
path -- A path string
>>> resolvepath(r"foo
\\
bar
\\
..
\\
baz")
'foo/baz'
"""
return
pathjoin
(
path
)
def
makerelative
(
path
):
"""Makes a path relative by removing initial separator.
path -- A normalised path
>>> makerelative("/foo/bar")
'foo/bar'
"""
if
path
.
startswith
(
'/'
):
return
path
[
1
:]
return
path
...
...
@@ -261,8 +296,10 @@ class FS(object):
return
pathjoin
(
'/'
,
pathname
)
return
pathname
def
getsyspath
(
self
,
path
):
raise
NoSysPathError
(
"NO_SYS_PATH"
,
path
)
def
getsyspath
(
self
,
path
,
default
=
None
):
if
default
is
None
:
raise
NoSysPathError
(
"NO_SYS_PATH"
,
path
)
return
default
def
open
(
self
,
path
,
mode
=
"r"
,
buffering
=-
1
,
**
kwargs
):
raise
UnsupportedError
(
"UNSUPPORTED"
)
...
...
@@ -400,17 +437,6 @@ class FS(object):
def
getsize
(
self
,
path
):
return
self
.
getinfo
(
path
)[
'size'
]
def
makefile
(
self
,
path
,
data
):
f
=
None
try
:
f
=
self
.
open
(
path
,
"wb"
)
f
.
write
(
data
)
finally
:
if
f
is
not
None
:
f
.
close
()
return
True
class
SubFS
(
FS
):
...
...
fs/osfs.py
View file @
20640d1a
...
...
@@ -18,7 +18,7 @@ class OSFS(FS):
def
__str__
(
self
):
return
"<OSFS
\"
%
s
\"
>"
%
self
.
root_path
def
getsyspath
(
self
,
path
):
def
getsyspath
(
self
,
path
,
default
=
None
):
sys_path
=
os
.
path
.
join
(
self
.
root_path
,
makerelative
(
self
.
_resolve
(
path
)))
return
sys_path
...
...
fs/tests.py
View file @
20640d1a
...
...
@@ -6,6 +6,7 @@ import fs
class
TestHelpers
(
unittest
.
TestCase
):
def
test_isabsolutepath
(
self
):
"""fs.isabsolutepath tests"""
tests
=
[
(
''
,
False
),
(
'/'
,
True
),
(
'/A/B'
,
True
),
...
...
@@ -16,6 +17,7 @@ class TestHelpers(unittest.TestCase):
self
.
assertEqual
(
fs
.
isabsolutepath
(
path
),
result
)
def
test_normpath
(
self
):
"""fs.normpath tests"""
tests
=
[
(
"
\\
a
\\
b
\\
c"
,
"/a/b/c"
),
(
""
,
""
),
(
"/a/b/c"
,
"/a/b/c"
),
...
...
@@ -24,6 +26,7 @@ class TestHelpers(unittest.TestCase):
self
.
assertEqual
(
fs
.
normpath
(
path
),
result
)
def
test_pathjon
(
self
):
"""fs.pathjoin tests"""
tests
=
[
(
""
,
"a"
,
"a"
),
(
"a"
,
"a"
,
"a/a"
),
(
"a/b"
,
"../c"
,
"a/c"
),
...
...
@@ -51,6 +54,7 @@ class TestHelpers(unittest.TestCase):
self
.
assertRaises
(
fs
.
PathError
,
fs
.
pathjoin
,
"a/b/../../../d"
)
def
test_makerelative
(
self
):
"""fs.makerelative tests"""
tests
=
[
(
"/a/b"
,
"a/b"
),
(
"a/b"
,
"a/b"
),
(
"/"
,
""
)
]
...
...
@@ -59,7 +63,8 @@ class TestHelpers(unittest.TestCase):
print
path
,
result
self
.
assertEqual
(
fs
.
makerelative
(
path
),
result
)
def
test_absolute
(
self
):
def
test_makeabsolute
(
self
):
"""fs.makeabsolute tests"""
tests
=
[
(
"/a/b"
,
"/a/b"
),
(
"a/b"
,
"/a/b"
),
(
"/"
,
"/"
)
]
...
...
@@ -68,6 +73,7 @@ class TestHelpers(unittest.TestCase):
self
.
assertEqual
(
fs
.
makeabsolute
(
path
),
result
)
def
test_iteratepath
(
self
):
"""fs.iteratepath tests"""
tests
=
[
(
"a/b"
,
[
"a"
,
"b"
]),
(
""
,
[]
),
(
"aaa/bbb/ccc"
,
[
"aaa"
,
"bbb"
,
"ccc"
]),
...
...
@@ -82,6 +88,7 @@ class TestHelpers(unittest.TestCase):
self
.
assertEqual
(
list
(
fs
.
_iteratepath
(
"a/b/c/d"
,
2
)),
[
"a"
,
"b"
,
"c/d"
])
def
test_pathsplit
(
self
):
"""fs.pathsplit tests"""
tests
=
[
(
"a/b"
,
(
"a"
,
"b"
)),
(
"a/b/c"
,
(
"a/b"
,
"c"
)),
(
"a"
,
(
""
,
"a"
)),
...
...
@@ -119,7 +126,7 @@ class TestFS(unittest.TestCase):
return
os
.
path
.
exists
(
os
.
path
.
join
(
self
.
temp_dir
,
p
))
def
test_makedir
(
self
):
"""osfs.makedir tests"""
check
=
self
.
check
self
.
fs
.
makedir
(
"a"
)
...
...
@@ -137,7 +144,7 @@ class TestFS(unittest.TestCase):
def
test_removedir
(
self
):
"""osfs.removedir tests"""
check
=
self
.
check
self
.
fs
.
makedir
(
"a"
)
self
.
assert_
(
check
(
"a"
))
...
...
@@ -159,20 +166,13 @@ class TestFS(unittest.TestCase):
self
.
assert_
(
not
check
(
"foo"
))
def
test_rename
(
self
):
"""osfs.rename tests"""
check
=
self
.
check
self
.
fs
.
open
(
"foo.txt"
,
'wt'
)
.
write
(
"Hello, World!"
)
self
.
assert_
(
check
(
"foo.txt"
))
self
.
fs
.
rename
(
"foo.txt"
,
"bar.txt"
)
self
.
assert_
(
check
(
"bar.txt"
))
def
test_makefile
(
self
):
check
=
self
.
check
self
.
fs
.
makefile
(
"foo.txt"
,
"Hello, World!"
)
data
=
self
.
fs
.
open
(
"foo.txt"
)
.
read
()
self
.
assertEqual
(
data
,
"Hello, World!"
)
if
__name__
==
"__main__"
:
#t = TestFS()
...
...
fs/utils.py
View file @
20640d1a
import
shutil
def
copyfile
(
src_fs
,
src_path
,
dst_fs
,
dst_path
,
chunk_size
=
1024
*
16
):
def
copy
_
file
(
src_fs
,
src_path
,
dst_fs
,
dst_path
,
chunk_size
=
1024
*
16
):
def
getsyspath
(
_fs
,
path
):
try
:
return
_fs
.
getsyspath
(
path
)
except
NoSysPathError
:
return
""
"""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.
src_syspath
=
src_fs
.
getsyspath
(
src_path
)
dst_syspath
=
dst_fs
.
getsyspath
(
dst_path
)
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
:
...
...
@@ -28,9 +33,22 @@ def copyfile(src_fs, src_path, dst_fs, dst_path, chunk_size=1024*16):
if
not
chunk
:
break
dst
.
write
(
chunk
)
finally
:
if
src
is
not
None
:
src
.
close
()
if
dst
is
not
None
:
dst
.
close
()
\ No newline at end of file
dst
.
close
()
def
get_total_data
(
count_fs
):
"""Returns the total number of bytes contained within files.
count_fs -- A filesystem object
"""
total
=
0
for
f
in
count_fs
.
walkfiles
(
absolute
=
True
):
total
+=
count_fs
.
getsize
(
f
)
return
total
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment