lektor-website/content/docs/api/db/obj/contents.lr

64 lines
2.2 KiB
Plaintext
Raw Normal View History

2015-12-19 14:52:17 +01:00
title: SourceObject
---
summary: The basic interface of all source objects.
---
module: lektor.db
---
type: class
---
body:
Source objects is the common interface for all things that come out of the
database. There are two types of source objects:
* [Records :ref](../record/) which are pages and attachments from the
`contents/` folder.
* [Assets :ref](../asset/) which are files and directories from the
`assets/` folder.
Whatever object you have in hands that comes from the database, they will
at least provide a minimal useful set of API methods and properties.
Plugins can subclass source objects to come up with their own source
objects if needed. In addition to that there is a special source object
called the `VirtualSourceObject` which is more useful for plugin usage.
## Virtual Source Objects
Most plugins will not have source objects that actually originate on the
file system. This means that their "source" is entirely virtual. Because
this is a very common situation there is a base class, the
`VirtualSourceObject` which plugins can subclass. The constructor takes one
2016-01-09 10:24:39 +01:00
argument which is the source record the virtual source lives below. Virtual
sources are separated from the records they belong to with an at sign (`@`)
in the path. The part after the at sign is called the “virtual path”.
For instance in the below example the canonical path for the object would be
the record's path + `@source`. So if the record was `/hello` then the
path would be `/hello@source`. The true base record it belongs to can be
referenced from the [record :ref](record/) property.
2015-12-19 14:52:17 +01:00
```python
from lektor.sourceobj import VirtualSourceObject
2016-01-09 10:24:39 +01:00
from lektor.utils import build_url
2015-12-19 14:52:17 +01:00
class Source(VirtualSourceObject):
2016-01-09 10:24:39 +01:00
@property
def path(self):
return self.record.path + '@source'
2015-12-19 14:52:17 +01:00
@property
def source_content(self):
2016-01-09 10:24:39 +01:00
with open(self.record.source_filename) as f:
2015-12-19 14:52:17 +01:00
return f.read().decode('utf-8')
@property
def url_path(self):
2016-01-09 10:24:39 +01:00
return build_url([self.record.url_path, 'source.txt'])
2015-12-19 14:52:17 +01:00
```
For more information see [add-build-program
2016-01-09 10:24:39 +01:00
:ref](../../environment/add-build-program/) as well as
[virtualpathresolver :ref](../../environment/virtualpathresolver/).