'Returns a cookielib.Cookie based on a set-cookie header line'

def set_cookie(self, kaka, request):

    if (not kaka):
        return
    part = urlparse.urlparse(request.url)
    _domain = part.hostname
    logger.debug(("%s:    '%s'" % (_domain, kaka)))
    for (cookie_name, morsel) in kaka.items():
        std_attr = ATTRS.copy()
        std_attr['name'] = cookie_name
        _tmp = morsel.coded_value
        if (_tmp.startswith('"') and _tmp.endswith('"')):
            std_attr['value'] = _tmp[1:(-1)]
        else:
            std_attr['value'] = _tmp
        std_attr['version'] = 0
        for attr in morsel.keys():
            if (attr in ATTRS):
                if morsel[attr]:
                    if (attr == 'expires'):
                        std_attr[attr] = _since_epoch(morsel[attr])
                    elif (attr == 'path'):
                        if morsel[attr].endswith(','):
                            std_attr[attr] = morsel[attr][:(-1)]
                        else:
                            std_attr[attr] = morsel[attr]
                    else:
                        std_attr[attr] = morsel[attr]
            elif (attr == 'max-age'):
                if morsel['max-age']:
                    std_attr['expires'] = (time.time() + int(morsel['max-age']))
        for (att, item) in PAIRS.items():
            if std_attr[att]:
                std_attr[item] = True
        if std_attr['domain']:
            if std_attr['domain'].startswith('.'):
                std_attr['domain_initial_dot'] = True
        else:
            std_attr['domain'] = _domain
            std_attr['domain_specified'] = True
        if (morsel['max-age'] is 0):
            try:
                self.cookiejar.clear(domain=std_attr['domain'], path=std_attr['path'], name=std_attr['name'])
            except ValueError:
                pass
        elif (morsel['expires'] < utc_now()):
            try:
                self.cookiejar.clear(domain=std_attr['domain'], path=std_attr['path'], name=std_attr['name'])
            except ValueError:
                pass
        else:
            new_cookie = cookielib.Cookie(**std_attr)
            self.cookiejar.set_cookie(new_cookie)
