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
32e8c355
Commit
32e8c355
authored
Jul 24, 2009
by
rfkelly0
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
thread-safe RemoteFileBuffer
parent
f2e350a7
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
8 deletions
+53
-8
fs/__init__.py
+4
-3
fs/remote.py
+49
-5
No files found.
fs/__init__.py
View file @
32e8c355
...
...
@@ -10,6 +10,7 @@ implementations of this interface such as:
TempFS: a temporary filesystem that's automatically cleared on exit
MemoryFS: a filesystem that exists only in memory
ZipFS: access a zipfile like a filesystem
SFTPFS: access files on a SFTP server
S3FS: access files stored in Amazon S3
"""
...
...
@@ -17,11 +18,11 @@ implementations of this interface such as:
__version__
=
"0.2.0"
__author__
=
"Will McGugan (will@willmcgugan.com)"
# 'base' imports * from 'path' and 'errors', so their
contents
# will be available here as well.
# 'base' imports * from 'path' and 'errors', so their
#
contents
will be available here as well.
from
base
import
*
# provide these by default so people c
na b
e 'fs.path.basename' etc.
# provide these by default so people c
an us
e 'fs.path.basename' etc.
import
errors
import
path
...
...
fs/remote.py
View file @
32e8c355
...
...
@@ -2,6 +2,20 @@
fs.remote: utilities for interfacing with remote filesystems
This module provides reusable utility functions that can be used to construct
FS subclasses interfacing with a remote filesystem. These include:
RemoteFileBuffer: a file-like object that locally buffers the contents
of a remote file, writing them back on flush() or close().
ConnectionManagerFS: a WrapFS subclass that tracks the connection state
of a remote FS, and allows client code to wait for
a connection to be re-established.
CacheFS: a WrapFS subclass that caces file and directory meta-data in
memory, to speed access to a remote FS.
"""
import
time
...
...
@@ -49,13 +63,18 @@ class RemoteFileBuffer(object):
The owning filesystem, path and mode must be provided. If the
optional argument 'rfile' is provided, it must be a read()-able
object containing the initial file contents.
object
or a string
containing the initial file contents.
"""
self
.
file
=
TempFile
()
self
.
fs
=
fs
self
.
path
=
path
self
.
mode
=
mode
self
.
closed
=
False
self
.
_flushed
=
False
if
hasattr
(
fs
,
"_lock"
):
self
.
_lock
=
copy
.
deepcopy
(
fs
.
_lock
)
else
:
self
.
_lock
=
threading
.
RLock
()
if
rfile
is
not
None
:
if
hasattr
(
rfile
,
"read"
):
data
=
rfile
.
read
(
1024
*
256
)
...
...
@@ -71,13 +90,21 @@ class RemoteFileBuffer(object):
if
not
self
.
closed
:
self
.
close
()
# This is lifted straight from the stdlib's tempfile.py
def
__getattr__
(
self
,
name
):
file
=
self
.
__dict__
[
'file'
]
a
=
getattr
(
file
,
name
)
if
not
issubclass
(
type
(
a
),
type
(
0
)):
setattr
(
self
,
name
,
a
)
if
not
callable
(
a
):
return
a
@wraps
(
a
)
def
call_with_lock
(
*
args
,
**
kwds
):
self
.
_lock
.
acquire
()
try
:
self
.
_flushed
=
False
return
a
(
*
args
,
**
kwds
)
finally
:
self
.
_lock
.
release
()
setattr
(
self
,
name
,
call_with_lock
)
return
call_with_lock
def
__enter__
(
self
):
self
.
file
.
__enter__
()
...
...
@@ -90,20 +117,37 @@ class RemoteFileBuffer(object):
def
__iter__
(
self
):
return
iter
(
self
.
file
)
def
truncate
(
self
,
size
=
None
):
self
.
_lock
.
acquire
()
try
:
self
.
file
.
truncate
(
size
)
self
.
flush
()
finally
:
self
.
_lock
.
release
()
def
flush
(
self
):
self
.
_lock
.
acquire
()
try
:
self
.
file
.
flush
()
if
"w"
in
self
.
mode
or
"a"
in
self
.
mode
or
"+"
in
self
.
mode
:
pos
=
self
.
file
.
tell
()
self
.
file
.
seek
(
0
)
self
.
fs
.
setcontents
(
self
.
path
,
self
.
file
)
self
.
file
.
seek
(
pos
)
self
.
_flushed
=
True
finally
:
self
.
_lock
.
release
()
def
close
(
self
):
self
.
_lock
.
acquire
()
try
:
self
.
closed
=
True
if
"w"
in
self
.
mode
or
"a"
in
self
.
mode
or
"+"
in
self
.
mode
:
self
.
file
.
seek
(
0
)
self
.
fs
.
setcontents
(
self
.
path
,
self
.
file
)
self
.
file
.
close
()
finally
:
self
.
_lock
.
release
()
class
ConnectionManagerFS
(
WrapFS
):
...
...
@@ -285,7 +329,7 @@ def _cached_method(func):
class
CacheFS
(
WrapFS
):
"""Simple wrapper to cache meta-data of a remote filesystems.
This FS wrapper implements a simplistic cache that can help speedup
This FS wrapper implements a simplistic cache that can help speed
up
access to a remote filesystem. File and directory meta-data is cached
but the actual file contents are not.
"""
...
...
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