Skip to content

Commit

Permalink
Minor Parser.get_parser() cleanup (guard clauses).
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon24 committed Jul 9, 2024
1 parent 80afebf commit 6df7892
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/reader/_parser/_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,25 +314,25 @@ def get_parser(
ParseError: No parser matches.
"""
parser = self.get_parser_by_url(url)
if not parser:
if not mime_type:
mime_type, _ = mimetypes.guess_type(url)

# https://tools.ietf.org/html/rfc7231#section-3.1.1.5
#
# > If a Content-Type header field is not present, the recipient
# > MAY either assume a media type of "application/octet-stream"
# > ([RFC2046], Section 4.5.1) or examine the data to determine its type.
#
if not mime_type:
mime_type = 'application/octet-stream'

parser = self.get_parser_by_mime_type(mime_type)
if not parser:
raise ParseError(url, message=f"no parser for MIME type {mime_type!r}")

return parser, mime_type
if parser := self.get_parser_by_url(url):
return parser, mime_type

if not mime_type:
mime_type, _ = mimetypes.guess_type(url)

# https://tools.ietf.org/html/rfc7231#section-3.1.1.5
#
# > If a Content-Type header field is not present, the recipient
# > MAY either assume a media type of "application/octet-stream"
# > ([RFC2046], Section 4.5.1) or examine the data to determine its type.
#
if not mime_type:
mime_type = 'application/octet-stream'

if parser := self.get_parser_by_mime_type(mime_type):
return parser, mime_type

raise ParseError(url, message=f"no parser for MIME type {mime_type!r}")

def validate_url(self, url: str) -> None:
"""Check if ``url`` is valid without actually retrieving it.
Expand Down

0 comments on commit 6df7892

Please sign in to comment.