class documentation

Baseclass for entries in a ZIM file.

An entry stores informations about an article/file in the ZIM file.

Class Method from_file Read an entry from a file.
Method __init__ The default constructor.
Method after_flush_or_read This method should be called after this object has been read and/or flushed to disk. In other words, it should be called at least once whenever this object matches the state of the object on the disk.
Method flush Write this entry to the archive if it is dirty.
Method full_url.setter The full URL, composed of namespace and url.
Method get_disk_size Calculate the size of this object when written to a file.
Method is_article.setter Whether this entry is an article or not.
Method mimetype.setter The mimetype as a string. Only available when bound.
Method mimetype_id.setter The index of the mimetype in the mimetypelist.
Method namespace.setter The namespace this entry is part of.
Method parameters.setter The extra parameters of this entry.
Method remove Remove this entry from the archive it is bound to.
Method resolve If this entry is a redirect, follow this redirect and any subsequent redirects until a non-redirect is reached and return it. Otherwise, return this entry.
Method revision.setter The revision of the content of this entry. Unused, must be 0..
Method title.setter The title of this entry.
Method to_bytes Serialize this entry into a bytestring and return it.
Method url.setter The URL of this entry.
Constant FORMAT_MT the struct format to parse the mimetype
Constant LENGTH_MT length of the mimetype part of the struct
Constant MIMETYPE_ID_REDIRECT mimetype index that identifies redirects
Instance Variable mimetype_id The index of the mimetype in the mimetypelist.
Instance Variable namespace The namespace this entry is part of.
Instance Variable url The URL of this entry.
Property full_url The full URL, composed of namespace and url.
Property is_article Whether this entry is an article or not.
Property is_redirect True if this entry is a redirect, False otherwise.
Property mimetype The mimetype as string. Only available when bound.
Property parameters The extra parameters of this entry.
Property revision The revision of the content of this entry. Unused, must be 0.
Property title The title of this entry.
Property unmodified_full_url The full URL of this entry of the time this entry was last read or flushed.
Method _get_mimetype_size Return the size of the mimetype on disk in bytes.
Instance Variable _force_is_article force-set the article state of this entry
Instance Variable _mimetype_id index of mimetype in the mimetypelist.
Instance Variable _namespace namespace this entry is part of
Instance Variable _old_full_url full URL of entry as of last read/flush
Instance Variable _parameters extra parameters. Unused, must be None.
Instance Variable _revision the revision of the content of this entry. Unused, must be 0.
Instance Variable _title title of this entry
Instance Variable _url url of this entry

Inherited from BindableMixIn:

Method bind Bind this object to a ZIM file.
Method unbind Unbind this object. Can be called multiple times.
Property bound Whether this object is bound to a ZIM file or not.
Property zim The bound ZIM archive, if any is bound. Otherwise None.
Instance Variable _zim the bound ZIM archive or None

Inherited from ModifiableMixIn (via BindableMixIn):

Method add_submodifiable Add another modifiable object as a child of this one.
Method dirty.setter Setter for ModifiableMixIn.dirty
Method ensure_mutable If this object is non-mutable, raise an Exception.
Method get_initial_disk_size Return the size of this object on disk as it has been read.
Method get_unmodified_disk_size Return the size of this object when written to a file before any modifications has been made since the last read/flush.
Method mark_dirty Convenience function to mark this object as dirty.
Method remove_submodifiable Remove a submodifiable from this object.
Instance Variable dirty True if this object or a sub-modifiable has been modified.
Instance Variable mutable if not nonzero, prevent modifications of this object.
Instance Variable _dirty a boolean flag that's nonzero if this object has been modified
Instance Variable _old_disk_size the size of this object on disk before any modifications since the last flush/read
Instance Variable _submodifiables a list of child objects, whose dirty state will affect this objects dirty state.
def from_file(cls, f, seek=None):

Read an entry from a file.

Parameters
f:file-likefile-like object to read from
seek:int or Noneif specified, seek this position of the file before reading
Returns
pyzim.entry.BaseEntry (or subclass thereof)the entry in the correct subclass
def __init__(self, mimetype_id, namespace, revision, url, title, parameters):

The default constructor.

Parameters
mimetype_id:intindex of mimetype in the mimetypelist.
namespace:strnamespace this entry is part of
revision:intthe revision of the content of this entry. Unused, must be 0.
url:strurl of this entry
title:str or Nonetitle of this entry
parameters:listextra parameters. Unused, must be empty.
def after_flush_or_read(self):

This method should be called after this object has been read and/or flushed to disk. In other words, it should be called at least once whenever this object matches the state of the object on the disk.

This method sets the old disk size, which allows us to late free the allocated space of the old object on disk. Thus, this method requires ModifiableMixIn.get_disk_size to work.

In addition, the object will be marked as non-dirty afterwards.

def flush(self):

Write this entry to the archive if it is dirty.

Raises
pyzim.exceptions.NonMutableif this entry is set to be immutable
pyzim.exceptions.BindRequiredwhen not bound
def full_url(self, value):

The full URL, composed of namespace and url.

Parameters
value:str or bytesvalue to set
Raises
TypeErroron invalid type
ValueErroron invalid value (e.g. empty) @raises pyzim.exceptions.NonMutable: if this entry is set to be immutable.
def get_disk_size(self):

Calculate the size of this object when written to a file.

NOTE: in this context, size refers to the direct size of the object. If this object contains references to other objects, their sizes will not be included. For example, a pyzim.entry.ContentEntry also links to a blob, but this function will only return the size of the entry itself, excluding the referenced blob.

Returns
intthe size, in bytes
def is_article(self, value):

Whether this entry is an article or not.

Parameters
value:boolvalue to set
Raises
TypeErroron invalid type
ValueErrorif value is incompatible with namespace
pyzim.exceptions.NonMutableif this entry is set to be immutable
def mimetype(self, value):

The mimetype as a string. Only available when bound.

Parameters
value:str or bytesvalue to set
Raises
TypeErroron invalid type
ValueErrorif value is invalid (e.g. empty)
pyzim.exceptions.NonMutableif this entry is set to be immutable
pyzim.exceptions.BindRequiredwhen not bound
def mimetype_id(self, value):

The index of the mimetype in the mimetypelist.

Parameters
value:intvalue to set
Raises
TypeErrorif value is not an integer
pyzim.exceptions.NonMutableif this entry is set to be inmutable.
ValueErrorif the value is invalid (e.g. negative)
def namespace(self, value):

The namespace this entry is part of.

If the namespace is not "C", BaseEntry.is_article will be set to False.

Parameters
value:str or bytesvalue to set
Raises
TypeErrorif value is not a string or bytes.
pyzim.exceptions.NonMutableif this entry is set to be inmutable.
ValueErrorif the value is invalid (e.g. length != 1)
def parameters(self, value):

The extra parameters of this entry.

Parameters
value:listvalue to set
Raises
TypeErrorif value is not the expected type.
pyzim.exceptions.NonMutableif this entry is set to be inmutable.
ValueErrorif the value is invalid.
def remove(self, blob='empty'):

Remove this entry from the archive it is bound to.

Parameters
blob:strpassed to pyzim.archive.Zim.remove_entry_by_full_url
Raises
TypeErroron invalid type
ValueErroron invalid value
pyzim.exceptions.BindRequiredwhen not bound
pyzim.exceptions.NonMutableif this entry is set to be inmutable.
def resolve(self):

If this entry is a redirect, follow this redirect and any subsequent redirects until a non-redirect is reached and return it. Otherwise, return this entry.

This requires this entry to be bound.

Returns
pyzim.entry.BaseEntrythe first non-redirect entry
Raises
pyzim.exceptions.BindRequiredif not bound
def revision(self, value):

The revision of the content of this entry. Unused, must be 0..

Parameters
value:intvalue to set
Raises
TypeErrorif value is not an integer
pyzim.exceptions.NonMutableif this entry is set to be inmutable.
ValueErrorif the value is invalid (currently, != 0 due to not being implemented by the standard)
def title(self, value):

The title of this entry.

Parameters
value:str or bytes or Nonevalue to set
Raises
TypeErrorif value is not a string nor bytes nor None.
pyzim.exceptions.NonMutableif this entry is set to be inmutable.
ValueErrorif the value is invalid (e.g. contains null bytes)
def to_bytes(self):

Serialize this entry into a bytestring and return it.

Returns
bytesa bytestring representing the content of this entry.
def url(self, value):

The URL of this entry.

Parameters
value:str or bytesvalue to set
Raises
TypeErrorif value is not a string nor bytes.
pyzim.exceptions.NonMutableif this entry is set to be inmutable.
ValueErrorif the value is invalid (e.g. contains null bytes)
FORMAT_MT: str =

the struct format to parse the mimetype

Value
constants.ENDIAN + 'H'
LENGTH_MT: int =

length of the mimetype part of the struct

Value
struct.calcsize(FORMAT_MT)
MIMETYPE_ID_REDIRECT: int =

mimetype index that identifies redirects

Value
65535
mimetype_id: int =

The index of the mimetype in the mimetypelist.

namespace: str =

The namespace this entry is part of.

url: str =

The URL of this entry.

full_url: str =

The full URL, composed of namespace and url.

is_article: bool =

Whether this entry is an article or not.

Raises
pyzim.exceptions.BindRequiredwhen not bound
is_redirect: bool =

True if this entry is a redirect, False otherwise.

mimetype: str =

The mimetype as string. Only available when bound.

Raises
pyzim.exceptions.BindRequiredwhen not bound
parameters: list =

The extra parameters of this entry.

Currently, this must be 0.

revision: int =

The revision of the content of this entry. Unused, must be 0.

title: str =

The title of this entry.

If the title is empty, the URL will be used instead.

unmodified_full_url: str =

The full URL of this entry of the time this entry was last read or flushed.

def _get_mimetype_size(self):

Return the size of the mimetype on disk in bytes.

This is a helper method that may be called by subclasses to help their implementation of BaseEntry.get_disk_size-

Returns
intthe size of the mimetype on disk, in bytes
_force_is_article: bool or None =

force-set the article state of this entry

_mimetype_id: int =

index of mimetype in the mimetypelist.

_namespace: str =

namespace this entry is part of

_old_full_url: str =

full URL of entry as of last read/flush

_parameters: None =

extra parameters. Unused, must be None.

_revision: int =

the revision of the content of this entry. Unused, must be 0.

_title: str =

title of this entry

_url: str =

url of this entry