Records

Defines records, which are used to make storable objects serializable.

dman.model.record.is_removable(obj)[source]

Check if an object is removable.

class dman.model.record.NoFile(type: str, info: str)[source]

Returned when no file was found in the record target.

class dman.model.record.UnWritable(type: str, info: str)[source]

Returned when no data could be written to the record target file.

class dman.model.record.ExcUnWritable(type: str, info: str, trace: Trace)[source]

Returned when no data could be written to the record target file due to an exception.

class dman.model.record.UnReadable(type: str, info: str, target: str)[source]

Returned when the record target file could not be read.

class dman.model.record.ExcUnReadable(type: str, info: str, trace: Trace, target: str)[source]

Returned when the record target file could not be read due to an exception.

exception dman.model.record.StoreError[source]

Raised when storing failed in such a way that serialization should be cancelled. Usually caused by the quit option was passed when a file is overwritten.

class dman.model.record.Context(mnt: Mount, subdir: PathLike = '')[source]

Serialization context that keeps track of the current folder.

property directory

Absolute directory that the context controls.

classmethod from_directory(directory: str, gitignore: bool = True)[source]

Create a context based on a directory.

classmethod mount(key: str, *, subdir: PathLike = '', cluster: bool = True, generator: Optional[str] = None, base: Optional[PathLike] = None, gitignore: bool = True)[source]

Get a context from a mount point. The path of the file is determined as described below.

If the files are clustered then the path is <base>/<generator>/<subdir>/<key>/<key>.<ext> If cluster is set to False then the path is <base>/<generator>/<subdir>/<key>.<ext>

When base is not provided then it is set to .dman if it does not exist an exception is raised.

When generator is not provided it will automatically be set based on the location of the script relative to the .dman folder (again raising an exception if it is not found). For example if the script is located in <project-root>/examples/folder/script.py and .dman is located in <project-root>/.dman. Then generator is set to cache/examples:folder:script (i.e. the / is replaced by : in the output).

Parameters:
  • key (str) – Key for the file.

  • subdir (os.PathLike, optional) – Specifies optional subdirectory in generator folder. Defaults to “”.

  • cluster (bool, optional) – A subfolder key is automatically created when set to True. Defaults to True.

  • generator (str, optional) – Specifies the generator that created the file. Defaults to script label.

  • base (os.PathLike, optional) – Specifies the root folder. Defaults to “.dman”.

  • gitignore (bool, optional) – Specifies whether files added to this mount point should be ignored.

serialize_list(ser: list)[source]

Serialize a list. Automatically turns the list into an dman.mlist if it contains a storable object.

serialize_dict(ser: dict)[source]

Serialize a dict. Automatically turns the dict into an dman.mdict if it contains a storable object.

absolute(target: PathLike)[source]

Get the path of a target relative to the mount point.

write(target: PathLike, storable)[source]

Write a storable to a target.

read(target: PathLike, expected)[source]

Read a storable of some expected type from a target.

remove(target: Optional[PathLike] = None, *, obj=None)[source]

Remove object or file, stored at specified target.

join(subdir: PathLike)[source]

Create a context relative to this one based on the subdirectory.

open(target: PathLike, *args, **kwargs)[source]

Open a file, registered by this context.

The signature is identical to the standard open command.

prepare(target: PathLike, *, choice: Optional[str] = None) Tuple[Context, Target][source]

Prepare a target for writing a storable to.

close()[source]

Close this context point.

Empty subdirectories are deleted and a gitignore is created if requested on creation.

dman.model.record.is_unloaded(obj)[source]

Check if an object is unloaded.

dman.model.record.is_undefined(obj)[source]

Check if an object is undefined.

class dman.model.record.Unloaded(type: str, target: PathLike, base: PathLike, context: Context)[source]

Unloaded storable object.

type: str

Storable type

target: PathLike

Target relative to the context

base: PathLike

The directory of the context

context: Context

The context used when the record was deserialized.

property path

Path where the content of this storable is located.

class dman.model.record.Undefined(type: str)[source]

Undefined storable object.

type: str

Storable type

class dman.model.record.Record(content: Any, target: Optional[Target] = None, preload: bool = False)[source]

Wraps a storable in such a way to make it serializable.

Keeps track of the file the storable is written to and by default only loads the content when it is requested.

When no target is given a unique file name is determined using uuid4. The suffix of the file is determined automatically based on the type.

exists()[source]

Check if the file associated with this record exists.

isvalid(*, load: bool = False)[source]

Check whether the content was loaded successfully.

property target

The target path where content is written.

property sto_type

The storable type string of the content.

property content

Get the content of this method, loading it if not done before.

load()[source]

Load the content of this record from the file.

store(context: BaseContext)[source]

Store the content of this record.

dman.model.record.record(content: ~typing.Any, /, *, stem: str = <dman.core.path._Auto object>, suffix: str = <dman.core.path._Auto object>, name: str = <dman.core.path._Auto object>, subdir: ~os.PathLike = '', preload: str = False)[source]
Wrap a storable object in a serializable record.

The target path is specified as:

  • ./subdir/stem+suffix or

  • ./subdir/name.

Parameters:
  • content – The storable object.

  • stem (str) – The stem of a file.

  • suffix (str) – The suffix or extension of the file (e.g. '.json').

  • name (str) – The full name of the file.

  • subdir (str) – The subdirectory in which to store te file.

  • preload (bool) – When True the file will be loaded during deserialization.

Raises:

ValueError – if a name and a stem and/or suffix are specified.

dman.model.record.remove(obj, context: Optional[Context] = None)[source]

Remove all files created by an object.