Package advene :: Package model :: Package core :: Module content :: Class WithContentMixin
[hide private]
[frames] | no frames]

Class WithContentMixin

source code

Known Subclasses:

I provide functionality for elements with a content.

I assume that I will be mixed in subclasses of PackageElement.

Note that there are 4 kinds of contents:

  backend-stored contents:
    those contents have no URL, and are stored directly in the backend;
    their data can be modified through this class.
  external contents:
    they have a URL at which their data is stored; their data can not be
    modified through this class.
  packaged contents:
    they have a URL in the special ``packaged:`` scheme, meaning that their
    data is stored in the local filesystem (usually extracted from a zipped
    package); their data can be modified through this class.
  empty content:
    only authorized for relations; they are marked by the special mimetype
    "x-advene/none"; neither their model, URL nor data can be modified.

It follows that content-related properties are not independant from one
another. Property `content_mimetype` has higher priority, as it is used to
decide whether the content is empty or not (hence whether the other
properties can be set or not). Then `content_url` is used to decide between
the three other kinds of content, in order to decide whether `content_data`
can be set or not. See the documentation of each property for more detail.

Content initialization
======================

According to CODING_STYLE, a mixin class should not require any
initialization. However, whenever an element is instantiated from backend
data, its content information is available, so it would be a waste of
CPU time (and possibly network traffic) to reload those info from the
backend from the sake of purity.

Hence, this class provides a method _instantiate_content, to be used as an
optimizationin the instantiate class method of element classes.
However, the implementation does not rely on the fact that the mixin will
be initialized with this method.

Content handler
===============

Some element types, like views and queries, require a content handler which
depends on their content's mimetype. This mixin provides a hook method,
named `_update_content_handler`, which is invoked after the mimetype is
modified.

Instance Methods [hide private]
 
_instantiate_content(self, mimetype, model, url)
This is method is for optimization only: it is not strictly required (though recommended) to call it at instantiate time (see class docstring).
source code
 
_check_content(self, mimetype=None, model_id=None, url=None)
Check that the provided values (assumed to be the future values of the corresponding attributes) are valid and consistent (with each other *and* with unmodified attributes).
source code
 
_update_caches(self, old_idref, new_idref, element, relation)
:see-also: `advene.model.core.element.PackageElement._update_caches`
source code
 
_update_content_handler(self)
See :class:`WithContentMixin` documentation.
source code
 
_load_content_info(self)
Load the content info (mimetype, model, url).
source code
 
__store_info(self)
store info in backend
source code
 
__store_data(self)
store data in backend
source code
 
get_content_model(self, default=None)
Return the resource used as the model of the content of this element.
source code
 
get_content_as_synced_file(self)
Return a file-like object giving access to the content data.
source code
 
_get_content_mimetype(self)
The mimetype of this element's content.
source code
 
_set_content_mimetype(self, mimetype) source code
 
_get_content_is_textual(self)
This property indicates if this element's content data can be handled as text.
source code
 
_get_content_model(self)
The resource used as the model of the content of this element.
source code
 
_set_content_model(self, resource)
FIXME: missing docstring.
source code
 
_get_content_model_id(self)
The id-ref of the content model, or an empty string.
source code
 
_get_content_url(self)
This property holds the URL of the content, or an empty string.
source code
 
_set_content_url(self, url)
See `_get_content_url`.
source code
 
_get_content_data(self)
This property holds the data of the content.
source code
 
_set_content_data(self, data)
See `_get_content_data`.
source code
 
_get_content_parsed(self) source code
 
_set_content_parsed(self, parsed) source code
 
_get_content_as_file(self)
This property returns a *copy* of this element's content data wrapped in a file-like object.
source code
 
_get_content(self)
Return a `Content` instance representing the content.
source code
Class Methods [hide private]
 
_check_content_cls(cls, mimetype, model, url, _func=<function _check_content at 0x8f0125c>)
This is a classmethod variant of _check_content, to be use in _instantiate (note that all parameters must be provided in this variant).
source code
Static Methods [hide private]
 
__model_wref() source code
 
__cached_content() source code
Class Variables [hide private]
  __mimetype = None
  __model_id = None
  __url = None
  __data = None
  __as_synced_file = None
  __handler = None
Properties [hide private]
  content
Return a `Content` instance representing the content.
  content_as_file
This property returns a *copy* of this element's content data wrapped in a file-like object.
  content_data
This property holds the data of the content.
  content_is_textual
This property indicates if this element's content data can be handled as text.
  content_mimetype
The mimetype of this element's content.
  content_model
The resource used as the model of the content of this element.
  content_model_id
The id-ref of the content model, or an empty string.
  content_parsed
  content_url
This property holds the URL of the content, or an empty string.
Method Details [hide private]

_instantiate_content(self, mimetype, model, url)

source code 

This is method is for optimization only: it is not strictly required (though recommended) to call it at instantiate time (see class docstring).

No integrity constraint is checked: the backend is assumed to be sane.

_check_content(self, mimetype=None, model_id=None, url=None)

source code 

Check that the provided values (assumed to be the future values of the corresponding attributes) are valid and consistent (with each other *and* with unmodified attributes).

NB: we do *not* check that model_id identifies a resource. Use _check_reference for that.

get_content_model(self, default=None)

source code 

Return the resource used as the model of the content of this element.

Return None if that content has no model. If the model can not be retrieved, the default value is returned.

See also `content_model` and `content_model_id`.

get_content_as_synced_file(self)

source code 

Return a file-like object giving access to the content data.

The file-like object is updatable unless the content is external.

It is an error to try to modify the data while such a file-like object is opened. An exception will thus be raised whenever this method is invoked or `content_data` is set before a previously returned file-like object is closed.

See also `content_data`.

_get_content_mimetype(self)

source code 

The mimetype of this element's content.

If set to "x-advene/none", the other properties are erased and become unsettable. Note that it is only possible for relations.

Decorators:
  • @autoproperty

_set_content_mimetype(self, mimetype)

source code 
Decorators:
  • @autoproperty

_get_content_is_textual(self)

source code 

This property indicates if this element's content data can be handled as text.

It uses the mimetypes registered with `advene.model.content.register.register_textual_mimetype`.

Decorators:
  • @autoproperty

_get_content_model(self)

source code 

The resource used as the model of the content of this element.

None if that content has no model. If the model can not be retrieved, an exception is raised.

See also `get_content_model` and `content_model_id`.

Decorators:
  • @autoproperty

_set_content_model(self, resource)

source code 

FIXME: missing docstring.

Decorators:
  • @autoproperty

_get_content_model_id(self)

source code 

The id-ref of the content model, or an empty string.

This is a read-only property giving the id-ref of the resource held by `content_model`, or an empty string if there is no model.

Note that this property is accessible even if the corresponding model is unreachable.

See also `get_content_model` and `content_model`.

Decorators:
  • @autoproperty

_get_content_url(self)

source code 

This property holds the URL of the content, or an empty string.

Its value determines whether the content is backend-stored, external or packaged.

Note that setting a standard URL (i.e. not in the ``packaged:`` scheme) to a backend-stored or packaged URL will discard its data. On the other hand, changing from backend-store to packaged and vice-versa keeps the data.

Finally, note that setting the URL to one in the ``packaged:`` model will automatically create a temporary directory and set the PACKAGED_ROOT metadata of the package to that directory.

Decorators:
  • @autoproperty

_set_content_url(self, url)

source code 

See `_get_content_url`.

Decorators:
  • @autoproperty

_get_content_data(self)

source code 

This property holds the data of the content.

It can be read whatever the kind of content (backend-stored, external or packaged). However, only backend-stored and packaged can have their data safely modified. Trying to set the data of an external content will raise a `ValueError`. Its `content_url` must first be set to the empty string or a ``packaged:`` URL.

See also `get_content_as_synced_file`.

Decorators:
  • @autoproperty

_set_content_data(self, data)

source code 

See `_get_content_data`.

Decorators:
  • @autoproperty

_get_content_parsed(self)

source code 
Decorators:
  • @autoproperty

_set_content_parsed(self, parsed)

source code 
Decorators:
  • @autoproperty

_get_content_as_file(self)

source code 

This property returns a *copy* of this element's content data wrapped in a file-like object.

Note that the returned file-like object may be writable, but the written data will *not* be reflected back to the content. Also, if the content data is modified between the moment where this method is called and the moment the file-like object is actually read, thoes changes will not be included in the read data.

For a synchronized file-like object, see `get_content_as_synced_file`.

Decorators:
  • @autoproperty

_get_content(self)

source code 

Return a `Content` instance representing the content.

Decorators:
  • @autoproperty

Property Details [hide private]

content

Return a `Content` instance representing the content.

Get Method:
_get_content(self) - Return a `Content` instance representing the content.

content_as_file

This property returns a *copy* of this element's content data wrapped in a file-like object.

Note that the returned file-like object may be writable, but the written data will *not* be reflected back to the content. Also, if the content data is modified between the moment where this method is called and the moment the file-like object is actually read, thoes changes will not be included in the read data.

For a synchronized file-like object, see `get_content_as_synced_file`.

Get Method:
_get_content_as_file(self) - This property returns a *copy* of this element's content data wrapped in a file-like object.

content_data

This property holds the data of the content.

It can be read whatever the kind of content (backend-stored, external or packaged). However, only backend-stored and packaged can have their data safely modified. Trying to set the data of an external content will raise a `ValueError`. Its `content_url` must first be set to the empty string or a ``packaged:`` URL.

See also `get_content_as_synced_file`.

Get Method:
_get_content_data(self) - This property holds the data of the content.
Set Method:
_set_content_data(self, data) - See `_get_content_data`.

content_is_textual

This property indicates if this element's content data can be handled as text.

It uses the mimetypes registered with `advene.model.content.register.register_textual_mimetype`.

Get Method:
_get_content_is_textual(self) - This property indicates if this element's content data can be handled as text.

content_mimetype

The mimetype of this element's content.

If set to "x-advene/none", the other properties are erased and become unsettable. Note that it is only possible for relations.

Get Method:
_get_content_mimetype(self) - The mimetype of this element's content.
Set Method:
_set_content_mimetype(self, mimetype)

content_model

The resource used as the model of the content of this element.

None if that content has no model. If the model can not be retrieved, an exception is raised.

See also `get_content_model` and `content_model_id`.

Get Method:
_get_content_model(self) - The resource used as the model of the content of this element.
Set Method:
_set_content_model(self, resource) - FIXME: missing docstring.

content_model_id

The id-ref of the content model, or an empty string.

This is a read-only property giving the id-ref of the resource held by `content_model`, or an empty string if there is no model.

Note that this property is accessible even if the corresponding model is unreachable.

See also `get_content_model` and `content_model`.

Get Method:
_get_content_model_id(self) - The id-ref of the content model, or an empty string.

content_parsed

Get Method:
_get_content_parsed(self)
Set Method:
_set_content_parsed(self, parsed)

content_url

This property holds the URL of the content, or an empty string.

Its value determines whether the content is backend-stored, external or packaged.

Note that setting a standard URL (i.e. not in the ``packaged:`` scheme) to a backend-stored or packaged URL will discard its data. On the other hand, changing from backend-store to packaged and vice-versa keeps the data.

Finally, note that setting the URL to one in the ``packaged:`` model will automatically create a temporary directory and set the PACKAGED_ROOT metadata of the package to that directory.

Get Method:
_get_content_url(self) - This property holds the URL of the content, or an empty string.
Set Method:
_set_content_url(self, url) - See `_get_content_url`.