本文整理匯總了Python中mimetypes.init方法的典型用法代碼示例。如果您正苦於以下問題:Python mimetypes.init方法的具體用法?Python mimetypes.init怎麽用?Python mimetypes.init使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mimetypes
的用法示例。
在下文中一共展示了mimetypes.init方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_non_latin_extension
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def test_non_latin_extension(self):
import _winreg
class MockWinreg(object):
def __getattr__(self, name):
if name == 'EnumKey':
return lambda key, i: _winreg.EnumKey(key, i) + "\xa3"
elif name == 'OpenKey':
return lambda key, name: _winreg.OpenKey(key, name.rstrip("\xa3"))
elif name == 'QueryValueEx':
return lambda subkey, label: (u'текст/простой' , _winreg.REG_SZ)
return getattr(_winreg, name)
mimetypes._winreg = MockWinreg()
try:
# this used to throw an exception if registry contained non-Latin
# characters in extensions (issue #9291)
mimetypes.init()
finally:
mimetypes._winreg = _winreg
示例2: test_non_latin_type
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def test_non_latin_type(self):
import _winreg
class MockWinreg(object):
def __getattr__(self, name):
if name == 'QueryValueEx':
return lambda subkey, label: (u'текст/простой', _winreg.REG_SZ)
return getattr(_winreg, name)
mimetypes._winreg = MockWinreg()
try:
# this used to throw an exception if registry contained non-Latin
# characters in content types (issue #9291)
mimetypes.init()
finally:
mimetypes._winreg = _winreg
示例3: test_registry_read_error
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def test_registry_read_error(self):
import _winreg
class MockWinreg(object):
def OpenKey(self, key, name):
if key != _winreg.HKEY_CLASSES_ROOT:
raise WindowsError(5, "Access is denied")
return _winreg.OpenKey(key, name)
def __getattr__(self, name):
return getattr(_winreg, name)
mimetypes._winreg = MockWinreg()
try:
mimetypes.init()
finally:
mimetypes._winreg = _winreg
示例4: fileDir
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def fileDir(dirInput, recursive):
if os.path.isdir(dirInput):
print "Scraping Files in Local Directory {0}- {1}".format('(recursively) ' if recursive else '', dirInput)
mimetypes.init()
fileTypes = ('*.conf', '*.cs', '*.js', '*.c', '*.cpp', '*.c++', '*.pdf', '*.docx', '*.txt', '*.csv', '*.py', '*.cpp', '*.pl', '*.log', '*.rtf', '*.css', '*.dat', '*.html', '*.php', '*.pps', '*.ppt', '*.pptx', '*.sh', '*.xml', '*.xsl')
if recursive:
for root, dirs, files in os.walk(dirInput):
for fileType in fileTypes:
for file_name in fnmatch.filter(files, fileType):
file_path = os.path.join(root, file_name)
file_type, file_encoding = mimetypes.guess_type(file_path)
localFile(file_path)
else:
for fileType in fileTypes:
for globFile in glob.glob(os.path.join(dirInput, fileType)):
file_type, file_encoding = mimetypes.guess_type(globFile)
localFile(globFile)
else:
print 'Error accessing directory: {0}'.format(dirInput)
示例5: distribute
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def distribute(self, filepath, allow_folders=True):
self._checkpath(filepath)
path = os.path.abspath(os.path.join(self.prefix, filepath))
if not os.path.exists(path):
return ("invalid", None, None)
if os.path.isdir(path):
if not allow_folders:
return ("invalid", None, None)
zipf = zipstream.ZipFile()
for root, _, files in os.walk(path):
for filename in files:
file_path = os.path.join(root, filename)
arcpath = os.path.relpath(file_path, path)
zipf.write(file_path, arcpath)
return ("local", "application/zip", zipf.__iter__()) #the __iter__ is only present to fix a bug in web.py for py3; it only recognizes
#iterable that possess a __next__. ZipFile.__iter__ returns an iterable in the web.py
#sense
elif os.path.isfile(path):
mimetypes.init()
mime_type = mimetypes.guess_type(path)
return ("local", mime_type[0], open(path, 'rb'))
示例6: getFileExtensions
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def getFileExtensions(mime):
if(not mimetypes.inited):
mimetypes.init()
initKnowMimetypes()
types = mime.split(";")
result = []
for t in types:
if(t):
res = mimetypes.guess_all_extensions(t)
#print("getting extensions for mime " + str(t) + " " + str(res))
result.extend(res)
if(len(result) == 0):
result.append(".*")
return result
示例7: problem_init_view
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def problem_init_view(request, problem):
problem = get_object_or_404(Problem, code=problem)
if not problem.is_editable_by(request.user):
raise Http404()
try:
with problem_data_storage.open(os.path.join(problem.code, 'init.yml'), 'rb') as f:
data = utf8text(f.read()).rstrip('\n')
except IOError:
raise Http404()
return render(request, 'problem/yaml.html', {
'raw_source': data, 'highlighted_source': highlight_code(data, 'yaml'),
'title': _('Generated init.yml for %s') % problem.name,
'content_title': mark_safe(escape(_('Generated init.yml for %s')) % (
format_html('<a href="{1}">{0}</a>', problem.name,
reverse('problem_detail', args=[problem.code])))),
})
示例8: init
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def init(self):
with flx.HBox():
self.leaflet = LeafletWidget(
flex=1,
center=(52, 4.1),
zoom=12,
show_scale=lambda: self.cbs.checked,
show_layers=lambda: self.cbl.checked,
)
with flx.VBox():
self.btna = flx.Button(text='Add SeaMap')
self.btnr = flx.Button(text='Remove SeaMap')
self.cbs = flx.CheckBox(text='Show scale')
self.cbl = flx.CheckBox(text='Show layers')
self.list = flx.VBox()
flx.Widget(flex=1)
self.leaflet.add_layer('http://a.tile.openstreetmap.org/', 'OpenStreetMap')
示例9: upload_fake_file
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def upload_fake_file():
if not request.files:
return "", 401
if not mimetypes.inited:
mimetypes.init()
for key, item in request.files.items():
if item.filename:
filetype = ".{}".format(item.filename.split(".")[-1])
if filetype in mimetypes.suffix_map:
if not item.content_type:
return "", 400
# Try to download each of the files downloaded to /tmp and
# then remove them
for key in request.files:
file_to_save = request.files[key]
path = os.path.join("/tmp", file_to_save.filename)
file_to_save.save(path)
return "", 200
示例10: load_project
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def load_project(path=None):
"""
Read project from directory passed as the argument and return Project
object. If no argument is given, the project is read from the current
directory.
"""
if not path:
p = os.getcwd()
else:
p = os.path.abspath(path)
while not os.path.isdir(os.path.join(p, ".smt")):
oldp, p = p, os.path.dirname(p)
if p == oldp:
raise IOError("No Sumatra project exists in the current directory or above it.")
mimetypes.init(mimetypes.knownfiles + [os.path.join(p, ".smt", "mime.types")])
# try:
prj = _load_project_from_json(p)
# except Exception:
# prj = _load_project_from_pickle(p)
return prj
示例11: _setup_mimetypes
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def _setup_mimetypes():
"""Pre-initialize global mimetype map."""
if not mimetypes.inited:
mimetypes.init()
mimetypes.types_map['.dwg'] = 'image/x-dwg'
mimetypes.types_map['.ico'] = 'image/x-icon'
mimetypes.types_map['.bz2'] = 'application/x-bzip2'
mimetypes.types_map['.gz'] = 'application/x-gzip'
示例12: get_file
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def get_file(request, path):
"""
Returns a file from the upload directory
"""
try:
f = open(os.path.join(settings.MEDIA_ROOT, path), 'r')
except IOError:
raise Http404
mimetypes.init()
t, e = mimetypes.guess_type(f.name)
return HttpResponse(f.read(), t)
示例13: test_registry_parsing
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def test_registry_parsing(self):
# the original, minimum contents of the MIME database in the
# Windows registry is undocumented AFAIK.
# Use file types that should *always* exist:
eq = self.assertEqual
mimetypes.init()
db = mimetypes.MimeTypes()
eq(db.guess_type("foo.txt"), ("text/plain", None))
eq(db.guess_type("image.jpg"), ("image/jpeg", None))
eq(db.guess_type("image.png"), ("image/png", None))
示例14: test_type_map_values
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def test_type_map_values(self):
import _winreg
class MockWinreg(object):
def __getattr__(self, name):
if name == 'QueryValueEx':
return lambda subkey, label: (u'text/plain', _winreg.REG_SZ)
return getattr(_winreg, name)
mimetypes._winreg = MockWinreg()
try:
mimetypes.init()
self.assertTrue(isinstance(mimetypes.types_map.values()[0], str))
finally:
mimetypes._winreg = _winreg
示例15: guess_mime_type
# 需要導入模塊: import mimetypes [as 別名]
# 或者: from mimetypes import init [as 別名]
def guess_mime_type(filename):
"""Guess mime type based of file extension."""
if not mimetypes.inited:
mimetypes.init()
return mimetypes.guess_type(filename)[0]