Skip to content

Commit

Permalink
Merge pull request #33 from Ousret/develop
Browse files Browse the repository at this point in the history
Release 2.2.0
  • Loading branch information
Ousret authored May 20, 2020
2 parents b6918ec + 719b6f5 commit 5c3d27c
Show file tree
Hide file tree
Showing 14 changed files with 522 additions and 168 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: python
cache: pip

python:
- "3.5"
- "3.6"
- "3.7"
- "3.8"
Expand All @@ -11,6 +12,7 @@ python:

jobs:
allow_failures:
- python: "3.5"
- python: "3.9-dev" # See https://github.com/python/mypy/issues/8627
- python: "pypy3"

Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,20 @@ charset = headers['Content-Type'].split(';')[-1].split('=')[-1].replace('"', '')

* A backwards-compatible syntax using bracket style.
* Capability to alter headers using simple, human-readable operator notation `+` and `-`.
* Flexibility if headers are from IMAP4 or HTTP, use as you need with one library.
* Ability to parse any object and extract recognized headers from it, it also support UTF-8 encoded headers.
* Flexibility if headers are from an email or HTTP, use as you need with one library.
* Ability to parse any object and extract recognized headers from it, it also supports UTF-8 encoded headers.
* Fully type-annotated.
* Provide great auto-completion in Python interpreter or any capable IDE.
* Absolutely no dependencies.
* No dependencies. And never will be.
* 90% test coverage.

Plus all the features that you would expect from handling headers...

* Properties syntax for headers and attribute in header.
* Supports headers and attributes OneToOne, OneToMany and ManySquashedIntoOne.
* Capable of parsing `bytes`, `fp`, `str`, `dict`, `email.Message`, `requests.Response` and `httpx._models.Response`.
* Automatically unquote and unfold value of an attribute when retrieving it.
* Capable of parsing `bytes`, `fp`, `str`, `dict`, `email.Message`, `requests.Response`, `httpx._models.Response` and `urllib3.HTTPResponse`.
* Automatically unquote and unfold the value of an attribute when retrieving it.
* Keep headers and attributes ordering.
* Case insensitive with header name and attribute key.
* Character `-` equal `_` in addition of above feature.
* Any syntax you like, we like.
Expand Down Expand Up @@ -104,7 +105,7 @@ headers.set_cookie[0]._1p_jar # output: 2020-03-16-21
headers.set_cookie[0]["1P_JAR"] # output: 2020-03-16-21
```

Since v2.1 you can transform an Header object to its target `CustomHeader` subclass in order to access more methods.
Since v2.1 you can transform an Header object to its target `CustomHeader` subclass to access more methods.

```python
from kiss_headers import parse_it, get_polymorphic, SetCookie
Expand Down
4 changes: 2 additions & 2 deletions kiss_headers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Kiss-Headers
~~~~~~~~~~~~~~
Kiss-Headers is a headers, HTTP or IMAP4 _(message, email)_ flavour, utility, written in Python, for humans.
Kiss-Headers is a headers, HTTP or IMAP4 _(message, email)_ flavour, utility, written in pure Python, for humans.
Object oriented headers. Keep it sweet and simple.
Basic usage:
Expand Down Expand Up @@ -87,5 +87,5 @@
XFrameOptions,
XXssProtection,
)
from kiss_headers.models import Header, Headers, lock_output_type
from kiss_headers.models import Attributes, Header, Headers, lock_output_type
from kiss_headers.version import VERSION, __version__
13 changes: 8 additions & 5 deletions kiss_headers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ def parse_it(raw_headers: Any) -> Headers:
for header_name in raw_headers.raw.headers:
for header_content in raw_headers.raw.headers.getlist(header_name):
headers.append((header_name, header_content))
elif r in ["httpx._models.Response", "urllib3.response.HTTPResponse"]:
elif r in [
"httpx._models.Response",
"urllib3.response.HTTPResponse",
]: # pragma: no cover
headers = raw_headers.headers.items()

if headers is None:
raise TypeError(
raise TypeError( # pragma: no cover
"Cannot parse type {type_} as it is not supported by kiss-header.".format(
type_=type(raw_headers)
)
Expand Down Expand Up @@ -98,7 +101,7 @@ def explain(headers: Headers) -> CaseInsensitiveDict:
Return a brief explanation of each header present in headers if available.
"""
if not Header.__subclasses__():
raise LookupError(
raise LookupError( # pragma: no cover
"You cannot use explain() function without properly importing the public package."
)

Expand Down Expand Up @@ -126,8 +129,8 @@ def explain(headers: Headers) -> CaseInsensitiveDict:
def get_polymorphic(
target: Union[Headers, Header], desired_output: Type[T]
) -> Union[T, List[T], None]:
"""Experimental. Transform an Header or Headers object to its target `CustomHeader` subclass
in order to access more ready-to-use methods. eg. You have an Header object named 'Set-Cookie' and you wish
"""Experimental. Transform a Header or Headers object to its target `CustomHeader` subclass
to access more ready-to-use methods. eg. You have a Header object named 'Set-Cookie' and you wish
to extract the expiration date as a datetime.
>>> header = Header("Set-Cookie", "1P_JAR=2020-03-16-21; expires=Wed, 15-Apr-2020 21:27:31 GMT")
>>> header["expires"]
Expand Down
8 changes: 6 additions & 2 deletions kiss_headers/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,11 @@ def get_cookie_value(
self, cookie_name: str, __default: Optional[str] = None
) -> Optional[str]:
"""Retrieve associated value with a given cookie name."""
return str(self[cookie_name]) if cookie_name in self else __default
return (
str(self[cookie_name]).replace('\\"', "")
if cookie_name in self
else __default
)


class SetCookie(CustomHeader):
Expand Down Expand Up @@ -735,7 +739,7 @@ def get_cookie_name(self) -> str:

def get_cookie_value(self) -> str:
"""Extract the cookie value."""
return str(self[self.get_cookie_name()])
return str(self[self.get_cookie_name()]).replace('\\"', "")


class StrictTransportSecurity(CustomHeader):
Expand Down
Loading

0 comments on commit 5c3d27c

Please sign in to comment.