我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用mimetypes.inited()。
def read_config(): global settings global extensions_map exist = os.path.isfile(setting_file_name) if not exist: print 'Creating config file...' shutil.copyfile(default_setting_file_name, setting_file_name) print 'Edit config.json and launch the script again.' sys.exit() with open(setting_file_name) as data_file: settings = json.load(data_file) #################################################################### #Load default mimetypes and update them with config.json extensions# #################################################################### if not mimetypes.inited: mimetypes.init() # try to read system mime.types extensions_map = mimetypes.types_map.copy() extensions_map.update({ '': 'application/octet-stream' # Default }) extensions_map.update(settings['extensions']) # Read extensions from config.json ##################################################################### return
def guess_mime_type_from_filename(file_path): """ Guess the type of a file based on its filename or URL. """ if not mimetypes.inited: mimetypes.init() mimetypes.add_type('application/javascript', '.jse') mt = mimetypes.guess_type(file_path)[0] if mt: return mt
def test_usesGlobalInitFunction(self): """ By default, C{mimetypes.init} is called. """ # Checking mimetypes.inited doesn't always work, because # something, somewhere, calls mimetypes.init. Yay global # mutable state :) if _PY3: signature = inspect.signature(static.loadMimeTypes) self.assertIs(signature.parameters["init"].default, mimetypes.init) else: args, _, _, defaults = inspect.getargspec(static.loadMimeTypes) defaultInit = defaults[args.index("init")] self.assertIs(defaultInit, mimetypes.init)