score.pyfilesystem¶
This module provides configurable file systems for your project using PyFilesystem2.
Quickstart¶
Add score.pyfilesystem to the modules list and define your file systems like this:
[score.init]
modules =
score.ctx
score.pyfilesystem
[pyfilesystem]
path.memory = mem://
path.tmp = mem://?scope=ctx
You can access the file system called memory as a member of the configured module, or using dict-like access:
>>> type(score.pyfilesystem.memory)
fs.memoryfs.MemoryFS
>>> score.pyfilesystem.memory is score.pyfilesystem['memory']
True
The other filesystem called tmp is only accessible through existing context objects. and will be destroyed along with the context object itself:
>>> with score.ctx.Context() as ctx:
... ctx.fs.tmp.open('foo.txt').write('bar')
Configuration¶
-
score.pyfilesystem.
init
(confdict, ctx=None)[source]¶ Initializes this module acoording to the SCORE module initialization guidelines with the following configuration keys:
- ctx.member.url fs
The name of the context member, that should be registered with the configured
score.ctx
module (if there is one). The default value allows you to always access configured file systems within ascore.ctx.Context
like the following:>>> ctx.fs['my_configured_fs']
- path.*
Configuration keys like path.foo are file system definitions that must contain a valid pyfilesystem URL. These values automatically
register
paths during initialization. The following example is for registering a memory file system called foo:path.foo = mem://
The only customization is a scope parameter, which can also define the scope of a file system. Valid values for this parameter are
global
(the default) andctx
. The same filesystem as above with a scope bound to a context would look like this:path.foo = mem://?scope=ctx
Details¶
Filesystem scope¶
There are two different modes for filesystems: They are either bound to the
ConfiguredPyfilesystemModule
, or they are bound to a
score.ctx.Context
.
In both cases, registered filesystems will only be created on demand, i.e. when they are first accessed. They will remain attached to their scope object until the garbage collector cleans everything up.
API¶
Configuration¶
-
score.pyfilesystem.
init
(confdict, ctx=None)[source] Initializes this module acoording to the SCORE module initialization guidelines with the following configuration keys:
- ctx.member.url fs
The name of the context member, that should be registered with the configured
score.ctx
module (if there is one). The default value allows you to always access configured file systems within ascore.ctx.Context
like the following:>>> ctx.fs['my_configured_fs']
- path.*
Configuration keys like path.foo are file system definitions that must contain a valid pyfilesystem URL. These values automatically
register
paths during initialization. The following example is for registering a memory file system called foo:path.foo = mem://
The only customization is a scope parameter, which can also define the scope of a file system. Valid values for this parameter are
global
(the default) andctx
. The same filesystem as above with a scope bound to a context would look like this:path.foo = mem://?scope=ctx
-
class
score.pyfilesystem.
Scope
¶ An
enumeration
of valid scope values for registered paths.-
GLOBAL = "global"
Global scope: the file system can be accessed at any time.
-
CTX = "ctx"
Context scope: the file system can only be accessed on context objects.
-
CONTEXT = "ctx"
Alias for the CTX value, above
-
-
class
score.pyfilesystem.
Path
¶ A
namedtuple
with the attributes name, url and scope. It stores the information about a single path registered viascore.pyfilesystem.ConfiguredPyfilesystemModule.register_path()
.
-
class
score.pyfilesystem.
ConfiguredPyfilesystemModule
[source]¶ This module’s
configuration class
.You can access all registered file systems either as attributes of this class, or using dict access:
>>> assert score.fs.foo is score.fs['foo']
-
register_path
(name, url, scope='global')[source]¶ Register a path with given name on this instance before the module is finalized. The url must be a valid pyfilesystem URL.
Valid values for scope are
Scope
instances, or their str equivalents (global
orctx
).
-