• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python mimetypes.init函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中mimetypes.init函数的典型用法代码示例。如果您正苦于以下问题:Python init函数的具体用法?Python init怎么用?Python init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了init函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: ziplib

def ziplib(config, handle):
  """
  Function to unpack a downloaded zip file by exploding it into multiple handles.
  zip_unpack: Unpack if true
  """
  import zipfile
  import calendar
  import mimetypes

  mimetypes.init()

  if not config.get("zip_unpack", False):
    yield handle
    return

  logger.info("Extracting since zip_unpack = true");

  z = zipfile.ZipFile(handle.fileobj)

  def convert(dt):
    """
    Convert a ZipInfo date_time into a unix timestamp (compatible with tar).
    """
    return calendar.timegm(dt)

  for i in z.infolist():
    mime = mimetypes.guess_type(i.filename)[0]
    yield ExtHandle(i.filename, i.file_size, convert(i.date_time), mime, z.open(i))

  return
开发者ID:pestro,项目名称:pusher,代码行数:30,代码来源:extlib.py


示例2: create_server

def create_server(ui, app):

    if ui.config('web', 'certificate'):
        if sys.version_info >= (2, 6):
            handler = _httprequesthandlerssl
        else:
            handler = _httprequesthandleropenssl
    else:
        handler = _httprequesthandler

    if ui.configbool('web', 'ipv6'):
        cls = IPv6HTTPServer
    else:
        cls = MercurialHTTPServer

    # ugly hack due to python issue5853 (for threaded use)
    import mimetypes; mimetypes.init()

    address = ui.config('web', 'address', '')
    port = util.getport(ui.config('web', 'port', 8000))
    try:
        return cls(ui, app, (address, port), handler)
    except socket.error, inst:
        raise util.Abort(_("cannot start server at '%s:%d': %s")
                         % (address, port, inst.args[1]))
开发者ID:jordigh,项目名称:mercurial-crew,代码行数:25,代码来源:server.py


示例3: __init__

    def __init__(self):

        remuco.PlayerAdapter.__init__(
            self,
            "Totem",
            mime_types=("audio", "video"),
            volume_known=True,
            playback_known=True,
            progress_known=True,
            file_actions=FILE_ACTIONS,
        )

        self.__to = None

        self.__signal_ids = ()

        self.__update_item = False
        self.__md_album = None
        self.__md_artist = None
        self.__md_title = None
        self.__last_mrl = None

        self.__seek_step_initial = 5000
        self.__seek_step = self.__seek_step_initial
        self.__css_sid = 0

        if not mimetypes.inited:
            mimetypes.init()
开发者ID:sayanriju,项目名称:remuco,代码行数:28,代码来源:remotem.py


示例4: get_file_types_list

	def get_file_types_list(self):
		file_list = self.get_file_list()
		if not file_list[0][1].has_key('mimetype'):
			mimetypes.init()
			for item in file_list:
				item[1]['mimetype'] = mimetypes.guess_type(item[0])[0]
		return file_list
开发者ID:ffagerho,项目名称:metrics-tools,代码行数:7,代码来源:metrics.py


示例5: GoogleSitesLogin

def GoogleSitesLogin(site_name=None, site_domain=None, debug=False):
    if site_domain is None:
        raise ValueError("site_domain is not valid")
    if site_name is None:
        raise ValueError("site_name is not valid")

    mimetypes.init()

    client = gdata.sites.client.SitesClient( source="wiki-push",
                                             site=site_name,
                                             domain=site_domain )
    client.http_client.debug = debug

    try:
        gdata.sample_util.authorize_client( client,
                        auth_type=gdata.sample_util.CLIENT_LOGIN,
                        service=client.auth_service,
                        source="wiki-push",
                        scopes=['http://sites.google.com/feeds/',
                                'https://sites.google.com/feeds/'] )
    except gdata.client.BadAuthentication:
        print "Invalid user credentials given."
        return None
    except gdata.client.Error:
        print "Login Error."
        return None

    return client
开发者ID:ericggregory,项目名称:gollumwiki-to-googlesites,代码行数:28,代码来源:gollumwiki-to-googlesites.py


示例6: main

def main():

    mimetypes.init()

    for info in scan_dir(sys.argv[1]):
       #add_file(info)
        print info['name'], datetime.fromtimestamp(info['mtime']).strftime('%d-%m-%Y')
开发者ID:Kristina-B,项目名称:Different-files,代码行数:7,代码来源:sync.py


示例7: handle_noargs

    def handle_noargs(self, **options):
        mimetypes.init()

        locked_print("===> Syncing static directory")
        pool = ThreadPool(20)

        # Sync every file in the static media dir with S3
        def pooled_sync_file(base, filename):
            pool.apply_async(self.sync_file, args=[base, filename])

        self.walk_tree([conf.SIMPLESTATIC_DIR], pooled_sync_file)
        pool.close()
        pool.join()
        locked_print("===> Static directory syncing complete")

        locked_print("===> Compressing and uploading CSS and JS")
        pool = ThreadPool(20)

        # Iterate over every template, looking for SimpleStaticNode
        def pooled_handle_template(base, filename):
            pool.apply_async(self.handle_template, args=[base, filename])

        self.walk_tree(list(settings.TEMPLATE_DIRS), pooled_handle_template)
        pool.close()
        pool.join()
        locked_print("===> Finished compressing and uploading CSS and JS")
开发者ID:ericflo,项目名称:django-simplestatic,代码行数:26,代码来源:static_sync.py


示例8: run

    def run(self):
        mimetypes.init()
        log.debug("Initialized mime type database.")
        screenshot_tube = self.config.get('beanstalk', 'screenshot_tube')
        self.beanstalk = politwoops.utils.beanstalk(
            host=self.config.get('beanstalk', 'host'),
            port=int(self.config.get('beanstalk', 'port')),
            watch=screenshot_tube,
            use=None)
        log.debug("Connected to queue.")

        while True:
            time.sleep(0.2)
            self.heart.beat()
            reserve_timeout = max(self.heart.interval.total_seconds() * 0.1, 2)
            job = self.beanstalk.reserve(timeout=reserve_timeout)
            if job:
                try:
                    tweet = anyjson.deserialize(job.body)
                    self.process_entities(tweet)
                    job.delete()
                except Exception as e:
                    log.error("Exception caught, burying screenshot job for tweet {tweet}: {e}",
                              tweet=tweet.get('id'), e=e)
                    job.bury()
开发者ID:monkeycycle,项目名称:politwoops-tweet-collector,代码行数:25,代码来源:screenshot-worker.py


示例9: __call__

    def __call__(self, value, *args, **kwargs):
        """
        :param value:
        :type value: :class:`list` of :class:`dict`
        :return:
        :rtype: :class:`django.forms.Form`
        """
        if value is None:
            return self.form_cls(*args, **kwargs)

        post_data = QueryDict("", mutable=True)
        file_data = QueryDict("", mutable=True)
        for obj in value:
            name = obj["name"]
            value = obj["value"]
            if name in self.form_cls.base_fields and isinstance(
                self.form_cls.base_fields[name], FileField
            ):
                mimetypes.init()
                basename = os.path.basename(value)
                (type_, __) = mimetypes.guess_type(basename)
                # it's a file => we need to simulate an uploaded one
                content = InMemoryUploadedFile(
                    io.BytesIO(b"\0"),
                    name,
                    basename,
                    type_ or "application/binary",
                    1,
                    "utf-8",
                )
                file_data.update({name: content})
            else:
                post_data.update({name: value})
        return self.form_cls(post_data, file_data, *args, **kwargs)
开发者ID:d9pouces,项目名称:django-floor,代码行数:34,代码来源:decorators.py


示例10: mimetype

 def mimetype(self):
     try:
         mimetypes.init()
         type = mimetypes.guess_type(self.file.name)[0]
     except:
         type = None
     return type
开发者ID:kawazrepos,项目名称:Kawaz2nd,代码行数:7,代码来源:models.py


示例11: encoding

 def encoding(self):
     try:
         mimetypes.init()
         encoding = mimetypes.guess_type(self.file.name)[1]
     except:
         encoding = None
     return encoding
开发者ID:kawazrepos,项目名称:Kawaz2nd,代码行数:7,代码来源:models.py


示例12: get_file_mimetype

 def get_file_mimetype(path):
     """
     Returns file mimetype
     """
     filename, file_extension = os.path.splitext(path)
     mimetypes.init()
     return mimetypes.types_map[file_extension]
开发者ID:DimaSavkov,项目名称:simple_http,代码行数:7,代码来源:storage.py


示例13: get_resource_content

def get_resource_content(request, source_name):
    """
        Returns the actual resource.
        Tries to guess the mimetype. If it fails, returns 
        application/octet-stream as fallback.
    """
    import mimetypes
    mimetypes.init()
    resource_id = request.GET.get('resource_id', None)
    if not resource_id:
        raise Http404

    try:
        source = Source.objects.get(name=source_name)
    except Source.DoesNotExist:
        response_data = {'error': 'Source not found'}
    else:
        response_data = Backend(source.backend_id).get_resource_content(source, resource_id)
        
    if 'content' in response_data.keys():
        content = response_data['content']
        mimetype, encoding = mimetypes.guess_type(response_data['name'])
        if not mimetype:
            mimetype = 'application/octet-stream'
    else:
        content = response_data
        mimetype = "application/json"
    return HttpResponse(content, mimetype=mimetype)
开发者ID:adieyal,项目名称:docrepo-backend,代码行数:28,代码来源:views.py


示例14: _ContentType

def _ContentType(requestedfile):
	"""
	Guess the file type based on the extension. This will be used 
	to decide whether to open the file in text or binary mode.
	"""

	base, ext = posixpath.splitext(requestedfile)

	# This uses the standard MIME types mapping library. If it isn't 
	# found, we check to see if it is xhtml (which might not be in the
	# local mime types library). If the type still is not found, it 
	# defaults to plain text.
	mimetypes.init()
	try:
		ctype = mimetypes.types_map[ext]
	except:
		if (ext.lower() == '.xhtml'):
			ctype = 'application/xhtml+xml'
		else:
			ctype = 'text/plain'

	# We need to augment this information with a set of image
	# types to use for the file mode.
	if (ext in ['.png', '.PNG', '.gif', '.GIF', '.jpeg', '.JPEG', 
		'.jpg', '.JPG']):
		fmode = 'rb'
	else:
		fmode = 'r'

	return fmode, ctype
开发者ID:ssrselvamraju,项目名称:mblogic-S2-client,代码行数:30,代码来源:MBWebPage.py


示例15: GET

    def GET(self, courseid, taskid, path):
        """ GET request """
        if User.is_logged_in():
            try:
                course = FrontendCourse(courseid)
                if not course.is_open_to_user(User.get_username()):
                    return renderer.course_unavailable()

                task = course.get_task(taskid)
                if not task.is_visible_by_user(User.get_username()):
                    return renderer.task_unavailable()

                path_norm = posixpath.normpath(urllib.unquote(path))
                public_folder_path = os.path.normpath(os.path.realpath(os.path.join(INGIniousConfiguration["tasks_directory"], courseid, taskid, "public")))
                file_path = os.path.normpath(os.path.realpath(os.path.join(public_folder_path, path_norm)))

                # Verify that we are still inside the public directory
                if os.path.normpath(os.path.commonprefix([public_folder_path, file_path])) != public_folder_path:
                    raise web.notfound()

                if os.path.isfile(file_path):
                    mimetypes.init()
                    mime_type = mimetypes.guess_type(file_path)
                    web.header('Content-Type', mime_type[0])
                    with open(file_path) as static_file:
                        return static_file.read()
                else:
                    raise web.notfound()
            except:
                if web.config.debug:
                    raise
                else:
                    raise web.notfound()
        else:
            return renderer.index(False)
开发者ID:GuillaumeDerval,项目名称:INGInious,代码行数:35,代码来源:tasks.py


示例16: GET

    def GET(self, courseid, taskid, path):
        """ GET request """
        if self.user_manager.session_logged_in():
            try:
                course = self.course_factory.get_course(courseid)
                if not self.user_manager.course_is_open_to_user(course):
                    return self.template_helper.get_renderer().course_unavailable()

                task = course.get_task(taskid)
                if not self.user_manager.task_is_visible_by_user(task):
                    return self.template_helper.get_renderer().task_unavailable()

                path_norm = posixpath.normpath(urllib.unquote(path))
                public_folder_path = os.path.normpath(os.path.realpath(os.path.join(task.get_directory_path(), "public")))
                file_path = os.path.normpath(os.path.realpath(os.path.join(public_folder_path, path_norm)))

                # Verify that we are still inside the public directory
                if os.path.normpath(os.path.commonprefix([public_folder_path, file_path])) != public_folder_path:
                    raise web.notfound()

                if os.path.isfile(file_path):
                    mimetypes.init()
                    mime_type = mimetypes.guess_type(file_path)
                    web.header('Content-Type', mime_type[0])
                    with open(file_path) as static_file:
                        return static_file.read()
                else:
                    raise web.notfound()
            except:
                if web.config.debug:
                    raise
                else:
                    raise web.notfound()
        else:
            return self.template_helper.get_renderer().index(self.user_manager.get_auth_methods_fields(), False)
开发者ID:suelambot,项目名称:INGInious,代码行数:35,代码来源:tasks.py


示例17: __init__

 def __init__(self, local_dir, verbose, quick):
     self.local_dir = local_dir
     self.verbose = verbose
     self.quick = quick
     parser = ConfigParser.SafeConfigParser()
     config_file_name = os.path.join(local_dir, DOT_S3SITESYNC)
     with open(config_file_name) as f:
         config_file_contents = unicode("[s3sitesync]\n" + f.read())
     parser.readfp(io.StringIO(config_file_contents), config_file_name)
     self.keyid = parser.get('s3sitesync', 'awsAccessKeyId')
     self.key = parser.get('s3sitesync', 'awsSecretAccessKey')
     self.bucket_name = parser.get('s3sitesync', 'bucketName')
     self.region = parser.get('s3sitesync', 'awsRegion')
     if not self.region in API_ENDPOINTS:
         raise Exception("Invalid region '%s'" % self.region)
     self.api_endpoint = API_ENDPOINTS[self.region]
     self.index_doc = parser.get('s3sitesync', 'indexDocument')
     self.error_doc = parser.get('s3sitesync', 'errorDocument')
     ignore_file_name = os.path.join(local_dir, DOT_S3IGNORE)
     self.ignore_patterns = []
     if os.path.isfile(ignore_file_name):
         with open(ignore_file_name) as f:
             for line in f:
                 line = line.strip()
                 if line:
                     pattern = os.path.join(local_dir, line)
                     self.ignore_patterns.append(pattern)
     self.ignore_patterns.append(os.path.join(local_dir, DOT_S3SITESYNC))
     self.ignore_patterns.append(os.path.join(local_dir, DOT_S3IGNORE))
     mimetypes.init()
     mimetypes.init([os.path.join(local_dir, DOT_S3MIMETYPES)])
开发者ID:mleonhard,项目名称:s3sitesync,代码行数:31,代码来源:s3sitesync.py


示例18: sort

def sort(dir, tmp_dir = path.abspath('/home/ndhuang/Pictures/'), all = False):
    mimetypes.init()
    mimetypes.add_type('image/x-nikon-nef', '.NEF')
    sort_dir = path.join(tmp_dir, 'sorting')

    img_files = os.listdir(dir)
    i = 0
    while i < len(img_files):
        img = img_files[i]
        if '.pp3' in img:
            img_files.remove(img)
            continue
        elif not all and not re.match('DSC_(\d){4}\.', img):
            img_files.remove(img)
            continue            
        mt = mimetypes.guess_type(img)[0]
        mt = mt.split('/')
        if mt[0] != 'image':
            raise RuntimeError('%s is not an image!' %img)
        else:
            i += 1

    os.mkdir(sort_dir)
    imgs = [[] for i in img_files]
    for i in range(len(img_files)):
        try:
            imgs[i] = Img(path.join(dir, img_files[i]))
        except KeyError:
            print '%s is missing EXIF data!' %img_files[i]
    # remove empty arrays
    while [] in imgs:
        imgs.remove([])
    imgs = sorted(imgs)
    
    pic_num = 1
    copies = 1
    for i, img in enumerate(imgs):
        ext = img.path[-3:]
        if i != 0 and imgs[i] == imgs[i - 1]:
            dst = path.join(sort_dir,
                            'DSC_%04d-%d.%s' %(pic_num - 1, copies, ext))
            copies += 1
        else:
            dst = path.join(sort_dir, 'DSC_%04d.%s' %(pic_num, ext))
            pic_num += 1
            copies = 1
        os.rename(img.path, dst)
        try:
            os.rename(img.path + '.out.pp3', dst + '.out.pp3')
        except OSError as err:
            pass
        try: 
            os.rename(img.path + '.pp3', dst + '.pp3')
        except OSError as err:
            pass

    for f in os.listdir(dir):
        os.rename(path.join(dir, f), path.join(sort_dir, f))
    os.rmdir(dir)
    os.rename(sort_dir, dir)
开发者ID:ndhuang,项目名称:python-lib,代码行数:60,代码来源:sortpics.py


示例19: __init__

  def __init__(self, filepath, chunk_size=None, convert=None,
               host=None, ssl=False, debug=False):
    self.client = gdata.docs.client.DocsClient(source=APP_NAME)
    self.client.ssl = ssl
    self.client.http_client.debug = debug
    self.convert = convert

    if host:
      self.client.host = host

    if chunk_size:
      self.chunk_size = chunk_size

    # Authenticate the user with CLientLogin, OAuth, or AuthSub.
    try:
      gdata.sample_util.authorize_client(
          self.client, service=self.client.auth_service, source=APP_NAME,
          scopes=self.client.auth_scopes)
    except gdata.client.BadAuthentication:
      exit('Invalid user credentials given.')
    except gdata.client.Error:
      exit('Login Error')

    mimetypes.init()  # Register common mimetypes on system.

    self.f = open(filepath)
    content_type = get_mimetype(self.f.name)
    file_size = os.path.getsize(self.f.name)

    self.uploader = gdata.client.ResumableUploader(
        self.client, self.f, content_type, file_size,
        chunk_size=self.chunk_size, desired_class=gdata.docs.data.DocsEntry)
开发者ID:racktivity,项目名称:ext-pylabs-core,代码行数:32,代码来源:resumable_upload_sample.py


示例20: download_generic

 def download_generic(self):
     id = self.request.params.get('id', None)
     filename = self.request.params.get('filename', None)
     if id is None or filename is None:
         return HTTPBadRequest()
     entry = DBSession.query(LuxDownloadUrl).filter(
                 LuxDownloadUrl.id == id).first()
     if entry is not None:
         if entry.protected and self.request.user is None:
             return HTTPUnauthorized()
         url = entry.url + filename
         try:
             data = urllib2.urlopen(url, None, 1800)
         except Exception as e:
             log.exception(e)
             data = None
             log.debug(url)
         mimetypes.init()
         type = "application/octet-stream"
         mimetype = mimetypes.guess_type(url)
         if mimetype[0] is not None:
             type = mimetype[0]
         headers = {"Content-Type": type,
                    "Content-Disposition": "attachment; filename=\""
                    + str(filename) + "\""}
         if data is not None:
             return Response(data.read(), headers=headers)
     return HTTPBadRequest()
开发者ID:Geoportail-Luxembourg,项目名称:geoportailv3,代码行数:28,代码来源:download.py



注:本文中的mimetypes.init函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python mimetypes.MIMEType类代码示例发布时间:2022-05-27
下一篇:
Python mimetypes.guess_type函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap