Items

class pykindle.items.Item(*args, **kwargs)[source]

Bases: dict

Item represents an asset of mobi files. This is a base class, you should inherit from it.

Mobi file consists of several assets and a manifest. Each item renders to a asset, including articles, images and manifest files.

Item class inherited from python dict class. You can set attributes in two ways:

>>> item = Item()
>>> item['href'] = 'path/to/item'
>>> assert item.href == 'path/to/item'
>>> item.href = 'path/to/item/changed'
>>> assert item['href'] = 'path/to/item/changed'
content

Readonly field. It just return the value of self.reader.render(self.source).

href

Mobi file is just like a html website, and each asset has it’s relative url in the mobi file. Html assets get other html assets and image assets by relative url.

id

Id field should be a string! Not integer! Just like html elements has a “id” field, mobi assets also have such field. The difference is that in mobi assets, “id” field in required.

media_type

Every mobi asset has a field to indicates what it is, for example, application/xhtml+xml represents an article, image/jpg represents a photo in jpg format.

reader

Smetimes we need to convert some files to mobi supported assets. That’s why we use reader. Reader should be a instance of pykindle.reader.Reader class. Item will call self.reader.render(self.source) to generate rendered assets.

source

Source is the main content of the asset. It will later send to self.reader.render to get the rendered result.

write(directory)[source]

Write the rendered content to a file. File path is calculated by os.path.join(directory, self.href).

class pykindle.items.ArticleItem(*args, **kwargs)[source]

Bases: pykindle.items.Item

ArticleItem represents an real html article. This is the main body of your book.

This class is used for inherit.

Default attributes:
title = Untitled author = No Author description = No description category = default
author

The author of article.

category

Optional. If you generates a magazine book, this field is used to calculate the magazine category. Deprecated. We will create a Category class to do this. :return:

content

Readonly field. It just return the value of self.reader.render(self.source).

description

The description of article.

title

The title of article.

class pykindle.items.HtmlArticleItem(*args, **kwargs)[source]

Bases: pykindle.items.ArticleItem

HtmlArticleItem represents a html article. Although all the articles in mobi files are html, we have other ArticleItem like MarkdownArticleItem to convert other formats to html.

class pykindle.items.MarkdownArticleItem(*args, **kwargs)[source]

Bases: pykindle.items.ArticleItem

MarkdownArticleItem reads a markdown source.

class pykindle.items.ImageItem(image_format='jpeg', *args, **kwargs)[source]

Bases: pykindle.items.Item

ImageItem represents a image asset. It has a positional argument, image_format=’jpeg’, alternative formats are jpeg, png, gif

class pykindle.items.CoverImageItem(image_format='jpeg', *args, **kwargs)[source]

Bases: pykindle.items.ImageItem

CoverImageItem represents a cover image.

class pykindle.items.NcxItem(*args, **kwargs)[source]

Bases: pykindle.items.Item

NcxItem is used to generate ncx file. Ncx file is the category of a book. It will be automatically added to the book when book.create is called.

class pykindle.items.MagazineNcxItem(*args, **kwargs)[source]

Bases: pykindle.items.NcxItem

MagazineNcxItem represents a magazine toc. The way to implement magazine is very ugly, and we’ll reconstruct these classes.

content

Readonly field. It just return the value of self.reader.render(self.source).

exception pykindle.items.FieldNotSetError(field)[source]

Bases: Exception

When user get a field of an item that has not been set yet, raise.