Codec handling

We can create codecs programatically. Codecs are registered as utilities for ICodec with the name of their python codec.

>>> from zope import component
>>> from zope.mimetype.interfaces import ICodec
>>> from zope.mimetype.codec import addCodec
>>> sorted(component.getUtilitiesFor(ICodec))
>>> addCodec('iso8859-1', 'Western (ISO-8859-1)')
>>> codec = component.getUtility(ICodec, name='iso8859-1')
>>> codec
<zope.mimetype.codec.Codec ...>
>>> addCodec('utf-8', 'Unicode (UTF-8)')
>>> codec2 = component.getUtility(ICodec, name='utf-8')

We can programmatically add charsets to a given codec. This registers each charset as a named utility for ICharset. It also registers the codec as a utility for ICharsetCodec with the name of the charset.

>>> from zope.mimetype.codec import addCharset
>>> from zope.mimetype.interfaces import ICharset, ICharsetCodec
>>> sorted(component.getUtilitiesFor(ICharset))
>>> sorted(component.getUtilitiesFor(ICharsetCodec))
>>> addCharset(, 'latin1')
>>> charset = component.getUtility(ICharset, name='latin1')
>>> charset
<zope.mimetype.codec.Charset ...>
>>> component.getUtility(ICharsetCodec, name='latin1') is codec

When adding a charset we can state that we want that charset to be the preferred charset for its codec.

>>> addCharset(, 'iso8859-1', preferred=True)
>>> addCharset(, 'utf-8', preferred=True)

A codec can have at most one preferred charset.

>>> addCharset(, 'test', preferred=True)
Traceback (most recent call last):
ValueError: Codec already has a preferred charset.

Preferred charsets are registered as utilities for ICodecPreferredCharset under the name of the python codec.

>>> from zope.mimetype.interfaces import ICodecPreferredCharset
>>> preferred = component.getUtility(ICodecPreferredCharset, name='iso8859-1')
>>> preferred
<zope.mimetype.codec.Charset ...>
>>> sorted(component.getUtilitiesFor(ICodecPreferredCharset))
[('iso8859-1', <zope.mimetype.codec.Charset ...>),
 ('utf-8', <zope.mimetype.codec.Charset ...>)]

We can look up a codec by the name of its charset:

>>> component.getUtility(ICharsetCodec, name='latin1') is codec
>>> component.getUtility(ICharsetCodec, name='utf-8') is codec2

Or we can look up all codecs:

>>> sorted(component.getUtilitiesFor(ICharsetCodec))
[('iso8859-1', <zope.mimetype.codec.Codec ...>),
 ('latin1', <zope.mimetype.codec.Codec ...>),
 ('test', <zope.mimetype.codec.Codec ...>),
 ('utf-8', <zope.mimetype.codec.Codec ...>)]