Decorator, which adds correct MIME type for python source to the decorated
    bottle API function.
432072
def python_mime(fn):
    
    @wraps(fn)
    def python_mime_decorator(*args, **kwargs):
        response.content_type = "text/x-python"

        return fn(*args, **kwargs)

    return python_mime_decorator
1346073
def mime_type(self, type_: Optional[MimeType] = None) -> str:
        
        key = self._validate_enum(item=type_, enum=MimeType)
        types = MIME_TYPES[key]
        return self.random.choice(types)
1230301
def useful_mimetype(text):
    
    if text is None:
        return False
    mimetype = normalize_mimetype(text)
    return mimetype not in [DEFAULT, PLAIN, None]
176389
def pygments_required(func):
    """"""
    def wrapper(*args, **kwargs):
        if not HAS_PYGMENTS:  # pragma: no cover
            raise ImportError(
                "Please install 'pygments' library to use syntax_color.")
        rv = func(*args, **kwargs)
        return rv
    return wrapper
1140247
def _response_mimetype_based_on_accept_header(self):
        
        response_mimetype = None

        if not request.accept_mimetypes:
            response_mimetype = self.default_mimetype
        else:
            all_media_types_wildcard = '*/*'

            for mimetype, q in request.accept_mimetypes:
                if mimetype == all_media_types_wildcard:
                    response_mimetype = self.default_mimetype
                    break
                if mimetype in self.response_formatters:
                    response_mimetype = mimetype
                    break

        return response_mimetype
1140119
def api(f):
    
    def wraps(self, *args, **kwargs):
        try:
            return f(self, *args, **kwargs)
        except Exception as e:
            logging.exception(e)
            return json_error_response(get_error_msg())

    return functools.update_wrapper(wraps, f)
710328
def log_to_logger(fn):
    
    @wraps(fn)
    def _log_to_logger(*args, **kwargs):
        actual_response = fn(*args, **kwargs)
        # modify this to log exactly what you need:
        logger.info('%s %s %s %s' % (bottle.request.remote_addr,
                                     bottle.request.method,
                                     bottle.request.url,
                                     bottle.response.status))
        return actual_response
    return _log_to_logger
541763
def get_mime(self, path, isdir):
        ''''''
        if isdir:
            file_type = FOLDER
        else:
            file_type = mimetypes.guess_type(path)[0]
            if not file_type:
                file_type = UNKNOWN
        return file_type
875877
def setup_for_bottle(
        auth, app, send_email=None, render=None,
        session=None, request=None, urloptions=None):
    import bottle

    auth.request = request or bottle.request
    if session is not None:
        auth.session = session
    if send_email:
        auth.send_email = send_email

    auth.render = render or bottle.template
    bottle.BaseTemplate.defaults['csrf_token'] = auth.get_csrf_token
    bottle.BaseTemplate.defaults['auth'] = auth

    

    @bottle.hook('before_request')
    def after_request():
        auth.session = session or getattr(bottle.request, 'session') \
            or bottle.request.environ.get('beaker.session')
        assert auth.session, 'Session not found'

        # By doing this, ``bottle.request`` now has a ``user`` attribute
        # that it's replaced by the real user object the first time is used.
        LazyUser(auth, bottle.request, user_name=auth.user_name)

    if auth.views:
        assert auth.render
        setup_for_bottle_views(auth, app, urloptions)
433781
def prepare(self, mime):
        
        for key in self:
            if key == 'Bcc' or key == 'Resent-Bcc':
                continue
            del mime[key]
            # Python 3.* email's compatibility layer will handle
            # unicode field values in proper way but Python 2
            # won't (it will encode not only additional field
            # values but also all header values)
            parsed_header, additional_fields = parse_header(
                self[key] if IS_PY3 else
                self[key].encode("utf-8")
            )
            mime.add_header(key, parsed_header, **additional_fields)
1227838
def jsonp(func):
    

    @functools.wraps(func)
    def decorated_view(*args, **kwargs):
        callback = request.args.get('callback', None)
        if callback:
            data = str(func(*args, **kwargs))
            content = str(callback) + '(' + data + ')'
            mimetype = 'application/javascript'
            return current_app.response_class(content, mimetype=mimetype)
        else:
            return func(*args, **kwargs)
    return decorated_view
335010
def _ostaunicode(src):
    # type: (str) -> bytes
    '''
    
    '''
    if have_py_3:
        bytename = src
    else:
        bytename = src.decode('utf-8')  # type: ignore

    try:
        enc = bytename.encode('latin-1')
        encbyte = b'\x08'
    except (UnicodeEncodeError, UnicodeDecodeError):
        enc = bytename.encode('utf-16_be')
        encbyte = b'\x10'
    return encbyte + enc
444052
def __update_common(self):
        """"""

        if "TCON" in self:
            # Get rid of "(xx)Foobr" format.
            self["TCON"].genres = self["TCON"].genres

        mimes = {"PNG": "image/png", "JPG": "image/jpeg"}
        for pic in self.getall("APIC"):
            if pic.mime in mimes:
                newpic = APIC(
                    encoding=pic.encoding, mime=mimes[pic.mime],
                    type=pic.type, desc=pic.desc, data=pic.data)
                self.add(newpic)
665771
def mime_type(instance):
    
    mime_pattern = re.compile(r'^(application|audio|font|image|message|model'
                              '|multipart|text|video)/[a-zA-Z0-9.+_-]+')
    for key, obj in instance['objects'].items():
        if ('type' in obj and obj['type'] == 'file' and 'mime_type' in obj):
            if enums.media_types():
                if obj['mime_type'] not in enums.media_types():
                    yield JSONError("The 'mime_type' property of object '%s' "
                                    "('%s') should be an IANA registered MIME "
                                    "Type of the form 'type/subtype'."
                                    % (key, obj['mime_type']), instance['id'],
                                    'mime-type')
            else:
                info("Can't reach IANA website; using regex for mime types.")
                if not mime_pattern.match(obj['mime_type']):
                    yield JSONError("The 'mime_type' property of object '%s' "
                                    "('%s') should be an IANA MIME Type of the"
                                    " form 'type/subtype'."
                                    % (key, obj['mime_type']), instance['id'],
                                    'mime-type')
877289
def _get_mime_type(self, buff):
        
        if self._magic is not None:
            return self._magic.id_buffer(buff)
        else:
            try:
                return mimetypes.guess_type("f." + imghdr.what(0, buff))[0]
            except (IOError, TypeError):
                logging.warning("Couldn't detect content type of avatar image"
                                ". Specify the 'contentType' parameter explicitly.")
                return None
49880
def replace(self, main_type=None, sub_type=None, params=None):
        
        if main_type is None:
            main_type = self.main_type

        if sub_type is None:
            sub_type = self.sub_type

        if params is None:
            params = self.params

        return MimeType(main_type, sub_type, params)
357860
def container(dec):
    
    # Credits: http://stackoverflow.com/a/1167248/1798683
    @wraps(dec)
    def meta_decorator(f):
        decorator = dec(f)
        decorator.orig_func = f
        return decorator
    return meta_decorator