Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes
MoinMoin.wikiutil.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 886 of file wikiutil.py.


Constructor & Destructor Documentation

def MoinMoin.wikiutil.MimeType.__init__ (   self,
  mimestr = None,
  filename = None 
)

Definition at line 889 of file wikiutil.py.

00889 
00890     def __init__(self, mimestr=None, filename=None):
00891         self.major = self.minor = None # sanitized mime type and subtype
00892         self.params = {} # parameters like "charset" or others
00893         self.charset = None # this stays None until we know for sure!
00894         self.raw_mimestr = mimestr
00895 
00896         if mimestr:
00897             self.parse_mimetype(mimestr)
00898         elif filename:
00899             self.parse_filename(filename)


Member Function Documentation

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

Definition at line 964 of file wikiutil.py.

00964 
00965     def content_type(self, major=None, minor=None, charset=None, params=None):
00966         """ return a string suitable for Content-Type header
00967         """
00968         major = major or self.major
00969         minor = minor or self.minor
00970         params = params or self.params or {}
00971         if major == 'text':
00972             charset = charset or self.charset or params.get('charset', config.charset)
00973             params['charset'] = charset
00974         mimestr = "%s/%s" % (major, minor)
00975         params = ['%s="%s"' % (key.lower(), value) for key, value in params.items()]
00976         params.insert(0, mimestr)
00977         return "; ".join(params)

Here is the caller graph for this function:

return a string major/minor only, no params 

Definition at line 978 of file wikiutil.py.

00978 
00979     def mime_type(self):
00980         """ return a string major/minor only, no params """
00981         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 982 of file wikiutil.py.

00982 
00983     def module_name(self):
00984         """ convert this mimetype to a string useable as python module name,
00985             we yield the exact module name first and then proceed to shorter
00986             module names (useful for falling back to them, if the more special
00987             module is not found) - e.g. first "text_python", next "text".
00988             Finally, we yield "application_octet_stream" as the most general
00989             mimetype we have.
00990             Hint: the fallback handler module for text/* should be implemented
00991                   in module "text" (not "text_plain")
00992         """
00993         mimetype = self.mime_type()
00994         modname = mimetype.replace("/", "_").replace("-", "_").replace(".", "_")
00995         fragments = modname.split('_')
00996         for length in range(len(fragments), 1, -1):
00997             yield "_".join(fragments[:length])
00998         yield self.raw_mimestr
00999         yield fragments[0]
01000         yield "application_octet_stream"
01001 

Here is the call graph for this function:

def MoinMoin.wikiutil.MimeType.parse_filename (   self,
  filename 
)

Definition at line 900 of file wikiutil.py.

00900 
00901     def parse_filename(self, filename):
00902         mtype, encoding = mimetypes.guess_type(filename)
00903         if mtype is None:
00904             mtype = 'application/octet-stream'
00905         self.parse_mimetype(mtype)

Here is the call graph for this function:

def MoinMoin.wikiutil.MimeType.parse_format (   self,
  format 
)
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 929 of file wikiutil.py.

00929 
00930     def parse_format(self, format):
00931         """ maps from what we currently use on-page in a #format xxx processing
00932             instruction to a sanitized mimetype major, minor tuple.
00933             can also be user later for easier entry by the user, so he can just
00934             type "wiki" instead of "text/moin-wiki".
00935         """
00936         format = format.lower()
00937         if format in config.parser_text_mimetype:
00938             mimetype = 'text', format
00939         else:
00940             mapping = {
00941                 'wiki': ('text', 'moin-wiki'),
00942                 'irc': ('text', 'irssi'),
00943             }
00944             try:
00945                 mimetype = mapping[format]
00946             except KeyError:
00947                 mimetype = 'text', 'x-%s' % format
00948         return mimetype

Here is the caller graph for this function:

def MoinMoin.wikiutil.MimeType.parse_mimetype (   self,
  mimestr 
)
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 906 of file wikiutil.py.

00906 
00907     def parse_mimetype(self, mimestr):
00908         """ take a string like used in content-type and parse it into components,
00909             alternatively it also can process some abbreviated string like "wiki"
00910         """
00911         parameters = mimestr.split(";")
00912         parameters = [p.strip() for p in parameters]
00913         mimetype, parameters = parameters[0], parameters[1:]
00914         mimetype = mimetype.split('/')
00915         if len(mimetype) >= 2:
00916             major, minor = mimetype[:2] # we just ignore more than 2 parts
00917         else:
00918             major, minor = self.parse_format(mimetype[0])
00919         self.major = major.lower()
00920         self.minor = minor.lower()
00921         for param in parameters:
00922             key, value = param.split('=')
00923             if value[0] == '"' and value[-1] == '"': # remove quotes
00924                 value = value[1:-1]
00925             self.params[key.lower()] = value
00926         if 'charset' in self.params:
00927             self.charset = self.params['charset'].lower()
00928         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 949 of file wikiutil.py.

00949 
00950     def sanitize(self):
00951         """ convert to some representation that makes sense - this is not necessarily
00952             conformant to /etc/mime.types or IANA listing, but if something is
00953             readable text, we will return some text/* mimetype, not application/*,
00954             because we need text/plain as fallback and not application/octet-stream.
00955         """
00956         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 957 of file wikiutil.py.

00957 
00958     def spoil(self):
00959         """ this returns something conformant to /etc/mime.type or IANA as a string,
00960             kind of inverse operation of sanitize(), but doesn't change self
00961         """
00962         major, minor = MIMETYPES_spoil_mapping.get((self.major, self.minor), (self.major, self.minor))
00963         return self.content_type(major, minor)

Here is the call graph for this function:


Member Data Documentation

Definition at line 892 of file wikiutil.py.

Definition at line 890 of file wikiutil.py.

Definition at line 890 of file wikiutil.py.

Definition at line 891 of file wikiutil.py.

Definition at line 893 of file wikiutil.py.


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