Mapper API
Instances of datafile models have an additional datafile
proxy to manually interact with the filesystem. The following sections assume an empty filesystem and use the following sample datafile definition:
from datafiles import datafile
@datafile("my_models/{self.my_key}.yml", manual=True)
class MyModel:
my_key: str
my_value: int = 1
>>> model = MyModel("foo")
Many of the following examples are also shown in this notebook.
path
Get the full path to the mapped file:
>>> model.datafile.path
PosixPath("/Projects/Demo/my_models/foo.yml")
exists
Determine if the mapped file exists:
>>> model.datafile.exists
False
By default, the file is created automatically. Set manual=True
to disable this behavior.
save()
Manually save an object to the filesystem:
>>> model.datafile.save()
By default, this method is called automatically. Set manual=True
to disable this behavior.
load()
Manually load an object from the filesystem:
>>> model.datafile.load()
By default, this method is called automatically. Set manual=True
to disable this behavior.
modified
Determine if there are any unsynchronized changes on the filesystem:
$ echo "my_value: 42" > my_models/foo.yml
>>> model.datafile.modified
True
data
Access the parsed model attributes directly:
>>> model.datafile.data
ordereddict([("my_value", 1)])