# Path: src/rituals/util/_compat.py
# PY2 = sys.version_info[0] == 2
# PYPY = hasattr(sys, 'pypy_translation_info')
#     def reraise(tp, value, tb=None):
#     def decode_filename(filename):
#     def implements_iterator(cls):
#     def implements_to_string(cls):
#     def encode_filename(filename):
#     def decode_filename(filename):
# def with_metaclass(meta, *bases):
#         def __new__(cls, name, this_bases, d):
# def isodate(datestamp=None, microseconds=False):
#     class metaclass(meta):


import os
import re
import tempfile
import requests
from contextlib import contextmanager
from ._compat import urlparse, decode_filename
def url_as_file(url, ext=None):
    """
        Context manager that GETs a given `url` and provides it as a local file.

        The file is in a closed state upon entering the context,
        and removed when leaving it, if still there.

        To give the file name a specific extension, use `ext`;
        the extension can optionally include a separating dot,
        otherwise it will be added.

        Parameters:
            url (str): URL to retrieve.
            ext (str, optional): Extension for the generated filename.

        Yields:
            str: The path to a temporary file with the content of the URL.

        Raises:
            requests.RequestException: Base exception of ``requests``, see its
                docs for more detailed ones.

        Example:
            >>> import io, re, json
            >>> with url_as_file('https://api.github.com/meta', ext='json') as meta:
            ...     meta, json.load(io.open(meta, encoding='ascii'))['hooks']
            (u'/tmp/www-api.github.com-Ba5OhD.json', [u'192.30.252.0/22'])
    """
    if ext:
        ext = '.' + ext.strip('.')  # normalize extension

# Path: src/rituals/util/_compat.py
# PY2 = sys.version_info[0] == 2
# PYPY = hasattr(sys, 'pypy_translation_info')
#     def reraise(tp, value, tb=None):
#     def decode_filename(filename):
#     def implements_iterator(cls):
#     def implements_to_string(cls):
#     def encode_filename(filename):
#     def decode_filename(filename):
# def with_metaclass(meta, *bases):
#         def __new__(cls, name, this_bases, d):
# def isodate(datestamp=None, microseconds=False):
#     class metaclass(meta):


# Path: src/rituals/util/filesys.py
import os
import re
import tempfile
import requests
from contextlib import contextmanager
from ._compat import urlparse, decode_filename

def url_as_file(url, ext=None):
    """
        Context manager that GETs a given `url` and provides it as a local file.

        The file is in a closed state upon entering the context,
        and removed when leaving it, if still there.

        To give the file name a specific extension, use `ext`;
        the extension can optionally include a separating dot,
        otherwise it will be added.

        Parameters:
            url (str): URL to retrieve.
            ext (str, optional): Extension for the generated filename.

        Yields:
            str: The path to a temporary file with the content of the URL.

        Raises:
            requests.RequestException: Base exception of ``requests``, see its
                docs for more detailed ones.

        Example:
            >>> import io, re, json
            >>> with url_as_file('https://api.github.com/meta', ext='json') as meta:
            ...     meta, json.load(io.open(meta, encoding='ascii'))['hooks']
            (u'/tmp/www-api.github.com-Ba5OhD.json', [u'192.30.252.0/22'])
    """
    if ext:
        ext = '.' + ext.strip('.')  # normalize extension

    url_hint = 'www-{}-'.format(urlparse(url).hostname or 'any')