Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes
MoinMoin.script.migration.wikiutil160a.MimeType Class Reference

List of all members.

Public Member Functions

def __init__
def parse_filename
def parse_mimetype
def parse_format
def sanitize
def spoil
def content_type
def mime_type
def module_name

Public Attributes

 major
 minor
 params
 charset
 raw_mimestr

Detailed Description

represents a mimetype like text/plain 

Definition at line 856 of file wikiutil160a.py.


Constructor & Destructor Documentation

def MoinMoin.script.migration.wikiutil160a.MimeType.__init__ (   self,
  mimestr = None,
  filename = None 
)

Definition at line 859 of file wikiutil160a.py.

00859 
00860     def __init__(self, mimestr=None, filename=None):
00861         self.major = self.minor = None # sanitized mime type and subtype
00862         self.params = {} # parameters like "charset" or others
00863         self.charset = None # this stays None until we know for sure!
00864         self.raw_mimestr = mimestr
00865 
00866         if mimestr:
00867             self.parse_mimetype(mimestr)
00868         elif filename:
00869             self.parse_filename(filename)


Member Function Documentation

def MoinMoin.script.migration.wikiutil160a.MimeType.content_type (   self,
  major = None,
  minor = None,
  charset = None,
  params = None 
)
return a string suitable for Content-Type header

Definition at line 936 of file wikiutil160a.py.

00936 
00937     def content_type(self, major=None, minor=None, charset=None, params=None):
00938         """ return a string suitable for Content-Type header
00939         """
00940         major = major or self.major
00941         minor = minor or self.minor
00942         params = params or self.params or {}
00943         if major == 'text':
00944             charset = charset or self.charset or params.get('charset', config.charset)
00945             params['charset'] = charset
00946         mimestr = "%s/%s" % (major, minor)
00947         params = ['%s="%s"' % (key.lower(), value) for key, value in params.items()]
00948         params.insert(0, mimestr)
00949         return "; ".join(params)

Here is the caller graph for this function:

return a string major/minor only, no params 

Definition at line 950 of file wikiutil160a.py.

00950 
00951     def mime_type(self):
00952         """ return a string major/minor only, no params """
00953         return "%s/%s" % (self.major, self.minor)

Here is the caller graph for this function:

convert this mimetype to a string useable as python module name,
    we yield the exact module name first and then proceed to shorter
    module names (useful for falling back to them, if the more special
    module is not found) - e.g. first "text_python", next "text".
    Finally, we yield "application_octet_stream" as the most general
    mimetype we have.
    Hint: the fallback handler module for text/* should be implemented
  in module "text" (not "text_plain")

Definition at line 954 of file wikiutil160a.py.

00954 
00955     def module_name(self):
00956         """ convert this mimetype to a string useable as python module name,
00957             we yield the exact module name first and then proceed to shorter
00958             module names (useful for falling back to them, if the more special
00959             module is not found) - e.g. first "text_python", next "text".
00960             Finally, we yield "application_octet_stream" as the most general
00961             mimetype we have.
00962             Hint: the fallback handler module for text/* should be implemented
00963                   in module "text" (not "text_plain")
00964         """
00965         mimetype = self.mime_type()
00966         modname = mimetype.replace("/", "_").replace("-", "_").replace(".", "_")
00967         fragments = modname.split('_')
00968         for length in range(len(fragments), 1, -1):
00969             yield "_".join(fragments[:length])
00970         yield self.raw_mimestr
00971         yield fragments[0]
00972         yield "application_octet_stream"
00973 

Here is the call graph for this function:

Definition at line 870 of file wikiutil160a.py.

00870 
00871     def parse_filename(self, filename):
00872         mtype, encoding = mimetypes.guess_type(filename)
00873         if mtype is None:
00874             mtype = 'application/octet-stream'
00875         self.parse_mimetype(mtype)

Here is the call graph for this function:

maps from what we currently use on-page in a #format xxx processing
    instruction to a sanitized mimetype major, minor tuple.
    can also be user later for easier entry by the user, so he can just
    type "wiki" instead of "text/moin-wiki".

Definition at line 899 of file wikiutil160a.py.

00899 
00900     def parse_format(self, format):
00901         """ maps from what we currently use on-page in a #format xxx processing
00902             instruction to a sanitized mimetype major, minor tuple.
00903             can also be user later for easier entry by the user, so he can just
00904             type "wiki" instead of "text/moin-wiki".
00905         """
00906         format = format.lower()
00907         if format in ('plain', 'csv', 'rst', 'docbook', 'latex', 'tex', 'html', 'css',
00908                       'xml', 'python', 'perl', 'php', 'ruby', 'javascript',
00909                       'cplusplus', 'java', 'pascal', 'diff', 'gettext', 'xslt', ):
00910             mimetype = 'text', format
00911         else:
00912             mapping = {
00913                 'wiki': ('text', 'moin-wiki'),
00914                 'irc': ('text', 'irssi'),
00915             }
00916             try:
00917                 mimetype = mapping[format]
00918             except KeyError:
00919                 mimetype = 'text', 'x-%s' % format
00920         return mimetype

Here is the caller graph for this function:

take a string like used in content-type and parse it into components,
    alternatively it also can process some abbreviated string like "wiki"

Definition at line 876 of file wikiutil160a.py.

00876 
00877     def parse_mimetype(self, mimestr):
00878         """ take a string like used in content-type and parse it into components,
00879             alternatively it also can process some abbreviated string like "wiki"
00880         """
00881         parameters = mimestr.split(";")
00882         parameters = [p.strip() for p in parameters]
00883         mimetype, parameters = parameters[0], parameters[1:]
00884         mimetype = mimetype.split('/')
00885         if len(mimetype) >= 2:
00886             major, minor = mimetype[:2] # we just ignore more than 2 parts
00887         else:
00888             major, minor = self.parse_format(mimetype[0])
00889         self.major = major.lower()
00890         self.minor = minor.lower()
00891         for param in parameters:
00892             key, value = param.split('=')
00893             if value[0] == '"' and value[-1] == '"': # remove quotes
00894                 value = value[1:-1]
00895             self.params[key.lower()] = value
00896         if self.params.has_key('charset'):
00897             self.charset = self.params['charset'].lower()
00898         self.sanitize()

Here is the call graph for this function:

Here is the caller graph for this function:

convert to some representation that makes sense - this is not necessarily
    conformant to /etc/mime.types or IANA listing, but if something is
    readable text, we will return some text/* mimetype, not application/*,
    because we need text/plain as fallback and not application/octet-stream.

Definition at line 921 of file wikiutil160a.py.

00921 
00922     def sanitize(self):
00923         """ convert to some representation that makes sense - this is not necessarily
00924             conformant to /etc/mime.types or IANA listing, but if something is
00925             readable text, we will return some text/* mimetype, not application/*,
00926             because we need text/plain as fallback and not application/octet-stream.
00927         """
00928         self.major, self.minor = MIMETYPES_sanitize_mapping.get((self.major, self.minor), (self.major, self.minor))

Here is the caller graph for this function:

this returns something conformant to /etc/mime.type or IANA as a string,
    kind of inverse operation of sanitize(), but doesn't change self

Definition at line 929 of file wikiutil160a.py.

00929 
00930     def spoil(self):
00931         """ this returns something conformant to /etc/mime.type or IANA as a string,
00932             kind of inverse operation of sanitize(), but doesn't change self
00933         """
00934         major, minor = MIMETYPES_spoil_mapping.get((self.major, self.minor), (self.major, self.minor))
00935         return self.content_type(major, minor)

Here is the call graph for this function:


Member Data Documentation

Definition at line 862 of file wikiutil160a.py.

Definition at line 860 of file wikiutil160a.py.

Definition at line 860 of file wikiutil160a.py.

Definition at line 861 of file wikiutil160a.py.

Definition at line 863 of file wikiutil160a.py.


The documentation for this class was generated from the following file: