Skip to content

Commit

Permalink
Improve readibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Pacu2 committed Aug 23, 2017
1 parent bb3a4db commit 65a02f7
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions weasyprint/__init__.py
Expand Up @@ -299,9 +299,18 @@ def _select_source(guess=None, filename=None, url=None, file_obj=None,
if base_url is not None:
base_url = ensure_url(base_url)

nones = [
param is None for param in (guess, filename, url, file_obj, string)]
if nones == [False, True, True, True, True]:
selected_params = [
param is not None for
param in (guess, filename, url, file_obj, string)]
if len(selected_params) != 1:
raise TypeError('Expected exactly one source, got ' + (
', '.join(
name for i, name in enumerate(
('guess', 'filename', 'url', 'file_obj', 'string'))
if not selected_params[i]
) or 'nothing'
))
elif guess:
if hasattr(guess, 'read'):
type_ = 'file_obj'
elif url_is_absolute(guess):
Expand All @@ -316,12 +325,12 @@ def _select_source(guess=None, filename=None, url=None, file_obj=None,
**{str(type_): guess})
with result as result:
yield result
elif nones == [True, False, True, True, True]:
elif filename:
if base_url is None:
base_url = path2url(filename)
with open(filename, 'rb') as file_obj:
yield 'file_obj', file_obj, base_url, None
elif nones == [True, True, False, True, True]:
elif url:
with fetch(url_fetcher, url) as result:
if check_css_mime_type and result['mime_type'] != 'text/css':
LOGGER.error(
Expand All @@ -338,24 +347,16 @@ def _select_source(guess=None, filename=None, url=None, file_obj=None,
yield (
'file_obj', result['file_obj'], base_url,
proto_encoding)
elif nones == [True, True, True, False, True]:
elif file_obj:
if base_url is None:
# filesystem file-like objects have a 'name' attribute.
name = getattr(file_obj, 'name', None)
# Some streams have a .name like '<stdin>', not a filename.
if name and not name.startswith('<'):
base_url = ensure_url(name)
yield 'file_obj', file_obj, base_url, None
elif nones == [True, True, True, True, False]:
elif string:
yield 'string', string, base_url, None
else:
raise TypeError('Expected exactly one source, got ' + (
', '.join(
name for i, name in enumerate(
('guess', 'filename', 'url', 'file_obj', 'string'))
if not nones[i]
) or 'nothing'
))

# Work around circular imports.
from .css import preprocess_stylesheet # noqa
Expand Down

0 comments on commit 65a02f7

Please sign in to comment.