Leaf Python Library
The LPL is a Python package that is able to bind Python functions to an LGL protocol. The LPL needs to know the protocol (just a string in LGL code) and where to find the functions mentioned in the protocol. The binding between protocol's filters and Python functions is performed simply by searching for LGL nodes and Python functions having the same name. With this information, the LPL can build a leaf.prj.project Python object, which is the main interface to all Leaf's features. Supposing that a str object called protocol contains valid LGL code and the functions to bind are contained in a file named example.py, the following code builds a Leaf project:from leaf.prj import project pr = project('example', 'protocol')
sys.path.insert(0,'.')
The project and protocol objects
A Leaf project can include a number of protocols. This is mainly due to the alternative protocols feature that is still under development. An usual Leaf project at the moment will include only one unnamed protocols, that can be accessed this way:pt = prj.protocols['']
Requesting a resource
The protocol object is used to request a resource. If you have an LGL protocol like the following:/ func2 func1 < \ func3 ;
pt.provide(func2)
pt.provide(func3)
Resource management, session recovery and protocol consistency
The LPE automatically stores all produced resources internally in primary memory and dumps them to the disk as soon as they are built the first time. The provide method first checks if a resource is available in primary memory; if not, it searches for a dump on the disk; if there is no previous dump the resource is built on the fly applying the process seen in the previous section. The newly available resource is immediately dumped to the disk.Leaf tries to keep your source code consistent with the stored resources. To this aim, your source code is stored together with the resources it produces. If you change the code, Leaf will detect the change by comparing the working code with the stored code as soon as you request an update, automatically untrust-ing the involved filters. The untrust method clears the resources produced by a filter and all its descendants. You can also use the trust method to force Leaf to accept a given resource as the output of a filter.
When a project object is built, the corresponding dumps are searched for on the disk and automatically loaded, including resources and source code that produced them. This way, Leaf will be able to persistently keep track of code changes across different working sessions.
protocol methods
The following is a summary of the operation that you can perform an a protocol. It is extracted from the online help of the LPE.
clear | Clears and undumps a resource. |
dumpOff | Switches dumping OFF. |
dumpOff | Switches dumping ON. |
export | Exports the protocol to a pdf file, including docstrings. |
getinputs | Collects all input resources that are input to the given filter and returns a copy of them in a list. |
list | Lists the state of all resources. |
dumpOff | Switches dumping ON. |
provide | Provides a resource. The resource is returned if available, loaded from disk if dumped, built on the fly otherwise. |
rebuild | Clears a resource, then provides it. |
run | Provides all leaf (final) resources. |
trust | Assign a resource to a filter without invalidating dependent resources. |
run | Provides all leaf (final) resources. |
untrust | Clears a resource and all its dependent. |