Convert a Morsel object into a Cookie containing the one k/v pair.
(morsel: Morsel[Any])
| 529 | |
| 530 | |
| 531 | def morsel_to_cookie(morsel: Morsel[Any]) -> Cookie: |
| 532 | """Convert a Morsel object into a Cookie containing the one k/v pair.""" |
| 533 | |
| 534 | expires: int | None = None |
| 535 | if morsel["max-age"]: |
| 536 | try: |
| 537 | expires = int(time.time() + int(morsel["max-age"])) |
| 538 | except ValueError: |
| 539 | raise TypeError(f"max-age: {morsel['max-age']} must be integer") |
| 540 | elif morsel["expires"]: |
| 541 | time_template = "%a, %d-%b-%Y %H:%M:%S GMT" |
| 542 | expires = calendar.timegm(time.strptime(morsel["expires"], time_template)) |
| 543 | return create_cookie( |
| 544 | comment=morsel["comment"], |
| 545 | comment_url=bool(morsel["comment"]), |
| 546 | discard=False, |
| 547 | domain=morsel["domain"], |
| 548 | expires=expires, |
| 549 | name=morsel.key, |
| 550 | path=morsel["path"], |
| 551 | port=None, |
| 552 | rest={"HttpOnly": morsel["httponly"]}, |
| 553 | rfc2109=False, |
| 554 | secure=bool(morsel["secure"]), |
| 555 | value=morsel.value, |
| 556 | version=morsel["version"] or 0, |
| 557 | ) |
| 558 | |
| 559 | |
| 560 | _CookieJarT = TypeVar("_CookieJarT", bound=CookieJar) |