Module with resources dealing with the file repository.
aiida.repository.
AbstractRepositoryBackend
Bases: object
object
Class that defines the abstract interface for an object repository.
The repository backend only deals with raw bytes, both when creating new objects as well as when returning a stream or the content of an existing object. The encoding and decoding of the byte content should be done by the client upstream. The file repository backend is also not expected to keep any kind of file hierarchy but must be assumed to be a simple flat data store. When files are created in the file object repository, the implementation will return a string-based key with which the content of the stored object can be addressed. This key is guaranteed to be unique and persistent. Persisting the key or mapping it onto a virtual file hierarchy is again up to the client upstream.
__abstractmethods__
__dict__
__module__
__weakref__
list of weak references to the object (if defined)
_abc_impl
_put_object_from_filelike
delete_object
Delete the object from the repository.
key – fully qualified identifier for the object within the repository.
FileNotFoundError – if the file does not exist.
OSError – if the file could not be deleted.
erase
Delete the repository itself and all its contents.
Note
This should not merely delete the contents of the repository but any resources it created. For example, if the repository is essentially a folder on disk, the folder itself should also be deleted, not just its contents.
get_object_content
Return the content of a object identified by key.
OSError – if the file could not be opened.
get_object_hash
Return the SHA-256 hash of an object stored under the given key.
Important
A SHA-256 hash should always be returned, to ensure consistency across different repository implementations.
has_object
Return whether the repository has an object with the given key.
True if the object exists, False otherwise.
initialise
Initialise the repository if it hasn’t already been initialised.
kwargs – parameters for the initialisation.
is_initialised
Return whether the repository has been initialised.
is_readable_byte_stream
open
Open a file handle to an object stored under the given key.
this should only be used to open a handle to read an existing file. To write a new file use the method put_object_from_filelike instead.
put_object_from_filelike
yield a byte stream object.
put_object_from_file
Store a new object with contents of the file located at filepath on this file system.
filepath – absolute path of file whose contents to copy to the repository.
the generated fully qualified identifier for the object within the repository.
TypeError – if the handle is not a byte stream.
Store the byte contents of a file in the repository.
handle – filelike object with the byte content to be stored.
uuid
Return the unique identifier of the repository.
DiskObjectStoreRepositoryBackend
Bases: aiida.repository.backend.abstract.AbstractRepositoryBackend
aiida.repository.backend.abstract.AbstractRepositoryBackend
Implementation of the AbstractRepositoryBackend using the disk-object-store as the backend.
disk-object-store
__init__
Initialize self. See help(type(self)) for accurate signature.
__str__
Return the string representation of this repository.
container
File
Data class representing a file object.
__eq__
Return whether this instance is equal to another file object instance.
__hash__
Construct a new instance.
name – The final element of the file path
file_type – Identifies whether the File is a file or a directory
key – A key to map the file to its contents in the backend repository (file only)
objects – Mapping of child names to child Files (directory only)
ValueError – If a key is defined for a directory, or objects are defined for a file
__repr__
Return repr(self).
file_type
Return the file type of the file object.
from_serialized
Construct a new instance from a serialized instance.
serialized – the serialized instance.
the reconstructed file object.
key
Return the key of the file object.
name
Return the name of the file object.
objects
Return the objects of the file object.
serialize
Serialize the metadata into a JSON-serializable format.
the serialization format is optimized to reduce the size in bytes.
dictionary with the content metadata.
FileType
Bases: enum.Enum
enum.Enum
Enumeration to represent the type of a file object.
DIRECTORY
FILE
Repository
File repository.
This class provides an interface to a backend file repository instance, but unlike the backend repository, this class keeps a reference of the virtual file hierarchy. This means that through this interface, a client can create files and directories with a file hierarchy, just as they would on a local file system, except it is completely virtual as the files are stored by the backend which can store them in a completely flat structure. This also means that the internal virtual hierarchy of a Repository instance does not necessarily represent all the files that are stored by repository backend. The repository exposes a mere subset of all the file objects stored in the backend. This is why object deletion is also implemented as a soft delete, by default, where the files are just removed from the internal virtual hierarchy, but not in the actual backend. This is because those objects can be referenced by other instances.
Construct a new instance with empty metadata.
backend – instance of repository backend to use to actually store the file objects. By default, an instance of the SandboxRepositoryBackend will be created.
SandboxRepositoryBackend
_file_cls
alias of aiida.repository.common.File
aiida.repository.common.File
_insert_file
Insert a new file object in the object mapping.
this assumes the path is a valid relative path, so should be checked by the caller.
path – the relative path where to store the object in the repository.
_pre_process_path
Validate and convert the path to instance of pathlib.PurePosixPath.
pathlib.PurePosixPath
This should be called by every method of this class before doing anything, such that it can safely assume that the path is a pathlib.PurePosixPath object, which makes path manipulation a lot easier.
path – the path as a pathlib.PurePosixPath object or None.
TypeError – if the type of path was not a str nor a pathlib.PurePosixPath instance.
backend
Return the current repository backend.
the repository backend.
clone
Clone the contents of another repository instance.
create_directory
Create a new directory with the given path.
path – the relative path of the directory.
the created directory.
TypeError – if the path is not a string or Path, or is an absolute path.
Path
delete
Delete the repository.
This will not just delete the contents of the repository but also the repository itself and all of its assets. For example, if the repository is stored inside a folder on disk, the folder may be deleted.
Soft delete the object from the repository.
can only delete file objects, but not directories.
path – the relative path of the object within the repository.
hard_delete – when true, not only remove the file from the internal mapping but also call through to the delete_object method of the actual repository backend.
IsADirectoryError – if the object is a directory and not a file.
Delete all objects from the repository.
Construct an instance where the metadata is initialized from the serialized content.
backend – instance of repository backend to use to actually store the file objects.
get_directory
Return the directory object at the given path.
the File representing the object located at the given relative path.
FileNotFoundError – if no object exists for the given path.
NotADirectoryError – if the object at the given path is not a directory.
get_file
Return the file object at the given path.
path – the relative path of the file object.
IsADirectoryError – if the object at the given path is not a directory.
get_file_keys
Return the keys of all file objects contained within this repository.
list of keys, which map a file to its content in the backend repository.
get_object
Return the object at the given path.
Return the content of a object identified by path.
Return whether the repository has an object with the given path.
hash
Generate a hash of the repository’s contents.
Warning
this will read the content of all file objects contained within the virtual hierarchy into memory.
the hash representing the contents of the repository.
kwargs – keyword argument that will be passed to the initialise call of the backend.
is_empty
Return whether the repository is empty.
True if the repository contains no file objects.
list_object_names
Return a sorted list of the object names contained in this repository, optionally in the given sub directory.
a list of File named tuples representing the objects present in directory with the given path.
list_objects
Return a list of the objects contained in this repository sorted by name, optionally in given sub directory.
Open a file handle to an object stored under the given path.
Store a new object under path with contents of the file located at filepath on the local file system.
filepath – absolute path of file whose contents to copy to the repository
TypeError – if the path is not a string and relative path, or the handle is not a byte stream.
put_object_from_tree
Store the entire contents of filepath on the local file system in the repository with under given path.
filepath – absolute path of the directory whose contents to copy to the repository.
path – the relative path where to store the objects in the repository.
TypeError – if the filepath is not a string or Path, or is a relative path.
reset
set_backend
Set the backend for this repository.
backend – the repository backend.
TypeError – if the type of the backend is invalid.
Return the unique identifier of the repository or None if it doesn’t have one.
None
walk
Walk over the directories and files contained within this repository.
the order of the dirname and filename lists that are returned is not necessarily sorted. This is in line with the os.walk implementation where the order depends on the underlying file system used.
os.walk
path – the relative path of the directory within the repository whose contents to walk.
tuples of root, dirnames and filenames just like os.walk, with the exception that the root path is always relative with respect to the repository root, instead of an absolute path and it is an instance of pathlib.PurePosixPath instead of a normal string
Implementation of the AbstractRepositoryBackend using a sandbox folder on disk as the backend.
__del__
Delete the entire sandbox folder if it was instantiated and still exists.
sandbox
Return the sandbox instance of this repository.
A sandbox folder does not have the concept of a unique identifier and so always returns None.
Module with resources common to the repository.
aiida.repository.common.
Module for the implementation of a file repository.
aiida.repository.repository.