class documentation

class ZimTranslator(object):

Constructor: ZimTranslator()

View In Hierarchy

The ZimTranslator provides functionality to "translate" one ZIM file into another.

"Translate" basically means that you can apply changes to the ZIM. For example, you could read a ZIM, convert all titles to upper case, and write them to the output ZIM.

Method __init__ The default constructor.
Method finalize Called when the translation process is finished.
Method handle_default Handle an encountered item for which no namespace-specific handler method was defined.
Method handle_header Process the header.
Method handle_item Handle an encountered item.
Method handle_M Handle an item in the M namespace.
Method handle_redirect Handle a redirect in the input ZIM file.
Method handle_X Handle an item in the X namespace.
Method open_in Open the input ZIM file and return it.
Method open_out Open the output ZIM file and return it.
Method translate Run the translation process.
Instance Variable zim_in the input ZIM during the translation process
Instance Variable zim_out the output ZIM during the translation process
def __init__(self):

The default constructor.

def finalize(self):

Called when the translation process is finished.

By default, this method flushes the output, then closes the input and output ZIM files.

def handle_default(self, item):

Handle an encountered item for which no namespace-specific handler method was defined.

By default this will return the original item, adding it to the output ZIM.

Parameters
item:pyzim.item.Itemitem to process
Returns
None or (iterable of) pyzim.item.Itemitem(s) to add to output ZIM or None
def handle_header(self):

Process the header.

The ZIM input and output files are available as attributes. This method will be called before the content translation process starts. If you want to make changes to the header later on, you can do so both in the various handle_ methods as well as the ZimTranslator.finalize method.

You can also do other tasks during this method, like setting the mainpage.

By default this method will copy the mainpage URL.

def handle_item(self, item):

Handle an encountered item.

If this method returns a value other than None, it is presumed to either be an item to add or an iterable of items to add to the output ZIM.

By default, this method will look for a method called handle_<namespace> and call it with the item, returning the return value. If no such method exists, ZimTranslator.handle_default will be called.

Parameters
item:pyzim.item.Itemitem to process
Returns
None or (iterable of) pyzim.item.Itemitem(s) to add to output ZIM or None
def handle_M(self, item):

Handle an item in the M namespace.

The M namespace contains metadata, including MCounter. As pyzim generates some entries (like MCounter), adding these ZIMs to the output ZIM may result in suboptimal or even wrong behavior. Thus, the default method only adds items the current pyzim implementation does not create on its own. The exact behavior is expected to change, so you should implement your own method if you want consistent behavior here.

def handle_redirect(self, entry):

Handle a redirect in the input ZIM file.

The default implementation adds the redirect to the output ZIM file.

Parameters
entry:pyzim.entry.RedirectEntryredirect entry to handle
def handle_X(self, item):

Handle an item in the X namespace.

The X namespace contains search indexes. As pyzim generates some (e.g. title lists), adding these items to the output ZIM may result in suboptimal or even wrong behavior. Thus, the default method only adds items the current pyzim implementation does not create on its own. The exact behavior is expected to change, so you should implement your own method if you want consistent behavior here.

Parameters
item:pyzim.item.Itemitem to process
Returns
None or (iterable of) pyzim.item.Itemitem(s) to add to output ZIM or None
def open_in(self):

Open the input ZIM file and return it.

This method needs to be overwritten in subclasses. It is recommended that the ZIM file uses some cluster caching and a caching cluster (e.g. pyzim.cluster.OffsetRememberingCluster.

Returns
pyzim.archive.Zimthe ZIM file opened for input
def open_out(self):

Open the output ZIM file and return it.

This method is guaranteed to be called only after ZimTranslator.open_in has been called.

This method needs to be overwritten in subclasses.

Returns
pyzim.archive.Zimthe ZIM file opened for output
def translate(self):

Run the translation process.

the input ZIM during the translation process

the output ZIM during the translation process