Usage¶
We give a usage tutorial here; refer to the man page viat(1) (included in the wheels) for more details. The examples directory contains more thorough examples like scripts for managing dotfiles and bibliographies.
Command-line usage¶
First, a vault must be initialized:
The vault is determined by a .viat subfolder that contains config.toml and storage.toml files (JSON is also supported for both). We can immediately set attributes for any file on the file system:
$ viat update tractatus.pdf '{"author": "Ludwig Wittgenstein", "year": 1921}'
Warning: File 'tractatus.pdf' is not being tracked.
{"author": "Ludwig Wittgenstein", "year": 1921}
All stored attributes for the file get printed; in this case the only stored attributes are those we have just added. We also get a warning saying that the vault's tracker does not know about this file.
The role of the tracker is to enumerate the files that are explicitly tracked by the vault. The default glob-based tracking provider requires explicit patterns. We can track all PDF files in the root of the vault using the following configuration:
With this, we can add new properties without warnings:
The above worked because "true" is a valid JSON value; if we were to set a string instead, we would have to escape it in quotes, which is inconvenient. Instead, we can treat the value as a string by passing the --raw flag:
Scripting¶
Tracking is useful for ensuring consistency with the file system, but also for shell scripting.
For example, the following command produces a table of variables:
$ viat shell-export
path=tractatus.pdf publisher='Annalen der Naturphilosophie' rating=4 author='Ludwig Wittgenstein' year=1921
This can be utilized in bash as follows:
viat shell-export | while read line; do
eval "export $line"
# The attributes are now exported as variables
done
In fish shell this is even simpler:
for line in (viat shell-export)
eval "export $line"
# The attributes are now exported as variables
end
If we add another file, zarathustra.pdf, and if the tracker lists it after tractatus.pdf, then Viat would try to reset the missing attributes to avoid reusing variables from the previous loop iteration:
$ viat shell-export
path=tractatus.pdf publisher='Annalen der Naturphilosophie' rating=4 author='Ludwig Wittgenstein' year=1921
path=zarathustra.pdf publisher= rating= author= year=
Schemas¶
At this point, .viat/storage.toml should now look as follows:
["tractatus.pdf"]
author = "Ludwig Wittgenstein"
year = 1921
rating = 4
publisher = "Annalen der Naturphilosophie"
It makes sense to utilize JSON schemas. Let us add the following to .viat/schema.json:
Now we can no longer set the year to anything that is not a number:
$ viat set tractatus.pdf --raw year string
Error: Validation error for 'tractatus.pdf': data.year must be number.
If we manually change the year to "string", we will get a warning when loading the vault:
$ viat get tractatus.pdf year
Warning: Validation error in stored data for 'tractatus.pdf': data.year must be number.
4
(Re)moving files¶
If we move tractatus.pdf to book.pdf, viat will no longer know about it:
$ viat get book.pdf rating
Warning: File 'book.pdf' is not being tracked.
Error: Attribute 'rating' has not been set for 'book.pdf'.
Such discrepancies are straightforward to determine:
We provide the helpers viat mv and viat rm, but otherwise avoid being too clever.
Programmatic usage¶
The programmatic usage should be straightforward. Here is a brief continuation of the above example:
vault = autoload_vault()
assert vault.tracker.is_tracked('tractatus.pdf')
# This context manager validates and writes the file upon exiting.
# If no mutators have been used, no validation and writing is performed.
with vault.storage as conn:
# The inner lock-based context managers allow either read-only or read-write operations.
# Readers are Mapping instances.
with conn.get_reader('tractatus.pdf') as reader:
print(mut['year'])
# Mutators are MutableMapping instances.
with conn.get_mutator('tractatus.pdf') as mut:
mut['year'] = 1921
Refer to the API reference for details.