norfs¶
Normalized filesystem. This library offers a common interface to interact with multiple filesystems, local or remote:
import norfs.helpers
local = norfs.helpers.local()
demo_file = local.file('demo.txt')
demo_file.write(b'Hello World')
print(demo_file.read())
Installation¶
Install with pip: pip install norfs.
You can also download the source code from the git repository.
Supported Filesystems¶
The library is easily extensible! If you want a new filesystem supported implement it yourself or create an issue.
Local filesystem¶
Implements the norfs contract to work with files and directories in the local filesystem:
import norfs.helpers
local = norfs.helpers.local()
my_directory = local.dir("/my/local/directory")
my_file = local.file("/my/local/file.txt")
S3¶
Implements the norfs contract to work with files and directories in an S3 compatible store:
import norfs.helpers
import boto3
s3 = norfs.helpers.s3(s3_client=boto3.client("s3"))
my_directory = s3.dir("myBucket/my/s3/directory")
my_file = s3.file("myBucket/my/s3/file.txt")
In-memory¶
Implements the norfs contract to work with files and directories in a very simple in-memory filesystem:
import norfs.helpers
memory = norfs.helpers.memory()
my_directory = memory.dir("/my/memory/directory")
my_file = memory.file("/my/memory/file.txt")
The norfs interface¶
The most public interface norfs exposes is composed of the Clients, the Filesystem objects and the helpers module.
Clients¶
- 
class 
norfs.client.CopyClient(copier: norfs.copy.base.Copier)[source] Bases:
objectnorfs.client.CopyClientprovides a unified simple copy API for anynorfs.filesystem.Fileornorfs.filesystem.Directoryfrom any file system. It is usually accessed by usingnorfs.helpers.get_copy_client():import norfs.helpers local = norfs.helpers.local() cp_local_only = norfs.helpers.get_copy_client(local) cp_local_only.copy(local.file('source_file.txt'), local.file('target_file.txt')) memory = norfs.helpers.memory() import boto3 s3 = norfs.helpers.s3(s3_client=boto3.client('s3')) cp_for_all = norfs.helpers.get_copy_client(local, s3, memory) cp_for_all.copy(s3.file('myBucket/source_file.txt'), local.file('target_file.txt'))
norfs.helpers.get_copy_client()returns anorfs.client.CopyClientinstance configured with copy strategies for each of the file system clients passed.A
norfs.copy.base.Copiercan have copy policies set for a pair of source and destination file systems to implement a better strategy of copying between them than read source and write destination.norfs.helpers.get_copy_client()helps you by setting these for you.A
norfs.client.CopyClientexposes the following interface:- 
property 
copier The
norfs.copy.base.Copierinstance managed by the client.
- 
copy(src: norfs.filesystem.BaseFileSystemObject, dst: norfs.filesystem.BaseFileSystemObject) → None[source] Copies
srctodst, no mater the file systems they are on.srcanddstcan by bothnorfs.filesystem.Fileornorfs.filesystem.Directory. The only operation not supported is copying from anorfs.filesystem.Directoryinto anorfs.filesystem.Fileas it does not make sense.If source is a
norfs.filesystem.Directoryand destination is anorfs.filesystem.Fileit raises aTypeError.On copy failure it raises a
norfs.fs.base.FileSystemOperationError.
- 
property 
 
- 
class 
norfs.client.FileSystemClient(fs: norfs.fs.base.BaseFileSystem)[source] Bases:
objectnorfs.client.FileSystemClientprovides a way to access the file system objects of a given file system. It is a handy class that provides easy access to :class:norfs.filesystem.File andnorfs.filesystem.Directoryinstances. It is usually obtained usingnorfs.helpers:import norfs.helpers local_fs_client = norfs.helpers.local() memory_fs_client = norfs.helpers.memory() import boto3 s3_fs_client = norfs.helpers.s3(s3_client=boto3.client('s3'))
A
norfs.client.FileSystemClientexposes the following interface:- 
dir(path: str) → norfs.filesystem.Directory[source] Returns a
norfs.filesystem.Directoryinstance for the given path in the managed file system.
- 
file(path: str) → norfs.filesystem.File[source] Returns a
norfs.filesystem.Fileinstance for the given path in the managed file system.
- 
property 
fs The
norfs.filesystem.BaseFileSystemObjectthe client is managing.
- 
 
Filesystem objects¶
norfs.filesystem.BaseFileSystemObject represents any object in the filesystem. It is the most abstract
representation.
A norfs.filesystem.BaseFileSystemObject exposes the following interface:
- 
class 
norfs.filesystem.BaseFileSystemObject(filesystem: norfs.fs.base.BaseFileSystem, path_str: Optional[str], *, _path: Optional[norfs.fs.base.Path] = None)[source] Bases:
object- 
as_dir() → norfs.filesystem.Directory[source] Returns itself as a Directory instance or raises a
NotADirectoryError.
- 
as_file() → norfs.filesystem.File[source] Returns itself as a
norfs.filesystem.Fileinstance or raises anorfs.fs.base.NotAFileError.
- 
copy_object() → norfs.copy.base.CopyFileSystemObject[source] 
- 
copy_to(dst: norfs.filesystem.BaseFileSystemObject, strategy: norfs.copy.base.CopyStrategy) → None[source] 
- 
exists() → bool[source] Returns whether self exists in the file system.
- 
is_dir() → bool[source] Returns whether self is a Directory.
- 
is_file() → bool[source] Returns whether self is a File.
- 
property 
name The name of self.
- 
parent() → norfs.filesystem.Directory[source] Return parent
norfs.filesystem.Directoryof self.
- 
property 
path The full, absolute, path of self in the file system.
- 
remove() → None[source] Tries to remove self from the file system. On failure it raises a
norfs.fs.base.FileSystemOperationError
- 
property 
uri The URI that points to self in the file system.
- 
 
- 
class 
norfs.filesystem.Directory(filesystem: norfs.fs.base.BaseFileSystem, path_str: Optional[str], *, _path: Optional[norfs.fs.base.Path] = None)[source] Bases:
norfs.filesystem.BaseFileSystemObject- 
as_dir() → norfs.filesystem.Directory[source] Returns itself as a
norfs.filesystem.Directoryinstance or raises aNotADirectoryError.
- 
copy_object() → norfs.copy.base.CopyFileSystemObject[source] 
- 
file(path: str) → norfs.filesystem.File[source] Returns a
norfs.filesystem.Filewith its path as being the given path relative to the current directory.
- 
is_dir() → bool[source] Returns whether self is a
norfs.filesystem.Directory.
- 
list() → Iterable[norfs.filesystem.BaseFileSystemObject][source] Returns the contents of the
norfs.filesystem.Directoryin the file system as a list ofnorfs.filesystem.BaseFileSystemObjects.If the
norfs.filesystem.Directorydoes not exist the list will be empty.
- 
remove() → None[source] Tries to remove self from the file system.
On failure it raises a
norfs.fs.base.FileSystemOperationError
- 
subdir(path: str) → norfs.filesystem.Directory[source] Returns a
norfs.filesystem.Directorywith its path as being the given path relative to the current directory.
- 
 
- 
class 
norfs.filesystem.File(filesystem: norfs.fs.base.BaseFileSystem, path_str: Optional[str], *, _path: Optional[norfs.fs.base.Path] = None)[source] Bases:
norfs.filesystem.BaseFileSystemObject- 
as_file() → norfs.filesystem.File[source] Returns itself as a
norfs.filesystem.Fileinstance or raises anorfs.fs.base.NotAFileError.
- 
copy_object() → norfs.copy.base.CopyFileSystemObject[source] 
- 
is_file() → bool[source] Returns whether self is a
norfs.filesystem.File.
- 
read() → bytes[source] Returns the contents of the file.
If it fails to read the file a
norfs.fs.base.FileSystemOperationErrorwill be raised.
- 
remove() → None[source] Tries to remove self from the file system.
On failure it raises a
norfs.fs.base.FileSystemOperationError
- 
set_perms(policies: List[norfs.permissions.Policy]) → None[source] Set the access policies for the file.
The actual meaning of the scopes and permissions depends on the underlying
norfs.fs.base.BaseFileSystemimplementation.
- 
set_props(*, content_type: Optional[str] = None, tags: Optional[Dict[str, str]] = None, metadata: Optional[Dict[str, str]] = None) → None[source] Set the properties for the file.
The actual setting of the metadata depends on the underlying
norfs.fs.base.BaseFileSystemimplementation.
- 
write(content: bytes) → None[source] Sets the contents of the file. If the parent directory does not exist it is created.
If it fails to write the file a
norfs.fs.base.FileSystemOperationErrorwill be raised.
- 
 
Helpers module¶
The norfs.helpers offers functions that serve as shortcuts for common operations with the library.
The norfs.helpers contains the following functions:
- 
norfs.helpers.get_copy_client(*args: norfs.client.FileSystemClient) → norfs.client.CopyClient[source] Helper function to get a
norfs.copy.base.CopyClientinstance configured with copy strategies for the given file systems. This function only sets the built-innorfs.copy.base.CopyStrategys for the built-in file systems, all other will be ignored. For example:# Given s3_boto_1 = boto3.client('s3') s3_boto_2 = boto3.client('s3', endpoint_url='http://my.s3.endpoint') local = norfs.helpers.local() s3_1 = norfs.helpers.s3(s3_boto_1) s3_2 = norfs.helpers.s3(s3_boto_2) memory = norfs.helpers.memory() # Doing cp = get_copy_client(local, s3_1, s3_2, memory) # Is equivalent to copier = norfs.copy.base.Copier(norfs.copy.base.GenericCopyStrategy()) copier.set_copy_policy(local.fs, local.fs, norfs.copy.local.LocalToLocalCopyStrategy()) copier.set_copy_policy(local.fs, s3_1.fs, norfs.copy.local.LocalToS3CopyStrategy(s3_boto_1)) copier.set_copy_policy(local.fs, s3_2.fs, norfs.copy.local.LocalToS3CopyStrategy(s3_boto_2)) copier.set_copy_policy(s3_1.fs, local.fs, norfs.copy.s3.S3ToLocalCopyStrategy()) copier.set_copy_policy(s3_1.fs, s3_1.fs, norfs.copy.s3.S3ToS3CopyStrategy(s3_boto_1)) copier.set_copy_policy(s3_2.fs, local.fs, norfs.copy.s3.S3ToLocalCopyStrategy()) copier.set_copy_policy(s3_2.fs, s3_2.fs, norfs.copy.s3.S3ToS3CopyStrategy(s3_boto_2)) cp = norfs.client.CopyClient(copier)
- 
norfs.helpers.local() → norfs.client.FileSystemClient[source] Returns a new
norfs.client.FileSystemClientmanaging a new instance ofnorfs.fs.local.LocalFileSystem.
- 
norfs.helpers.memory(**kwargs: Any) → norfs.client.FileSystemClient[source] Returns a new
norfs.client.FileSystemClientmanaging a new instance ofnorfs.fs.memory.MemoryFileSystem.kwargsis passed directly to the file system constructor.
- 
norfs.helpers.s3(*args: Any, **kwargs: Any) → norfs.client.FileSystemClient[source] Returns a new
norfs.client.FileSystemClientmanaging a new instance ofnorfs.fs.s3.S3FileSystem.argsandkwargsare passed directly to the file system constructor.