"""MIMEType helpersThese helpers depend on the WebOb package."""importmimetypesclassMIMETypes(object):"""MIMETypes registration mapping The MIMETypes object class provides a single point to hold onto all the registered mimetypes, and their association extensions. It's used by the mimetypes function to determine the appropriate content type to return to a client. """aliases={}definit(cls):"""Loads a default mapping of extensions and mimetypes These are suitable for most web applications by default. Additional types can be added with the using the mimetypes module. """mimetypes.init()init=classmethod(init)defadd_alias(cls,alias,mimetype):"""Creates a MIMEType alias to a full mimetype These aliases may not include /'s. Examples include html->text/html, xml->application/xml."""if'/'inalias:raiseValueError("MIMEType aliases may not contain '/'")cls.aliases[alias]=mimetypeadd_alias=classmethod(add_alias)def__init__(self,environ):self.env=environdef_set_responce_conetent_type(self,mimetype):if'pylons.pylons'inself.env:self.env['pylons.pylons'].response.content_type=mimetypereturnmimetypedefmimetype(self,content_type):"""Check the PATH_INFO of the current request and clients HTTP Accept to attempt to use the appropriate mime-type If a content-type is matched, the appropriate response content type is set as well. This works best with URLs that end in extensions that differentiate content-type. Examples: http://example.com/example, http://example.com/example.xml, http://example.com/example.csv Since browsers generally allow for any content-type, but should be sent HTML when possible, the html mimetype check should always come first, as shown in the example below. Example:: # some code likely in environment.py MIMETypes.init() MIMETypes.alias('html', 'text/html') MIMETypes.alias('xml', 'application/xml') MIMETypes.alias('csv', 'text/csv') # code in a controller def somaction(self): # prepare a bunch of data # ...... # prepare MIMETypes object m = MIMETypes(request.environ) if m.mimetype('html'): return render('/some/template.html') elif m.mimetype('atom'): return render('/some/xml_template.xml') elif m.mimetype('csv'): # write the data to a csv file return csvfile else: abort(404) """importwebobifcontent_typeinMIMETypes.aliases:content_type=MIMETypes.aliases[content_type]path=self.env['PATH_INFO']guess_from_url=mimetypes.guess_type(path)[0]possible_from_accept_header=Nonehas_extension=Falseiflen(path.split('/'))>1:last_part=path.split('/')[-1]if'.'inlast_part:has_extension=Trueif'HTTP_ACCEPT'inself.env:possible_from_accept_header=webob.acceptparse.MIMEAccept('ACCEPT',self.env['HTTP_ACCEPT'])ifhas_extension==False:ifpossible_from_accept_headerisNone:returnself._set_responce_conetent_type(content_type)elifcontent_typeinpossible_from_accept_header:returnself._set_responce_conetent_type(content_type)else:returnFalseifcontent_type==guess_from_url:# Guessed same mimetypereturnself._set_responce_conetent_type(content_type)elifguess_from_urlisNoneandcontent_typeinpossible_from_accept_header:returnself._set_responce_conetent_type(content_type)else:returnFalse