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 |
Handle an encountered item for which no namespace-specific handler method was defined. |
| Method | handle |
Process the header. |
| Method | handle |
Handle an encountered item. |
| Method | handle_ |
Handle an item in the M namespace. |
| Method | handle |
Handle a redirect in the input ZIM file. |
| Method | handle_ |
Handle an item in the X namespace. |
| Method | open |
Open the input ZIM file and return it. |
| Method | open |
Open the output ZIM file and return it. |
| Method | translate |
Run the translation process. |
| Instance Variable | zim |
the input ZIM during the translation process |
| Instance Variable | zim |
the output ZIM during the translation process |
Called when the translation process is finished.
By default, this method flushes the output, then closes the input and output ZIM files.
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.Item | item to process |
| Returns | |
None or (iterable of) pyzim.item.Item | item(s) to add to output ZIM or None |
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.
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.Item | item to process |
| Returns | |
None or (iterable of) pyzim.item.Item | item(s) to add to output ZIM or None |
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.
Handle a redirect in the input ZIM file.
The default implementation adds the redirect to the output ZIM file.
| Parameters | |
entry:pyzim.entry.RedirectEntry | redirect entry to handle |
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.Item | item to process |
| Returns | |
None or (iterable of) pyzim.item.Item | item(s) to add to output ZIM or None |
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.Zim | the ZIM file opened for input |
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.Zim | the ZIM file opened for output |