Package trac :: Package mimeview :: Module api

Module api

source code

File metadata management.

The `trac.mimeview` package centralizes the intelligence related to
file metadata, principally concerning the `type` (MIME type) of the content
and, if relevant, concerning the text encoding (charset) used by the content.

There are primarily two approaches for getting the MIME type of a given file:
 * taking advantage of existing conventions for the file name
 * examining the file content and applying various heuristics

The module also knows how to convert the file content from one type
to another type.

In some cases, only the `url` pointing to the file's content is actually
needed, that's why we avoid to read the file's content when it's not needed.

The actual `content` to be converted might be a `unicode` object,
but it can also be the raw byte string (`str`) object, or simply
an object that can be `read()`.

----
NOTE: for plugin developers

  The Mimeview API is quite complex and many things there are currently
  a bit difficult to work with (e.g. what an actual `content` might be,
  see the last paragraph of this description).

  So this area is mainly in a ''work in progress'' state, which will
  be improved upon in the near future (see [trac:ticket:3332 #3332]).

  In particular, if you are interested in writing `IContentConverter`
  and `IHTMLPreviewRenderer` components, note that those interfaces
  will be merged into a new style `IContentConverter`.
  Feel free to contribute remarks and suggestions for improvements
  to the corresponding ticket ([trac:ticket:3332 #3332]).

Classes
  Context
Rendering context.
  Mimeview
Generic HTML renderer for data, typically source code.
Functions
 
get_mimetype(filename, content=None, mime_map={'AUTHORS': 'text/plain', 'C': 'text/x-c++src', 'C#': 'text/x-...)
Guess the most probable MIME type of a file with the given name.
source code
 
ct_mimetype(content_type)
Return the mimetype part of a content type.
source code
 
is_binary(data)
Detect binary content by checking the first thousand bytes for zeroes.
source code
 
detect_unicode(data)
Detect different unicode charsets by looking for BOMs (Byte Order Mark).
source code
 
content_to_unicode(env, content, mimetype)
Retrieve an unicode object from a content to be previewed
source code
Function Details

get_mimetype(filename, content=None, mime_map={'AUTHORS': 'text/plain', 'C': 'text/x-c++src', 'C#': 'text/x-...)

source code 

Guess the most probable MIME type of a file with the given name.

filename is either a filename (the lookup will then use the suffix) or some arbitrary keyword.

content is either a str or an unicode string.

is_binary(data)

source code 

Detect binary content by checking the first thousand bytes for zeroes.

Operate on either str or unicode strings.

detect_unicode(data)

source code 

Detect different unicode charsets by looking for BOMs (Byte Order Mark).

Operate obviously only on str objects.