{
    "file_diffs": [
        {
            "old_file_content": "# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-\n# ex: set sts=4 ts=4 sw=4 noet:\n# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n#\n#   See COPYING file distributed along with the datalad package for the\n#   copyright and license terms.\n#\n# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n\nimport logging\nlgr = logging.getLogger('datalad.network')\n\nlgr.log(5, \"Importing support.network\")\nimport calendar\nimport email.utils\nimport os\nimport pickle\nimport re\nimport sys\nimport time\nimport iso8601\n\nfrom hashlib import md5\nfrom collections import OrderedDict\nfrom os.path import abspath, isabs\nfrom os.path import join as opj\nfrom os.path import dirname\nfrom ntpath import splitdrive as win_splitdrive\n\nfrom urllib.parse import urlsplit\nfrom urllib.request import Request\nfrom urllib.parse import unquote as urlunquote\nfrom urllib.parse import urljoin, urlparse, urlsplit, urlunparse, ParseResult\nfrom urllib.parse import parse_qsl\nfrom urllib.parse import urlencode\nfrom urllib.error import URLError\n\nfrom datalad.dochelpers import exc_str\nfrom datalad.utils import (\n    on_windows,\n    PurePath,\n    Path,\n)\nfrom datalad.utils import assure_dir, assure_bytes, assure_unicode, map_items\nfrom datalad import consts\nfrom datalad import cfg\nfrom datalad.support.cache import lru_cache\n\n# !!! Lazily import requests where needed -- needs 30ms or so\n# import requests\n\n# Change introduced in 3.7: Moved from RFC 2396 to RFC 3986 for quoting URL\n# strings. \"~\" is now included in the set of reserved characters.\n# For consistency we will provide urlquote\nif sys.version_info >= (3, 7):\n    from urllib.parse import quote as urlquote\nelse:\n    from urllib.parse import quote as _urlquote\n\n    def urlquote(url, safe='/', **kwargs):\n        safe += '~'\n        return _urlquote(url, safe=safe, **kwargs)\n\n    urlquote.__doc__ = _urlquote.__doc__ + \"\"\"\n\nThis DataLad version of the function assumes ~ to be a safe character to be\nconsistent with Python >= 3.7\n\"\"\"\n\n\ndef is_windows_path(path):\n    win_split = win_splitdrive(path)\n    # Note, that ntpath.splitdrive also deals with UNC paths. In that case\n    # the \"drive\" wouldn't be a windows drive letter followed by a colon\n    if win_split[0] and win_split[1] and \\\n            win_split[0].endswith(\":\") and len(win_split[0]) == 2:\n        # seems to be a windows path\n        return True\n    return False\n\n\ndef get_response_disposition_filename(s):\n    \"\"\"Given a string s as from HTTP Content-Disposition field in the response\n    return possibly present filename if any\n    \"\"\"\n    if not s:\n        return None\n    # If the response has Content-Disposition, try to get filename from it\n    cd = map(\n        lambda x: x.strip().split('=', 1) if '=' in x else [x.strip(), ''],\n        s.split(';')\n    )\n    # unify the key to be lower case and make it into a dict\n    cd = dict([[x[0].lower()] + x[1:] for x in cd])\n    if 'filename' in cd:\n        filename = cd['filename'].strip(\"\\\"'\")\n        return filename\n    return None\n\n\ndef get_url_disposition_filename(url, headers=None):\n    \"\"\"Get filename as possibly provided by the server in Content-Disposition\n    \"\"\"\n    if headers is None:\n        request = Request(url)\n        r = retry_urlopen(request)\n        # things are different in requests\n        if 'requests.' in str(r.__class__):\n            headers = r.headers\n        else:\n            headers = r.info()\n    else:\n        r = None\n    try:\n        return get_response_disposition_filename(headers.get('Content-Disposition', ''))\n    finally:\n        if r:\n            r.close()\n\n\ndef get_url_straight_filename(url, strip=None, allowdir=False):\n    \"\"\"Get file/dir name of the last path component of the URL\n\n    Parameters\n    ----------\n    strip: list, optional\n      If provided, listed names will not be considered and their\n      parent directory will be selected\n    allowdir: bool, optional\n      If url points to a \"directory\" (ends with /), empty string\n      would be returned unless allowdir is True, in which case the\n      name of the directory would be returned\n    \"\"\"\n    path = urlunquote(urlsplit(url).path)\n    path_parts = path.split('/')\n\n    if allowdir:\n        # strip empty ones\n        while len(path_parts) > 1 and not path_parts[-1]:\n            path_parts = path_parts[:-1]\n\n    if strip:\n        while path_parts and path_parts[-1] in strip:\n            path_parts = path_parts[:-1]\n\n    if path_parts:\n        return path_parts[-1]\n    else:\n        return None\n\n\ndef get_url_filename(url, headers=None, strip=None):\n    \"\"\"Get filename from the url, first consulting server about Content-Disposition\n    \"\"\"\n    filename = get_url_disposition_filename(url, headers)\n    if filename:\n        return filename\n    return get_url_straight_filename(url, strip=strip)\n\n\ndef get_url_response_stamp(url, response_info):\n    size, mtime = None, None\n    if 'Content-length' in response_info:\n        size = int(response_info['Content-length'])\n    if 'Last-modified' in response_info:\n        mtime = calendar.timegm(email.utils.parsedate(\n            response_info['Last-modified']))\n    return dict(size=size, mtime=mtime, url=url)\n\n\ndef get_tld(url):\n    \"\"\"Return top level domain from a url\n\n    Parameters\n    ----------\n    url : str\n    \"\"\"\n    # maybe use this instead to be safe:  https://pypi.python.org/pypi/tld\n    if not url.strip():\n        raise ValueError(\"Empty URL has no TLD\")\n    rec = urlsplit(url)\n    if not rec.netloc:\n        if not rec.scheme:\n            # There were no scheme provided thus netloc was empty -- must have been a simple 'path like'\n            return url.split('/', 1)[0]\n        else:\n            raise ValueError(\"It seems that only the scheme was provided without the net location/TLD\")\n    return rec.netloc\n\n\nfrom email.utils import parsedate_tz, mktime_tz\n\n\ndef rfc2822_to_epoch(datestr):\n    \"\"\"Given rfc2822 date/time format, return seconds since epoch\"\"\"\n    return mktime_tz(parsedate_tz(datestr))\n\n\ndef iso8601_to_epoch(datestr):\n    \"\"\"Given ISO 8601 date/time format, return in seconds since epoch\n\n    iso8601 is used to parse properly the time zone information, which\n    can't be parsed with standard datetime strptime\n    \"\"\"\n    return calendar.timegm(iso8601.parse_date(datestr).timetuple())\n\n\ndef __urlopen_requests(url):\n    # XXX Workaround for now for ... broken code\n    if isinstance(url, Request):\n        url = url.get_full_url()\n    from requests import Session\n    return Session().get(url, stream=True)\n\n\ndef retry_urlopen(url, retries=3):\n    for t in range(retries):\n        try:\n            return __urlopen_requests(url)\n        except URLError as e:\n            lgr.warning(\"Received exception while reading %s: %s\" % (url, e))\n            if t == retries - 1:\n                # if we have reached allowed number of retries -- reraise\n                raise\n\n\ndef is_url_quoted(url):\n    \"\"\"Return whether URL looks being already quoted\n    \"\"\"\n    try:\n        url_ = urlunquote(url)\n        return url != url_\n    except:  # problem with unquoting -- then it must be wasn't quoted (correctly)\n        # MIH: ValueError?\n        return False\n\n\ndef same_website(url_rec, u_rec):\n    \"\"\"Decide whether a link leads to external site\n\n    Parameters\n    ----------\n    url_rec: ParseResult\n      record for original url\n    u_rec: ParseResult\n      record for new url\n    \"\"\"\n    if isinstance(url_rec, str):\n        url_rec = urlparse(url_rec)\n    if isinstance(u_rec, str):\n        u_rec = urlparse(u_rec)\n    return (url_rec.netloc == u_rec.netloc)\n    # todo: collect more of sample cases.\n    # disabled below check while working on ratholeradio, since links\n    # could go to the parent and that is ok.  Figure out when it was\n    # desired not to go to the parent -- we might need explicit option\n    # and u_rec.path.startswith(url_rec.path)):\n\n\ndef dlurljoin(u_path, url):\n    url_rec = urlparse(url)  # probably duplicating parsing :-/ TODO\n    if url_rec.scheme:\n        # independent full url, so just return it\n        return url\n    if u_path.endswith('/'):  # should here be also a scheme use?\n        if url.startswith('/'):  # jump to the root\n            u_path_rec = urlparse(u_path)\n            return urljoin(urlunparse(\n                (u_path_rec.scheme, u_path_rec.netloc, '', '', '', '')), url)\n        else:\n            return os.path.join(u_path, url)\n    # TODO: recall where all this dirname came from and bring into the test\n    return urljoin(os.path.dirname(u_path) + '/', url)\n\n\n# TODO should it be a node maybe?\nclass SimpleURLStamper(object):\n    \"\"\"Gets a simple stamp about the URL: {url, time, size} of whatever was provided in the header\n    \"\"\"\n    def __init__(self, mode='full'):\n        self.mode = mode\n\n    def __call__(self, url):\n        # Extracted from above madness\n        # TODO: add mode alike to 'relaxed' where we would not\n        # care about content-disposition filename\n        # http://stackoverflow.com/questions/862173/how-to-download-a-file-using-python-in-a-smarter-way\n        request = Request(url)\n\n        # No traffic compression since we do not know how to identify\n        # exactly either it has to be decompressed\n        # request.add_header('Accept-encoding', 'gzip,deflate')\n        #\n        # TODO: think about stamping etc -- we seems to be redoing\n        # what git-annex does for us already... not really\n        r = retry_urlopen(request)\n        try:\n            r_info = r.info()\n            r_stamp = get_url_response_stamp(url, r_info)\n\n            return dict(mtime=r_stamp['mtime'], size=r_stamp['size'], url=url)\n        finally:\n            r.close()\n\n\n# TODO:  make it consistent/clear at what stage % encoding/decoding happens!\n# now it is a mix!\n\n#\n# Useful functionality in requests.models\n#  utils.requote_uri -- quote/unquote cycle to guarantee consistent appearance\n#  RequestEncodingMixin._encode_params -- Will successfully encode parameters when passed as a dict or a list of ...\n#  PreparedRequest().prepare_url(url, params) -- nicely cares about url encodings etc\n#\n\n@lru_cache(maxsize=100)\ndef _guess_ri_cls(ri):\n    \"\"\"Factory function which would determine which type of a ri a provided string is\"\"\"\n    TYPES = {\n        'url': URL,\n        'ssh':  SSHRI,\n        'file': PathRI,\n        'datalad': DataLadRI,\n        'git-transport': GitTransportRI,\n    }\n    if isinstance(ri, PurePath):\n        lgr.log(5, \"Detected file ri\")\n        return TYPES['file']\n    if is_windows_path(ri):\n        # OMG we got something from windows\n        lgr.log(5, \"Detected file ri\")\n        return TYPES['file']\n\n    # We assume that it is a URL and parse it. Depending on the result\n    # we might decide that it was something else ;)\n    fields = URL._pr_to_fields(urlparse(ri))\n    lgr.log(5, \"Parsed ri %s into fields %s\" % (ri, fields))\n    type_ = 'url'\n    # Special treatments\n    # file:///path should stay file:\n    if fields['scheme'] and fields['scheme'] not in {'file'} \\\n            and not fields['hostname']:\n        # transport::URL-or-path\n        if fields['path'].startswith(':'):  # there was ::\n            lgr.log(5, \"Assuming git transport style ri and returning\")\n            type_ = 'git-transport'\n        # dl+archive:... or just for ssh   hostname:path/p1\n        elif '+' not in fields['scheme']:\n            type_ = 'ssh'\n            lgr.log(5, \"Assuming ssh style ri, adjusted: %s\" % (fields,))\n\n    if not fields['scheme'] and not fields['hostname']:\n        parts = _split_colon(ri)\n        # if no illegal for username@hostname characters in the first part and\n        # we either had username@hostname or multiple :-separated parts\n        if not set(parts[0]).intersection(set('/\\\\#')) and (\n                fields['path'] and\n                '@' in fields['path'] or\n                len(parts) > 1\n        ):\n            # user@host:path/sp1\n            # or host_name: (hence parts check)\n            # TODO: we need a regex to catch those really, parts check is not suff\n            type_ = 'ssh'\n        elif ri.startswith('//'):\n            # e.g. // or ///path\n            type_ = 'datalad'\n        else:\n            type_ = 'file'\n\n    if not fields['scheme'] and fields['hostname']:\n        # e.g. //a/path\n        type_ = 'datalad'\n\n    cls = TYPES[type_]\n    # just parse the ri according to regex matchint ssh \"ri\" specs\n    lgr.log(5, \"Detected %s ri\" % type_)\n    return cls\n\n\nclass RI(object):\n    \"\"\"Resource Identifier - base class and a factory for URL, SSHRI, etc\n\n    Intended to be a R/O object (i.e. no fields should be changed in-place).\n    Subclasses define specific collections of fields they care about in _FIELDS\n    class variable.\n    The idea is that this class should help to break apart a URL, while being\n    able to rebuild itself into a string representation for reuse\n\n    `RI` could be used as factory, whenever type of the resource is unknown and\n    must be guessed from the string representation.  One of the subclasses will be\n    provided as output, e.g.\n\n    >>> RI('http://example.com')\n    URL(hostname='example.com', scheme='http')\n    >>> RI('example.com:path')\n    SSHRI(hostname='example.com', path='path')\n    \"\"\"\n\n    # All of the subclasses will provide path\n    _FIELDS = (\n        'path',\n    )\n\n    __slots__ = _FIELDS + ('_fields', '_str')\n\n    def __new__(cls, ri=None, **kwargs):\n        \"\"\"Used as a possible factory for known RI types\n\n        Returns\n        -------\n        RI\n           uninitialized RI object of appropriate class with _str\n           set to string representation if was provided\n\n        \"\"\"\n        if cls is RI and ri is not None:\n            # RI class was used as a factory\n            cls = _guess_ri_cls(ri)\n\n        if cls is RI:\n            # should we fail or just pretend we are nothing??? ;-) XXX\n            raise ValueError(\"Could not deduce RI type for %r\" % (ri,))\n\n        ri_obj = super(RI, cls).__new__(cls)\n        # Store internally original str\n        ri_obj._str = str(ri) if isinstance(ri, PurePath) else ri\n        return ri_obj\n\n    def __init__(self, ri=None, **fields):\n        \"\"\"\n        Parameters\n        ----------\n        ri: str, optional\n          String version of a resource specific for this class.  If you would like\n          a type of the resource be deduced, use RI(ri). Note that this value\n          will be passed to str(), so you do not have to cast it yourself.\n        **fields: dict, optional\n          The values for the fields defined in _FIELDS class variable.\n        \"\"\"\n        if ri and (bool(ri) == bool(fields)):\n            raise ValueError(\n                \"Specify either ri or breakdown from the fields, not both. \"\n                \"Got ri=%r, fields=%r\" % (ri, fields))\n\n        self._fields = self._get_blank_fields()\n        if ri is not None:\n            ri = str(ri)\n            fields = self._str_to_fields(ri)\n        self._set_from_fields(**fields)\n\n        # If was initialized from a string representation\n        if lgr.isEnabledFor(logging.DEBUG) and self._str is not None:\n            # well -- some ris might not unparse identically back\n            # strictly speaking, but let's assume they do\n            ri_ = self.as_str()\n            if ri != ri_:\n                lgr.debug(\"Parsed version of %s %r differs from original %r\",\n                          self.__class__.__name__, ri_, ri)\n\n    @classmethod\n    def _get_blank_fields(cls, **fields):\n        return OrderedDict(((f, fields.get(f, '')) for f in cls._FIELDS))\n\n    @property\n    def fields(self):\n        \"\"\"Returns shallow copy of fields to ease manipulations\"\"\"\n        return self._fields.copy()\n\n    def __repr__(self):\n        # since auto_repr doesn't support \"non-0\" values atm\n        return \"%s(%s)\" % (\n            self.__class__.__name__,\n            \", \".join([\"%s=%r\" % (k, v)\n                       for k, v in sorted(self._fields.items())\n                       if v]))\n\n    # Lazily evaluated if _str was not set\n    def __str__(self):\n        if self._str is None:\n            self._str = self.as_str()\n        return self._str\n\n    @classmethod\n    def from_str(cls, ri_str):\n        obj = cls(**cls._str_to_fields(ri_str))\n        obj._str = ri_str\n        return obj\n\n    @property\n    def localpath(self):\n        # by default RIs point to remote locations\n        raise ValueError(\"%s points to remote location\" % self)\n\n    # Apparently doesn't quite play nicely with multiple inheritence for MixIn'\n    # of regexp based URLs\n    #@abstractmethod\n    #@classmethod\n    #def _str_to_fields(cls, ri_str):\n    #    raise NotImplementedError\n\n    #\n    # If any field is specified, URL is not considered 'False', i.e.\n    # non-existing, although may be we could/shout omit having only\n    # scheme or port specified since it doesn't point to any useful\n    # location\n    #\n\n    def __bool__(self):\n        fields = self._fields\n        return any(fields.values())\n\n    #\n    # Helpers to deal with internal structures and conversions\n    #\n\n    def _set_from_fields(self, **fields):\n        unknown_fields = set(fields).difference(self._FIELDS)\n        if unknown_fields:\n            raise ValueError(\"Do not know about %s. Known fields for %s are: %s\"\n                             % (unknown_fields, self.__class__, self._FIELDS))\n\n        # encode dicts for query or fragment into\n        for f in {'query', 'fragment'}:\n            v = fields.get(f)\n            if isinstance(v, dict):\n\n                ev = urlencode(map_items(assure_bytes, v))\n                # / is reserved char within query\n                if f == 'fragment' and '%2F' not in str(v):\n                    # but seems to be ok'ish within the fragment which is\n                    # the last element of URI and anyways used only by the\n                    # client (i.e. by us here if used to compose the URL)\n                    # so let's return / back for clarity if there were no\n                    # awkward %2F to startswith\n                    ev = ev.replace('%2F', '/')\n                fields[f] = ev\n\n        self._fields.update(fields)\n\n    #\n    # Quick comparators\n    #\n\n    def __eq__(self, other):\n        if not isinstance(other, RI):\n            other = RI(other)\n        return isinstance(other, self.__class__) and dict(other._fields) == dict(self._fields)\n\n    def __ne__(self, other):\n        return not (self == other)\n\n    def __getattribute__(self, item):\n        if item.startswith('_') or item not in self._FIELDS:\n            return super(RI, self).__getattribute__(item)\n        else:\n            return self._fields[item]\n\n    def __setattr__(self, item, value):\n        if item.startswith('_') or item not in self._FIELDS:\n            super(RI, self).__setattr__(item, value)\n        else:\n            self._fields[item] = value\n            self._str = None\n\n\nclass URL(RI):\n    \"\"\"Universal resource locator\n\n    Although largely decorating urlparse.ParseResult, it\n    - doesn't mandate providing all parts of the URL\n    - doesn't require netloc but rather asks for separate username, password, and hostname\n    \"\"\"\n\n    _FIELDS = RI._FIELDS + (\n        'scheme',\n        'username',\n        'password',\n        'hostname', 'port',\n        'query',\n        'fragment',\n    )\n\n    def as_str(self):\n        \"\"\"Render URL as a string\"\"\"\n        return urlunparse(self.to_pr())\n\n    @classmethod\n    def _str_to_fields(cls, url_str):\n        fields = URL._pr_to_fields(urlparse(url_str))\n        fields['path'] = urlunquote(fields['path'])\n        return fields\n\n    def to_pr(self):\n        \"\"\"Convert URL to urlparse.ParseResults namedtuple\"\"\"\n        return self._fields_to_pr(self._fields)\n\n    @classmethod\n    def _fields_to_pr(cls, fields):\n        \"\"\"Recompose back fields dict to ParseResult\"\"\"\n        netloc = fields['username'] or ''\n        if fields['password']:\n            netloc += ':' + fields['password']\n        if netloc:\n            netloc += '@'\n        netloc += fields['hostname']\n        if fields['port']:\n            if fields['hostname'].count(':') >= 2:\n                # ipv6 -- need to enclose in []\n                netloc = '[%s]:%s' % (netloc, fields['port'])\n            else:\n                netloc += ':%s' % fields['port']\n\n        pr_fields = {\n            f: fields[f]\n            for f in cls._FIELDS\n            if f not in ('hostname', 'password', 'username', 'port')\n        }\n        pr_fields['netloc'] = netloc\n        pr_fields['params'] = ''\n        # We need to quote the path\n        pr_fields['path'] = urlquote(pr_fields['path'])\n        # TODO: figure out what to do with query/fragment... one step at a time\n        return ParseResult(**pr_fields)\n\n    @classmethod\n    def _pr_to_fields(cls, pr):\n        \"\"\"ParseResult is a tuple so immutable, which complicates adjusting it\n\n        This function converts ParseResult into dict\"\"\"\n\n        if pr.params:\n            lgr.warning(\"ParseResults contains params %r, which will be ignored\"\n                        % (pr.params,))\n\n        hostname_port = pr.netloc.split('@')[-1]\n        is_ipv6 = hostname_port.count(':') >= 2\n        # can't use just pr._asdict since we care to ask those properties\n        # such as .port , .hostname etc\n        # Forcing '' instead of None since those properties (.hostname), .password,\n        # .username return None if not available and we decided to uniformize\n        if is_ipv6:\n            rem = re.match(r'\\[(?P<hostname>.*)\\]:(?P<port>\\d+)', hostname_port)\n            if rem:\n                hostname, port = rem.groups()\n                port = int(port)\n            else:\n                hostname, port = hostname_port, ''\n\n            def _getattr(pr, f):\n                \"\"\"Helper for custom handling in case of ipv6 addresses which blows\n                stock ParseResults logic\"\"\"\n                if f == 'port':\n                    # for now not supported at all, so\n                    return port\n                elif f == 'hostname':\n                    return hostname\n                else:\n                    return getattr(pr, f)\n        else:\n            _getattr = getattr\n\n        return {f: (_getattr(pr, f) or '') for f in cls._FIELDS}\n\n    #\n    # Access helpers\n    #\n\n    def _parse_qs(self, s, auto_delist=True):\n        \"\"\"Helper around parse_qs to strip unneeded 'list'ing etc and return a dict of key=values\"\"\"\n        if not s:\n            return {}\n        out = map_items(assure_unicode, OrderedDict(parse_qsl(s, 1)))\n        if not auto_delist:\n            return out\n        for k in out:\n            v = out[k]\n            if isinstance(v, list) and len(v) == 1:\n                v = v[0]\n                out[k] = None if v == '' else v\n        return out\n\n    @property\n    def query_dict(self):\n        return self._parse_qs(self.query)\n\n    @property\n    def fragment_dict(self):\n        return self._parse_qs(self.fragment)\n\n    @property\n    def localpath(self):\n        if self.scheme != 'file':\n            raise ValueError(\n                \"Non 'file://' URL cannot be resolved to a local path\")\n        hostname = self.hostname\n        if not (hostname in (None, '', 'localhost', '::1')\n                or hostname.startswith('127.')):\n            raise ValueError(\"file:// URL does not point to 'localhost'\")\n        return self.path\n\n\nclass PathRI(RI):\n    \"\"\"RI pointing to a (local) file/directory\"\"\"\n    def as_str(self):\n        return self.path\n\n    @classmethod\n    def _str_to_fields(cls, url_str):\n        # str() to be compatible with pathlib objects\n        return dict(path=str(url_str))\n\n    @property\n    def localpath(self):\n        return self.path\n\n    @property\n    def posixpath(self):\n        if is_windows_path(self.path):\n            win_split = win_splitdrive(self.path)\n            return \"/\" + win_split[0][0] + win_split[1].replace('\\\\', '/')\n        else:\n            return self.path\n\n\nclass RegexBasedURLMixin(object):\n    \"\"\"Base class for URLs which we could simple parse using regular expressions\"\"\"\n\n    _REGEX = None\n\n    # not used ATM but possible ;)\n    # @classmethod\n    # def is_str_matches(cls, url_str):\n    #     return bool(cls._REGEX.match(url_str))\n\n    @classmethod\n    def _str_to_fields(cls, url_str):\n        re_match = cls._REGEX.match(url_str)\n        if not re_match:\n            # TODO: custom error?\n            raise ValueError(\n                \"Cannot handle URL '%s': categorized as %r, but does not match syntax.%s\"\n                % (url_str,\n                   cls,\n                   \" Did you intent to use '///'?\" if url_str.startswith('//') else '')\n            )\n        fields = cls._get_blank_fields()\n        fields.update({k: v for k, v in re_match.groupdict().items() if v})\n        cls._normalize_fields(fields)\n        return fields\n\n    @classmethod\n    def _normalize_fields(self, fields):\n        \"\"\"Helper to be ran if any of the fields need to be normalized after parsing\"\"\"\n        pass\n\n\nclass SSHRI(RI, RegexBasedURLMixin):\n    \"\"\"RI pointing to a remote location reachable via SSH\"\"\"\n\n    _FIELDS = RI._FIELDS + (\n        'username',\n        'hostname',\n        'port',\n    )\n\n    _REGEX = re.compile(r'((?P<username>\\S*)@)?(?P<hostname>[^#/\\\\:]+)(\\:(?P<path>.*))?$')\n\n    @classmethod\n    def _normalize_fields(cls, fields):\n        if fields['path'] and fields['path'].startswith('//'):\n            # Let's normalize for now to avoid multiple leading slashes\n            fields['path'] = '/' + fields['path'].lstrip('/')\n        # escape path so we have direct representation of the path to work with\n        fields['path'] = unescape_ssh_path(fields['path'])\n\n    def as_str(self, escape=False):\n        fields = self.fields  # copy so we could escape symbols\n        url_fmt = '{hostname}'\n        if fields['username']:\n            url_fmt = \"{username}@\" + url_fmt\n        if fields['path']:\n            url_fmt += ':{path}'\n        if escape:\n            fields['path'] = escape_ssh_path(fields['path'])\n        return url_fmt.format(**fields)\n\n    # TODO:\n    # we can \"support\" localhost:path as localpaths\n\n\nclass DataLadRI(RI, RegexBasedURLMixin):\n    \"\"\"RI pointing to datasets within default DataLad super-dataset\"\"\"\n\n    _FIELDS = RI._FIELDS + (\n        'remote',\n    )\n\n    # For now or forever we don't deal with any fragments or other special stuff\n    _REGEX = re.compile(r'//(?P<remote>[^\\s/]*)/(?P<path>.*)$')\n\n    # do they need to be normalized??? loosing track ...\n\n    def as_str(self):\n        return \"//{remote}/{path}\".format(**self._fields)\n\n    def as_git_url(self):\n        \"\"\"Dereference /// into original URLs which could be used by git for cloning\n\n        Returns\n        -------\n        str\n          URL string to reference the DataLadRI from its /// form\n        \"\"\"\n        if self.remote:\n            raise NotImplementedError(\"not supported ATM to reference additional remotes\")\n        return \"{}{}\".format(consts.DATASETS_TOPURL, urlquote(self.path))\n\n\nclass GitTransportRI(RI, RegexBasedURLMixin):\n    \"\"\"RI for some other RI with git transport prefix\"\"\"\n\n    # TODO: check how crticial to \"inherit\" RI._FIELDS asking to provide path\n    _FIELDS = RI._FIELDS + (\n        'transport',\n        'RI',\n    )\n\n    # Due to poor design, `ri` argument already present in various\n    # places intermixed with **kwargs treatment. So we will use RI\n    # here instead of ri.\n    _REGEX = re.compile(r'(?P<transport>[A-Za-z0-9][A-Za-z0-9+.-]*)::(?P<RI>.*)$')\n\n    def as_str(self):\n        return '{self.transport}::{self.RI}'.format(self=self)\n\n\ndef _split_colon(s, maxsplit=1):\n    \"\"\"Split on unescaped colon\"\"\"\n    return re.compile(r'(?<!\\\\):').split(s, maxsplit=maxsplit)\n\n# \\ should be first to deal with\n_SSH_ESCAPED_CHARACTERS = '\\\\#&;`|*?~<>^()[]{}$\\'\" '\n\n\n# TODO: RF using re.sub\ndef escape_ssh_path(path):\n    \"\"\"Escape all special characters present in the path\"\"\"\n    for c in _SSH_ESCAPED_CHARACTERS:\n        if c in path:\n            path = path.replace(c, '\\\\' + c)\n    return path\n\n\ndef unescape_ssh_path(path):\n    \"\"\"Un-escape all special characters present in the path\"\"\"\n    for c in _SSH_ESCAPED_CHARACTERS[::-1]:\n        if c in path:\n            path = path.replace('\\\\' + c, c)\n    return path\n\n\ndef parse_url_opts(url):\n    \"\"\"Given a string with url-style query, split into content before # and options as dict\"\"\"\n    url = URL(url)\n    # we need to filter out query and fragment to get the base url\n    fields = url.fields\n    fields.pop('query')\n    fields.pop('fragment')\n    opts = url.query_dict\n    return str(URL(**fields)), opts\n\n\n# TODO: should we just define URL.good_for_git or smth like that? ;)\n# although git also understands regular paths\ndef is_url(ri):\n    \"\"\"Returns whether argument is a resource identifier what datalad should treat as a URL\n\n    This includes ssh \"urls\" which git understands.\n\n    Parameters\n    ----------\n    ri : str or RI\n      The resource identifier (as a string or RI) to \"analyze\"\n    \"\"\"\n    if not isinstance(ri, RI):\n        try:\n            ri = RI(ri)\n        except:  # MIH: MemoryError?\n            return False\n    return isinstance(ri, (URL, SSHRI))\n\n\n# TODO: RF to remove duplication\ndef is_datalad_compat_ri(ri):\n    \"\"\"Returns whether argument is a resource identifier what datalad should treat as a URL\n\n    including its own DataLadRI\n    \"\"\"\n    if not isinstance(ri, RI):\n        try:\n            ri = RI(ri)\n        except:  # MIH: MemoryError?\n            return False\n    return isinstance(ri, (URL, SSHRI, DataLadRI))\n\n\n# TODO: better name? additionally may be move to SSHRI.is_valid() or sth.\ndef is_ssh(ri):\n    \"\"\"helper to determine, whether `ri` requires an SSH connection\n\n    Parameters\n    ----------\n    ri: str or RI\n\n    Returns\n    -------\n    bool\n    \"\"\"\n\n    # not exactly fitting the doc, but we actually can deal not necessarily with\n    # string or RI only, but with everything RI itself can deal with:\n    _ri = RI(ri) if not isinstance(ri, RI) else ri\n\n    return isinstance(_ri, SSHRI) \\\n        or (isinstance(_ri, URL) and _ri.scheme == 'ssh')\n\n\ndef get_local_file_url(fname, compatibility='git-annex'):\n    \"\"\"Return OS specific URL pointing to a local file\n\n    Parameters\n    ----------\n    fname : string\n        Filename.  If not absolute, abspath is used\n    compatibility : {'git', 'git-annex'}, optional\n        On Windows, and only on that platform, file:// URLs may need to look\n        different depending on the use case or consuming application. This\n        switch selects different compatibility modes: 'git' for use with\n        Git commands (e.g. `clone` or `submodule add`); 'git-annex` for\n        Git-annex command input (e.g. `addurl`). On any other platform this\n        setting has no effect.\n    \"\"\"\n    # we resolve the path to circumwent potential short paths on unfortunate\n    # platforms that would ruin the URL format.\n    #path = Path(fname).resolve().absolute()\n    path = Path(fname).resolve()\n    if on_windows:\n        # in addition we need to absolute(), as on windows resolve() doesn't\n        # imply that\n        path = path.absolute().as_posix()\n        furl = 'file://{}{}'.format(\n            '/' if compatibility == 'git' else '',\n            urlquote(\n                re.sub(r'([a-zA-Z]):', r'\\1', path)\n            ))\n    else:\n        # TODO:  need to fix for all the encoding etc\n        furl = str(URL(scheme='file', path=str(path)))\n    return furl\n\n\ndef get_url_cache_filename(url, name=None):\n    \"\"\"Return a filename where to cache online doc from a url\"\"\"\n    if not name:\n        name = \"misc\"\n    cache_dir = opj(cfg.obtain('datalad.locations.cache'), name)\n    doc_fname = opj(\n        cache_dir,\n        '{}-{}.p{}'.format(\n            urlsplit(url).netloc,\n            md5(url.encode('utf-8')).hexdigest(),\n            pickle.HIGHEST_PROTOCOL)\n    )\n    return doc_fname\n\n\ndef get_cached_url_content(url, name=None, fetcher=None, maxage=None):\n    \"\"\"Loader of a document from a url, which caches loaded instance on disk\n\n    Doesn't do anything smart about http headers etc which could provide\n    information for cache/proxy servers for how long to retain etc\n\n    TODO: theoretically it is not network specific at all -- and just a memoize\n    pattern, but may be some time we would make it treat headers etc correctly.\n    And ATM would support any URL we support via providers/downloaders\n\n    Parameters\n    ----------\n    fetcher: callable, optional\n       Function to call with url if needed to be refetched\n    maxage: float, optional\n       Age in days to retain valid for.  <0 - would retain forever.  If None -\n       would consult the config, 0 - would force to reload\n    \"\"\"\n    doc_fname = get_url_cache_filename(url, name)\n    if maxage is None:\n        maxage = float(cfg.get('datalad.locations.cache-maxage'))\n\n    doc = None\n    if os.path.exists(doc_fname) and maxage != 0:\n\n        fage = (time.time() - os.stat(doc_fname).st_mtime)/(24. * 3600)\n        if maxage < 0 or fage < maxage:\n            try:\n                lgr.debug(\"use cached request result to '%s' from %s\", url, doc_fname)\n                doc = pickle.load(open(doc_fname, 'rb'))\n            except Exception as e:  # it is OK to ignore any error and fall back on the true source\n                lgr.warning(\n                    \"cannot load cache from '%s', fall back to download: %s\",\n                    doc_fname, exc_str(e))\n\n    if doc is None:\n        if fetcher is None:\n            from datalad.downloaders.providers import Providers\n            providers = Providers.from_config_files()\n            fetcher = providers.fetch\n\n        doc = fetcher(url)\n        assure_dir(dirname(doc_fname))\n        # use pickle to store the entire request result dict\n        pickle.dump(doc, open(doc_fname, 'wb'))\n        lgr.debug(\"stored result of request to '{}' in {}\".format(url, doc_fname))\n    return doc\n\nlgr.log(5, \"Done importing support.network\")\n",
            "new_file_content": "# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-\n# ex: set sts=4 ts=4 sw=4 noet:\n# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n#\n#   See COPYING file distributed along with the datalad package for the\n#   copyright and license terms.\n#\n# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n\nimport logging\nlgr = logging.getLogger('datalad.network')\n\nlgr.log(5, \"Importing support.network\")\nimport calendar\nimport email.utils\nimport os\nimport pickle\nimport re\nimport sys\nimport time\nimport iso8601\n\nfrom hashlib import md5\nfrom collections import OrderedDict\nfrom os.path import abspath, isabs\nfrom os.path import join as opj\nfrom os.path import dirname\nfrom ntpath import splitdrive as win_splitdrive\n\nfrom urllib.parse import urlsplit\nfrom urllib.request import Request\nfrom urllib.parse import unquote as urlunquote\nfrom urllib.parse import urljoin, urlparse, urlsplit, urlunparse, ParseResult\nfrom urllib.parse import parse_qsl\nfrom urllib.parse import urlencode\nfrom urllib.error import URLError\n\nfrom datalad.dochelpers import exc_str\nfrom datalad.utils import (\n    on_windows,\n    PurePath,\n    Path,\n)\nfrom datalad.utils import assure_dir, assure_bytes, assure_unicode, map_items\nfrom datalad import consts\nfrom datalad import cfg\nfrom datalad.support.cache import lru_cache\n\n# !!! Lazily import requests where needed -- needs 30ms or so\n# import requests\n\n# Change introduced in 3.7: Moved from RFC 2396 to RFC 3986 for quoting URL\n# strings. \"~\" is now included in the set of reserved characters.\n# For consistency we will provide urlquote\nif sys.version_info >= (3, 7):\n    from urllib.parse import quote as urlquote\nelse:\n    from urllib.parse import quote as _urlquote\n\n    def urlquote(url, safe='/', **kwargs):\n        safe += '~'\n        return _urlquote(url, safe=safe, **kwargs)\n\n    urlquote.__doc__ = _urlquote.__doc__ + \"\"\"\n\nThis DataLad version of the function assumes ~ to be a safe character to be\nconsistent with Python >= 3.7\n\"\"\"\n\n\ndef is_windows_path(path):\n    win_split = win_splitdrive(path)\n    # Note, that ntpath.splitdrive also deals with UNC paths. In that case\n    # the \"drive\" wouldn't be a windows drive letter followed by a colon\n    if win_split[0] and win_split[1] and \\\n            win_split[0].endswith(\":\") and len(win_split[0]) == 2:\n        # seems to be a windows path\n        return True\n    return False\n\n\ndef get_response_disposition_filename(s):\n    \"\"\"Given a string s as from HTTP Content-Disposition field in the response\n    return possibly present filename if any\n    \"\"\"\n    if not s:\n        return None\n    # If the response has Content-Disposition, try to get filename from it\n    cd = map(\n        lambda x: x.strip().split('=', 1) if '=' in x else [x.strip(), ''],\n        s.split(';')\n    )\n    # unify the key to be lower case and make it into a dict\n    cd = dict([[x[0].lower()] + x[1:] for x in cd])\n    if 'filename' in cd:\n        filename = cd['filename'].strip(\"\\\"'\")\n        return filename\n    return None\n\n\ndef get_url_disposition_filename(url, headers=None):\n    \"\"\"Get filename as possibly provided by the server in Content-Disposition\n    \"\"\"\n    if headers is None:\n        request = Request(url)\n        r = retry_urlopen(request)\n        # things are different in requests\n        if 'requests.' in str(r.__class__):\n            headers = r.headers\n        else:\n            headers = r.info()\n    else:\n        r = None\n    try:\n        return get_response_disposition_filename(headers.get('Content-Disposition', ''))\n    finally:\n        if r:\n            r.close()\n\n\ndef get_url_straight_filename(url, strip=None, allowdir=False):\n    \"\"\"Get file/dir name of the last path component of the URL\n\n    Parameters\n    ----------\n    strip: list, optional\n      If provided, listed names will not be considered and their\n      parent directory will be selected\n    allowdir: bool, optional\n      If url points to a \"directory\" (ends with /), empty string\n      would be returned unless allowdir is True, in which case the\n      name of the directory would be returned\n    \"\"\"\n    path = urlunquote(urlsplit(url).path)\n    path_parts = path.split('/')\n\n    if allowdir:\n        # strip empty ones\n        while len(path_parts) > 1 and not path_parts[-1]:\n            path_parts = path_parts[:-1]\n\n    if strip:\n        while path_parts and path_parts[-1] in strip:\n            path_parts = path_parts[:-1]\n\n    if path_parts:\n        return path_parts[-1]\n    else:\n        return None\n\n\ndef get_url_filename(url, headers=None, strip=None):\n    \"\"\"Get filename from the url, first consulting server about Content-Disposition\n    \"\"\"\n    filename = get_url_disposition_filename(url, headers)\n    if filename:\n        return filename\n    return get_url_straight_filename(url, strip=strip)\n\n\ndef get_url_response_stamp(url, response_info):\n    size, mtime = None, None\n    if 'Content-length' in response_info:\n        size = int(response_info['Content-length'])\n    if 'Last-modified' in response_info:\n        mtime = calendar.timegm(email.utils.parsedate(\n            response_info['Last-modified']))\n    return dict(size=size, mtime=mtime, url=url)\n\n\ndef get_tld(url):\n    \"\"\"Return top level domain from a url\n\n    Parameters\n    ----------\n    url : str\n    \"\"\"\n    # maybe use this instead to be safe:  https://pypi.python.org/pypi/tld\n    if not url.strip():\n        raise ValueError(\"Empty URL has no TLD\")\n    rec = urlsplit(url)\n    if not rec.netloc:\n        if not rec.scheme:\n            # There were no scheme provided thus netloc was empty -- must have been a simple 'path like'\n            return url.split('/', 1)[0]\n        else:\n            raise ValueError(\"It seems that only the scheme was provided without the net location/TLD\")\n    return rec.netloc\n\n\nfrom email.utils import parsedate_tz, mktime_tz\n\n\ndef rfc2822_to_epoch(datestr):\n    \"\"\"Given rfc2822 date/time format, return seconds since epoch\"\"\"\n    return mktime_tz(parsedate_tz(datestr))\n\n\ndef iso8601_to_epoch(datestr):\n    \"\"\"Given ISO 8601 date/time format, return in seconds since epoch\n\n    iso8601 is used to parse properly the time zone information, which\n    can't be parsed with standard datetime strptime\n    \"\"\"\n    return calendar.timegm(iso8601.parse_date(datestr).utctimetuple())\n\n\ndef __urlopen_requests(url):\n    # XXX Workaround for now for ... broken code\n    if isinstance(url, Request):\n        url = url.get_full_url()\n    from requests import Session\n    return Session().get(url, stream=True)\n\n\ndef retry_urlopen(url, retries=3):\n    for t in range(retries):\n        try:\n            return __urlopen_requests(url)\n        except URLError as e:\n            lgr.warning(\"Received exception while reading %s: %s\" % (url, e))\n            if t == retries - 1:\n                # if we have reached allowed number of retries -- reraise\n                raise\n\n\ndef is_url_quoted(url):\n    \"\"\"Return whether URL looks being already quoted\n    \"\"\"\n    try:\n        url_ = urlunquote(url)\n        return url != url_\n    except:  # problem with unquoting -- then it must be wasn't quoted (correctly)\n        # MIH: ValueError?\n        return False\n\n\ndef same_website(url_rec, u_rec):\n    \"\"\"Decide whether a link leads to external site\n\n    Parameters\n    ----------\n    url_rec: ParseResult\n      record for original url\n    u_rec: ParseResult\n      record for new url\n    \"\"\"\n    if isinstance(url_rec, str):\n        url_rec = urlparse(url_rec)\n    if isinstance(u_rec, str):\n        u_rec = urlparse(u_rec)\n    return (url_rec.netloc == u_rec.netloc)\n    # todo: collect more of sample cases.\n    # disabled below check while working on ratholeradio, since links\n    # could go to the parent and that is ok.  Figure out when it was\n    # desired not to go to the parent -- we might need explicit option\n    # and u_rec.path.startswith(url_rec.path)):\n\n\ndef dlurljoin(u_path, url):\n    url_rec = urlparse(url)  # probably duplicating parsing :-/ TODO\n    if url_rec.scheme:\n        # independent full url, so just return it\n        return url\n    if u_path.endswith('/'):  # should here be also a scheme use?\n        if url.startswith('/'):  # jump to the root\n            u_path_rec = urlparse(u_path)\n            return urljoin(urlunparse(\n                (u_path_rec.scheme, u_path_rec.netloc, '', '', '', '')), url)\n        else:\n            return os.path.join(u_path, url)\n    # TODO: recall where all this dirname came from and bring into the test\n    return urljoin(os.path.dirname(u_path) + '/', url)\n\n\n# TODO should it be a node maybe?\nclass SimpleURLStamper(object):\n    \"\"\"Gets a simple stamp about the URL: {url, time, size} of whatever was provided in the header\n    \"\"\"\n    def __init__(self, mode='full'):\n        self.mode = mode\n\n    def __call__(self, url):\n        # Extracted from above madness\n        # TODO: add mode alike to 'relaxed' where we would not\n        # care about content-disposition filename\n        # http://stackoverflow.com/questions/862173/how-to-download-a-file-using-python-in-a-smarter-way\n        request = Request(url)\n\n        # No traffic compression since we do not know how to identify\n        # exactly either it has to be decompressed\n        # request.add_header('Accept-encoding', 'gzip,deflate')\n        #\n        # TODO: think about stamping etc -- we seems to be redoing\n        # what git-annex does for us already... not really\n        r = retry_urlopen(request)\n        try:\n            r_info = r.info()\n            r_stamp = get_url_response_stamp(url, r_info)\n\n            return dict(mtime=r_stamp['mtime'], size=r_stamp['size'], url=url)\n        finally:\n            r.close()\n\n\n# TODO:  make it consistent/clear at what stage % encoding/decoding happens!\n# now it is a mix!\n\n#\n# Useful functionality in requests.models\n#  utils.requote_uri -- quote/unquote cycle to guarantee consistent appearance\n#  RequestEncodingMixin._encode_params -- Will successfully encode parameters when passed as a dict or a list of ...\n#  PreparedRequest().prepare_url(url, params) -- nicely cares about url encodings etc\n#\n\n@lru_cache(maxsize=100)\ndef _guess_ri_cls(ri):\n    \"\"\"Factory function which would determine which type of a ri a provided string is\"\"\"\n    TYPES = {\n        'url': URL,\n        'ssh':  SSHRI,\n        'file': PathRI,\n        'datalad': DataLadRI,\n        'git-transport': GitTransportRI,\n    }\n    if isinstance(ri, PurePath):\n        lgr.log(5, \"Detected file ri\")\n        return TYPES['file']\n    if is_windows_path(ri):\n        # OMG we got something from windows\n        lgr.log(5, \"Detected file ri\")\n        return TYPES['file']\n\n    # We assume that it is a URL and parse it. Depending on the result\n    # we might decide that it was something else ;)\n    fields = URL._pr_to_fields(urlparse(ri))\n    lgr.log(5, \"Parsed ri %s into fields %s\" % (ri, fields))\n    type_ = 'url'\n    # Special treatments\n    # file:///path should stay file:\n    if fields['scheme'] and fields['scheme'] not in {'file'} \\\n            and not fields['hostname']:\n        # transport::URL-or-path\n        if fields['path'].startswith(':'):  # there was ::\n            lgr.log(5, \"Assuming git transport style ri and returning\")\n            type_ = 'git-transport'\n        # dl+archive:... or just for ssh   hostname:path/p1\n        elif '+' not in fields['scheme']:\n            type_ = 'ssh'\n            lgr.log(5, \"Assuming ssh style ri, adjusted: %s\" % (fields,))\n\n    if not fields['scheme'] and not fields['hostname']:\n        parts = _split_colon(ri)\n        # if no illegal for username@hostname characters in the first part and\n        # we either had username@hostname or multiple :-separated parts\n        if not set(parts[0]).intersection(set('/\\\\#')) and (\n                fields['path'] and\n                '@' in fields['path'] or\n                len(parts) > 1\n        ):\n            # user@host:path/sp1\n            # or host_name: (hence parts check)\n            # TODO: we need a regex to catch those really, parts check is not suff\n            type_ = 'ssh'\n        elif ri.startswith('//'):\n            # e.g. // or ///path\n            type_ = 'datalad'\n        else:\n            type_ = 'file'\n\n    if not fields['scheme'] and fields['hostname']:\n        # e.g. //a/path\n        type_ = 'datalad'\n\n    cls = TYPES[type_]\n    # just parse the ri according to regex matchint ssh \"ri\" specs\n    lgr.log(5, \"Detected %s ri\" % type_)\n    return cls\n\n\nclass RI(object):\n    \"\"\"Resource Identifier - base class and a factory for URL, SSHRI, etc\n\n    Intended to be a R/O object (i.e. no fields should be changed in-place).\n    Subclasses define specific collections of fields they care about in _FIELDS\n    class variable.\n    The idea is that this class should help to break apart a URL, while being\n    able to rebuild itself into a string representation for reuse\n\n    `RI` could be used as factory, whenever type of the resource is unknown and\n    must be guessed from the string representation.  One of the subclasses will be\n    provided as output, e.g.\n\n    >>> RI('http://example.com')\n    URL(hostname='example.com', scheme='http')\n    >>> RI('example.com:path')\n    SSHRI(hostname='example.com', path='path')\n    \"\"\"\n\n    # All of the subclasses will provide path\n    _FIELDS = (\n        'path',\n    )\n\n    __slots__ = _FIELDS + ('_fields', '_str')\n\n    def __new__(cls, ri=None, **kwargs):\n        \"\"\"Used as a possible factory for known RI types\n\n        Returns\n        -------\n        RI\n           uninitialized RI object of appropriate class with _str\n           set to string representation if was provided\n\n        \"\"\"\n        if cls is RI and ri is not None:\n            # RI class was used as a factory\n            cls = _guess_ri_cls(ri)\n\n        if cls is RI:\n            # should we fail or just pretend we are nothing??? ;-) XXX\n            raise ValueError(\"Could not deduce RI type for %r\" % (ri,))\n\n        ri_obj = super(RI, cls).__new__(cls)\n        # Store internally original str\n        ri_obj._str = str(ri) if isinstance(ri, PurePath) else ri\n        return ri_obj\n\n    def __init__(self, ri=None, **fields):\n        \"\"\"\n        Parameters\n        ----------\n        ri: str, optional\n          String version of a resource specific for this class.  If you would like\n          a type of the resource be deduced, use RI(ri). Note that this value\n          will be passed to str(), so you do not have to cast it yourself.\n        **fields: dict, optional\n          The values for the fields defined in _FIELDS class variable.\n        \"\"\"\n        if ri and (bool(ri) == bool(fields)):\n            raise ValueError(\n                \"Specify either ri or breakdown from the fields, not both. \"\n                \"Got ri=%r, fields=%r\" % (ri, fields))\n\n        self._fields = self._get_blank_fields()\n        if ri is not None:\n            ri = str(ri)\n            fields = self._str_to_fields(ri)\n        self._set_from_fields(**fields)\n\n        # If was initialized from a string representation\n        if lgr.isEnabledFor(logging.DEBUG) and self._str is not None:\n            # well -- some ris might not unparse identically back\n            # strictly speaking, but let's assume they do\n            ri_ = self.as_str()\n            if ri != ri_:\n                lgr.debug(\"Parsed version of %s %r differs from original %r\",\n                          self.__class__.__name__, ri_, ri)\n\n    @classmethod\n    def _get_blank_fields(cls, **fields):\n        return OrderedDict(((f, fields.get(f, '')) for f in cls._FIELDS))\n\n    @property\n    def fields(self):\n        \"\"\"Returns shallow copy of fields to ease manipulations\"\"\"\n        return self._fields.copy()\n\n    def __repr__(self):\n        # since auto_repr doesn't support \"non-0\" values atm\n        return \"%s(%s)\" % (\n            self.__class__.__name__,\n            \", \".join([\"%s=%r\" % (k, v)\n                       for k, v in sorted(self._fields.items())\n                       if v]))\n\n    # Lazily evaluated if _str was not set\n    def __str__(self):\n        if self._str is None:\n            self._str = self.as_str()\n        return self._str\n\n    @classmethod\n    def from_str(cls, ri_str):\n        obj = cls(**cls._str_to_fields(ri_str))\n        obj._str = ri_str\n        return obj\n\n    @property\n    def localpath(self):\n        # by default RIs point to remote locations\n        raise ValueError(\"%s points to remote location\" % self)\n\n    # Apparently doesn't quite play nicely with multiple inheritence for MixIn'\n    # of regexp based URLs\n    #@abstractmethod\n    #@classmethod\n    #def _str_to_fields(cls, ri_str):\n    #    raise NotImplementedError\n\n    #\n    # If any field is specified, URL is not considered 'False', i.e.\n    # non-existing, although may be we could/shout omit having only\n    # scheme or port specified since it doesn't point to any useful\n    # location\n    #\n\n    def __bool__(self):\n        fields = self._fields\n        return any(fields.values())\n\n    #\n    # Helpers to deal with internal structures and conversions\n    #\n\n    def _set_from_fields(self, **fields):\n        unknown_fields = set(fields).difference(self._FIELDS)\n        if unknown_fields:\n            raise ValueError(\"Do not know about %s. Known fields for %s are: %s\"\n                             % (unknown_fields, self.__class__, self._FIELDS))\n\n        # encode dicts for query or fragment into\n        for f in {'query', 'fragment'}:\n            v = fields.get(f)\n            if isinstance(v, dict):\n\n                ev = urlencode(map_items(assure_bytes, v))\n                # / is reserved char within query\n                if f == 'fragment' and '%2F' not in str(v):\n                    # but seems to be ok'ish within the fragment which is\n                    # the last element of URI and anyways used only by the\n                    # client (i.e. by us here if used to compose the URL)\n                    # so let's return / back for clarity if there were no\n                    # awkward %2F to startswith\n                    ev = ev.replace('%2F', '/')\n                fields[f] = ev\n\n        self._fields.update(fields)\n\n    #\n    # Quick comparators\n    #\n\n    def __eq__(self, other):\n        if not isinstance(other, RI):\n            other = RI(other)\n        return isinstance(other, self.__class__) and dict(other._fields) == dict(self._fields)\n\n    def __ne__(self, other):\n        return not (self == other)\n\n    def __getattribute__(self, item):\n        if item.startswith('_') or item not in self._FIELDS:\n            return super(RI, self).__getattribute__(item)\n        else:\n            return self._fields[item]\n\n    def __setattr__(self, item, value):\n        if item.startswith('_') or item not in self._FIELDS:\n            super(RI, self).__setattr__(item, value)\n        else:\n            self._fields[item] = value\n            self._str = None\n\n\nclass URL(RI):\n    \"\"\"Universal resource locator\n\n    Although largely decorating urlparse.ParseResult, it\n    - doesn't mandate providing all parts of the URL\n    - doesn't require netloc but rather asks for separate username, password, and hostname\n    \"\"\"\n\n    _FIELDS = RI._FIELDS + (\n        'scheme',\n        'username',\n        'password',\n        'hostname', 'port',\n        'query',\n        'fragment',\n    )\n\n    def as_str(self):\n        \"\"\"Render URL as a string\"\"\"\n        return urlunparse(self.to_pr())\n\n    @classmethod\n    def _str_to_fields(cls, url_str):\n        fields = URL._pr_to_fields(urlparse(url_str))\n        fields['path'] = urlunquote(fields['path'])\n        return fields\n\n    def to_pr(self):\n        \"\"\"Convert URL to urlparse.ParseResults namedtuple\"\"\"\n        return self._fields_to_pr(self._fields)\n\n    @classmethod\n    def _fields_to_pr(cls, fields):\n        \"\"\"Recompose back fields dict to ParseResult\"\"\"\n        netloc = fields['username'] or ''\n        if fields['password']:\n            netloc += ':' + fields['password']\n        if netloc:\n            netloc += '@'\n        netloc += fields['hostname']\n        if fields['port']:\n            if fields['hostname'].count(':') >= 2:\n                # ipv6 -- need to enclose in []\n                netloc = '[%s]:%s' % (netloc, fields['port'])\n            else:\n                netloc += ':%s' % fields['port']\n\n        pr_fields = {\n            f: fields[f]\n            for f in cls._FIELDS\n            if f not in ('hostname', 'password', 'username', 'port')\n        }\n        pr_fields['netloc'] = netloc\n        pr_fields['params'] = ''\n        # We need to quote the path\n        pr_fields['path'] = urlquote(pr_fields['path'])\n        # TODO: figure out what to do with query/fragment... one step at a time\n        return ParseResult(**pr_fields)\n\n    @classmethod\n    def _pr_to_fields(cls, pr):\n        \"\"\"ParseResult is a tuple so immutable, which complicates adjusting it\n\n        This function converts ParseResult into dict\"\"\"\n\n        if pr.params:\n            lgr.warning(\"ParseResults contains params %r, which will be ignored\"\n                        % (pr.params,))\n\n        hostname_port = pr.netloc.split('@')[-1]\n        is_ipv6 = hostname_port.count(':') >= 2\n        # can't use just pr._asdict since we care to ask those properties\n        # such as .port , .hostname etc\n        # Forcing '' instead of None since those properties (.hostname), .password,\n        # .username return None if not available and we decided to uniformize\n        if is_ipv6:\n            rem = re.match(r'\\[(?P<hostname>.*)\\]:(?P<port>\\d+)', hostname_port)\n            if rem:\n                hostname, port = rem.groups()\n                port = int(port)\n            else:\n                hostname, port = hostname_port, ''\n\n            def _getattr(pr, f):\n                \"\"\"Helper for custom handling in case of ipv6 addresses which blows\n                stock ParseResults logic\"\"\"\n                if f == 'port':\n                    # for now not supported at all, so\n                    return port\n                elif f == 'hostname':\n                    return hostname\n                else:\n                    return getattr(pr, f)\n        else:\n            _getattr = getattr\n\n        return {f: (_getattr(pr, f) or '') for f in cls._FIELDS}\n\n    #\n    # Access helpers\n    #\n\n    def _parse_qs(self, s, auto_delist=True):\n        \"\"\"Helper around parse_qs to strip unneeded 'list'ing etc and return a dict of key=values\"\"\"\n        if not s:\n            return {}\n        out = map_items(assure_unicode, OrderedDict(parse_qsl(s, 1)))\n        if not auto_delist:\n            return out\n        for k in out:\n            v = out[k]\n            if isinstance(v, list) and len(v) == 1:\n                v = v[0]\n                out[k] = None if v == '' else v\n        return out\n\n    @property\n    def query_dict(self):\n        return self._parse_qs(self.query)\n\n    @property\n    def fragment_dict(self):\n        return self._parse_qs(self.fragment)\n\n    @property\n    def localpath(self):\n        if self.scheme != 'file':\n            raise ValueError(\n                \"Non 'file://' URL cannot be resolved to a local path\")\n        hostname = self.hostname\n        if not (hostname in (None, '', 'localhost', '::1')\n                or hostname.startswith('127.')):\n            raise ValueError(\"file:// URL does not point to 'localhost'\")\n        return self.path\n\n\nclass PathRI(RI):\n    \"\"\"RI pointing to a (local) file/directory\"\"\"\n    def as_str(self):\n        return self.path\n\n    @classmethod\n    def _str_to_fields(cls, url_str):\n        # str() to be compatible with pathlib objects\n        return dict(path=str(url_str))\n\n    @property\n    def localpath(self):\n        return self.path\n\n    @property\n    def posixpath(self):\n        if is_windows_path(self.path):\n            win_split = win_splitdrive(self.path)\n            return \"/\" + win_split[0][0] + win_split[1].replace('\\\\', '/')\n        else:\n            return self.path\n\n\nclass RegexBasedURLMixin(object):\n    \"\"\"Base class for URLs which we could simple parse using regular expressions\"\"\"\n\n    _REGEX = None\n\n    # not used ATM but possible ;)\n    # @classmethod\n    # def is_str_matches(cls, url_str):\n    #     return bool(cls._REGEX.match(url_str))\n\n    @classmethod\n    def _str_to_fields(cls, url_str):\n        re_match = cls._REGEX.match(url_str)\n        if not re_match:\n            # TODO: custom error?\n            raise ValueError(\n                \"Cannot handle URL '%s': categorized as %r, but does not match syntax.%s\"\n                % (url_str,\n                   cls,\n                   \" Did you intent to use '///'?\" if url_str.startswith('//') else '')\n            )\n        fields = cls._get_blank_fields()\n        fields.update({k: v for k, v in re_match.groupdict().items() if v})\n        cls._normalize_fields(fields)\n        return fields\n\n    @classmethod\n    def _normalize_fields(self, fields):\n        \"\"\"Helper to be ran if any of the fields need to be normalized after parsing\"\"\"\n        pass\n\n\nclass SSHRI(RI, RegexBasedURLMixin):\n    \"\"\"RI pointing to a remote location reachable via SSH\"\"\"\n\n    _FIELDS = RI._FIELDS + (\n        'username',\n        'hostname',\n        'port',\n    )\n\n    _REGEX = re.compile(r'((?P<username>\\S*)@)?(?P<hostname>[^#/\\\\:]+)(\\:(?P<path>.*))?$')\n\n    @classmethod\n    def _normalize_fields(cls, fields):\n        if fields['path'] and fields['path'].startswith('//'):\n            # Let's normalize for now to avoid multiple leading slashes\n            fields['path'] = '/' + fields['path'].lstrip('/')\n        # escape path so we have direct representation of the path to work with\n        fields['path'] = unescape_ssh_path(fields['path'])\n\n    def as_str(self, escape=False):\n        fields = self.fields  # copy so we could escape symbols\n        url_fmt = '{hostname}'\n        if fields['username']:\n            url_fmt = \"{username}@\" + url_fmt\n        if fields['path']:\n            url_fmt += ':{path}'\n        if escape:\n            fields['path'] = escape_ssh_path(fields['path'])\n        return url_fmt.format(**fields)\n\n    # TODO:\n    # we can \"support\" localhost:path as localpaths\n\n\nclass DataLadRI(RI, RegexBasedURLMixin):\n    \"\"\"RI pointing to datasets within default DataLad super-dataset\"\"\"\n\n    _FIELDS = RI._FIELDS + (\n        'remote',\n    )\n\n    # For now or forever we don't deal with any fragments or other special stuff\n    _REGEX = re.compile(r'//(?P<remote>[^\\s/]*)/(?P<path>.*)$')\n\n    # do they need to be normalized??? loosing track ...\n\n    def as_str(self):\n        return \"//{remote}/{path}\".format(**self._fields)\n\n    def as_git_url(self):\n        \"\"\"Dereference /// into original URLs which could be used by git for cloning\n\n        Returns\n        -------\n        str\n          URL string to reference the DataLadRI from its /// form\n        \"\"\"\n        if self.remote:\n            raise NotImplementedError(\"not supported ATM to reference additional remotes\")\n        return \"{}{}\".format(consts.DATASETS_TOPURL, urlquote(self.path))\n\n\nclass GitTransportRI(RI, RegexBasedURLMixin):\n    \"\"\"RI for some other RI with git transport prefix\"\"\"\n\n    # TODO: check how crticial to \"inherit\" RI._FIELDS asking to provide path\n    _FIELDS = RI._FIELDS + (\n        'transport',\n        'RI',\n    )\n\n    # Due to poor design, `ri` argument already present in various\n    # places intermixed with **kwargs treatment. So we will use RI\n    # here instead of ri.\n    _REGEX = re.compile(r'(?P<transport>[A-Za-z0-9][A-Za-z0-9+.-]*)::(?P<RI>.*)$')\n\n    def as_str(self):\n        return '{self.transport}::{self.RI}'.format(self=self)\n\n\ndef _split_colon(s, maxsplit=1):\n    \"\"\"Split on unescaped colon\"\"\"\n    return re.compile(r'(?<!\\\\):').split(s, maxsplit=maxsplit)\n\n# \\ should be first to deal with\n_SSH_ESCAPED_CHARACTERS = '\\\\#&;`|*?~<>^()[]{}$\\'\" '\n\n\n# TODO: RF using re.sub\ndef escape_ssh_path(path):\n    \"\"\"Escape all special characters present in the path\"\"\"\n    for c in _SSH_ESCAPED_CHARACTERS:\n        if c in path:\n            path = path.replace(c, '\\\\' + c)\n    return path\n\n\ndef unescape_ssh_path(path):\n    \"\"\"Un-escape all special characters present in the path\"\"\"\n    for c in _SSH_ESCAPED_CHARACTERS[::-1]:\n        if c in path:\n            path = path.replace('\\\\' + c, c)\n    return path\n\n\ndef parse_url_opts(url):\n    \"\"\"Given a string with url-style query, split into content before # and options as dict\"\"\"\n    url = URL(url)\n    # we need to filter out query and fragment to get the base url\n    fields = url.fields\n    fields.pop('query')\n    fields.pop('fragment')\n    opts = url.query_dict\n    return str(URL(**fields)), opts\n\n\n# TODO: should we just define URL.good_for_git or smth like that? ;)\n# although git also understands regular paths\ndef is_url(ri):\n    \"\"\"Returns whether argument is a resource identifier what datalad should treat as a URL\n\n    This includes ssh \"urls\" which git understands.\n\n    Parameters\n    ----------\n    ri : str or RI\n      The resource identifier (as a string or RI) to \"analyze\"\n    \"\"\"\n    if not isinstance(ri, RI):\n        try:\n            ri = RI(ri)\n        except:  # MIH: MemoryError?\n            return False\n    return isinstance(ri, (URL, SSHRI))\n\n\n# TODO: RF to remove duplication\ndef is_datalad_compat_ri(ri):\n    \"\"\"Returns whether argument is a resource identifier what datalad should treat as a URL\n\n    including its own DataLadRI\n    \"\"\"\n    if not isinstance(ri, RI):\n        try:\n            ri = RI(ri)\n        except:  # MIH: MemoryError?\n            return False\n    return isinstance(ri, (URL, SSHRI, DataLadRI))\n\n\n# TODO: better name? additionally may be move to SSHRI.is_valid() or sth.\ndef is_ssh(ri):\n    \"\"\"helper to determine, whether `ri` requires an SSH connection\n\n    Parameters\n    ----------\n    ri: str or RI\n\n    Returns\n    -------\n    bool\n    \"\"\"\n\n    # not exactly fitting the doc, but we actually can deal not necessarily with\n    # string or RI only, but with everything RI itself can deal with:\n    _ri = RI(ri) if not isinstance(ri, RI) else ri\n\n    return isinstance(_ri, SSHRI) \\\n        or (isinstance(_ri, URL) and _ri.scheme == 'ssh')\n\n\ndef get_local_file_url(fname, compatibility='git-annex'):\n    \"\"\"Return OS specific URL pointing to a local file\n\n    Parameters\n    ----------\n    fname : string\n        Filename.  If not absolute, abspath is used\n    compatibility : {'git', 'git-annex'}, optional\n        On Windows, and only on that platform, file:// URLs may need to look\n        different depending on the use case or consuming application. This\n        switch selects different compatibility modes: 'git' for use with\n        Git commands (e.g. `clone` or `submodule add`); 'git-annex` for\n        Git-annex command input (e.g. `addurl`). On any other platform this\n        setting has no effect.\n    \"\"\"\n    # we resolve the path to circumwent potential short paths on unfortunate\n    # platforms that would ruin the URL format.\n    #path = Path(fname).resolve().absolute()\n    path = Path(fname).resolve()\n    if on_windows:\n        # in addition we need to absolute(), as on windows resolve() doesn't\n        # imply that\n        path = path.absolute().as_posix()\n        furl = 'file://{}{}'.format(\n            '/' if compatibility == 'git' else '',\n            urlquote(\n                re.sub(r'([a-zA-Z]):', r'\\1', path)\n            ))\n    else:\n        # TODO:  need to fix for all the encoding etc\n        furl = str(URL(scheme='file', path=str(path)))\n    return furl\n\n\ndef get_url_cache_filename(url, name=None):\n    \"\"\"Return a filename where to cache online doc from a url\"\"\"\n    if not name:\n        name = \"misc\"\n    cache_dir = opj(cfg.obtain('datalad.locations.cache'), name)\n    doc_fname = opj(\n        cache_dir,\n        '{}-{}.p{}'.format(\n            urlsplit(url).netloc,\n            md5(url.encode('utf-8')).hexdigest(),\n            pickle.HIGHEST_PROTOCOL)\n    )\n    return doc_fname\n\n\ndef get_cached_url_content(url, name=None, fetcher=None, maxage=None):\n    \"\"\"Loader of a document from a url, which caches loaded instance on disk\n\n    Doesn't do anything smart about http headers etc which could provide\n    information for cache/proxy servers for how long to retain etc\n\n    TODO: theoretically it is not network specific at all -- and just a memoize\n    pattern, but may be some time we would make it treat headers etc correctly.\n    And ATM would support any URL we support via providers/downloaders\n\n    Parameters\n    ----------\n    fetcher: callable, optional\n       Function to call with url if needed to be refetched\n    maxage: float, optional\n       Age in days to retain valid for.  <0 - would retain forever.  If None -\n       would consult the config, 0 - would force to reload\n    \"\"\"\n    doc_fname = get_url_cache_filename(url, name)\n    if maxage is None:\n        maxage = float(cfg.get('datalad.locations.cache-maxage'))\n\n    doc = None\n    if os.path.exists(doc_fname) and maxage != 0:\n\n        fage = (time.time() - os.stat(doc_fname).st_mtime)/(24. * 3600)\n        if maxage < 0 or fage < maxage:\n            try:\n                lgr.debug(\"use cached request result to '%s' from %s\", url, doc_fname)\n                doc = pickle.load(open(doc_fname, 'rb'))\n            except Exception as e:  # it is OK to ignore any error and fall back on the true source\n                lgr.warning(\n                    \"cannot load cache from '%s', fall back to download: %s\",\n                    doc_fname, exc_str(e))\n\n    if doc is None:\n        if fetcher is None:\n            from datalad.downloaders.providers import Providers\n            providers = Providers.from_config_files()\n            fetcher = providers.fetch\n\n        doc = fetcher(url)\n        assure_dir(dirname(doc_fname))\n        # use pickle to store the entire request result dict\n        pickle.dump(doc, open(doc_fname, 'wb'))\n        lgr.debug(\"stored result of request to '{}' in {}\".format(url, doc_fname))\n    return doc\n\nlgr.log(5, \"Done importing support.network\")\n",
            "header": {
                "file": {
                    "path": "datalad/support/network.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "bc9211f08",
                "new_commit_hash": "b291ebbfa",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/datalad/support/network.py"
            },
            "plus_file": {
                "path": "b/datalad/support/network.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 202,
                            "length": 7
                        },
                        "new_range": {
                            "start": 202,
                            "length": 7
                        },
                        "section": "def iso8601_to_epoch(datestr):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "    iso8601 is used to parse properly the time zone information, which",
                                "type": "context"
                            },
                            {
                                "content": "    can't be parsed with standard datetime strptime",
                                "type": "context"
                            },
                            {
                                "content": "    \"\"\"",
                                "type": "context"
                            },
                            {
                                "content": "    return calendar.timegm(iso8601.parse_date(datestr).timetuple())",
                                "type": "deleted"
                            },
                            {
                                "content": "    return calendar.timegm(iso8601.parse_date(datestr).utctimetuple())",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "def __urlopen_requests(url):",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "datalad/support/network.py",
                            "type": "function",
                            "name": "iso8601_to_epoch",
                            "content": "def iso8601_to_epoch(datestr):\n    \"\"\"Given ISO 8601 date/time format, return in seconds since epoch\n\n    iso8601 is used to parse properly the time zone information, which\n    can't be parsed with standard datetime strptime\n    \"\"\"\n    return calendar.timegm(iso8601.parse_date(datestr).timetuple())",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 199,
                            "end_lineno": 205,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                }
            ]
        },
        {
            "old_file_content": "# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-\n# ex: set sts=4 ts=4 sw=4 noet:\n# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n#\n#   See COPYING file distributed along with the datalad package for the\n#   copyright and license terms.\n#\n# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n\nimport logging\n\nimport os\nfrom os.path import (\n    join as opj,\n    isabs,\n)\nfrom collections import OrderedDict\n\nfrom datalad.distribution.dataset import Dataset\n\nfrom datalad.utils import (\n    Path,\n    PurePosixPath,\n    on_windows,\n)\n\nfrom datalad.tests.utils import (\n    eq_,\n    neq_,\n    ok_,\n    nok_,\n    assert_raises,\n    skip_if_on_windows,\n    swallow_logs,\n    assert_in,\n    get_most_obscure_supported_name,\n    SkipTest,\n    known_failure_githubci_win,\n    with_tempfile,\n    assert_status,\n)\n\nfrom datalad.support.network import (\n    DataLadRI,\n    GitTransportRI,\n    PathRI,\n    RI,\n    SSHRI,\n    URL,\n    _split_colon,\n    dlurljoin,\n    get_local_file_url,\n    get_response_disposition_filename,\n    get_tld,\n    get_url_straight_filename,\n    is_datalad_compat_ri,\n    is_ssh,\n    is_url,\n    iso8601_to_epoch,\n    parse_url_opts,\n    same_website,\n)\n\n\ndef test_same_website():\n    ok_(same_website(\"http://a.b\", \"http://a.b/2014/01/xxx/\"))\n    ok_(same_website(\"http://a.b/page/2/\", \"http://a.b/2014/01/xxx/\"))\n    ok_(same_website(\"https://a.b/page/2/\", \"http://a.b/2014/01/xxx/\"))\n    ok_(same_website(\"http://a.b/page/2/\", \"https://a.b/2014/01/xxx/\"))\n\n\ndef test_get_tld():\n    eq_(get_tld('http://example.com'), 'example.com')\n    eq_(get_tld('http://example.com/1'), 'example.com')\n    eq_(get_tld('http://example.com/1/2'), 'example.com')\n    eq_(get_tld('example.com/1/2'), 'example.com')\n    eq_(get_tld('s3://example.com/1/2'), 'example.com')\n    assert_raises(ValueError, get_tld, \"\")\n    assert_raises(ValueError, get_tld, \"s3://\")\n    assert_raises(ValueError, get_tld, \"http://\")\n\ndef test_dlurljoin():\n    eq_(dlurljoin('http://a.b/', 'f'), 'http://a.b/f')\n    eq_(dlurljoin('http://a.b/page', 'f'), 'http://a.b/f')\n    eq_(dlurljoin('http://a.b/dir/', 'f'), 'http://a.b/dir/f')\n    eq_(dlurljoin('http://a.b/dir/', 'http://url'), 'http://url')\n    eq_(dlurljoin('http://a.b/dir/', '/'), 'http://a.b/')\n    eq_(dlurljoin('http://a.b/dir/', '/x/y'), 'http://a.b/x/y')\n\ndef _test_get_url_straight_filename(suf):\n    eq_(get_url_straight_filename('http://a.b/' + suf), '')\n    eq_(get_url_straight_filename('http://a.b/p1' + suf), 'p1')\n    eq_(get_url_straight_filename('http://a.b/p1/' + suf), '')\n    eq_(get_url_straight_filename('http://a.b/p1/' + suf, allowdir=True), 'p1')\n    eq_(get_url_straight_filename('http://a.b/p1/p2' + suf), 'p2')\n    eq_(get_url_straight_filename('http://a.b/p1/p2/' + suf), '')\n    eq_(get_url_straight_filename('http://a.b/p1/p2/' + suf, allowdir=True), 'p2')\n    eq_(get_url_straight_filename('http://a.b/p1/p2/' + suf, allowdir=True, strip=('p2', 'xxx')), 'p1')\n    eq_(get_url_straight_filename('http://a.b/p1/p2/' + suf, strip=('p2', 'xxx')), '')\n\ndef test_get_url_straight_filename():\n    yield _test_get_url_straight_filename, ''\n    yield _test_get_url_straight_filename, '#'\n    yield _test_get_url_straight_filename, '#tag'\n    yield _test_get_url_straight_filename, '#tag/obscure'\n    yield _test_get_url_straight_filename, '?param=1'\n    yield _test_get_url_straight_filename, '?param=1&another=/'\n\nfrom ..network import rfc2822_to_epoch\ndef test_rfc2822_to_epoch():\n    eq_(rfc2822_to_epoch(\"Thu, 16 Oct 2014 01:16:17 EDT\"), 1413436577)\n\n\ndef test_get_response_disposition_filename():\n    eq_(get_response_disposition_filename('attachment;filename=\"Part1-Subjects1-99.tar\"'), \"Part1-Subjects1-99.tar\")\n    eq_(get_response_disposition_filename('attachment'), None)\n\n\ndef test_parse_url_opts():\n    url = 'http://map.org/api/download/?id=157'\n    output = parse_url_opts(url)\n    eq_(output, ('http://map.org/api/download/', {'id': '157'}))\n\n    url = 's3://bucket/save/?key=891'\n    output = parse_url_opts(url)\n    eq_(output, ('s3://bucket/save/', {'key': '891'}))\n\n    url = 'http://map.org/api/download/?id=98&code=13'\n    output = parse_url_opts(url)\n    eq_(output, ('http://map.org/api/download/', {'id': '98', 'code': '13'}))\n\n\ndef test_split_colon():\n    eq_(_split_colon('a:b'), ['a', 'b'])\n    eq_(_split_colon('a:b:c'), ['a', 'b:c'])\n    eq_(_split_colon('a:b:c', 2), ['a', 'b', 'c'])\n    eq_(_split_colon('ab'), ['ab'])\n    eq_(_split_colon(r'a\\:b'), [r'a\\:b'])\n\n\ndef test_url_eq():\n    eq_(URL(), URL())\n    # doesn't make sense to ask what kind of a url it is an empty URL\n    #eq_(RI(), RI())\n    neq_(URL(), URL(hostname='x'))\n    # Different types aren't equal even if have the same fields values\n    neq_(URL(path='x'), PathRI(path='x'))\n    neq_(URL(hostname='x'), SSHRI(hostname='x'))\n    neq_(str(URL(hostname='x')), str(SSHRI(hostname='x')))\n\n\ndef _check_ri(ri, cls, exact_str=True, localpath=None, **fields):\n    \"\"\"just a helper to carry out few checks on urls\"\"\"\n    with swallow_logs(new_level=logging.DEBUG) as cml:\n        ri_ = cls(**fields)\n        murl = RI(ri)\n        eq_(murl.__class__, cls)  # not just a subclass\n        eq_(murl, ri_)\n        if isinstance(ri, str):\n            eq_(str(RI(ri)), ri)\n        eq_(eval(repr(ri_)), ri)  # repr leads back to identical ri_\n        eq_(ri, ri_)  # just in case ;)  above should fail first if smth is wrong\n        if not exact_str:\n            assert_in('Parsed version of', cml.out)\n    (eq_ if exact_str else neq_)(str(ri), str(ri_))  # that we can reconstruct it EXACTLY on our examples\n    # and that we have access to all those fields\n    nok_(set(fields).difference(set(cls._FIELDS)))\n    for f, v in fields.items():\n        eq_(getattr(ri_, f), v)\n\n    if localpath:\n        eq_(ri_.localpath, localpath)\n        old_localpath = ri_.localpath  # for a test below\n    else:\n        # if not given -- must be a remote url, should raise exception\n        with assert_raises(ValueError):\n            ri_.localpath\n\n    # This one does not have a path. TODO: either proxy path from its .RI or adjust\n    # hierarchy of classes to make it more explicit\n    if cls == GitTransportRI:\n        return\n    # do changes in the path persist?\n    old_str = str(ri_)\n    ri_.path = newpath = opj(ri_.path, 'sub')\n    eq_(ri_.path, newpath)\n    neq_(str(ri_), old_str)\n    if localpath:\n        eq_(ri_.localpath, opj(old_localpath, 'sub'))\n\n\n\ndef test_url_base():\n    # Basic checks\n    assert_raises(ValueError, URL, \"http://example.com\", hostname='example.com')\n    url = URL(\"http://example.com\")\n    eq_(url.hostname, 'example.com')\n    eq_(url.scheme, 'http')\n    eq_(url.port, '')  # not specified -- empty strings\n    eq_(url.username, '')  # not specified -- empty strings\n    eq_(repr(url), \"URL(hostname='example.com', scheme='http')\")\n    eq_(url, \"http://example.com\")  # automagic coercion in __eq__\n\n    neq_(URL(), URL(hostname='x'))\n\n    smth = URL('smth')\n    eq_(smth.hostname, '')\n    ok_(bool(smth))\n    nok_(bool(URL()))\n\n    assert_raises(ValueError, url._set_from_fields, unknown='1')\n\n    with swallow_logs(new_level=logging.WARNING) as cml:\n        # we don't \"care\" about params ATM so there is a warning if there are any\n        purl = URL(\"http://example.com/;param\")\n        eq_(str(purl), 'http://example.com/;param')  # but we do maintain original string\n        assert_in('ParseResults contains params', cml.out)\n        eq_(purl.as_str(), 'http://example.com/')\n\n\n@known_failure_githubci_win\ndef test_url_samples():\n    _check_ri(\"http://example.com\", URL, scheme='http', hostname=\"example.com\")\n    # \"complete\" one for classical http\n    _check_ri(\"http://user:pw@example.com:8080/p/sp?p1=v1&p2=v2#frag\", URL,\n              scheme='http', hostname=\"example.com\", port=8080,\n              username='user', password='pw', path='/p/sp',\n              query='p1=v1&p2=v2', fragment='frag')\n\n    # sample one for ssh with specifying the scheme\n    # XXX? might be useful?  https://github.com/FriendCode/giturlparse.py\n    _check_ri(\"ssh://host/path/sp1\", URL, scheme='ssh', hostname='host', path='/path/sp1')\n    _check_ri(\"user@host:path/sp1\", SSHRI,\n              hostname='host', path='path/sp1', username='user')\n    _check_ri(\"host:path/sp1\", SSHRI, hostname='host', path='path/sp1')\n    _check_ri(\"host:path\", SSHRI, hostname='host', path='path')\n    _check_ri(\"host:/path\", SSHRI, hostname='host', path='/path')\n    _check_ri(\"user@host\", SSHRI, hostname='host', username='user')\n    # TODO!!!  should this be a legit URL like this?\n    # _check_ri(\"host\", SSHRI, hostname='host'))\n    eq_(repr(RI(\"host:path\")), \"SSHRI(hostname='host', path='path')\")\n\n    # And now perspective 'datalad', implicit=True urls pointing to the canonical center location\n    _check_ri(\"///\", DataLadRI)\n    _check_ri(\"///p/s1\", DataLadRI, path='p/s1')\n    # could be considered by someone as \"URI reference\" relative to scheme\n    _check_ri(\"//a/\", DataLadRI, remote='a')\n    _check_ri(\"//a/data\", DataLadRI, path='data', remote='a')\n\n    # here we will do custom magic allowing only schemes with + in them, such as dl+archive\n    # or not so custom as\n    _check_ri(\"hg+https://host/user/proj\", URL,\n              scheme=\"hg+https\", hostname='host', path='/user/proj')\n    # \"old\" style\n    _check_ri(\"dl+archive:KEY/path/sp1#size=123\", URL,\n              scheme='dl+archive', path='KEY/path/sp1', fragment='size=123')\n    # \"new\" style\n    _check_ri(\"dl+archive:KEY#path=path/sp1&size=123\", URL,\n              scheme='dl+archive', path='KEY', fragment='path=path/sp1&size=123')\n    # actually above one is probably wrong since we need to encode the path\n    _check_ri(\"dl+archive:KEY#path=path%2Fbsp1&size=123\", URL,\n              scheme='dl+archive', path='KEY', fragment='path=path%2Fbsp1&size=123')\n\n    #https://en.wikipedia.org/wiki/File_URI_scheme\n    _check_ri(\"file://host\", URL, scheme='file', hostname='host')\n    _check_ri(\"file://host/path/sp1\", URL, scheme='file', hostname='host', path='/path/sp1')\n    # stock libraries of Python aren't quite ready for ipv6\n    ipv6address = '2001:db8:85a3::8a2e:370:7334'\n    _check_ri(\"file://%s/path/sp1\" % ipv6address, URL,\n              scheme='file', hostname=ipv6address, path='/path/sp1')\n    for lh in ('localhost', '::1', '', '127.3.4.155'):\n        _check_ri(\"file://%s/path/sp1\" % lh, URL, localpath='/path/sp1',\n                  scheme='file', hostname=lh, path='/path/sp1')\n    _check_ri('http://[1fff:0:a88:85a3::ac1f]:8001/index.html', URL,\n              scheme='http', hostname='1fff:0:a88:85a3::ac1f', port=8001, path='/index.html')\n    _check_ri(\"file:///path/sp1\", URL, localpath='/path/sp1', scheme='file', path='/path/sp1')\n    # we don't do any magical comprehension for home paths/drives for windows\n    # of file:// urls, thus leaving /~ and /c: for now:\n    _check_ri(\"file:///~/path/sp1\", URL, localpath='/~/path/sp1', scheme='file', path='/~/path/sp1')\n    _check_ri(\"file:///%7E/path/sp1\", URL, localpath='/~/path/sp1', scheme='file', path='/~/path/sp1', exact_str=False)\n    # not sure but let's check\n    _check_ri(\"file:///c:/path/sp1\", URL, localpath='/c:/path/sp1', scheme='file', path='/c:/path/sp1', exact_str=False)\n\n    # and now implicit paths or actually they are also \"URI references\"\n    _check_ri(\"f\", PathRI, localpath='f', path='f')\n    _check_ri(\"f/s1\", PathRI, localpath='f/s1', path='f/s1')\n    _check_ri(PurePosixPath(\"f\"), PathRI, localpath='f', path='f')\n    _check_ri(PurePosixPath(\"f/s1\"), PathRI, localpath='f/s1', path='f/s1')\n    # colons are problematic and might cause confusion into SSHRI\n    _check_ri(\"f/s:1\", PathRI, localpath='f/s:1', path='f/s:1')\n    _check_ri(\"f/s:\", PathRI, localpath='f/s:', path='f/s:')\n    _check_ri(\"/f\", PathRI, localpath='/f', path='/f')\n    _check_ri(\"/f/s1\", PathRI, localpath='/f/s1', path='/f/s1')\n\n    # some github ones, just to make sure\n    _check_ri(\"git://host/user/proj\", URL, scheme=\"git\", hostname=\"host\", path=\"/user/proj\")\n    _check_ri(\"git@host:user/proj\", SSHRI, hostname=\"host\", path=\"user/proj\", username='git')\n\n    _check_ri('weired:/', SSHRI, hostname='weired', path='/')\n    # since schema is not allowing some symbols so we need to add additional check\n    _check_ri('weired_url:/', SSHRI, hostname='weired_url', path='/')\n    _check_ri('example.com:/', SSHRI, hostname='example.com', path='/')\n    _check_ri('example.com:path/sp1', SSHRI, hostname='example.com', path='path/sp1')\n    _check_ri('example.com/path/sp1\\:fname', PathRI, localpath='example.com/path/sp1\\:fname',\n              path='example.com/path/sp1\\:fname')\n    # ssh is as stupid as us, so we will stay \"Consistently\" dumb\n    \"\"\"\n    $> ssh example.com/path/sp1:fname\n    ssh: Could not resolve hostname example.com/path/sp1:fname: Name or service not known\n\n    edit 20190516 yoh: but this looks like a perfectly valid path.\n    SSH knows that it is not a path but its SSHRI so it can stay dumb.\n    We are trying to be smart and choose between RIs (even when we know that\n    it is e.g. a file).\n    \"\"\"\n    _check_ri('e.com/p/sp:f', PathRI, localpath='e.com/p/sp:f', path='e.com/p/sp:f')\n    _check_ri('user@someho.st/mydir', PathRI, localpath='user@someho.st/mydir', path='user@someho.st/mydir')\n\n    # SSHRIs have .port, but it is empty\n    eq_(SSHRI(hostname='example.com').port, '')\n\n    # check that we are getting a warning logged when url can't be reconstructed\n    # precisely\n    # actually failed to come up with one -- becomes late here\n    #_check_ri(\"http://host///..//p\", scheme='http', path='/..//p')\n\n    # actually this one is good enough to trigger a warning and I still don't know\n    # what it should exactly be!?\n    with swallow_logs(new_level=logging.DEBUG) as cml:\n        weired_str = 'weired://'\n        weired_url = RI(weired_str)\n        repr(weired_url)\n        cml.assert_logged(\n            'Parsed version of SSHRI .weired:/. '\n            'differs from original .weired://.'\n        )\n        # but we store original str\n        eq_(str(weired_url), weired_str)\n        neq_(weired_url.as_str(), weired_str)\n\n\n    raise SkipTest(\"TODO: file://::1/some does complain about parsed version dropping ::1\")\n\n\ndef test_git_transport_ri():\n    _check_ri(\"gcrypt::http://somewhere\", GitTransportRI, RI='http://somewhere', transport='gcrypt')\n    # man git-push says\n    #  <transport>::<address>\n    #    where <address> may be a path, a server and path, or an arbitrary URL-like string...\n    # so full path to my.com/... should be ok?\n    _check_ri(\"http::/my.com/some/path\", GitTransportRI, RI='/my.com/some/path', transport='http')\n    # some ssh server.  And we allow for some additional chars in transport.\n    # Git doesn't define since it does not care! we will then be flexible too\n    _check_ri(\"trans-port::server:path\", GitTransportRI, RI='server:path', transport='trans-port')\n\n\ndef _test_url_quote_path(cls, clskwargs, target_url):\n    path = '/ \"\\';a&b&cd `| '\n    if not (cls is PathRI):\n        clskwargs['hostname'] = hostname = 'example.com'\n    url = cls(path=path, **clskwargs)\n    eq_(url.path, path)\n    if 'hostname' in clskwargs:\n        eq_(url.hostname, hostname)\n    # all nasty symbols should be quoted\n    url_str = str(url)\n    eq_(url_str, target_url)\n    # no side-effects:\n    eq_(url.path, path)\n    if 'hostname' in clskwargs:\n        eq_(url.hostname, hostname)\n\n    # and figured out and unquoted\n    url_ = RI(url_str)\n    ok_(isinstance(url_, cls))\n    eq_(url_.path, path)\n    if 'hostname' in clskwargs:\n        eq_(url.hostname, hostname)\n\n\ndef test_url_quote_path():\n    yield _test_url_quote_path, SSHRI, {}, r'example.com:/ \"' + r\"';a&b&cd `| \"\n    yield _test_url_quote_path, URL, {'scheme': \"http\"}, 'http://example.com/%20%22%27%3Ba%26b%26cd%20%60%7C%20'\n    yield _test_url_quote_path, PathRI, {}, r'/ \"' + r\"';a&b&cd `| \"  # nothing is done to file:implicit\n\n\ndef test_url_compose_archive_one():\n    url = URL(scheme='dl+archive', path='KEY',\n              fragment=OrderedDict((('path', 'f/p/ s+'), ('size', 30))))\n    # funny - space is encoded as + but + is %2B\n    eq_(str(url), 'dl+archive:KEY#path=f/p/+s%2B&size=30')\n    eq_(url.fragment_dict, {'path': 'f/p/ s+', 'size': '30'})\n\n\ndef test_url_fragments_and_query():\n    url = URL(hostname=\"host\", query=OrderedDict((('a', 'x/b'), ('b', 'y'))))\n    eq_(str(url), '//host?a=x%2Fb&b=y')\n    eq_(url.query, 'a=x%2Fb&b=y')\n    eq_(url.query_dict, {'a': 'x/b', 'b': 'y'})\n\n    url = URL(hostname=\"host\", fragment=OrderedDict((('b', 'x/b'), ('a', 'y'))))\n    eq_(str(url), '//host#b=x/b&a=y')\n    eq_(url.fragment, 'b=x/b&a=y')\n    eq_(url.fragment_dict, {'a': 'y', 'b': 'x/b'})\n\n    fname = get_most_obscure_supported_name()\n    url = URL(hostname=\"host\", fragment={'a': fname})\n    eq_(url.fragment_dict, {'a': fname})\n\n\ndef test_url_dicts():\n    eq_(URL(\"http://host\").query_dict, {})\n\n\n@skip_if_on_windows\ndef test_get_url_path_on_fileurls():\n    eq_(URL('file:///a').path, '/a')\n    eq_(URL('file:///a/b').path, '/a/b')\n    eq_(URL('file:///a/b').localpath, '/a/b')\n    eq_(URL('file:///a/b#id').path, '/a/b')\n    eq_(URL('file:///a/b?whatever').path, '/a/b')\n\n\ndef test_is_url():\n    ok_(is_url('file://localhost/some'))\n    ok_(is_url('http://localhost'))\n    ok_(is_url('ssh://me@localhost'))\n    # in current understanding it is indeed a url but an 'ssh', implicit=True, not just\n    # a useless scheme=weired with a hope to point to a netloc\n    with swallow_logs():\n        ok_(is_url('weired://'))\n    nok_(is_url('relative'))\n    nok_(is_url('/absolute'))\n    ok_(is_url('like@sshlogin'))  # actually we do allow ssh:implicit urls ATM\n    nok_(is_url(''))\n    nok_(is_url(' '))\n    nok_(is_url(123))  # stuff of other types wouldn't be considered a URL\n\n    # we can pass RI instance directly\n    ok_(is_url(RI('file://localhost/some')))\n    nok_(is_url(RI('relative')))\n\n\n# TODO: RF with test_is_url to avoid duplication\ndef test_is_datalad_compat_ri():\n    ok_(is_datalad_compat_ri('ssh://user:passw@host/path'))\n    ok_(is_datalad_compat_ri('http://example.com'))\n    ok_(is_datalad_compat_ri('file://localhost/some'))\n    ok_(is_datalad_compat_ri('///localhost/some'))\n    nok_(is_datalad_compat_ri('relative'))\n    nok_(is_datalad_compat_ri('.///localhost/some'))\n    nok_(is_datalad_compat_ri(123))\n\n\ndef test_get_local_file_url():\n    for path, url in (\n                # relpaths are special-cased below\n                ('test.txt', 'test.txt'),\n            ) + (\n                ('C:\\\\Windows\\\\notepad.exe', 'file://C/Windows/notepad.exe'),\n            ) if on_windows else (\n                # static copy of \"most_obscore_name\"\n                (' \"\\';a&b&cΔЙקم๗あ `| ',\n                 # and translation by google chrome\n                 \"%20%22%27%3Ba%26b%26c%CE%94%D0%99%D7%A7%D9%85%E0%B9%97%E3%81%82%20%60%7C%20\"),\n                ('/a', 'file:///a'),\n                ('/a/b/c', 'file:///a/b/c'),\n                ('/a~', 'file:///a~'),\n                # there are no files with trailing slashes in the name\n                #('/a b/', 'file:///a%20b/'),\n                ('/a b/name', 'file:///a%20b/name'),\n            ):\n        if isabs(path):\n            eq_(get_local_file_url(path), url)\n        else:\n            eq_(get_local_file_url(path),\n                '/'.join((\n                    get_local_file_url(os.getcwd()),\n                    url))\n            )\n\n\n@with_tempfile(mkdir=True)\ndef test_get_local_file_url_compatibility(path):\n    # smoke test for file:// URL compatibility with other datalad/git/annex\n    # pieces\n    path = Path(path)\n    ds1 = Dataset(path / 'ds1').create()\n    ds2 = Dataset(path / 'ds2').create()\n    testfile = path / 'testfile.txt'\n    testfile.write_text('some')\n\n    # compat with annex addurl\n    ds1.repo.add_url_to_file(\n        'test.txt',\n        get_local_file_url(testfile, compatibility='git-annex'))\n\n    # compat with git clone/submodule\n    assert_status(\n        'ok',\n        ds1.clone(get_local_file_url(ds2.path, compatibility='git'),\n                  result_xfm=None, return_type='generator'))\n\n\ndef test_is_ssh():\n\n    ssh_locators = [\"ssh://host\",\n                    \"ssh://host/some/where\",\n                    \"user@host:path/sp1\",\n                    \"user@host:/absolute/path/sp1\",\n                    \"host:path/sp1\",\n                    \"host:/absolute/path/sp1\",\n                    \"user@host\"]\n    for ri in ssh_locators:\n        ok_(is_ssh(ri), \"not considered ssh (string): %s\" % ri)\n        ok_(is_ssh(RI(ri)), \"not considered ssh (RI): %s\" % ri)\n\n    non_ssh_locators = [\"file://path/to\",\n                        \"/abs/path\",\n                        \"../rel/path\",\n                        \"http://example.com\",\n                        \"git://host/user/proj\",\n                        \"s3://bucket/save/?key=891\"]\n    for ri in non_ssh_locators:\n        ok_(not is_ssh(ri), \"considered ssh (string): %s\" % ri)\n        ok_(not is_ssh(RI(ri)), \"considered ssh (RI): %s\" % ri)\n\n\ndef test_iso8601_to_epoch():\n    epoch = 1467901515\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15+00:00'), epoch)\n    # zone information is actually not used\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15+11:00'), epoch)\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15Z'), epoch)\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15'), epoch)\n\n    eq_(iso8601_to_epoch('2016-07-07T14:25:14'), epoch-1)\n",
            "new_file_content": "# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-\n# ex: set sts=4 ts=4 sw=4 noet:\n# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n#\n#   See COPYING file distributed along with the datalad package for the\n#   copyright and license terms.\n#\n# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n\nimport logging\n\nimport os\nfrom os.path import (\n    join as opj,\n    isabs,\n)\nfrom collections import OrderedDict\n\nfrom datalad.distribution.dataset import Dataset\n\nfrom datalad.utils import (\n    Path,\n    PurePosixPath,\n    on_windows,\n)\n\nfrom datalad.tests.utils import (\n    eq_,\n    neq_,\n    ok_,\n    nok_,\n    assert_raises,\n    skip_if_on_windows,\n    swallow_logs,\n    assert_in,\n    get_most_obscure_supported_name,\n    SkipTest,\n    known_failure_githubci_win,\n    with_tempfile,\n    assert_status,\n)\n\nfrom datalad.support.network import (\n    DataLadRI,\n    GitTransportRI,\n    PathRI,\n    RI,\n    SSHRI,\n    URL,\n    _split_colon,\n    dlurljoin,\n    get_local_file_url,\n    get_response_disposition_filename,\n    get_tld,\n    get_url_straight_filename,\n    is_datalad_compat_ri,\n    is_ssh,\n    is_url,\n    iso8601_to_epoch,\n    parse_url_opts,\n    same_website,\n)\n\n\ndef test_same_website():\n    ok_(same_website(\"http://a.b\", \"http://a.b/2014/01/xxx/\"))\n    ok_(same_website(\"http://a.b/page/2/\", \"http://a.b/2014/01/xxx/\"))\n    ok_(same_website(\"https://a.b/page/2/\", \"http://a.b/2014/01/xxx/\"))\n    ok_(same_website(\"http://a.b/page/2/\", \"https://a.b/2014/01/xxx/\"))\n\n\ndef test_get_tld():\n    eq_(get_tld('http://example.com'), 'example.com')\n    eq_(get_tld('http://example.com/1'), 'example.com')\n    eq_(get_tld('http://example.com/1/2'), 'example.com')\n    eq_(get_tld('example.com/1/2'), 'example.com')\n    eq_(get_tld('s3://example.com/1/2'), 'example.com')\n    assert_raises(ValueError, get_tld, \"\")\n    assert_raises(ValueError, get_tld, \"s3://\")\n    assert_raises(ValueError, get_tld, \"http://\")\n\ndef test_dlurljoin():\n    eq_(dlurljoin('http://a.b/', 'f'), 'http://a.b/f')\n    eq_(dlurljoin('http://a.b/page', 'f'), 'http://a.b/f')\n    eq_(dlurljoin('http://a.b/dir/', 'f'), 'http://a.b/dir/f')\n    eq_(dlurljoin('http://a.b/dir/', 'http://url'), 'http://url')\n    eq_(dlurljoin('http://a.b/dir/', '/'), 'http://a.b/')\n    eq_(dlurljoin('http://a.b/dir/', '/x/y'), 'http://a.b/x/y')\n\ndef _test_get_url_straight_filename(suf):\n    eq_(get_url_straight_filename('http://a.b/' + suf), '')\n    eq_(get_url_straight_filename('http://a.b/p1' + suf), 'p1')\n    eq_(get_url_straight_filename('http://a.b/p1/' + suf), '')\n    eq_(get_url_straight_filename('http://a.b/p1/' + suf, allowdir=True), 'p1')\n    eq_(get_url_straight_filename('http://a.b/p1/p2' + suf), 'p2')\n    eq_(get_url_straight_filename('http://a.b/p1/p2/' + suf), '')\n    eq_(get_url_straight_filename('http://a.b/p1/p2/' + suf, allowdir=True), 'p2')\n    eq_(get_url_straight_filename('http://a.b/p1/p2/' + suf, allowdir=True, strip=('p2', 'xxx')), 'p1')\n    eq_(get_url_straight_filename('http://a.b/p1/p2/' + suf, strip=('p2', 'xxx')), '')\n\ndef test_get_url_straight_filename():\n    yield _test_get_url_straight_filename, ''\n    yield _test_get_url_straight_filename, '#'\n    yield _test_get_url_straight_filename, '#tag'\n    yield _test_get_url_straight_filename, '#tag/obscure'\n    yield _test_get_url_straight_filename, '?param=1'\n    yield _test_get_url_straight_filename, '?param=1&another=/'\n\nfrom ..network import rfc2822_to_epoch\ndef test_rfc2822_to_epoch():\n    eq_(rfc2822_to_epoch(\"Thu, 16 Oct 2014 01:16:17 EDT\"), 1413436577)\n\n\ndef test_get_response_disposition_filename():\n    eq_(get_response_disposition_filename('attachment;filename=\"Part1-Subjects1-99.tar\"'), \"Part1-Subjects1-99.tar\")\n    eq_(get_response_disposition_filename('attachment'), None)\n\n\ndef test_parse_url_opts():\n    url = 'http://map.org/api/download/?id=157'\n    output = parse_url_opts(url)\n    eq_(output, ('http://map.org/api/download/', {'id': '157'}))\n\n    url = 's3://bucket/save/?key=891'\n    output = parse_url_opts(url)\n    eq_(output, ('s3://bucket/save/', {'key': '891'}))\n\n    url = 'http://map.org/api/download/?id=98&code=13'\n    output = parse_url_opts(url)\n    eq_(output, ('http://map.org/api/download/', {'id': '98', 'code': '13'}))\n\n\ndef test_split_colon():\n    eq_(_split_colon('a:b'), ['a', 'b'])\n    eq_(_split_colon('a:b:c'), ['a', 'b:c'])\n    eq_(_split_colon('a:b:c', 2), ['a', 'b', 'c'])\n    eq_(_split_colon('ab'), ['ab'])\n    eq_(_split_colon(r'a\\:b'), [r'a\\:b'])\n\n\ndef test_url_eq():\n    eq_(URL(), URL())\n    # doesn't make sense to ask what kind of a url it is an empty URL\n    #eq_(RI(), RI())\n    neq_(URL(), URL(hostname='x'))\n    # Different types aren't equal even if have the same fields values\n    neq_(URL(path='x'), PathRI(path='x'))\n    neq_(URL(hostname='x'), SSHRI(hostname='x'))\n    neq_(str(URL(hostname='x')), str(SSHRI(hostname='x')))\n\n\ndef _check_ri(ri, cls, exact_str=True, localpath=None, **fields):\n    \"\"\"just a helper to carry out few checks on urls\"\"\"\n    with swallow_logs(new_level=logging.DEBUG) as cml:\n        ri_ = cls(**fields)\n        murl = RI(ri)\n        eq_(murl.__class__, cls)  # not just a subclass\n        eq_(murl, ri_)\n        if isinstance(ri, str):\n            eq_(str(RI(ri)), ri)\n        eq_(eval(repr(ri_)), ri)  # repr leads back to identical ri_\n        eq_(ri, ri_)  # just in case ;)  above should fail first if smth is wrong\n        if not exact_str:\n            assert_in('Parsed version of', cml.out)\n    (eq_ if exact_str else neq_)(str(ri), str(ri_))  # that we can reconstruct it EXACTLY on our examples\n    # and that we have access to all those fields\n    nok_(set(fields).difference(set(cls._FIELDS)))\n    for f, v in fields.items():\n        eq_(getattr(ri_, f), v)\n\n    if localpath:\n        eq_(ri_.localpath, localpath)\n        old_localpath = ri_.localpath  # for a test below\n    else:\n        # if not given -- must be a remote url, should raise exception\n        with assert_raises(ValueError):\n            ri_.localpath\n\n    # This one does not have a path. TODO: either proxy path from its .RI or adjust\n    # hierarchy of classes to make it more explicit\n    if cls == GitTransportRI:\n        return\n    # do changes in the path persist?\n    old_str = str(ri_)\n    ri_.path = newpath = opj(ri_.path, 'sub')\n    eq_(ri_.path, newpath)\n    neq_(str(ri_), old_str)\n    if localpath:\n        eq_(ri_.localpath, opj(old_localpath, 'sub'))\n\n\n\ndef test_url_base():\n    # Basic checks\n    assert_raises(ValueError, URL, \"http://example.com\", hostname='example.com')\n    url = URL(\"http://example.com\")\n    eq_(url.hostname, 'example.com')\n    eq_(url.scheme, 'http')\n    eq_(url.port, '')  # not specified -- empty strings\n    eq_(url.username, '')  # not specified -- empty strings\n    eq_(repr(url), \"URL(hostname='example.com', scheme='http')\")\n    eq_(url, \"http://example.com\")  # automagic coercion in __eq__\n\n    neq_(URL(), URL(hostname='x'))\n\n    smth = URL('smth')\n    eq_(smth.hostname, '')\n    ok_(bool(smth))\n    nok_(bool(URL()))\n\n    assert_raises(ValueError, url._set_from_fields, unknown='1')\n\n    with swallow_logs(new_level=logging.WARNING) as cml:\n        # we don't \"care\" about params ATM so there is a warning if there are any\n        purl = URL(\"http://example.com/;param\")\n        eq_(str(purl), 'http://example.com/;param')  # but we do maintain original string\n        assert_in('ParseResults contains params', cml.out)\n        eq_(purl.as_str(), 'http://example.com/')\n\n\n@known_failure_githubci_win\ndef test_url_samples():\n    _check_ri(\"http://example.com\", URL, scheme='http', hostname=\"example.com\")\n    # \"complete\" one for classical http\n    _check_ri(\"http://user:pw@example.com:8080/p/sp?p1=v1&p2=v2#frag\", URL,\n              scheme='http', hostname=\"example.com\", port=8080,\n              username='user', password='pw', path='/p/sp',\n              query='p1=v1&p2=v2', fragment='frag')\n\n    # sample one for ssh with specifying the scheme\n    # XXX? might be useful?  https://github.com/FriendCode/giturlparse.py\n    _check_ri(\"ssh://host/path/sp1\", URL, scheme='ssh', hostname='host', path='/path/sp1')\n    _check_ri(\"user@host:path/sp1\", SSHRI,\n              hostname='host', path='path/sp1', username='user')\n    _check_ri(\"host:path/sp1\", SSHRI, hostname='host', path='path/sp1')\n    _check_ri(\"host:path\", SSHRI, hostname='host', path='path')\n    _check_ri(\"host:/path\", SSHRI, hostname='host', path='/path')\n    _check_ri(\"user@host\", SSHRI, hostname='host', username='user')\n    # TODO!!!  should this be a legit URL like this?\n    # _check_ri(\"host\", SSHRI, hostname='host'))\n    eq_(repr(RI(\"host:path\")), \"SSHRI(hostname='host', path='path')\")\n\n    # And now perspective 'datalad', implicit=True urls pointing to the canonical center location\n    _check_ri(\"///\", DataLadRI)\n    _check_ri(\"///p/s1\", DataLadRI, path='p/s1')\n    # could be considered by someone as \"URI reference\" relative to scheme\n    _check_ri(\"//a/\", DataLadRI, remote='a')\n    _check_ri(\"//a/data\", DataLadRI, path='data', remote='a')\n\n    # here we will do custom magic allowing only schemes with + in them, such as dl+archive\n    # or not so custom as\n    _check_ri(\"hg+https://host/user/proj\", URL,\n              scheme=\"hg+https\", hostname='host', path='/user/proj')\n    # \"old\" style\n    _check_ri(\"dl+archive:KEY/path/sp1#size=123\", URL,\n              scheme='dl+archive', path='KEY/path/sp1', fragment='size=123')\n    # \"new\" style\n    _check_ri(\"dl+archive:KEY#path=path/sp1&size=123\", URL,\n              scheme='dl+archive', path='KEY', fragment='path=path/sp1&size=123')\n    # actually above one is probably wrong since we need to encode the path\n    _check_ri(\"dl+archive:KEY#path=path%2Fbsp1&size=123\", URL,\n              scheme='dl+archive', path='KEY', fragment='path=path%2Fbsp1&size=123')\n\n    #https://en.wikipedia.org/wiki/File_URI_scheme\n    _check_ri(\"file://host\", URL, scheme='file', hostname='host')\n    _check_ri(\"file://host/path/sp1\", URL, scheme='file', hostname='host', path='/path/sp1')\n    # stock libraries of Python aren't quite ready for ipv6\n    ipv6address = '2001:db8:85a3::8a2e:370:7334'\n    _check_ri(\"file://%s/path/sp1\" % ipv6address, URL,\n              scheme='file', hostname=ipv6address, path='/path/sp1')\n    for lh in ('localhost', '::1', '', '127.3.4.155'):\n        _check_ri(\"file://%s/path/sp1\" % lh, URL, localpath='/path/sp1',\n                  scheme='file', hostname=lh, path='/path/sp1')\n    _check_ri('http://[1fff:0:a88:85a3::ac1f]:8001/index.html', URL,\n              scheme='http', hostname='1fff:0:a88:85a3::ac1f', port=8001, path='/index.html')\n    _check_ri(\"file:///path/sp1\", URL, localpath='/path/sp1', scheme='file', path='/path/sp1')\n    # we don't do any magical comprehension for home paths/drives for windows\n    # of file:// urls, thus leaving /~ and /c: for now:\n    _check_ri(\"file:///~/path/sp1\", URL, localpath='/~/path/sp1', scheme='file', path='/~/path/sp1')\n    _check_ri(\"file:///%7E/path/sp1\", URL, localpath='/~/path/sp1', scheme='file', path='/~/path/sp1', exact_str=False)\n    # not sure but let's check\n    _check_ri(\"file:///c:/path/sp1\", URL, localpath='/c:/path/sp1', scheme='file', path='/c:/path/sp1', exact_str=False)\n\n    # and now implicit paths or actually they are also \"URI references\"\n    _check_ri(\"f\", PathRI, localpath='f', path='f')\n    _check_ri(\"f/s1\", PathRI, localpath='f/s1', path='f/s1')\n    _check_ri(PurePosixPath(\"f\"), PathRI, localpath='f', path='f')\n    _check_ri(PurePosixPath(\"f/s1\"), PathRI, localpath='f/s1', path='f/s1')\n    # colons are problematic and might cause confusion into SSHRI\n    _check_ri(\"f/s:1\", PathRI, localpath='f/s:1', path='f/s:1')\n    _check_ri(\"f/s:\", PathRI, localpath='f/s:', path='f/s:')\n    _check_ri(\"/f\", PathRI, localpath='/f', path='/f')\n    _check_ri(\"/f/s1\", PathRI, localpath='/f/s1', path='/f/s1')\n\n    # some github ones, just to make sure\n    _check_ri(\"git://host/user/proj\", URL, scheme=\"git\", hostname=\"host\", path=\"/user/proj\")\n    _check_ri(\"git@host:user/proj\", SSHRI, hostname=\"host\", path=\"user/proj\", username='git')\n\n    _check_ri('weired:/', SSHRI, hostname='weired', path='/')\n    # since schema is not allowing some symbols so we need to add additional check\n    _check_ri('weired_url:/', SSHRI, hostname='weired_url', path='/')\n    _check_ri('example.com:/', SSHRI, hostname='example.com', path='/')\n    _check_ri('example.com:path/sp1', SSHRI, hostname='example.com', path='path/sp1')\n    _check_ri('example.com/path/sp1\\:fname', PathRI, localpath='example.com/path/sp1\\:fname',\n              path='example.com/path/sp1\\:fname')\n    # ssh is as stupid as us, so we will stay \"Consistently\" dumb\n    \"\"\"\n    $> ssh example.com/path/sp1:fname\n    ssh: Could not resolve hostname example.com/path/sp1:fname: Name or service not known\n\n    edit 20190516 yoh: but this looks like a perfectly valid path.\n    SSH knows that it is not a path but its SSHRI so it can stay dumb.\n    We are trying to be smart and choose between RIs (even when we know that\n    it is e.g. a file).\n    \"\"\"\n    _check_ri('e.com/p/sp:f', PathRI, localpath='e.com/p/sp:f', path='e.com/p/sp:f')\n    _check_ri('user@someho.st/mydir', PathRI, localpath='user@someho.st/mydir', path='user@someho.st/mydir')\n\n    # SSHRIs have .port, but it is empty\n    eq_(SSHRI(hostname='example.com').port, '')\n\n    # check that we are getting a warning logged when url can't be reconstructed\n    # precisely\n    # actually failed to come up with one -- becomes late here\n    #_check_ri(\"http://host///..//p\", scheme='http', path='/..//p')\n\n    # actually this one is good enough to trigger a warning and I still don't know\n    # what it should exactly be!?\n    with swallow_logs(new_level=logging.DEBUG) as cml:\n        weired_str = 'weired://'\n        weired_url = RI(weired_str)\n        repr(weired_url)\n        cml.assert_logged(\n            'Parsed version of SSHRI .weired:/. '\n            'differs from original .weired://.'\n        )\n        # but we store original str\n        eq_(str(weired_url), weired_str)\n        neq_(weired_url.as_str(), weired_str)\n\n\n    raise SkipTest(\"TODO: file://::1/some does complain about parsed version dropping ::1\")\n\n\ndef test_git_transport_ri():\n    _check_ri(\"gcrypt::http://somewhere\", GitTransportRI, RI='http://somewhere', transport='gcrypt')\n    # man git-push says\n    #  <transport>::<address>\n    #    where <address> may be a path, a server and path, or an arbitrary URL-like string...\n    # so full path to my.com/... should be ok?\n    _check_ri(\"http::/my.com/some/path\", GitTransportRI, RI='/my.com/some/path', transport='http')\n    # some ssh server.  And we allow for some additional chars in transport.\n    # Git doesn't define since it does not care! we will then be flexible too\n    _check_ri(\"trans-port::server:path\", GitTransportRI, RI='server:path', transport='trans-port')\n\n\ndef _test_url_quote_path(cls, clskwargs, target_url):\n    path = '/ \"\\';a&b&cd `| '\n    if not (cls is PathRI):\n        clskwargs['hostname'] = hostname = 'example.com'\n    url = cls(path=path, **clskwargs)\n    eq_(url.path, path)\n    if 'hostname' in clskwargs:\n        eq_(url.hostname, hostname)\n    # all nasty symbols should be quoted\n    url_str = str(url)\n    eq_(url_str, target_url)\n    # no side-effects:\n    eq_(url.path, path)\n    if 'hostname' in clskwargs:\n        eq_(url.hostname, hostname)\n\n    # and figured out and unquoted\n    url_ = RI(url_str)\n    ok_(isinstance(url_, cls))\n    eq_(url_.path, path)\n    if 'hostname' in clskwargs:\n        eq_(url.hostname, hostname)\n\n\ndef test_url_quote_path():\n    yield _test_url_quote_path, SSHRI, {}, r'example.com:/ \"' + r\"';a&b&cd `| \"\n    yield _test_url_quote_path, URL, {'scheme': \"http\"}, 'http://example.com/%20%22%27%3Ba%26b%26cd%20%60%7C%20'\n    yield _test_url_quote_path, PathRI, {}, r'/ \"' + r\"';a&b&cd `| \"  # nothing is done to file:implicit\n\n\ndef test_url_compose_archive_one():\n    url = URL(scheme='dl+archive', path='KEY',\n              fragment=OrderedDict((('path', 'f/p/ s+'), ('size', 30))))\n    # funny - space is encoded as + but + is %2B\n    eq_(str(url), 'dl+archive:KEY#path=f/p/+s%2B&size=30')\n    eq_(url.fragment_dict, {'path': 'f/p/ s+', 'size': '30'})\n\n\ndef test_url_fragments_and_query():\n    url = URL(hostname=\"host\", query=OrderedDict((('a', 'x/b'), ('b', 'y'))))\n    eq_(str(url), '//host?a=x%2Fb&b=y')\n    eq_(url.query, 'a=x%2Fb&b=y')\n    eq_(url.query_dict, {'a': 'x/b', 'b': 'y'})\n\n    url = URL(hostname=\"host\", fragment=OrderedDict((('b', 'x/b'), ('a', 'y'))))\n    eq_(str(url), '//host#b=x/b&a=y')\n    eq_(url.fragment, 'b=x/b&a=y')\n    eq_(url.fragment_dict, {'a': 'y', 'b': 'x/b'})\n\n    fname = get_most_obscure_supported_name()\n    url = URL(hostname=\"host\", fragment={'a': fname})\n    eq_(url.fragment_dict, {'a': fname})\n\n\ndef test_url_dicts():\n    eq_(URL(\"http://host\").query_dict, {})\n\n\n@skip_if_on_windows\ndef test_get_url_path_on_fileurls():\n    eq_(URL('file:///a').path, '/a')\n    eq_(URL('file:///a/b').path, '/a/b')\n    eq_(URL('file:///a/b').localpath, '/a/b')\n    eq_(URL('file:///a/b#id').path, '/a/b')\n    eq_(URL('file:///a/b?whatever').path, '/a/b')\n\n\ndef test_is_url():\n    ok_(is_url('file://localhost/some'))\n    ok_(is_url('http://localhost'))\n    ok_(is_url('ssh://me@localhost'))\n    # in current understanding it is indeed a url but an 'ssh', implicit=True, not just\n    # a useless scheme=weired with a hope to point to a netloc\n    with swallow_logs():\n        ok_(is_url('weired://'))\n    nok_(is_url('relative'))\n    nok_(is_url('/absolute'))\n    ok_(is_url('like@sshlogin'))  # actually we do allow ssh:implicit urls ATM\n    nok_(is_url(''))\n    nok_(is_url(' '))\n    nok_(is_url(123))  # stuff of other types wouldn't be considered a URL\n\n    # we can pass RI instance directly\n    ok_(is_url(RI('file://localhost/some')))\n    nok_(is_url(RI('relative')))\n\n\n# TODO: RF with test_is_url to avoid duplication\ndef test_is_datalad_compat_ri():\n    ok_(is_datalad_compat_ri('ssh://user:passw@host/path'))\n    ok_(is_datalad_compat_ri('http://example.com'))\n    ok_(is_datalad_compat_ri('file://localhost/some'))\n    ok_(is_datalad_compat_ri('///localhost/some'))\n    nok_(is_datalad_compat_ri('relative'))\n    nok_(is_datalad_compat_ri('.///localhost/some'))\n    nok_(is_datalad_compat_ri(123))\n\n\ndef test_get_local_file_url():\n    for path, url in (\n                # relpaths are special-cased below\n                ('test.txt', 'test.txt'),\n            ) + (\n                ('C:\\\\Windows\\\\notepad.exe', 'file://C/Windows/notepad.exe'),\n            ) if on_windows else (\n                # static copy of \"most_obscore_name\"\n                (' \"\\';a&b&cΔЙקم๗あ `| ',\n                 # and translation by google chrome\n                 \"%20%22%27%3Ba%26b%26c%CE%94%D0%99%D7%A7%D9%85%E0%B9%97%E3%81%82%20%60%7C%20\"),\n                ('/a', 'file:///a'),\n                ('/a/b/c', 'file:///a/b/c'),\n                ('/a~', 'file:///a~'),\n                # there are no files with trailing slashes in the name\n                #('/a b/', 'file:///a%20b/'),\n                ('/a b/name', 'file:///a%20b/name'),\n            ):\n        if isabs(path):\n            eq_(get_local_file_url(path), url)\n        else:\n            eq_(get_local_file_url(path),\n                '/'.join((\n                    get_local_file_url(os.getcwd()),\n                    url))\n            )\n\n\n@with_tempfile(mkdir=True)\ndef test_get_local_file_url_compatibility(path):\n    # smoke test for file:// URL compatibility with other datalad/git/annex\n    # pieces\n    path = Path(path)\n    ds1 = Dataset(path / 'ds1').create()\n    ds2 = Dataset(path / 'ds2').create()\n    testfile = path / 'testfile.txt'\n    testfile.write_text('some')\n\n    # compat with annex addurl\n    ds1.repo.add_url_to_file(\n        'test.txt',\n        get_local_file_url(testfile, compatibility='git-annex'))\n\n    # compat with git clone/submodule\n    assert_status(\n        'ok',\n        ds1.clone(get_local_file_url(ds2.path, compatibility='git'),\n                  result_xfm=None, return_type='generator'))\n\n\ndef test_is_ssh():\n\n    ssh_locators = [\"ssh://host\",\n                    \"ssh://host/some/where\",\n                    \"user@host:path/sp1\",\n                    \"user@host:/absolute/path/sp1\",\n                    \"host:path/sp1\",\n                    \"host:/absolute/path/sp1\",\n                    \"user@host\"]\n    for ri in ssh_locators:\n        ok_(is_ssh(ri), \"not considered ssh (string): %s\" % ri)\n        ok_(is_ssh(RI(ri)), \"not considered ssh (RI): %s\" % ri)\n\n    non_ssh_locators = [\"file://path/to\",\n                        \"/abs/path\",\n                        \"../rel/path\",\n                        \"http://example.com\",\n                        \"git://host/user/proj\",\n                        \"s3://bucket/save/?key=891\"]\n    for ri in non_ssh_locators:\n        ok_(not is_ssh(ri), \"considered ssh (string): %s\" % ri)\n        ok_(not is_ssh(RI(ri)), \"considered ssh (RI): %s\" % ri)\n\n\ndef test_iso8601_to_epoch():\n    epoch = 1467901515\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15+00:00'), epoch)\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15+11:00'),\n        epoch - 11 * 60 * 60)\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15Z'), epoch)\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15'), epoch)\n\n    eq_(iso8601_to_epoch('2016-07-07T14:25:14'), epoch-1)\n",
            "header": {
                "file": {
                    "path": "datalad/support/tests/test_network.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "f585f3d8a",
                "new_commit_hash": "49237f44a",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/datalad/support/tests/test_network.py"
            },
            "plus_file": {
                "path": "b/datalad/support/tests/test_network.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 529,
                            "length": 8
                        },
                        "new_range": {
                            "start": 529,
                            "length": 8
                        },
                        "section": "def test_is_ssh():"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "def test_iso8601_to_epoch():",
                                "type": "context"
                            },
                            {
                                "content": "    epoch = 1467901515",
                                "type": "context"
                            },
                            {
                                "content": "    eq_(iso8601_to_epoch('2016-07-07T14:25:15+00:00'), epoch)",
                                "type": "context"
                            },
                            {
                                "content": "    # zone information is actually not used",
                                "type": "deleted"
                            },
                            {
                                "content": "    eq_(iso8601_to_epoch('2016-07-07T14:25:15+11:00'), epoch)",
                                "type": "deleted"
                            },
                            {
                                "content": "    eq_(iso8601_to_epoch('2016-07-07T14:25:15+11:00'),",
                                "type": "added"
                            },
                            {
                                "content": "        epoch - 11 * 60 * 60)",
                                "type": "added"
                            },
                            {
                                "content": "    eq_(iso8601_to_epoch('2016-07-07T14:25:15Z'), epoch)",
                                "type": "context"
                            },
                            {
                                "content": "    eq_(iso8601_to_epoch('2016-07-07T14:25:15'), epoch)",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "datalad/support/tests/test_network.py",
                            "type": "function",
                            "name": "test_iso8601_to_epoch",
                            "content": "def test_iso8601_to_epoch():\n    epoch = 1467901515\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15+00:00'), epoch)\n    # zone information is actually not used\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15+11:00'), epoch)\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15Z'), epoch)\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15'), epoch)\n\n    eq_(iso8601_to_epoch('2016-07-07T14:25:14'), epoch-1)",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 529,
                            "end_lineno": 537,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                }
            ]
        }
    ],
    "old_commit_hash": "3a85e9a1603f2f47ad768eb729f984bead61d681^",
    "new_commit_hash": "3a85e9a1603f2f47ad768eb729f984bead61d681",
    "commit_message": "BF: make iso8601_to_epoch respect time zone\n\nThanks @kyleam for the patch",
    "commit_date": "2020-09-17T12:51:17-04:00",
    "metadata": {}
}
{
    "repo_name": "datalad",
    "new_commit_hash": "3a85e9a1603f2f47ad768eb729f984bead61d681",
    "test_file_codes": [
        "# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-\n# ex: set sts=4 ts=4 sw=4 noet:\n# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n#\n#   See COPYING file distributed along with the datalad package for the\n#   copyright and license terms.\n#\n# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n\nimport logging\n\nimport os\nfrom os.path import (\n    join as opj,\n    isabs,\n)\nfrom collections import OrderedDict\n\nfrom datalad.distribution.dataset import Dataset\n\nfrom datalad.utils import (\n    Path,\n    PurePosixPath,\n    on_windows,\n)\n\nfrom datalad.tests.utils import (\n    eq_,\n    neq_,\n    ok_,\n    nok_,\n    assert_raises,\n    skip_if_on_windows,\n    swallow_logs,\n    assert_in,\n    get_most_obscure_supported_name,\n    SkipTest,\n    known_failure_githubci_win,\n    with_tempfile,\n    assert_status,\n)\n\nfrom datalad.support.network import (\n    DataLadRI,\n    GitTransportRI,\n    PathRI,\n    RI,\n    SSHRI,\n    URL,\n    _split_colon,\n    dlurljoin,\n    get_local_file_url,\n    get_response_disposition_filename,\n    get_tld,\n    get_url_straight_filename,\n    is_datalad_compat_ri,\n    is_ssh,\n    is_url,\n    iso8601_to_epoch,\n    parse_url_opts,\n    same_website,\n)\n\n\ndef test_same_website():\n    ok_(same_website(\"http://a.b\", \"http://a.b/2014/01/xxx/\"))\n    ok_(same_website(\"http://a.b/page/2/\", \"http://a.b/2014/01/xxx/\"))\n    ok_(same_website(\"https://a.b/page/2/\", \"http://a.b/2014/01/xxx/\"))\n    ok_(same_website(\"http://a.b/page/2/\", \"https://a.b/2014/01/xxx/\"))\n\n\ndef test_get_tld():\n    eq_(get_tld('http://example.com'), 'example.com')\n    eq_(get_tld('http://example.com/1'), 'example.com')\n    eq_(get_tld('http://example.com/1/2'), 'example.com')\n    eq_(get_tld('example.com/1/2'), 'example.com')\n    eq_(get_tld('s3://example.com/1/2'), 'example.com')\n    assert_raises(ValueError, get_tld, \"\")\n    assert_raises(ValueError, get_tld, \"s3://\")\n    assert_raises(ValueError, get_tld, \"http://\")\n\ndef test_dlurljoin():\n    eq_(dlurljoin('http://a.b/', 'f'), 'http://a.b/f')\n    eq_(dlurljoin('http://a.b/page', 'f'), 'http://a.b/f')\n    eq_(dlurljoin('http://a.b/dir/', 'f'), 'http://a.b/dir/f')\n    eq_(dlurljoin('http://a.b/dir/', 'http://url'), 'http://url')\n    eq_(dlurljoin('http://a.b/dir/', '/'), 'http://a.b/')\n    eq_(dlurljoin('http://a.b/dir/', '/x/y'), 'http://a.b/x/y')\n\ndef _test_get_url_straight_filename(suf):\n    eq_(get_url_straight_filename('http://a.b/' + suf), '')\n    eq_(get_url_straight_filename('http://a.b/p1' + suf), 'p1')\n    eq_(get_url_straight_filename('http://a.b/p1/' + suf), '')\n    eq_(get_url_straight_filename('http://a.b/p1/' + suf, allowdir=True), 'p1')\n    eq_(get_url_straight_filename('http://a.b/p1/p2' + suf), 'p2')\n    eq_(get_url_straight_filename('http://a.b/p1/p2/' + suf), '')\n    eq_(get_url_straight_filename('http://a.b/p1/p2/' + suf, allowdir=True), 'p2')\n    eq_(get_url_straight_filename('http://a.b/p1/p2/' + suf, allowdir=True, strip=('p2', 'xxx')), 'p1')\n    eq_(get_url_straight_filename('http://a.b/p1/p2/' + suf, strip=('p2', 'xxx')), '')\n\ndef test_get_url_straight_filename():\n    yield _test_get_url_straight_filename, ''\n    yield _test_get_url_straight_filename, '#'\n    yield _test_get_url_straight_filename, '#tag'\n    yield _test_get_url_straight_filename, '#tag/obscure'\n    yield _test_get_url_straight_filename, '?param=1'\n    yield _test_get_url_straight_filename, '?param=1&another=/'\n\nfrom datalad.support.network import rfc2822_to_epoch\ndef test_rfc2822_to_epoch():\n    eq_(rfc2822_to_epoch(\"Thu, 16 Oct 2014 01:16:17 EDT\"), 1413436577)\n\n\ndef test_get_response_disposition_filename():\n    eq_(get_response_disposition_filename('attachment;filename=\"Part1-Subjects1-99.tar\"'), \"Part1-Subjects1-99.tar\")\n    eq_(get_response_disposition_filename('attachment'), None)\n\n\ndef test_parse_url_opts():\n    url = 'http://map.org/api/download/?id=157'\n    output = parse_url_opts(url)\n    eq_(output, ('http://map.org/api/download/', {'id': '157'}))\n\n    url = 's3://bucket/save/?key=891'\n    output = parse_url_opts(url)\n    eq_(output, ('s3://bucket/save/', {'key': '891'}))\n\n    url = 'http://map.org/api/download/?id=98&code=13'\n    output = parse_url_opts(url)\n    eq_(output, ('http://map.org/api/download/', {'id': '98', 'code': '13'}))\n\n\ndef test_split_colon():\n    eq_(_split_colon('a:b'), ['a', 'b'])\n    eq_(_split_colon('a:b:c'), ['a', 'b:c'])\n    eq_(_split_colon('a:b:c', 2), ['a', 'b', 'c'])\n    eq_(_split_colon('ab'), ['ab'])\n    eq_(_split_colon(r'a\\:b'), [r'a\\:b'])\n\n\ndef test_url_eq():\n    eq_(URL(), URL())\n    # doesn't make sense to ask what kind of a url it is an empty URL\n    #eq_(RI(), RI())\n    neq_(URL(), URL(hostname='x'))\n    # Different types aren't equal even if have the same fields values\n    neq_(URL(path='x'), PathRI(path='x'))\n    neq_(URL(hostname='x'), SSHRI(hostname='x'))\n    neq_(str(URL(hostname='x')), str(SSHRI(hostname='x')))\n\n\ndef _check_ri(ri, cls, exact_str=True, localpath=None, **fields):\n    \"\"\"just a helper to carry out few checks on urls\"\"\"\n    with swallow_logs(new_level=logging.DEBUG) as cml:\n        ri_ = cls(**fields)\n        murl = RI(ri)\n        eq_(murl.__class__, cls)  # not just a subclass\n        eq_(murl, ri_)\n        if isinstance(ri, str):\n            eq_(str(RI(ri)), ri)\n        eq_(eval(repr(ri_)), ri)  # repr leads back to identical ri_\n        eq_(ri, ri_)  # just in case ;)  above should fail first if smth is wrong\n        if not exact_str:\n            assert_in('Parsed version of', cml.out)\n    (eq_ if exact_str else neq_)(str(ri), str(ri_))  # that we can reconstruct it EXACTLY on our examples\n    # and that we have access to all those fields\n    nok_(set(fields).difference(set(cls._FIELDS)))\n    for f, v in fields.items():\n        eq_(getattr(ri_, f), v)\n\n    if localpath:\n        eq_(ri_.localpath, localpath)\n        old_localpath = ri_.localpath  # for a test below\n    else:\n        # if not given -- must be a remote url, should raise exception\n        with assert_raises(ValueError):\n            ri_.localpath\n\n    # This one does not have a path. TODO: either proxy path from its .RI or adjust\n    # hierarchy of classes to make it more explicit\n    if cls == GitTransportRI:\n        return\n    # do changes in the path persist?\n    old_str = str(ri_)\n    ri_.path = newpath = opj(ri_.path, 'sub')\n    eq_(ri_.path, newpath)\n    neq_(str(ri_), old_str)\n    if localpath:\n        eq_(ri_.localpath, opj(old_localpath, 'sub'))\n\n\n\ndef test_url_base():\n    # Basic checks\n    assert_raises(ValueError, URL, \"http://example.com\", hostname='example.com')\n    url = URL(\"http://example.com\")\n    eq_(url.hostname, 'example.com')\n    eq_(url.scheme, 'http')\n    eq_(url.port, '')  # not specified -- empty strings\n    eq_(url.username, '')  # not specified -- empty strings\n    eq_(repr(url), \"URL(hostname='example.com', scheme='http')\")\n    eq_(url, \"http://example.com\")  # automagic coercion in __eq__\n\n    neq_(URL(), URL(hostname='x'))\n\n    smth = URL('smth')\n    eq_(smth.hostname, '')\n    ok_(bool(smth))\n    nok_(bool(URL()))\n\n    assert_raises(ValueError, url._set_from_fields, unknown='1')\n\n    with swallow_logs(new_level=logging.WARNING) as cml:\n        # we don't \"care\" about params ATM so there is a warning if there are any\n        purl = URL(\"http://example.com/;param\")\n        eq_(str(purl), 'http://example.com/;param')  # but we do maintain original string\n        assert_in('ParseResults contains params', cml.out)\n        eq_(purl.as_str(), 'http://example.com/')\n\n\n@known_failure_githubci_win\ndef test_url_samples():\n    _check_ri(\"http://example.com\", URL, scheme='http', hostname=\"example.com\")\n    # \"complete\" one for classical http\n    _check_ri(\"http://user:pw@example.com:8080/p/sp?p1=v1&p2=v2#frag\", URL,\n              scheme='http', hostname=\"example.com\", port=8080,\n              username='user', password='pw', path='/p/sp',\n              query='p1=v1&p2=v2', fragment='frag')\n\n    # sample one for ssh with specifying the scheme\n    # XXX? might be useful?  https://github.com/FriendCode/giturlparse.py\n    _check_ri(\"ssh://host/path/sp1\", URL, scheme='ssh', hostname='host', path='/path/sp1')\n    _check_ri(\"user@host:path/sp1\", SSHRI,\n              hostname='host', path='path/sp1', username='user')\n    _check_ri(\"host:path/sp1\", SSHRI, hostname='host', path='path/sp1')\n    _check_ri(\"host:path\", SSHRI, hostname='host', path='path')\n    _check_ri(\"host:/path\", SSHRI, hostname='host', path='/path')\n    _check_ri(\"user@host\", SSHRI, hostname='host', username='user')\n    # TODO!!!  should this be a legit URL like this?\n    # _check_ri(\"host\", SSHRI, hostname='host'))\n    eq_(repr(RI(\"host:path\")), \"SSHRI(hostname='host', path='path')\")\n\n    # And now perspective 'datalad', implicit=True urls pointing to the canonical center location\n    _check_ri(\"///\", DataLadRI)\n    _check_ri(\"///p/s1\", DataLadRI, path='p/s1')\n    # could be considered by someone as \"URI reference\" relative to scheme\n    _check_ri(\"//a/\", DataLadRI, remote='a')\n    _check_ri(\"//a/data\", DataLadRI, path='data', remote='a')\n\n    # here we will do custom magic allowing only schemes with + in them, such as dl+archive\n    # or not so custom as\n    _check_ri(\"hg+https://host/user/proj\", URL,\n              scheme=\"hg+https\", hostname='host', path='/user/proj')\n    # \"old\" style\n    _check_ri(\"dl+archive:KEY/path/sp1#size=123\", URL,\n              scheme='dl+archive', path='KEY/path/sp1', fragment='size=123')\n    # \"new\" style\n    _check_ri(\"dl+archive:KEY#path=path/sp1&size=123\", URL,\n              scheme='dl+archive', path='KEY', fragment='path=path/sp1&size=123')\n    # actually above one is probably wrong since we need to encode the path\n    _check_ri(\"dl+archive:KEY#path=path%2Fbsp1&size=123\", URL,\n              scheme='dl+archive', path='KEY', fragment='path=path%2Fbsp1&size=123')\n\n    #https://en.wikipedia.org/wiki/File_URI_scheme\n    _check_ri(\"file://host\", URL, scheme='file', hostname='host')\n    _check_ri(\"file://host/path/sp1\", URL, scheme='file', hostname='host', path='/path/sp1')\n    # stock libraries of Python aren't quite ready for ipv6\n    ipv6address = '2001:db8:85a3::8a2e:370:7334'\n    _check_ri(\"file://%s/path/sp1\" % ipv6address, URL,\n              scheme='file', hostname=ipv6address, path='/path/sp1')\n    for lh in ('localhost', '::1', '', '127.3.4.155'):\n        _check_ri(\"file://%s/path/sp1\" % lh, URL, localpath='/path/sp1',\n                  scheme='file', hostname=lh, path='/path/sp1')\n    _check_ri('http://[1fff:0:a88:85a3::ac1f]:8001/index.html', URL,\n              scheme='http', hostname='1fff:0:a88:85a3::ac1f', port=8001, path='/index.html')\n    _check_ri(\"file:///path/sp1\", URL, localpath='/path/sp1', scheme='file', path='/path/sp1')\n    # we don't do any magical comprehension for home paths/drives for windows\n    # of file:// urls, thus leaving /~ and /c: for now:\n    _check_ri(\"file:///~/path/sp1\", URL, localpath='/~/path/sp1', scheme='file', path='/~/path/sp1')\n    _check_ri(\"file:///%7E/path/sp1\", URL, localpath='/~/path/sp1', scheme='file', path='/~/path/sp1', exact_str=False)\n    # not sure but let's check\n    _check_ri(\"file:///c:/path/sp1\", URL, localpath='/c:/path/sp1', scheme='file', path='/c:/path/sp1', exact_str=False)\n\n    # and now implicit paths or actually they are also \"URI references\"\n    _check_ri(\"f\", PathRI, localpath='f', path='f')\n    _check_ri(\"f/s1\", PathRI, localpath='f/s1', path='f/s1')\n    _check_ri(PurePosixPath(\"f\"), PathRI, localpath='f', path='f')\n    _check_ri(PurePosixPath(\"f/s1\"), PathRI, localpath='f/s1', path='f/s1')\n    # colons are problematic and might cause confusion into SSHRI\n    _check_ri(\"f/s:1\", PathRI, localpath='f/s:1', path='f/s:1')\n    _check_ri(\"f/s:\", PathRI, localpath='f/s:', path='f/s:')\n    _check_ri(\"/f\", PathRI, localpath='/f', path='/f')\n    _check_ri(\"/f/s1\", PathRI, localpath='/f/s1', path='/f/s1')\n\n    # some github ones, just to make sure\n    _check_ri(\"git://host/user/proj\", URL, scheme=\"git\", hostname=\"host\", path=\"/user/proj\")\n    _check_ri(\"git@host:user/proj\", SSHRI, hostname=\"host\", path=\"user/proj\", username='git')\n\n    _check_ri('weired:/', SSHRI, hostname='weired', path='/')\n    # since schema is not allowing some symbols so we need to add additional check\n    _check_ri('weired_url:/', SSHRI, hostname='weired_url', path='/')\n    _check_ri('example.com:/', SSHRI, hostname='example.com', path='/')\n    _check_ri('example.com:path/sp1', SSHRI, hostname='example.com', path='path/sp1')\n    _check_ri('example.com/path/sp1\\:fname', PathRI, localpath='example.com/path/sp1\\:fname',\n              path='example.com/path/sp1\\:fname')\n    # ssh is as stupid as us, so we will stay \"Consistently\" dumb\n    \"\"\"\n    $> ssh example.com/path/sp1:fname\n    ssh: Could not resolve hostname example.com/path/sp1:fname: Name or service not known\n\n    edit 20190516 yoh: but this looks like a perfectly valid path.\n    SSH knows that it is not a path but its SSHRI so it can stay dumb.\n    We are trying to be smart and choose between RIs (even when we know that\n    it is e.g. a file).\n    \"\"\"\n    _check_ri('e.com/p/sp:f', PathRI, localpath='e.com/p/sp:f', path='e.com/p/sp:f')\n    _check_ri('user@someho.st/mydir', PathRI, localpath='user@someho.st/mydir', path='user@someho.st/mydir')\n\n    # SSHRIs have .port, but it is empty\n    eq_(SSHRI(hostname='example.com').port, '')\n\n    # check that we are getting a warning logged when url can't be reconstructed\n    # precisely\n    # actually failed to come up with one -- becomes late here\n    #_check_ri(\"http://host///..//p\", scheme='http', path='/..//p')\n\n    # actually this one is good enough to trigger a warning and I still don't know\n    # what it should exactly be!?\n    with swallow_logs(new_level=logging.DEBUG) as cml:\n        weired_str = 'weired://'\n        weired_url = RI(weired_str)\n        repr(weired_url)\n        cml.assert_logged(\n            'Parsed version of SSHRI .weired:/. '\n            'differs from original .weired://.'\n        )\n        # but we store original str\n        eq_(str(weired_url), weired_str)\n        neq_(weired_url.as_str(), weired_str)\n\n\n    raise SkipTest(\"TODO: file://::1/some does complain about parsed version dropping ::1\")\n\n\ndef test_git_transport_ri():\n    _check_ri(\"gcrypt::http://somewhere\", GitTransportRI, RI='http://somewhere', transport='gcrypt')\n    # man git-push says\n    #  <transport>::<address>\n    #    where <address> may be a path, a server and path, or an arbitrary URL-like string...\n    # so full path to my.com/... should be ok?\n    _check_ri(\"http::/my.com/some/path\", GitTransportRI, RI='/my.com/some/path', transport='http')\n    # some ssh server.  And we allow for some additional chars in transport.\n    # Git doesn't define since it does not care! we will then be flexible too\n    _check_ri(\"trans-port::server:path\", GitTransportRI, RI='server:path', transport='trans-port')\n\n\ndef _test_url_quote_path(cls, clskwargs, target_url):\n    path = '/ \"\\';a&b&cd `| '\n    if not (cls is PathRI):\n        clskwargs['hostname'] = hostname = 'example.com'\n    url = cls(path=path, **clskwargs)\n    eq_(url.path, path)\n    if 'hostname' in clskwargs:\n        eq_(url.hostname, hostname)\n    # all nasty symbols should be quoted\n    url_str = str(url)\n    eq_(url_str, target_url)\n    # no side-effects:\n    eq_(url.path, path)\n    if 'hostname' in clskwargs:\n        eq_(url.hostname, hostname)\n\n    # and figured out and unquoted\n    url_ = RI(url_str)\n    ok_(isinstance(url_, cls))\n    eq_(url_.path, path)\n    if 'hostname' in clskwargs:\n        eq_(url.hostname, hostname)\n\n\ndef test_url_quote_path():\n    yield _test_url_quote_path, SSHRI, {}, r'example.com:/ \"' + r\"';a&b&cd `| \"\n    yield _test_url_quote_path, URL, {'scheme': \"http\"}, 'http://example.com/%20%22%27%3Ba%26b%26cd%20%60%7C%20'\n    yield _test_url_quote_path, PathRI, {}, r'/ \"' + r\"';a&b&cd `| \"  # nothing is done to file:implicit\n\n\ndef test_url_compose_archive_one():\n    url = URL(scheme='dl+archive', path='KEY',\n              fragment=OrderedDict((('path', 'f/p/ s+'), ('size', 30))))\n    # funny - space is encoded as + but + is %2B\n    eq_(str(url), 'dl+archive:KEY#path=f/p/+s%2B&size=30')\n    eq_(url.fragment_dict, {'path': 'f/p/ s+', 'size': '30'})\n\n\ndef test_url_fragments_and_query():\n    url = URL(hostname=\"host\", query=OrderedDict((('a', 'x/b'), ('b', 'y'))))\n    eq_(str(url), '//host?a=x%2Fb&b=y')\n    eq_(url.query, 'a=x%2Fb&b=y')\n    eq_(url.query_dict, {'a': 'x/b', 'b': 'y'})\n\n    url = URL(hostname=\"host\", fragment=OrderedDict((('b', 'x/b'), ('a', 'y'))))\n    eq_(str(url), '//host#b=x/b&a=y')\n    eq_(url.fragment, 'b=x/b&a=y')\n    eq_(url.fragment_dict, {'a': 'y', 'b': 'x/b'})\n\n    fname = get_most_obscure_supported_name()\n    url = URL(hostname=\"host\", fragment={'a': fname})\n    eq_(url.fragment_dict, {'a': fname})\n\n\ndef test_url_dicts():\n    eq_(URL(\"http://host\").query_dict, {})\n\n\n@skip_if_on_windows\ndef test_get_url_path_on_fileurls():\n    eq_(URL('file:///a').path, '/a')\n    eq_(URL('file:///a/b').path, '/a/b')\n    eq_(URL('file:///a/b').localpath, '/a/b')\n    eq_(URL('file:///a/b#id').path, '/a/b')\n    eq_(URL('file:///a/b?whatever').path, '/a/b')\n\n\ndef test_is_url():\n    ok_(is_url('file://localhost/some'))\n    ok_(is_url('http://localhost'))\n    ok_(is_url('ssh://me@localhost'))\n    # in current understanding it is indeed a url but an 'ssh', implicit=True, not just\n    # a useless scheme=weired with a hope to point to a netloc\n    with swallow_logs():\n        ok_(is_url('weired://'))\n    nok_(is_url('relative'))\n    nok_(is_url('/absolute'))\n    ok_(is_url('like@sshlogin'))  # actually we do allow ssh:implicit urls ATM\n    nok_(is_url(''))\n    nok_(is_url(' '))\n    nok_(is_url(123))  # stuff of other types wouldn't be considered a URL\n\n    # we can pass RI instance directly\n    ok_(is_url(RI('file://localhost/some')))\n    nok_(is_url(RI('relative')))\n\n\n# TODO: RF with test_is_url to avoid duplication\ndef test_is_datalad_compat_ri():\n    ok_(is_datalad_compat_ri('ssh://user:passw@host/path'))\n    ok_(is_datalad_compat_ri('http://example.com'))\n    ok_(is_datalad_compat_ri('file://localhost/some'))\n    ok_(is_datalad_compat_ri('///localhost/some'))\n    nok_(is_datalad_compat_ri('relative'))\n    nok_(is_datalad_compat_ri('.///localhost/some'))\n    nok_(is_datalad_compat_ri(123))\n\n\ndef test_get_local_file_url():\n    for path, url in (\n                # relpaths are special-cased below\n                ('test.txt', 'test.txt'),\n            ) + (\n                ('C:\\\\Windows\\\\notepad.exe', 'file://C/Windows/notepad.exe'),\n            ) if on_windows else (\n                # static copy of \"most_obscore_name\"\n                (' \"\\';a&b&cΔЙקم๗あ `| ',\n                 # and translation by google chrome\n                 \"%20%22%27%3Ba%26b%26c%CE%94%D0%99%D7%A7%D9%85%E0%B9%97%E3%81%82%20%60%7C%20\"),\n                ('/a', 'file:///a'),\n                ('/a/b/c', 'file:///a/b/c'),\n                ('/a~', 'file:///a~'),\n                # there are no files with trailing slashes in the name\n                #('/a b/', 'file:///a%20b/'),\n                ('/a b/name', 'file:///a%20b/name'),\n            ):\n        if isabs(path):\n            eq_(get_local_file_url(path), url)\n        else:\n            eq_(get_local_file_url(path),\n                '/'.join((\n                    get_local_file_url(os.getcwd()),\n                    url))\n            )\n\n\n@with_tempfile(mkdir=True)\ndef test_get_local_file_url_compatibility(path):\n    # smoke test for file:// URL compatibility with other datalad/git/annex\n    # pieces\n    path = Path(path)\n    ds1 = Dataset(path / 'ds1').create()\n    ds2 = Dataset(path / 'ds2').create()\n    testfile = path / 'testfile.txt'\n    testfile.write_text('some')\n\n    # compat with annex addurl\n    ds1.repo.add_url_to_file(\n        'test.txt',\n        get_local_file_url(testfile, compatibility='git-annex'))\n\n    # compat with git clone/submodule\n    assert_status(\n        'ok',\n        ds1.clone(get_local_file_url(ds2.path, compatibility='git'),\n                  result_xfm=None, return_type='generator'))\n\n\ndef test_is_ssh():\n\n    ssh_locators = [\"ssh://host\",\n                    \"ssh://host/some/where\",\n                    \"user@host:path/sp1\",\n                    \"user@host:/absolute/path/sp1\",\n                    \"host:path/sp1\",\n                    \"host:/absolute/path/sp1\",\n                    \"user@host\"]\n    for ri in ssh_locators:\n        ok_(is_ssh(ri), \"not considered ssh (string): %s\" % ri)\n        ok_(is_ssh(RI(ri)), \"not considered ssh (RI): %s\" % ri)\n\n    non_ssh_locators = [\"file://path/to\",\n                        \"/abs/path\",\n                        \"../rel/path\",\n                        \"http://example.com\",\n                        \"git://host/user/proj\",\n                        \"s3://bucket/save/?key=891\"]\n    for ri in non_ssh_locators:\n        ok_(not is_ssh(ri), \"considered ssh (string): %s\" % ri)\n        ok_(not is_ssh(RI(ri)), \"considered ssh (RI): %s\" % ri)\n\n\ndef test_iso8601_to_epoch():\n    epoch = 1467901515\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15+00:00'), epoch)\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15+11:00'),\n        epoch - 11 * 60 * 60)\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15Z'), epoch)\n    eq_(iso8601_to_epoch('2016-07-07T14:25:15'), epoch)\n\n    eq_(iso8601_to_epoch('2016-07-07T14:25:14'), epoch-1)\n",
        "import pytest\nimport tempfile\nimport os\n\n\n@pytest.fixture\ndef path():\n    with tempfile.TemporaryDirectory() as tmpdir:\n        yield tmpdir\n\n\n@pytest.fixture(autouse=True)\ndef setup_git_config():\n    with tempfile.TemporaryDirectory() as home:\n        old_home = os.environ.get(\"HOME\")\n        os.environ[\"HOME\"] = home\n        os.system('git config --global user.name \"DataLad Tester\"')\n        os.system('git config --global user.email \"test@example.com\"')\n        yield\n        if old_home:\n            os.environ[\"HOME\"] = old_home\n"
    ],
    "test_file_names": [
        "test_1.py",
        "conftest.py"
    ],
    "setup_res_code": 0,
    "setup_res_stdout": "Starting Datalad installation attempts...\nVerifying installation...\nDatalad version: 0.13.3.dev62\n✅ Installation successful!\nSuccessfully installed Datalad using Python 3.9\n",
    "setup_res_stderr": "Using CPython 3.9.21\nCreating virtual environment at: .venv\nActivate with: source .venv/bin/activate\nResolved 25 packages in 93ms\nInstalled 25 packages in 2.92s\n + coverage==7.6.10\n + docopt==0.6.2\n + exceptiongroup==1.2.2\n + grabbit==0.2.6\n + importlib-resources==6.4.5\n + iniconfig==2.0.0\n + nibabel==5.3.2\n + num2words==0.5.14\n + numpy==2.0.2\n + packaging==24.2\n + pandas==2.2.3\n + patsy==1.0.1\n + pluggy==1.5.0\n + pybids==0.6.5\n + pytest==8.3.4\n + pytest-cov==6.0.0\n + python-dateutil==2.9.0.post0\n + pytz==2024.2\n + scipy==1.13.1\n + setuptools==75.6.0\n + six==1.17.0\n + tomli==2.2.1\n + typing-extensions==4.12.2\n + tzdata==2024.2\n + zipp==3.21.0\nResolved 63 packages in 4.61s\nPrepared 1 package in 4.84s\nInstalled 57 packages in 307ms\n + annexremote==1.6.6\n + appdirs==1.4.4\n + backports-tarfile==1.2.0\n + beautifulsoup4==4.12.3\n + boto==2.49.0\n + certifi==2024.12.14\n + cffi==1.17.1\n + chardet==5.2.0\n + charset-normalizer==3.4.1\n + citeproc-py==0.6.0\n + cryptography==44.0.0\n + datalad==0.13.3 (from file:///home/gcpuser/buckets/local_repoeval_bucket/repos/datalad_3a85e9a1603f2f47ad768eb729f984bead61d681)\n + deprecated==1.2.15\n + distro==1.9.0\n + duecredit==0.10.2\n + exifread==3.0.0\n + fasteners==0.19\n + httpretty==1.1.4\n + humanize==4.11.0\n + idna==3.10\n + importlib-metadata==8.5.0\n + iso8601==2.1.0\n + jaraco-classes==3.4.0\n + jaraco-context==6.0.1\n + jaraco-functools==4.1.0\n + jeepney==0.8.0\n + jsmin==3.0.1\n + keyring==25.6.0\n + keyrings-alt==5.0.2\n + looseversion==1.3.0\n + lxml==5.3.0\n + more-itertools==10.5.0\n + msgpack==1.1.0\n + multidict==6.1.0\n + mutagen==1.47.0\n + nose==1.3.7\n + patool==1.15.0\n + pillow==11.0.0\n + propcache==0.2.1\n + pycparser==2.22\n + pygithub==2.5.0\n + pyjwt==2.10.1\n + pynacl==1.5.0\n + pyperclip==1.9.0\n + python-xmp-toolkit==2.0.2\n + pyyaml==6.0.2\n + requests==2.32.3\n + requests-ftp==0.3.1\n + secretstorage==3.3.3\n + simplejson==3.19.3\n + soupsieve==2.6\n + tqdm==4.67.1\n + urllib3==1.26.20\n + vcrpy==7.0.0\n + whoosh==2.7.4\n + wrapt==1.17.0\n + yarl==1.18.3\nResolved 97 packages in 259ms\nInstalled 32 packages in 779ms\n + alabaster==0.7.16\n + asv==0.6.4\n + asv-runner==0.2.1\n + babel==2.16.0\n + build==1.2.2.post1\n + distlib==0.3.9\n + docutils==0.21.2\n + filelock==3.16.1\n + gprof2dot==2024.6.6\n + imagesize==1.4.1\n + jinja2==3.1.5\n + json5==0.10.0\n + markupsafe==3.0.2\n + nose-timer==1.0.1\n + platformdirs==4.3.6\n + psutil==6.1.1\n + pygments==2.18.0\n + pympler==1.1\n + pypandoc==1.14\n + pyproject-hooks==1.2.0\n + snowballstemmer==2.2.0\n + sphinx==7.4.7\n + sphinx-rtd-theme==3.0.2\n + sphinxcontrib-applehelp==2.0.0\n + sphinxcontrib-devhelp==2.0.0\n + sphinxcontrib-htmlhelp==2.1.0\n + sphinxcontrib-jquery==4.1\n + sphinxcontrib-jsmath==1.0.1\n + sphinxcontrib-qthelp==2.0.0\n + sphinxcontrib-serializinghtml==2.0.0\n + tabulate==0.9.0\n + virtualenv==20.28.0\n",
    "new_commit_res_code": 1,
    "new_commit_res_stdout": "============================= test session starts ==============================\nplatform linux -- Python 3.9.21, pytest-8.3.4, pluggy-1.5.0\nrootdir: /home/gcpuser/buckets/local_repoeval_bucket/repos/datalad_3a85e9a1603f2f47ad768eb729f984bead61d681\nplugins: cov-6.0.0\ncollected 23 items\n\nr2e_tests/test_1.py ...x......s.x.......F..                              [100%]\n\n=================================== FAILURES ===================================\n____________________ test_get_local_file_url_compatibility _____________________\n\narg = (), kw = {'path': '/tmp/tmp-page2annexr0rxsn4h'}\nfilename = '/tmp/datalad_temp_test_get_local_file_url_compatibilityl2fyfx2j'\n\n    @wraps(t)\n    def  _wrap_with_tempfile(*arg, **kw):\n        if 'dir' not in tkwargs.keys():\n            # if not specified otherwise, respect datalad.tests.temp.dir config\n            # as this is a test helper\n            tkwargs['dir'] = dl_cfg.get(\"datalad.tests.temp.dir\")\n        with make_tempfile(wrapped=t, **tkwargs) as filename:\n>           return t(*(arg + (filename,)), **kw)\nE           TypeError: test_get_local_file_url_compatibility() got multiple values for argument 'path'\n\ndatalad/tests/utils.py:731: TypeError\n==================================== PASSES ====================================\n________________________________ test_url_base _________________________________\n------------------------------ Captured log call -------------------------------\nWARNING  datalad.network:network.py:633 ParseResults contains params 'param', which will be ignored\n=========================== short test summary info ============================\nPASSED r2e_tests/test_1.py::test_same_website\nPASSED r2e_tests/test_1.py::test_get_tld\nPASSED r2e_tests/test_1.py::test_dlurljoin\nPASSED r2e_tests/test_1.py::test_rfc2822_to_epoch\nPASSED r2e_tests/test_1.py::test_get_response_disposition_filename\nPASSED r2e_tests/test_1.py::test_parse_url_opts\nPASSED r2e_tests/test_1.py::test_split_colon\nPASSED r2e_tests/test_1.py::test_url_eq\nPASSED r2e_tests/test_1.py::test_url_base\nPASSED r2e_tests/test_1.py::test_git_transport_ri\nPASSED r2e_tests/test_1.py::test_url_compose_archive_one\nPASSED r2e_tests/test_1.py::test_url_fragments_and_query\nPASSED r2e_tests/test_1.py::test_url_dicts\nPASSED r2e_tests/test_1.py::test_get_url_path_on_fileurls\nPASSED r2e_tests/test_1.py::test_is_url\nPASSED r2e_tests/test_1.py::test_is_datalad_compat_ri\nPASSED r2e_tests/test_1.py::test_get_local_file_url\nPASSED r2e_tests/test_1.py::test_is_ssh\nPASSED r2e_tests/test_1.py::test_iso8601_to_epoch\nSKIPPED [1] .venv/lib/python3.9/site-packages/_pytest/unittest.py:385: TODO: file://::1/some does complain about parsed version dropping ::1\nXFAIL r2e_tests/test_1.py::test_get_url_straight_filename - reason: [NOTRUN] yield tests were removed in pytest 4.0 - test_get_url_straight_filename will be ignored\nXFAIL r2e_tests/test_1.py::test_url_quote_path - reason: [NOTRUN] yield tests were removed in pytest 4.0 - test_url_quote_path will be ignored\nFAILED r2e_tests/test_1.py::test_get_local_file_url_compatibility - TypeError...\n============== 1 failed, 19 passed, 1 skipped, 2 xfailed in 5.18s ==============\n",
    "new_commit_res_stderr": "",
    "old_commit_res_code": 1,
    "old_commit_res_stdout": "============================= test session starts ==============================\nplatform linux -- Python 3.9.21, pytest-8.3.4, pluggy-1.5.0\nrootdir: /home/gcpuser/buckets/local_repoeval_bucket/repos/datalad_3a85e9a1603f2f47ad768eb729f984bead61d681\nplugins: cov-6.0.0\ncollected 23 items\n\nr2e_tests/test_1.py ...x......s.x.......F.F                              [100%]\n\n=================================== FAILURES ===================================\n____________________ test_get_local_file_url_compatibility _____________________\n\narg = (), kw = {'path': '/tmp/tmp-page2annex934tyid8'}\nfilename = '/tmp/datalad_temp_test_get_local_file_url_compatibilityuak7rxq9'\n\n    @wraps(t)\n    def  _wrap_with_tempfile(*arg, **kw):\n        if 'dir' not in tkwargs.keys():\n            # if not specified otherwise, respect datalad.tests.temp.dir config\n            # as this is a test helper\n            tkwargs['dir'] = dl_cfg.get(\"datalad.tests.temp.dir\")\n        with make_tempfile(wrapped=t, **tkwargs) as filename:\n>           return t(*(arg + (filename,)), **kw)\nE           TypeError: test_get_local_file_url_compatibility() got multiple values for argument 'path'\n\ndatalad/tests/utils.py:731: TypeError\n____________________________ test_iso8601_to_epoch _____________________________\n\n    def test_iso8601_to_epoch():\n        epoch = 1467901515\n        eq_(iso8601_to_epoch('2016-07-07T14:25:15+00:00'), epoch)\n>       eq_(iso8601_to_epoch('2016-07-07T14:25:15+11:00'),\n            epoch - 11 * 60 * 60)\n\nr2e_tests/test_1.py:532: \n_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \n\na = 1467901515, b = 1467861915, msg = None\n\n    def eq_(a, b, msg=None):\n        \"\"\"Shorthand for 'assert a == b, \"%r != %r\" % (a, b)\n        \"\"\"\n        if not a == b:\n>           raise AssertionError(msg or \"%r != %r\" % (a, b))\nE           AssertionError: 1467901515 != 1467861915\n\n.venv/lib/python3.9/site-packages/nose/tools/trivial.py:29: AssertionError\n==================================== PASSES ====================================\n________________________________ test_url_base _________________________________\n------------------------------ Captured log call -------------------------------\nWARNING  datalad.network:network.py:633 ParseResults contains params 'param', which will be ignored\n=========================== short test summary info ============================\nPASSED r2e_tests/test_1.py::test_same_website\nPASSED r2e_tests/test_1.py::test_get_tld\nPASSED r2e_tests/test_1.py::test_dlurljoin\nPASSED r2e_tests/test_1.py::test_rfc2822_to_epoch\nPASSED r2e_tests/test_1.py::test_get_response_disposition_filename\nPASSED r2e_tests/test_1.py::test_parse_url_opts\nPASSED r2e_tests/test_1.py::test_split_colon\nPASSED r2e_tests/test_1.py::test_url_eq\nPASSED r2e_tests/test_1.py::test_url_base\nPASSED r2e_tests/test_1.py::test_git_transport_ri\nPASSED r2e_tests/test_1.py::test_url_compose_archive_one\nPASSED r2e_tests/test_1.py::test_url_fragments_and_query\nPASSED r2e_tests/test_1.py::test_url_dicts\nPASSED r2e_tests/test_1.py::test_get_url_path_on_fileurls\nPASSED r2e_tests/test_1.py::test_is_url\nPASSED r2e_tests/test_1.py::test_is_datalad_compat_ri\nPASSED r2e_tests/test_1.py::test_get_local_file_url\nPASSED r2e_tests/test_1.py::test_is_ssh\nSKIPPED [1] .venv/lib/python3.9/site-packages/_pytest/unittest.py:385: TODO: file://::1/some does complain about parsed version dropping ::1\nXFAIL r2e_tests/test_1.py::test_get_url_straight_filename - reason: [NOTRUN] yield tests were removed in pytest 4.0 - test_get_url_straight_filename will be ignored\nXFAIL r2e_tests/test_1.py::test_url_quote_path - reason: [NOTRUN] yield tests were removed in pytest 4.0 - test_url_quote_path will be ignored\nFAILED r2e_tests/test_1.py::test_get_local_file_url_compatibility - TypeError...\nFAILED r2e_tests/test_1.py::test_iso8601_to_epoch - AssertionError: 146790151...\n============== 2 failed, 18 passed, 1 skipped, 2 xfailed in 1.63s ==============\n",
    "old_commit_res_stderr": ""
}
datalad/support/network.py datalad/support/tests/test_network.py
We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[ISSUE]

**Title:** `iso8601_to_epoch` Does Not Correctly Handle Time Zone Offsets

**Description:**
The `iso8601_to_epoch` function fails to account for time zone information in ISO 8601 date strings, resulting in incorrect epoch timestamps for non-UTC times.

**Example Buggy Code:**
```python
def iso8601_to_epoch(datestr):
    return calendar.timegm(iso8601.parse_date(datestr).timetuple())

# Usage
epoch = 1467901515
result = iso8601_to_epoch('2016-07-07T14:25:15+11:00')
print(result)  # Outputs: 1467901515
```
In this example, the input date string includes a `+11:00` time zone offset. However, the function ignores this offset and treats the time as UTC.

**Expected Behavior:**
The function should correctly parse the time zone offset and adjust the epoch timestamp accordingly. For the given example, the expected epoch should be `1467901515 - (11 * 60 * 60) = 1467861915`.

**Actual Behavior:**
The function ignores the time zone offset, resulting in an incorrect epoch timestamp that does not reflect the specified time zone.

[/ISSUE]
--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.

--- BEGIN FILE ---
```
### datalad/support/network.py
# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
# ex: set sts=4 ts=4 sw=4 noet:
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
#   See COPYING file distributed along with the datalad package for the
#   copyright and license terms.
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##

import logging
lgr = logging.getLogger('datalad.network')

lgr.log(5, "Importing support.network")
import calendar
import email.utils
import os
import pickle
import re
import sys
import time
import iso8601

from hashlib import md5
from collections import OrderedDict
from os.path import abspath, isabs
from os.path import join as opj
from os.path import dirname
from ntpath import splitdrive as win_splitdrive

from urllib.parse import urlsplit
from urllib.request import Request
from urllib.parse import unquote as urlunquote
from urllib.parse import urljoin, urlparse, urlsplit, urlunparse, ParseResult
from urllib.parse import parse_qsl
from urllib.parse import urlencode
from urllib.error import URLError

from datalad.dochelpers import exc_str
from datalad.utils import (
    on_windows,
    PurePath,
    Path,
)
from datalad.utils import assure_dir, assure_bytes, assure_unicode, map_items
from datalad import consts
from datalad import cfg
from datalad.support.cache import lru_cache

# !!! Lazily import requests where needed -- needs 30ms or so
# import requests

# Change introduced in 3.7: Moved from RFC 2396 to RFC 3986 for quoting URL
# strings. "~" is now included in the set of reserved characters.
# For consistency we will provide urlquote
if sys.version_info >= (3, 7):
    from urllib.parse import quote as urlquote
else:
    from urllib.parse import quote as _urlquote

    def urlquote(url, safe='/', **kwargs):
        safe += '~'
        return _urlquote(url, safe=safe, **kwargs)

    urlquote.__doc__ = _urlquote.__doc__ + """

This DataLad version of the function assumes ~ to be a safe character to be
consistent with Python >= 3.7
"""


def is_windows_path(path):
    win_split = win_splitdrive(path)
    # Note, that ntpath.splitdrive also deals with UNC paths. In that case
    # the "drive" wouldn't be a windows drive letter followed by a colon
    if win_split[0] and win_split[1] and \
            win_split[0].endswith(":") and len(win_split[0]) == 2:
        # seems to be a windows path
        return True
    return False


def get_response_disposition_filename(s):
    """Given a string s as from HTTP Content-Disposition field in the response
    return possibly present filename if any
    """
    if not s:
        return None
    # If the response has Content-Disposition, try to get filename from it
    cd = map(
        lambda x: x.strip().split('=', 1) if '=' in x else [x.strip(), ''],
        s.split(';')
    )
    # unify the key to be lower case and make it into a dict
    cd = dict([[x[0].lower()] + x[1:] for x in cd])
    if 'filename' in cd:
        filename = cd['filename'].strip("\"'")
        return filename
    return None


def get_url_disposition_filename(url, headers=None):
    """Get filename as possibly provided by the server in Content-Disposition
    """
    if headers is None:
        request = Request(url)
        r = retry_urlopen(request)
        # things are different in requests
        if 'requests.' in str(r.__class__):
            headers = r.headers
        else:
            headers = r.info()
    else:
        r = None
    try:
        return get_response_disposition_filename(headers.get('Content-Disposition', ''))
    finally:
        if r:
            r.close()


def get_url_straight_filename(url, strip=None, allowdir=False):
    """Get file/dir name of the last path component of the URL

    Parameters
    ----------
    strip: list, optional
      If provided, listed names will not be considered and their
      parent directory will be selected
    allowdir: bool, optional
      If url points to a "directory" (ends with /), empty string
      would be returned unless allowdir is True, in which case the
      name of the directory would be returned
    """
    path = urlunquote(urlsplit(url).path)
    path_parts = path.split('/')

    if allowdir:
        # strip empty ones
        while len(path_parts) > 1 and not path_parts[-1]:
            path_parts = path_parts[:-1]

    if strip:
        while path_parts and path_parts[-1] in strip:
            path_parts = path_parts[:-1]

    if path_parts:
        return path_parts[-1]
    else:
        return None


def get_url_filename(url, headers=None, strip=None):
    """Get filename from the url, first consulting server about Content-Disposition
    """
    filename = get_url_disposition_filename(url, headers)
    if filename:
        return filename
    return get_url_straight_filename(url, strip=strip)


def get_url_response_stamp(url, response_info):
    size, mtime = None, None
    if 'Content-length' in response_info:
        size = int(response_info['Content-length'])
    if 'Last-modified' in response_info:
        mtime = calendar.timegm(email.utils.parsedate(
            response_info['Last-modified']))
    return dict(size=size, mtime=mtime, url=url)


def get_tld(url):
    """Return top level domain from a url

    Parameters
    ----------
    url : str
    """
    # maybe use this instead to be safe:  https://pypi.python.org/pypi/tld
    if not url.strip():
        raise ValueError("Empty URL has no TLD")
    rec = urlsplit(url)
    if not rec.netloc:
        if not rec.scheme:
            # There were no scheme provided thus netloc was empty -- must have been a simple 'path like'
            return url.split('/', 1)[0]
        else:
            raise ValueError("It seems that only the scheme was provided without the net location/TLD")
    return rec.netloc


from email.utils import parsedate_tz, mktime_tz


def rfc2822_to_epoch(datestr):
    """Given rfc2822 date/time format, return seconds since epoch"""
    return mktime_tz(parsedate_tz(datestr))


def iso8601_to_epoch(datestr):
    """Given ISO 8601 date/time format, return in seconds since epoch

    iso8601 is used to parse properly the time zone information, which
    can't be parsed with standard datetime strptime
    """
    return calendar.timegm(iso8601.parse_date(datestr).timetuple())


def __urlopen_requests(url):
    # XXX Workaround for now for ... broken code
    if isinstance(url, Request):
        url = url.get_full_url()
    from requests import Session
    return Session().get(url, stream=True)


def retry_urlopen(url, retries=3):
    for t in range(retries):
        try:
            return __urlopen_requests(url)
        except URLError as e:
            lgr.warning("Received exception while reading %s: %s" % (url, e))
            if t == retries - 1:
                # if we have reached allowed number of retries -- reraise
                raise


def is_url_quoted(url):
    """Return whether URL looks being already quoted
    """
    try:
        url_ = urlunquote(url)
        return url != url_
    except:  # problem with unquoting -- then it must be wasn't quoted (correctly)
        # MIH: ValueError?
        return False


def same_website(url_rec, u_rec):
    """Decide whether a link leads to external site

    Parameters
    ----------
    url_rec: ParseResult
      record for original url
    u_rec: ParseResult
      record for new url
    """
    if isinstance(url_rec, str):
        url_rec = urlparse(url_rec)
    if isinstance(u_rec, str):
        u_rec = urlparse(u_rec)
    return (url_rec.netloc == u_rec.netloc)
    # todo: collect more of sample cases.
    # disabled below check while working on ratholeradio, since links
    # could go to the parent and that is ok.  Figure out when it was
    # desired not to go to the parent -- we might need explicit option
    # and u_rec.path.startswith(url_rec.path)):


def dlurljoin(u_path, url):
    url_rec = urlparse(url)  # probably duplicating parsing :-/ TODO
    if url_rec.scheme:
        # independent full url, so just return it
        return url
    if u_path.endswith('/'):  # should here be also a scheme use?
        if url.startswith('/'):  # jump to the root
            u_path_rec = urlparse(u_path)
            return urljoin(urlunparse(
                (u_path_rec.scheme, u_path_rec.netloc, '', '', '', '')), url)
        else:
            return os.path.join(u_path, url)
    # TODO: recall where all this dirname came from and bring into the test
    return urljoin(os.path.dirname(u_path) + '/', url)


# TODO should it be a node maybe?
class SimpleURLStamper(object):
    """Gets a simple stamp about the URL: {url, time, size} of whatever was provided in the header
    """
    def __init__(self, mode='full'):
        self.mode = mode

    def __call__(self, url):
        # Extracted from above madness
        # TODO: add mode alike to 'relaxed' where we would not
        # care about content-disposition filename
        # http://stackoverflow.com/questions/862173/how-to-download-a-file-using-python-in-a-smarter-way
        request = Request(url)

        # No traffic compression since we do not know how to identify
        # exactly either it has to be decompressed
        # request.add_header('Accept-encoding', 'gzip,deflate')
        #
        # TODO: think about stamping etc -- we seems to be redoing
        # what git-annex does for us already... not really
        r = retry_urlopen(request)
        try:
            r_info = r.info()
            r_stamp = get_url_response_stamp(url, r_info)

            return dict(mtime=r_stamp['mtime'], size=r_stamp['size'], url=url)
        finally:
            r.close()


# TODO:  make it consistent/clear at what stage % encoding/decoding happens!
# now it is a mix!

#
# Useful functionality in requests.models
#  utils.requote_uri -- quote/unquote cycle to guarantee consistent appearance
#  RequestEncodingMixin._encode_params -- Will successfully encode parameters when passed as a dict or a list of ...
#  PreparedRequest().prepare_url(url, params) -- nicely cares about url encodings etc
#

@lru_cache(maxsize=100)
def _guess_ri_cls(ri):
    """Factory function which would determine which type of a ri a provided string is"""
    TYPES = {
        'url': URL,
        'ssh':  SSHRI,
        'file': PathRI,
        'datalad': DataLadRI,
        'git-transport': GitTransportRI,
    }
    if isinstance(ri, PurePath):
        lgr.log(5, "Detected file ri")
        return TYPES['file']
    if is_windows_path(ri):
        # OMG we got something from windows
        lgr.log(5, "Detected file ri")
        return TYPES['file']

    # We assume that it is a URL and parse it. Depending on the result
    # we might decide that it was something else ;)
    fields = URL._pr_to_fields(urlparse(ri))
    lgr.log(5, "Parsed ri %s into fields %s" % (ri, fields))
    type_ = 'url'
    # Special treatments
    # file:///path should stay file:
    if fields['scheme'] and fields['scheme'] not in {'file'} \
            and not fields['hostname']:
        # transport::URL-or-path
        if fields['path'].startswith(':'):  # there was ::
            lgr.log(5, "Assuming git transport style ri and returning")
            type_ = 'git-transport'
        # dl+archive:... or just for ssh   hostname:path/p1
        elif '+' not in fields['scheme']:
            type_ = 'ssh'
            lgr.log(5, "Assuming ssh style ri, adjusted: %s" % (fields,))

    if not fields['scheme'] and not fields['hostname']:
        parts = _split_colon(ri)
        # if no illegal for username@hostname characters in the first part and
        # we either had username@hostname or multiple :-separated parts
        if not set(parts[0]).intersection(set('/\\#')) and (
                fields['path'] and
                '@' in fields['path'] or
                len(parts) > 1
        ):
            # user@host:path/sp1
            # or host_name: (hence parts check)
            # TODO: we need a regex to catch those really, parts check is not suff
            type_ = 'ssh'
        elif ri.startswith('//'):
            # e.g. // or ///path
            type_ = 'datalad'
        else:
            type_ = 'file'

    if not fields['scheme'] and fields['hostname']:
        # e.g. //a/path
        type_ = 'datalad'

    cls = TYPES[type_]
    # just parse the ri according to regex matchint ssh "ri" specs
    lgr.log(5, "Detected %s ri" % type_)
    return cls


class RI(object):
    """Resource Identifier - base class and a factory for URL, SSHRI, etc

    Intended to be a R/O object (i.e. no fields should be changed in-place).
    Subclasses define specific collections of fields they care about in _FIELDS
    class variable.
    The idea is that this class should help to break apart a URL, while being
    able to rebuild itself into a string representation for reuse

    `RI` could be used as factory, whenever type of the resource is unknown and
    must be guessed from the string representation.  One of the subclasses will be
    provided as output, e.g.

    >>> RI('http://example.com')
    URL(hostname='example.com', scheme='http')
    >>> RI('example.com:path')
    SSHRI(hostname='example.com', path='path')
    """

    # All of the subclasses will provide path
    _FIELDS = (
        'path',
    )

    __slots__ = _FIELDS + ('_fields', '_str')

    def __new__(cls, ri=None, **kwargs):
        """Used as a possible factory for known RI types

        Returns
        -------
        RI
           uninitialized RI object of appropriate class with _str
           set to string representation if was provided

        """
        if cls is RI and ri is not None:
            # RI class was used as a factory
            cls = _guess_ri_cls(ri)

        if cls is RI:
            # should we fail or just pretend we are nothing??? ;-) XXX
            raise ValueError("Could not deduce RI type for %r" % (ri,))

        ri_obj = super(RI, cls).__new__(cls)
        # Store internally original str
        ri_obj._str = str(ri) if isinstance(ri, PurePath) else ri
        return ri_obj

    def __init__(self, ri=None, **fields):
        """
        Parameters
        ----------
        ri: str, optional
          String version of a resource specific for this class.  If you would like
          a type of the resource be deduced, use RI(ri). Note that this value
          will be passed to str(), so you do not have to cast it yourself.
        **fields: dict, optional
          The values for the fields defined in _FIELDS class variable.
        """
        if ri and (bool(ri) == bool(fields)):
            raise ValueError(
                "Specify either ri or breakdown from the fields, not both. "
                "Got ri=%r, fields=%r" % (ri, fields))

        self._fields = self._get_blank_fields()
        if ri is not None:
            ri = str(ri)
            fields = self._str_to_fields(ri)
        self._set_from_fields(**fields)

        # If was initialized from a string representation
        if lgr.isEnabledFor(logging.DEBUG) and self._str is not None:
            # well -- some ris might not unparse identically back
            # strictly speaking, but let's assume they do
            ri_ = self.as_str()
            if ri != ri_:
                lgr.debug("Parsed version of %s %r differs from original %r",
                          self.__class__.__name__, ri_, ri)

    @classmethod
    def _get_blank_fields(cls, **fields):
        return OrderedDict(((f, fields.get(f, '')) for f in cls._FIELDS))

    @property
    def fields(self):
        """Returns shallow copy of fields to ease manipulations"""
        return self._fields.copy()

    def __repr__(self):
        # since auto_repr doesn't support "non-0" values atm
        return "%s(%s)" % (
            self.__class__.__name__,
            ", ".join(["%s=%r" % (k, v)
                       for k, v in sorted(self._fields.items())
                       if v]))

    # Lazily evaluated if _str was not set
    def __str__(self):
        if self._str is None:
            self._str = self.as_str()
        return self._str

    @classmethod
    def from_str(cls, ri_str):
        obj = cls(**cls._str_to_fields(ri_str))
        obj._str = ri_str
        return obj

    @property
    def localpath(self):
        # by default RIs point to remote locations
        raise ValueError("%s points to remote location" % self)

    # Apparently doesn't quite play nicely with multiple inheritence for MixIn'
    # of regexp based URLs
    #@abstractmethod
    #@classmethod
    #def _str_to_fields(cls, ri_str):
    #    raise NotImplementedError

    #
    # If any field is specified, URL is not considered 'False', i.e.
    # non-existing, although may be we could/shout omit having only
    # scheme or port specified since it doesn't point to any useful
    # location
    #

    def __bool__(self):
        fields = self._fields
        return any(fields.values())

    #
    # Helpers to deal with internal structures and conversions
    #

    def _set_from_fields(self, **fields):
        unknown_fields = set(fields).difference(self._FIELDS)
        if unknown_fields:
            raise ValueError("Do not know about %s. Known fields for %s are: %s"
                             % (unknown_fields, self.__class__, self._FIELDS))

        # encode dicts for query or fragment into
        for f in {'query', 'fragment'}:
            v = fields.get(f)
            if isinstance(v, dict):

                ev = urlencode(map_items(assure_bytes, v))
                # / is reserved char within query
                if f == 'fragment' and '%2F' not in str(v):
                    # but seems to be ok'ish within the fragment which is
                    # the last element of URI and anyways used only by the
                    # client (i.e. by us here if used to compose the URL)
                    # so let's return / back for clarity if there were no
                    # awkward %2F to startswith
                    ev = ev.replace('%2F', '/')
                fields[f] = ev

        self._fields.update(fields)

    #
    # Quick comparators
    #

    def __eq__(self, other):
        if not isinstance(other, RI):
            other = RI(other)
        return isinstance(other, self.__class__) and dict(other._fields) == dict(self._fields)

    def __ne__(self, other):
        return not (self == other)

    def __getattribute__(self, item):
        if item.startswith('_') or item not in self._FIELDS:
            return super(RI, self).__getattribute__(item)
        else:
            return self._fields[item]

    def __setattr__(self, item, value):
        if item.startswith('_') or item not in self._FIELDS:
            super(RI, self).__setattr__(item, value)
        else:
            self._fields[item] = value
            self._str = None


class URL(RI):
    """Universal resource locator

    Although largely decorating urlparse.ParseResult, it
    - doesn't mandate providing all parts of the URL
    - doesn't require netloc but rather asks for separate username, password, and hostname
    """

    _FIELDS = RI._FIELDS + (
        'scheme',
        'username',
        'password',
        'hostname', 'port',
        'query',
        'fragment',
    )

    def as_str(self):
        """Render URL as a string"""
        return urlunparse(self.to_pr())

    @classmethod
    def _str_to_fields(cls, url_str):
        fields = URL._pr_to_fields(urlparse(url_str))
        fields['path'] = urlunquote(fields['path'])
        return fields

    def to_pr(self):
        """Convert URL to urlparse.ParseResults namedtuple"""
        return self._fields_to_pr(self._fields)

    @classmethod
    def _fields_to_pr(cls, fields):
        """Recompose back fields dict to ParseResult"""
        netloc = fields['username'] or ''
        if fields['password']:
            netloc += ':' + fields['password']
        if netloc:
            netloc += '@'
        netloc += fields['hostname']
        if fields['port']:
            if fields['hostname'].count(':') >= 2:
                # ipv6 -- need to enclose in []
                netloc = '[%s]:%s' % (netloc, fields['port'])
            else:
                netloc += ':%s' % fields['port']

        pr_fields = {
            f: fields[f]
            for f in cls._FIELDS
            if f not in ('hostname', 'password', 'username', 'port')
        }
        pr_fields['netloc'] = netloc
        pr_fields['params'] = ''
        # We need to quote the path
        pr_fields['path'] = urlquote(pr_fields['path'])
        # TODO: figure out what to do with query/fragment... one step at a time
        return ParseResult(**pr_fields)

    @classmethod
    def _pr_to_fields(cls, pr):
        """ParseResult is a tuple so immutable, which complicates adjusting it

        This function converts ParseResult into dict"""

        if pr.params:
            lgr.warning("ParseResults contains params %r, which will be ignored"
                        % (pr.params,))

        hostname_port = pr.netloc.split('@')[-1]
        is_ipv6 = hostname_port.count(':') >= 2
        # can't use just pr._asdict since we care to ask those properties
        # such as .port , .hostname etc
        # Forcing '' instead of None since those properties (.hostname), .password,
        # .username return None if not available and we decided to uniformize
        if is_ipv6:
            rem = re.match(r'\[(?P<hostname>.*)\]:(?P<port>\d+)', hostname_port)
            if rem:
                hostname, port = rem.groups()
                port = int(port)
            else:
                hostname, port = hostname_port, ''

            def _getattr(pr, f):
                """Helper for custom handling in case of ipv6 addresses which blows
                stock ParseResults logic"""
                if f == 'port':
                    # for now not supported at all, so
                    return port
                elif f == 'hostname':
                    return hostname
                else:
                    return getattr(pr, f)
        else:
            _getattr = getattr

        return {f: (_getattr(pr, f) or '') for f in cls._FIELDS}

    #
    # Access helpers
    #

    def _parse_qs(self, s, auto_delist=True):
        """Helper around parse_qs to strip unneeded 'list'ing etc and return a dict of key=values"""
        if not s:
            return {}
        out = map_items(assure_unicode, OrderedDict(parse_qsl(s, 1)))
        if not auto_delist:
            return out
        for k in out:
            v = out[k]
            if isinstance(v, list) and len(v) == 1:
                v = v[0]
                out[k] = None if v == '' else v
        return out

    @property
    def query_dict(self):
        return self._parse_qs(self.query)

    @property
    def fragment_dict(self):
        return self._parse_qs(self.fragment)

    @property
    def localpath(self):
        if self.scheme != 'file':
            raise ValueError(
                "Non 'file://' URL cannot be resolved to a local path")
        hostname = self.hostname
        if not (hostname in (None, '', 'localhost', '::1')
                or hostname.startswith('127.')):
            raise ValueError("file:// URL does not point to 'localhost'")
        return self.path


class PathRI(RI):
    """RI pointing to a (local) file/directory"""
    def as_str(self):
        return self.path

    @classmethod
    def _str_to_fields(cls, url_str):
        # str() to be compatible with pathlib objects
        return dict(path=str(url_str))

    @property
    def localpath(self):
        return self.path

    @property
    def posixpath(self):
        if is_windows_path(self.path):
            win_split = win_splitdrive(self.path)
            return "/" + win_split[0][0] + win_split[1].replace('\\', '/')
        else:
            return self.path


class RegexBasedURLMixin(object):
    """Base class for URLs which we could simple parse using regular expressions"""

    _REGEX = None

    # not used ATM but possible ;)
    # @classmethod
    # def is_str_matches(cls, url_str):
    #     return bool(cls._REGEX.match(url_str))

    @classmethod
    def _str_to_fields(cls, url_str):
        re_match = cls._REGEX.match(url_str)
        if not re_match:
            # TODO: custom error?
            raise ValueError(
                "Cannot handle URL '%s': categorized as %r, but does not match syntax.%s"
                % (url_str,
                   cls,
                   " Did you intent to use '///'?" if url_str.startswith('//') else '')
            )
        fields = cls._get_blank_fields()
        fields.update({k: v for k, v in re_match.groupdict().items() if v})
        cls._normalize_fields(fields)
        return fields

    @classmethod
    def _normalize_fields(self, fields):
        """Helper to be ran if any of the fields need to be normalized after parsing"""
        pass


class SSHRI(RI, RegexBasedURLMixin):
    """RI pointing to a remote location reachable via SSH"""

    _FIELDS = RI._FIELDS + (
        'username',
        'hostname',
        'port',
    )

    _REGEX = re.compile(r'((?P<username>\S*)@)?(?P<hostname>[^#/\\:]+)(\:(?P<path>.*))?$')

    @classmethod
    def _normalize_fields(cls, fields):
        if fields['path'] and fields['path'].startswith('//'):
            # Let's normalize for now to avoid multiple leading slashes
            fields['path'] = '/' + fields['path'].lstrip('/')
        # escape path so we have direct representation of the path to work with
        fields['path'] = unescape_ssh_path(fields['path'])

    def as_str(self, escape=False):
        fields = self.fields  # copy so we could escape symbols
        url_fmt = '{hostname}'
        if fields['username']:
            url_fmt = "{username}@" + url_fmt
        if fields['path']:
            url_fmt += ':{path}'
        if escape:
            fields['path'] = escape_ssh_path(fields['path'])
        return url_fmt.format(**fields)

    # TODO:
    # we can "support" localhost:path as localpaths


class DataLadRI(RI, RegexBasedURLMixin):
    """RI pointing to datasets within default DataLad super-dataset"""

    _FIELDS = RI._FIELDS + (
        'remote',
    )

    # For now or forever we don't deal with any fragments or other special stuff
    _REGEX = re.compile(r'//(?P<remote>[^\s/]*)/(?P<path>.*)$')

    # do they need to be normalized??? loosing track ...

    def as_str(self):
        return "//{remote}/{path}".format(**self._fields)

    def as_git_url(self):
        """Dereference /// into original URLs which could be used by git for cloning

        Returns
        -------
        str
          URL string to reference the DataLadRI from its /// form
        """
        if self.remote:
            raise NotImplementedError("not supported ATM to reference additional remotes")
        return "{}{}".format(consts.DATASETS_TOPURL, urlquote(self.path))


class GitTransportRI(RI, RegexBasedURLMixin):
    """RI for some other RI with git transport prefix"""

    # TODO: check how crticial to "inherit" RI._FIELDS asking to provide path
    _FIELDS = RI._FIELDS + (
        'transport',
        'RI',
    )

    # Due to poor design, `ri` argument already present in various
    # places intermixed with **kwargs treatment. So we will use RI
    # here instead of ri.
    _REGEX = re.compile(r'(?P<transport>[A-Za-z0-9][A-Za-z0-9+.-]*)::(?P<RI>.*)$')

    def as_str(self):
        return '{self.transport}::{self.RI}'.format(self=self)


def _split_colon(s, maxsplit=1):
    """Split on unescaped colon"""
    return re.compile(r'(?<!\\):').split(s, maxsplit=maxsplit)

# \ should be first to deal with
_SSH_ESCAPED_CHARACTERS = '\\#&;`|*?~<>^()[]{}$\'" '


# TODO: RF using re.sub
def escape_ssh_path(path):
    """Escape all special characters present in the path"""
    for c in _SSH_ESCAPED_CHARACTERS:
        if c in path:
            path = path.replace(c, '\\' + c)
    return path


def unescape_ssh_path(path):
    """Un-escape all special characters present in the path"""
    for c in _SSH_ESCAPED_CHARACTERS[::-1]:
        if c in path:
            path = path.replace('\\' + c, c)
    return path


def parse_url_opts(url):
    """Given a string with url-style query, split into content before # and options as dict"""
    url = URL(url)
    # we need to filter out query and fragment to get the base url
    fields = url.fields
    fields.pop('query')
    fields.pop('fragment')
    opts = url.query_dict
    return str(URL(**fields)), opts


# TODO: should we just define URL.good_for_git or smth like that? ;)
# although git also understands regular paths
def is_url(ri):
    """Returns whether argument is a resource identifier what datalad should treat as a URL

    This includes ssh "urls" which git understands.

    Parameters
    ----------
    ri : str or RI
      The resource identifier (as a string or RI) to "analyze"
    """
    if not isinstance(ri, RI):
        try:
            ri = RI(ri)
        except:  # MIH: MemoryError?
            return False
    return isinstance(ri, (URL, SSHRI))


# TODO: RF to remove duplication
def is_datalad_compat_ri(ri):
    """Returns whether argument is a resource identifier what datalad should treat as a URL

    including its own DataLadRI
    """
    if not isinstance(ri, RI):
        try:
            ri = RI(ri)
        except:  # MIH: MemoryError?
            return False
    return isinstance(ri, (URL, SSHRI, DataLadRI))


# TODO: better name? additionally may be move to SSHRI.is_valid() or sth.
def is_ssh(ri):
    """helper to determine, whether `ri` requires an SSH connection

    Parameters
    ----------
    ri: str or RI

    Returns
    -------
    bool
    """

    # not exactly fitting the doc, but we actually can deal not necessarily with
    # string or RI only, but with everything RI itself can deal with:
    _ri = RI(ri) if not isinstance(ri, RI) else ri

    return isinstance(_ri, SSHRI) \
        or (isinstance(_ri, URL) and _ri.scheme == 'ssh')


def get_local_file_url(fname, compatibility='git-annex'):
    """Return OS specific URL pointing to a local file

    Parameters
    ----------
    fname : string
        Filename.  If not absolute, abspath is used
    compatibility : {'git', 'git-annex'}, optional
        On Windows, and only on that platform, file:// URLs may need to look
        different depending on the use case or consuming application. This
        switch selects different compatibility modes: 'git' for use with
        Git commands (e.g. `clone` or `submodule add`); 'git-annex` for
        Git-annex command input (e.g. `addurl`). On any other platform this
        setting has no effect.
    """
    # we resolve the path to circumwent potential short paths on unfortunate
    # platforms that would ruin the URL format.
    #path = Path(fname).resolve().absolute()
    path = Path(fname).resolve()
    if on_windows:
        # in addition we need to absolute(), as on windows resolve() doesn't
        # imply that
        path = path.absolute().as_posix()
        furl = 'file://{}{}'.format(
            '/' if compatibility == 'git' else '',
            urlquote(
                re.sub(r'([a-zA-Z]):', r'\1', path)
            ))
    else:
        # TODO:  need to fix for all the encoding etc
        furl = str(URL(scheme='file', path=str(path)))
    return furl


def get_url_cache_filename(url, name=None):
    """Return a filename where to cache online doc from a url"""
    if not name:
        name = "misc"
    cache_dir = opj(cfg.obtain('datalad.locations.cache'), name)
    doc_fname = opj(
        cache_dir,
        '{}-{}.p{}'.format(
            urlsplit(url).netloc,
            md5(url.encode('utf-8')).hexdigest(),
            pickle.HIGHEST_PROTOCOL)
    )
    return doc_fname


def get_cached_url_content(url, name=None, fetcher=None, maxage=None):
    """Loader of a document from a url, which caches loaded instance on disk

    Doesn't do anything smart about http headers etc which could provide
    information for cache/proxy servers for how long to retain etc

    TODO: theoretically it is not network specific at all -- and just a memoize
    pattern, but may be some time we would make it treat headers etc correctly.
    And ATM would support any URL we support via providers/downloaders

    Parameters
    ----------
    fetcher: callable, optional
       Function to call with url if needed to be refetched
    maxage: float, optional
       Age in days to retain valid for.  <0 - would retain forever.  If None -
       would consult the config, 0 - would force to reload
    """
    doc_fname = get_url_cache_filename(url, name)
    if maxage is None:
        maxage = float(cfg.get('datalad.locations.cache-maxage'))

    doc = None
    if os.path.exists(doc_fname) and maxage != 0:

        fage = (time.time() - os.stat(doc_fname).st_mtime)/(24. * 3600)
        if maxage < 0 or fage < maxage:
            try:
                lgr.debug("use cached request result to '%s' from %s", url, doc_fname)
                doc = pickle.load(open(doc_fname, 'rb'))
            except Exception as e:  # it is OK to ignore any error and fall back on the true source
                lgr.warning(
                    "cannot load cache from '%s', fall back to download: %s",
                    doc_fname, exc_str(e))

    if doc is None:
        if fetcher is None:
            from datalad.downloaders.providers import Providers
            providers = Providers.from_config_files()
            fetcher = providers.fetch

        doc = fetcher(url)
        assure_dir(dirname(doc_fname))
        # use pickle to store the entire request result dict
        pickle.dump(doc, open(doc_fname, 'wb'))
        lgr.debug("stored result of request to '{}' in {}".format(url, doc_fname))
    return doc

lgr.log(5, "Done importing support.network")

```
--- END FILE ---

Please first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.

Every *SEARCH/REPLACE* edit must use this format:
1. The file path
2. The start of search block: <<<<<<< SEARCH
3. A contiguous chunk of lines to search for in the existing source code
4. The dividing line: =======
5. The lines to replace into the source code
6. The end of the replace block: >>>>>>> REPLACE

Here is an example:

```python
### mathweb/flask/app.py
<<<<<<< SEARCH
from flask import Flask
=======
import math
from flask import Flask
>>>>>>> REPLACE
```
</solution>

Please note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!
Wrap each *SEARCH/REPLACE* edit in a code block as shown in the example above. If you have multiple *SEARCH/REPLACE* edits, use a separate code block for each one.
[ISSUE]

**Title:** `iso8601_to_epoch` Does Not Correctly Handle Time Zone Offsets

**Description:**
The `iso8601_to_epoch` function fails to account for time zone information in ISO 8601 date strings, resulting in incorrect epoch timestamps for non-UTC times.

**Example Buggy Code:**
```python
def iso8601_to_epoch(datestr):
    return calendar.timegm(iso8601.parse_date(datestr).timetuple())

# Usage
epoch = 1467901515
result = iso8601_to_epoch('2016-07-07T14:25:15+11:00')
print(result)  # Outputs: 1467901515
```
In this example, the input date string includes a `+11:00` time zone offset. However, the function ignores this offset and treats the time as UTC.

**Expected Behavior:**
The function should correctly parse the time zone offset and adjust the epoch timestamp accordingly. For the given example, the expected epoch should be `1467901515 - (11 * 60 * 60) = 1467861915`.

**Actual Behavior:**
The function ignores the time zone offset, resulting in an incorrect epoch timestamp that does not reflect the specified time zone.

[/ISSUE]
{
    "test_same_website": "PASSED",
    "test_get_tld": "PASSED",
    "test_dlurljoin": "PASSED",
    "test_rfc2822_to_epoch": "PASSED",
    "test_get_response_disposition_filename": "PASSED",
    "test_parse_url_opts": "PASSED",
    "test_split_colon": "PASSED",
    "test_url_eq": "PASSED",
    "test_url_base": "PASSED",
    "test_git_transport_ri": "PASSED",
    "test_url_compose_archive_one": "PASSED",
    "test_url_fragments_and_query": "PASSED",
    "test_url_dicts": "PASSED",
    "test_get_url_path_on_fileurls": "PASSED",
    "test_is_url": "PASSED",
    "test_is_datalad_compat_ri": "PASSED",
    "test_get_local_file_url": "PASSED",
    "test_is_ssh": "PASSED",
    "test_iso8601_to_epoch": "PASSED",
    "test_get_local_file_url_compatibility": "FAILED"
}
diff --git a/datalad/support/network.py b/datalad/support/network.py
index bc9211f08..b291ebbfa 100644
--- a/datalad/support/network.py
+++ b/datalad/support/network.py
@@ -202,7 +202,7 @@ def iso8601_to_epoch(datestr):
     iso8601 is used to parse properly the time zone information, which
     can't be parsed with standard datetime strptime
     """
-    return calendar.timegm(iso8601.parse_date(datestr).timetuple())
+    return calendar.timegm(iso8601.parse_date(datestr).utctimetuple())
 
 
 def __urlopen_requests(url):

{"datalad/support/network.py": "# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-\n# ex: set sts=4 ts=4 sw=4 noet:\n# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n#\n#   See COPYING file distributed along with the datalad package for the\n#   copyright and license terms.\n#\n# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##\n\nimport logging\nlgr = logging.getLogger('datalad.network')\n\nlgr.log(5, \"Importing support.network\")\nimport calendar\nimport email.utils\nimport os\nimport pickle\nimport re\nimport sys\nimport time\nimport iso8601\n\nfrom hashlib import md5\nfrom collections import OrderedDict\nfrom os.path import abspath, isabs\nfrom os.path import join as opj\nfrom os.path import dirname\nfrom ntpath import splitdrive as win_splitdrive\n\nfrom urllib.parse import urlsplit\nfrom urllib.request import Request\nfrom urllib.parse import unquote as urlunquote\nfrom urllib.parse import urljoin, urlparse, urlsplit, urlunparse, ParseResult\nfrom urllib.parse import parse_qsl\nfrom urllib.parse import urlencode\nfrom urllib.error import URLError\n\nfrom datalad.dochelpers import exc_str\nfrom datalad.utils import (\n    on_windows,\n    PurePath,\n    Path,\n)\nfrom datalad.utils import assure_dir, assure_bytes, assure_unicode, map_items\nfrom datalad import consts\nfrom datalad import cfg\nfrom datalad.support.cache import lru_cache\n\n# !!! Lazily import requests where needed -- needs 30ms or so\n# import requests\n\n# Change introduced in 3.7: Moved from RFC 2396 to RFC 3986 for quoting URL\n# strings. \"~\" is now included in the set of reserved characters.\n# For consistency we will provide urlquote\nif sys.version_info >= (3, 7):\n    from urllib.parse import quote as urlquote\nelse:\n    from urllib.parse import quote as _urlquote\n\n    def urlquote(url, safe='/', **kwargs):\n        safe += '~'\n        return _urlquote(url, safe=safe, **kwargs)\n\n    urlquote.__doc__ = _urlquote.__doc__ + \"\"\"\n\nThis DataLad version of the function assumes ~ to be a safe character to be\nconsistent with Python >= 3.7\n\"\"\"\n\n\ndef is_windows_path(path):\n    win_split = win_splitdrive(path)\n    # Note, that ntpath.splitdrive also deals with UNC paths. In that case\n    # the \"drive\" wouldn't be a windows drive letter followed by a colon\n    if win_split[0] and win_split[1] and \\\n            win_split[0].endswith(\":\") and len(win_split[0]) == 2:\n        # seems to be a windows path\n        return True\n    return False\n\n\ndef get_response_disposition_filename(s):\n    \"\"\"Given a string s as from HTTP Content-Disposition field in the response\n    return possibly present filename if any\n    \"\"\"\n    if not s:\n        return None\n    # If the response has Content-Disposition, try to get filename from it\n    cd = map(\n        lambda x: x.strip().split('=', 1) if '=' in x else [x.strip(), ''],\n        s.split(';')\n    )\n    # unify the key to be lower case and make it into a dict\n    cd = dict([[x[0].lower()] + x[1:] for x in cd])\n    if 'filename' in cd:\n        filename = cd['filename'].strip(\"\\\"'\")\n        return filename\n    return None\n\n\ndef get_url_disposition_filename(url, headers=None):\n    \"\"\"Get filename as possibly provided by the server in Content-Disposition\n    \"\"\"\n    if headers is None:\n        request = Request(url)\n        r = retry_urlopen(request)\n        # things are different in requests\n        if 'requests.' in str(r.__class__):\n            headers = r.headers\n        else:\n            headers = r.info()\n    else:\n        r = None\n    try:\n        return get_response_disposition_filename(headers.get('Content-Disposition', ''))\n    finally:\n        if r:\n            r.close()\n\n\ndef get_url_straight_filename(url, strip=None, allowdir=False):\n    \"\"\"Get file/dir name of the last path component of the URL\n\n    Parameters\n    ----------\n    strip: list, optional\n      If provided, listed names will not be considered and their\n      parent directory will be selected\n    allowdir: bool, optional\n      If url points to a \"directory\" (ends with /), empty string\n      would be returned unless allowdir is True, in which case the\n      name of the directory would be returned\n    \"\"\"\n    path = urlunquote(urlsplit(url).path)\n    path_parts = path.split('/')\n\n    if allowdir:\n        # strip empty ones\n        while len(path_parts) > 1 and not path_parts[-1]:\n            path_parts = path_parts[:-1]\n\n    if strip:\n        while path_parts and path_parts[-1] in strip:\n            path_parts = path_parts[:-1]\n\n    if path_parts:\n        return path_parts[-1]\n    else:\n        return None\n\n\ndef get_url_filename(url, headers=None, strip=None):\n    \"\"\"Get filename from the url, first consulting server about Content-Disposition\n    \"\"\"\n    filename = get_url_disposition_filename(url, headers)\n    if filename:\n        return filename\n    return get_url_straight_filename(url, strip=strip)\n\n\ndef get_url_response_stamp(url, response_info):\n    size, mtime = None, None\n    if 'Content-length' in response_info:\n        size = int(response_info['Content-length'])\n    if 'Last-modified' in response_info:\n        mtime = calendar.timegm(email.utils.parsedate(\n            response_info['Last-modified']))\n    return dict(size=size, mtime=mtime, url=url)\n\n\ndef get_tld(url):\n    \"\"\"Return top level domain from a url\n\n    Parameters\n    ----------\n    url : str\n    \"\"\"\n    # maybe use this instead to be safe:  https://pypi.python.org/pypi/tld\n    if not url.strip():\n        raise ValueError(\"Empty URL has no TLD\")\n    rec = urlsplit(url)\n    if not rec.netloc:\n        if not rec.scheme:\n            # There were no scheme provided thus netloc was empty -- must have been a simple 'path like'\n            return url.split('/', 1)[0]\n        else:\n            raise ValueError(\"It seems that only the scheme was provided without the net location/TLD\")\n    return rec.netloc\n\n\nfrom email.utils import parsedate_tz, mktime_tz\n\n\ndef rfc2822_to_epoch(datestr):\n    \"\"\"Given rfc2822 date/time format, return seconds since epoch\"\"\"\n    return mktime_tz(parsedate_tz(datestr))\n\n\ndef iso8601_to_epoch(datestr):\n    \"\"\"Given ISO 8601 date/time format, return in seconds since epoch\n\n    iso8601 is used to parse properly the time zone information, which\n    can't be parsed with standard datetime strptime\n    \"\"\"\n    return calendar.timegm(iso8601.parse_date(datestr).timetuple())\n\n\ndef __urlopen_requests(url):\n    # XXX Workaround for now for ... broken code\n    if isinstance(url, Request):\n        url = url.get_full_url()\n    from requests import Session\n    return Session().get(url, stream=True)\n\n\ndef retry_urlopen(url, retries=3):\n    for t in range(retries):\n        try:\n            return __urlopen_requests(url)\n        except URLError as e:\n            lgr.warning(\"Received exception while reading %s: %s\" % (url, e))\n            if t == retries - 1:\n                # if we have reached allowed number of retries -- reraise\n                raise\n\n\ndef is_url_quoted(url):\n    \"\"\"Return whether URL looks being already quoted\n    \"\"\"\n    try:\n        url_ = urlunquote(url)\n        return url != url_\n    except:  # problem with unquoting -- then it must be wasn't quoted (correctly)\n        # MIH: ValueError?\n        return False\n\n\ndef same_website(url_rec, u_rec):\n    \"\"\"Decide whether a link leads to external site\n\n    Parameters\n    ----------\n    url_rec: ParseResult\n      record for original url\n    u_rec: ParseResult\n      record for new url\n    \"\"\"\n    if isinstance(url_rec, str):\n        url_rec = urlparse(url_rec)\n    if isinstance(u_rec, str):\n        u_rec = urlparse(u_rec)\n    return (url_rec.netloc == u_rec.netloc)\n    # todo: collect more of sample cases.\n    # disabled below check while working on ratholeradio, since links\n    # could go to the parent and that is ok.  Figure out when it was\n    # desired not to go to the parent -- we might need explicit option\n    # and u_rec.path.startswith(url_rec.path)):\n\n\ndef dlurljoin(u_path, url):\n    url_rec = urlparse(url)  # probably duplicating parsing :-/ TODO\n    if url_rec.scheme:\n        # independent full url, so just return it\n        return url\n    if u_path.endswith('/'):  # should here be also a scheme use?\n        if url.startswith('/'):  # jump to the root\n            u_path_rec = urlparse(u_path)\n            return urljoin(urlunparse(\n                (u_path_rec.scheme, u_path_rec.netloc, '', '', '', '')), url)\n        else:\n            return os.path.join(u_path, url)\n    # TODO: recall where all this dirname came from and bring into the test\n    return urljoin(os.path.dirname(u_path) + '/', url)\n\n\n# TODO should it be a node maybe?\nclass SimpleURLStamper(object):\n    \"\"\"Gets a simple stamp about the URL: {url, time, size} of whatever was provided in the header\n    \"\"\"\n    def __init__(self, mode='full'):\n        self.mode = mode\n\n    def __call__(self, url):\n        # Extracted from above madness\n        # TODO: add mode alike to 'relaxed' where we would not\n        # care about content-disposition filename\n        # http://stackoverflow.com/questions/862173/how-to-download-a-file-using-python-in-a-smarter-way\n        request = Request(url)\n\n        # No traffic compression since we do not know how to identify\n        # exactly either it has to be decompressed\n        # request.add_header('Accept-encoding', 'gzip,deflate')\n        #\n        # TODO: think about stamping etc -- we seems to be redoing\n        # what git-annex does for us already... not really\n        r = retry_urlopen(request)\n        try:\n            r_info = r.info()\n            r_stamp = get_url_response_stamp(url, r_info)\n\n            return dict(mtime=r_stamp['mtime'], size=r_stamp['size'], url=url)\n        finally:\n            r.close()\n\n\n# TODO:  make it consistent/clear at what stage % encoding/decoding happens!\n# now it is a mix!\n\n#\n# Useful functionality in requests.models\n#  utils.requote_uri -- quote/unquote cycle to guarantee consistent appearance\n#  RequestEncodingMixin._encode_params -- Will successfully encode parameters when passed as a dict or a list of ...\n#  PreparedRequest().prepare_url(url, params) -- nicely cares about url encodings etc\n#\n\n@lru_cache(maxsize=100)\ndef _guess_ri_cls(ri):\n    \"\"\"Factory function which would determine which type of a ri a provided string is\"\"\"\n    TYPES = {\n        'url': URL,\n        'ssh':  SSHRI,\n        'file': PathRI,\n        'datalad': DataLadRI,\n        'git-transport': GitTransportRI,\n    }\n    if isinstance(ri, PurePath):\n        lgr.log(5, \"Detected file ri\")\n        return TYPES['file']\n    if is_windows_path(ri):\n        # OMG we got something from windows\n        lgr.log(5, \"Detected file ri\")\n        return TYPES['file']\n\n    # We assume that it is a URL and parse it. Depending on the result\n    # we might decide that it was something else ;)\n    fields = URL._pr_to_fields(urlparse(ri))\n    lgr.log(5, \"Parsed ri %s into fields %s\" % (ri, fields))\n    type_ = 'url'\n    # Special treatments\n    # file:///path should stay file:\n    if fields['scheme'] and fields['scheme'] not in {'file'} \\\n            and not fields['hostname']:\n        # transport::URL-or-path\n        if fields['path'].startswith(':'):  # there was ::\n            lgr.log(5, \"Assuming git transport style ri and returning\")\n            type_ = 'git-transport'\n        # dl+archive:... or just for ssh   hostname:path/p1\n        elif '+' not in fields['scheme']:\n            type_ = 'ssh'\n            lgr.log(5, \"Assuming ssh style ri, adjusted: %s\" % (fields,))\n\n    if not fields['scheme'] and not fields['hostname']:\n        parts = _split_colon(ri)\n        # if no illegal for username@hostname characters in the first part and\n        # we either had username@hostname or multiple :-separated parts\n        if not set(parts[0]).intersection(set('/\\\\#')) and (\n                fields['path'] and\n                '@' in fields['path'] or\n                len(parts) > 1\n        ):\n            # user@host:path/sp1\n            # or host_name: (hence parts check)\n            # TODO: we need a regex to catch those really, parts check is not suff\n            type_ = 'ssh'\n        elif ri.startswith('//'):\n            # e.g. // or ///path\n            type_ = 'datalad'\n        else:\n            type_ = 'file'\n\n    if not fields['scheme'] and fields['hostname']:\n        # e.g. //a/path\n        type_ = 'datalad'\n\n    cls = TYPES[type_]\n    # just parse the ri according to regex matchint ssh \"ri\" specs\n    lgr.log(5, \"Detected %s ri\" % type_)\n    return cls\n\n\nclass RI(object):\n    \"\"\"Resource Identifier - base class and a factory for URL, SSHRI, etc\n\n    Intended to be a R/O object (i.e. no fields should be changed in-place).\n    Subclasses define specific collections of fields they care about in _FIELDS\n    class variable.\n    The idea is that this class should help to break apart a URL, while being\n    able to rebuild itself into a string representation for reuse\n\n    `RI` could be used as factory, whenever type of the resource is unknown and\n    must be guessed from the string representation.  One of the subclasses will be\n    provided as output, e.g.\n\n    >>> RI('http://example.com')\n    URL(hostname='example.com', scheme='http')\n    >>> RI('example.com:path')\n    SSHRI(hostname='example.com', path='path')\n    \"\"\"\n\n    # All of the subclasses will provide path\n    _FIELDS = (\n        'path',\n    )\n\n    __slots__ = _FIELDS + ('_fields', '_str')\n\n    def __new__(cls, ri=None, **kwargs):\n        \"\"\"Used as a possible factory for known RI types\n\n        Returns\n        -------\n        RI\n           uninitialized RI object of appropriate class with _str\n           set to string representation if was provided\n\n        \"\"\"\n        if cls is RI and ri is not None:\n            # RI class was used as a factory\n            cls = _guess_ri_cls(ri)\n\n        if cls is RI:\n            # should we fail or just pretend we are nothing??? ;-) XXX\n            raise ValueError(\"Could not deduce RI type for %r\" % (ri,))\n\n        ri_obj = super(RI, cls).__new__(cls)\n        # Store internally original str\n        ri_obj._str = str(ri) if isinstance(ri, PurePath) else ri\n        return ri_obj\n\n    def __init__(self, ri=None, **fields):\n        \"\"\"\n        Parameters\n        ----------\n        ri: str, optional\n          String version of a resource specific for this class.  If you would like\n          a type of the resource be deduced, use RI(ri). Note that this value\n          will be passed to str(), so you do not have to cast it yourself.\n        **fields: dict, optional\n          The values for the fields defined in _FIELDS class variable.\n        \"\"\"\n        if ri and (bool(ri) == bool(fields)):\n            raise ValueError(\n                \"Specify either ri or breakdown from the fields, not both. \"\n                \"Got ri=%r, fields=%r\" % (ri, fields))\n\n        self._fields = self._get_blank_fields()\n        if ri is not None:\n            ri = str(ri)\n            fields = self._str_to_fields(ri)\n        self._set_from_fields(**fields)\n\n        # If was initialized from a string representation\n        if lgr.isEnabledFor(logging.DEBUG) and self._str is not None:\n            # well -- some ris might not unparse identically back\n            # strictly speaking, but let's assume they do\n            ri_ = self.as_str()\n            if ri != ri_:\n                lgr.debug(\"Parsed version of %s %r differs from original %r\",\n                          self.__class__.__name__, ri_, ri)\n\n    @classmethod\n    def _get_blank_fields(cls, **fields):\n        return OrderedDict(((f, fields.get(f, '')) for f in cls._FIELDS))\n\n    @property\n    def fields(self):\n        \"\"\"Returns shallow copy of fields to ease manipulations\"\"\"\n        return self._fields.copy()\n\n    def __repr__(self):\n        # since auto_repr doesn't support \"non-0\" values atm\n        return \"%s(%s)\" % (\n            self.__class__.__name__,\n            \", \".join([\"%s=%r\" % (k, v)\n                       for k, v in sorted(self._fields.items())\n                       if v]))\n\n    # Lazily evaluated if _str was not set\n    def __str__(self):\n        if self._str is None:\n            self._str = self.as_str()\n        return self._str\n\n    @classmethod\n    def from_str(cls, ri_str):\n        obj = cls(**cls._str_to_fields(ri_str))\n        obj._str = ri_str\n        return obj\n\n    @property\n    def localpath(self):\n        # by default RIs point to remote locations\n        raise ValueError(\"%s points to remote location\" % self)\n\n    # Apparently doesn't quite play nicely with multiple inheritence for MixIn'\n    # of regexp based URLs\n    #@abstractmethod\n    #@classmethod\n    #def _str_to_fields(cls, ri_str):\n    #    raise NotImplementedError\n\n    #\n    # If any field is specified, URL is not considered 'False', i.e.\n    # non-existing, although may be we could/shout omit having only\n    # scheme or port specified since it doesn't point to any useful\n    # location\n    #\n\n    def __bool__(self):\n        fields = self._fields\n        return any(fields.values())\n\n    #\n    # Helpers to deal with internal structures and conversions\n    #\n\n    def _set_from_fields(self, **fields):\n        unknown_fields = set(fields).difference(self._FIELDS)\n        if unknown_fields:\n            raise ValueError(\"Do not know about %s. Known fields for %s are: %s\"\n                             % (unknown_fields, self.__class__, self._FIELDS))\n\n        # encode dicts for query or fragment into\n        for f in {'query', 'fragment'}:\n            v = fields.get(f)\n            if isinstance(v, dict):\n\n                ev = urlencode(map_items(assure_bytes, v))\n                # / is reserved char within query\n                if f == 'fragment' and '%2F' not in str(v):\n                    # but seems to be ok'ish within the fragment which is\n                    # the last element of URI and anyways used only by the\n                    # client (i.e. by us here if used to compose the URL)\n                    # so let's return / back for clarity if there were no\n                    # awkward %2F to startswith\n                    ev = ev.replace('%2F', '/')\n                fields[f] = ev\n\n        self._fields.update(fields)\n\n    #\n    # Quick comparators\n    #\n\n    def __eq__(self, other):\n        if not isinstance(other, RI):\n            other = RI(other)\n        return isinstance(other, self.__class__) and dict(other._fields) == dict(self._fields)\n\n    def __ne__(self, other):\n        return not (self == other)\n\n    def __getattribute__(self, item):\n        if item.startswith('_') or item not in self._FIELDS:\n            return super(RI, self).__getattribute__(item)\n        else:\n            return self._fields[item]\n\n    def __setattr__(self, item, value):\n        if item.startswith('_') or item not in self._FIELDS:\n            super(RI, self).__setattr__(item, value)\n        else:\n            self._fields[item] = value\n            self._str = None\n\n\nclass URL(RI):\n    \"\"\"Universal resource locator\n\n    Although largely decorating urlparse.ParseResult, it\n    - doesn't mandate providing all parts of the URL\n    - doesn't require netloc but rather asks for separate username, password, and hostname\n    \"\"\"\n\n    _FIELDS = RI._FIELDS + (\n        'scheme',\n        'username',\n        'password',\n        'hostname', 'port',\n        'query',\n        'fragment',\n    )\n\n    def as_str(self):\n        \"\"\"Render URL as a string\"\"\"\n        return urlunparse(self.to_pr())\n\n    @classmethod\n    def _str_to_fields(cls, url_str):\n        fields = URL._pr_to_fields(urlparse(url_str))\n        fields['path'] = urlunquote(fields['path'])\n        return fields\n\n    def to_pr(self):\n        \"\"\"Convert URL to urlparse.ParseResults namedtuple\"\"\"\n        return self._fields_to_pr(self._fields)\n\n    @classmethod\n    def _fields_to_pr(cls, fields):\n        \"\"\"Recompose back fields dict to ParseResult\"\"\"\n        netloc = fields['username'] or ''\n        if fields['password']:\n            netloc += ':' + fields['password']\n        if netloc:\n            netloc += '@'\n        netloc += fields['hostname']\n        if fields['port']:\n            if fields['hostname'].count(':') >= 2:\n                # ipv6 -- need to enclose in []\n                netloc = '[%s]:%s' % (netloc, fields['port'])\n            else:\n                netloc += ':%s' % fields['port']\n\n        pr_fields = {\n            f: fields[f]\n            for f in cls._FIELDS\n            if f not in ('hostname', 'password', 'username', 'port')\n        }\n        pr_fields['netloc'] = netloc\n        pr_fields['params'] = ''\n        # We need to quote the path\n        pr_fields['path'] = urlquote(pr_fields['path'])\n        # TODO: figure out what to do with query/fragment... one step at a time\n        return ParseResult(**pr_fields)\n\n    @classmethod\n    def _pr_to_fields(cls, pr):\n        \"\"\"ParseResult is a tuple so immutable, which complicates adjusting it\n\n        This function converts ParseResult into dict\"\"\"\n\n        if pr.params:\n            lgr.warning(\"ParseResults contains params %r, which will be ignored\"\n                        % (pr.params,))\n\n        hostname_port = pr.netloc.split('@')[-1]\n        is_ipv6 = hostname_port.count(':') >= 2\n        # can't use just pr._asdict since we care to ask those properties\n        # such as .port , .hostname etc\n        # Forcing '' instead of None since those properties (.hostname), .password,\n        # .username return None if not available and we decided to uniformize\n        if is_ipv6:\n            rem = re.match(r'\\[(?P<hostname>.*)\\]:(?P<port>\\d+)', hostname_port)\n            if rem:\n                hostname, port = rem.groups()\n                port = int(port)\n            else:\n                hostname, port = hostname_port, ''\n\n            def _getattr(pr, f):\n                \"\"\"Helper for custom handling in case of ipv6 addresses which blows\n                stock ParseResults logic\"\"\"\n                if f == 'port':\n                    # for now not supported at all, so\n                    return port\n                elif f == 'hostname':\n                    return hostname\n                else:\n                    return getattr(pr, f)\n        else:\n            _getattr = getattr\n\n        return {f: (_getattr(pr, f) or '') for f in cls._FIELDS}\n\n    #\n    # Access helpers\n    #\n\n    def _parse_qs(self, s, auto_delist=True):\n        \"\"\"Helper around parse_qs to strip unneeded 'list'ing etc and return a dict of key=values\"\"\"\n        if not s:\n            return {}\n        out = map_items(assure_unicode, OrderedDict(parse_qsl(s, 1)))\n        if not auto_delist:\n            return out\n        for k in out:\n            v = out[k]\n            if isinstance(v, list) and len(v) == 1:\n                v = v[0]\n                out[k] = None if v == '' else v\n        return out\n\n    @property\n    def query_dict(self):\n        return self._parse_qs(self.query)\n\n    @property\n    def fragment_dict(self):\n        return self._parse_qs(self.fragment)\n\n    @property\n    def localpath(self):\n        if self.scheme != 'file':\n            raise ValueError(\n                \"Non 'file://' URL cannot be resolved to a local path\")\n        hostname = self.hostname\n        if not (hostname in (None, '', 'localhost', '::1')\n                or hostname.startswith('127.')):\n            raise ValueError(\"file:// URL does not point to 'localhost'\")\n        return self.path\n\n\nclass PathRI(RI):\n    \"\"\"RI pointing to a (local) file/directory\"\"\"\n    def as_str(self):\n        return self.path\n\n    @classmethod\n    def _str_to_fields(cls, url_str):\n        # str() to be compatible with pathlib objects\n        return dict(path=str(url_str))\n\n    @property\n    def localpath(self):\n        return self.path\n\n    @property\n    def posixpath(self):\n        if is_windows_path(self.path):\n            win_split = win_splitdrive(self.path)\n            return \"/\" + win_split[0][0] + win_split[1].replace('\\\\', '/')\n        else:\n            return self.path\n\n\nclass RegexBasedURLMixin(object):\n    \"\"\"Base class for URLs which we could simple parse using regular expressions\"\"\"\n\n    _REGEX = None\n\n    # not used ATM but possible ;)\n    # @classmethod\n    # def is_str_matches(cls, url_str):\n    #     return bool(cls._REGEX.match(url_str))\n\n    @classmethod\n    def _str_to_fields(cls, url_str):\n        re_match = cls._REGEX.match(url_str)\n        if not re_match:\n            # TODO: custom error?\n            raise ValueError(\n                \"Cannot handle URL '%s': categorized as %r, but does not match syntax.%s\"\n                % (url_str,\n                   cls,\n                   \" Did you intent to use '///'?\" if url_str.startswith('//') else '')\n            )\n        fields = cls._get_blank_fields()\n        fields.update({k: v for k, v in re_match.groupdict().items() if v})\n        cls._normalize_fields(fields)\n        return fields\n\n    @classmethod\n    def _normalize_fields(self, fields):\n        \"\"\"Helper to be ran if any of the fields need to be normalized after parsing\"\"\"\n        pass\n\n\nclass SSHRI(RI, RegexBasedURLMixin):\n    \"\"\"RI pointing to a remote location reachable via SSH\"\"\"\n\n    _FIELDS = RI._FIELDS + (\n        'username',\n        'hostname',\n        'port',\n    )\n\n    _REGEX = re.compile(r'((?P<username>\\S*)@)?(?P<hostname>[^#/\\\\:]+)(\\:(?P<path>.*))?$')\n\n    @classmethod\n    def _normalize_fields(cls, fields):\n        if fields['path'] and fields['path'].startswith('//'):\n            # Let's normalize for now to avoid multiple leading slashes\n            fields['path'] = '/' + fields['path'].lstrip('/')\n        # escape path so we have direct representation of the path to work with\n        fields['path'] = unescape_ssh_path(fields['path'])\n\n    def as_str(self, escape=False):\n        fields = self.fields  # copy so we could escape symbols\n        url_fmt = '{hostname}'\n        if fields['username']:\n            url_fmt = \"{username}@\" + url_fmt\n        if fields['path']:\n            url_fmt += ':{path}'\n        if escape:\n            fields['path'] = escape_ssh_path(fields['path'])\n        return url_fmt.format(**fields)\n\n    # TODO:\n    # we can \"support\" localhost:path as localpaths\n\n\nclass DataLadRI(RI, RegexBasedURLMixin):\n    \"\"\"RI pointing to datasets within default DataLad super-dataset\"\"\"\n\n    _FIELDS = RI._FIELDS + (\n        'remote',\n    )\n\n    # For now or forever we don't deal with any fragments or other special stuff\n    _REGEX = re.compile(r'//(?P<remote>[^\\s/]*)/(?P<path>.*)$')\n\n    # do they need to be normalized??? loosing track ...\n\n    def as_str(self):\n        return \"//{remote}/{path}\".format(**self._fields)\n\n    def as_git_url(self):\n        \"\"\"Dereference /// into original URLs which could be used by git for cloning\n\n        Returns\n        -------\n        str\n          URL string to reference the DataLadRI from its /// form\n        \"\"\"\n        if self.remote:\n            raise NotImplementedError(\"not supported ATM to reference additional remotes\")\n        return \"{}{}\".format(consts.DATASETS_TOPURL, urlquote(self.path))\n\n\nclass GitTransportRI(RI, RegexBasedURLMixin):\n    \"\"\"RI for some other RI with git transport prefix\"\"\"\n\n    # TODO: check how crticial to \"inherit\" RI._FIELDS asking to provide path\n    _FIELDS = RI._FIELDS + (\n        'transport',\n        'RI',\n    )\n\n    # Due to poor design, `ri` argument already present in various\n    # places intermixed with **kwargs treatment. So we will use RI\n    # here instead of ri.\n    _REGEX = re.compile(r'(?P<transport>[A-Za-z0-9][A-Za-z0-9+.-]*)::(?P<RI>.*)$')\n\n    def as_str(self):\n        return '{self.transport}::{self.RI}'.format(self=self)\n\n\ndef _split_colon(s, maxsplit=1):\n    \"\"\"Split on unescaped colon\"\"\"\n    return re.compile(r'(?<!\\\\):').split(s, maxsplit=maxsplit)\n\n# \\ should be first to deal with\n_SSH_ESCAPED_CHARACTERS = '\\\\#&;`|*?~<>^()[]{}$\\'\" '\n\n\n# TODO: RF using re.sub\ndef escape_ssh_path(path):\n    \"\"\"Escape all special characters present in the path\"\"\"\n    for c in _SSH_ESCAPED_CHARACTERS:\n        if c in path:\n            path = path.replace(c, '\\\\' + c)\n    return path\n\n\ndef unescape_ssh_path(path):\n    \"\"\"Un-escape all special characters present in the path\"\"\"\n    for c in _SSH_ESCAPED_CHARACTERS[::-1]:\n        if c in path:\n            path = path.replace('\\\\' + c, c)\n    return path\n\n\ndef parse_url_opts(url):\n    \"\"\"Given a string with url-style query, split into content before # and options as dict\"\"\"\n    url = URL(url)\n    # we need to filter out query and fragment to get the base url\n    fields = url.fields\n    fields.pop('query')\n    fields.pop('fragment')\n    opts = url.query_dict\n    return str(URL(**fields)), opts\n\n\n# TODO: should we just define URL.good_for_git or smth like that? ;)\n# although git also understands regular paths\ndef is_url(ri):\n    \"\"\"Returns whether argument is a resource identifier what datalad should treat as a URL\n\n    This includes ssh \"urls\" which git understands.\n\n    Parameters\n    ----------\n    ri : str or RI\n      The resource identifier (as a string or RI) to \"analyze\"\n    \"\"\"\n    if not isinstance(ri, RI):\n        try:\n            ri = RI(ri)\n        except:  # MIH: MemoryError?\n            return False\n    return isinstance(ri, (URL, SSHRI))\n\n\n# TODO: RF to remove duplication\ndef is_datalad_compat_ri(ri):\n    \"\"\"Returns whether argument is a resource identifier what datalad should treat as a URL\n\n    including its own DataLadRI\n    \"\"\"\n    if not isinstance(ri, RI):\n        try:\n            ri = RI(ri)\n        except:  # MIH: MemoryError?\n            return False\n    return isinstance(ri, (URL, SSHRI, DataLadRI))\n\n\n# TODO: better name? additionally may be move to SSHRI.is_valid() or sth.\ndef is_ssh(ri):\n    \"\"\"helper to determine, whether `ri` requires an SSH connection\n\n    Parameters\n    ----------\n    ri: str or RI\n\n    Returns\n    -------\n    bool\n    \"\"\"\n\n    # not exactly fitting the doc, but we actually can deal not necessarily with\n    # string or RI only, but with everything RI itself can deal with:\n    _ri = RI(ri) if not isinstance(ri, RI) else ri\n\n    return isinstance(_ri, SSHRI) \\\n        or (isinstance(_ri, URL) and _ri.scheme == 'ssh')\n\n\ndef get_local_file_url(fname, compatibility='git-annex'):\n    \"\"\"Return OS specific URL pointing to a local file\n\n    Parameters\n    ----------\n    fname : string\n        Filename.  If not absolute, abspath is used\n    compatibility : {'git', 'git-annex'}, optional\n        On Windows, and only on that platform, file:// URLs may need to look\n        different depending on the use case or consuming application. This\n        switch selects different compatibility modes: 'git' for use with\n        Git commands (e.g. `clone` or `submodule add`); 'git-annex` for\n        Git-annex command input (e.g. `addurl`). On any other platform this\n        setting has no effect.\n    \"\"\"\n    # we resolve the path to circumwent potential short paths on unfortunate\n    # platforms that would ruin the URL format.\n    #path = Path(fname).resolve().absolute()\n    path = Path(fname).resolve()\n    if on_windows:\n        # in addition we need to absolute(), as on windows resolve() doesn't\n        # imply that\n        path = path.absolute().as_posix()\n        furl = 'file://{}{}'.format(\n            '/' if compatibility == 'git' else '',\n            urlquote(\n                re.sub(r'([a-zA-Z]):', r'\\1', path)\n            ))\n    else:\n        # TODO:  need to fix for all the encoding etc\n        furl = str(URL(scheme='file', path=str(path)))\n    return furl\n\n\ndef get_url_cache_filename(url, name=None):\n    \"\"\"Return a filename where to cache online doc from a url\"\"\"\n    if not name:\n        name = \"misc\"\n    cache_dir = opj(cfg.obtain('datalad.locations.cache'), name)\n    doc_fname = opj(\n        cache_dir,\n        '{}-{}.p{}'.format(\n            urlsplit(url).netloc,\n            md5(url.encode('utf-8')).hexdigest(),\n            pickle.HIGHEST_PROTOCOL)\n    )\n    return doc_fname\n\n\ndef get_cached_url_content(url, name=None, fetcher=None, maxage=None):\n    \"\"\"Loader of a document from a url, which caches loaded instance on disk\n\n    Doesn't do anything smart about http headers etc which could provide\n    information for cache/proxy servers for how long to retain etc\n\n    TODO: theoretically it is not network specific at all -- and just a memoize\n    pattern, but may be some time we would make it treat headers etc correctly.\n    And ATM would support any URL we support via providers/downloaders\n\n    Parameters\n    ----------\n    fetcher: callable, optional\n       Function to call with url if needed to be refetched\n    maxage: float, optional\n       Age in days to retain valid for.  <0 - would retain forever.  If None -\n       would consult the config, 0 - would force to reload\n    \"\"\"\n    doc_fname = get_url_cache_filename(url, name)\n    if maxage is None:\n        maxage = float(cfg.get('datalad.locations.cache-maxage'))\n\n    doc = None\n    if os.path.exists(doc_fname) and maxage != 0:\n\n        fage = (time.time() - os.stat(doc_fname).st_mtime)/(24. * 3600)\n        if maxage < 0 or fage < maxage:\n            try:\n                lgr.debug(\"use cached request result to '%s' from %s\", url, doc_fname)\n                doc = pickle.load(open(doc_fname, 'rb'))\n            except Exception as e:  # it is OK to ignore any error and fall back on the true source\n                lgr.warning(\n                    \"cannot load cache from '%s', fall back to download: %s\",\n                    doc_fname, exc_str(e))\n\n    if doc is None:\n        if fetcher is None:\n            from datalad.downloaders.providers import Providers\n            providers = Providers.from_config_files()\n            fetcher = providers.fetch\n\n        doc = fetcher(url)\n        assure_dir(dirname(doc_fname))\n        # use pickle to store the entire request result dict\n        pickle.dump(doc, open(doc_fname, 'wb'))\n        lgr.debug(\"stored result of request to '{}' in {}\".format(url, doc_fname))\n    return doc\n\nlgr.log(5, \"Done importing support.network\")\n"}
A user will ask you to solve a task. You should first draft your thinking process (inner monologue). Then, generate the solution.

Your response format must follow the template below:
<think>
Your thoughts or/and draft, like working through an exercise on scratch paper. Be as casual and as long as you want until you are confident to generate a correct solution.
</think>
<solution>
Final solution presented to the user.
</solution>
system
We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[ISSUE]

**Title:** `iso8601_to_epoch` Does Not Correctly Handle Time Zone Offsets

**Description:**
The `iso8601_to_epoch` function fails to account for time zone information in ISO 8601 date strings, resulting in incorrect epoch timestamps for non-UTC times.

**Example Buggy Code:**
```python
def iso8601_to_epoch(datestr):
    return calendar.timegm(iso8601.parse_date(datestr).timetuple())

# Usage
epoch = 1467901515
result = iso8601_to_epoch('2016-07-07T14:25:15+11:00')
print(result)  # Outputs: 1467901515
```
In this example, the input date string includes a `+11:00` time zone offset. However, the function ignores this offset and treats the time as UTC.

**Expected Behavior:**
The function should correctly parse the time zone offset and adjust the epoch timestamp accordingly. For the given example, the expected epoch should be `1467901515 - (11 * 60 * 60) = 1467861915`.

**Actual Behavior:**
The function ignores the time zone offset, resulting in an incorrect epoch timestamp that does not reflect the specified time zone.

[/ISSUE]
--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.

--- BEGIN FILE ---
```
### datalad/support/network.py
# emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
# ex: set sts=4 ts=4 sw=4 noet:
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
#   See COPYING file distributed along with the datalad package for the
#   copyright and license terms.
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##

import logging
lgr = logging.getLogger('datalad.network')

lgr.log(5, "Importing support.network")
import calendar
import email.utils
import os
import pickle
import re
import sys
import time
import iso8601

from hashlib import md5
from collections import OrderedDict
from os.path import abspath, isabs
from os.path import join as opj
from os.path import dirname
from ntpath import splitdrive as win_splitdrive

from urllib.parse import urlsplit
from urllib.request import Request
from urllib.parse import unquote as urlunquote
from urllib.parse import urljoin, urlparse, urlsplit, urlunparse, ParseResult
from urllib.parse import parse_qsl
from urllib.parse import urlencode
from urllib.error import URLError

from datalad.dochelpers import exc_str
from datalad.utils import (
    on_windows,
    PurePath,
    Path,
)
from datalad.utils import assure_dir, assure_bytes, assure_unicode, map_items
from datalad import consts
from datalad import cfg
from datalad.support.cache import lru_cache

# !!! Lazily import requests where needed -- needs 30ms or so
# import requests

# Change introduced in 3.7: Moved from RFC 2396 to RFC 3986 for quoting URL
# strings. "~" is now included in the set of reserved characters.
# For consistency we will provide urlquote
if sys.version_info >= (3, 7):
    from urllib.parse import quote as urlquote
else:
    from urllib.parse import quote as _urlquote

    def urlquote(url, safe='/', **kwargs):
        safe += '~'
        return _urlquote(url, safe=safe, **kwargs)

    urlquote.__doc__ = _urlquote.__doc__ + """

This DataLad version of the function assumes ~ to be a safe character to be
consistent with Python >= 3.7
"""


def is_windows_path(path):
    win_split = win_splitdrive(path)
    # Note, that ntpath.splitdrive also deals with UNC paths. In that case
    # the "drive" wouldn't be a windows drive letter followed by a colon
    if win_split[0] and win_split[1] and \
            win_split[0].endswith(":") and len(win_split[0]) == 2:
        # seems to be a windows path
        return True
    return False


def get_response_disposition_filename(s):
    """Given a string s as from HTTP Content-Disposition field in the response
    return possibly present filename if any
    """
    if not s:
        return None
    # If the response has Content-Disposition, try to get filename from it
    cd = map(
        lambda x: x.strip().split('=', 1) if '=' in x else [x.strip(), ''],
        s.split(';')
    )
    # unify the key to be lower case and make it into a dict
    cd = dict([[x[0].lower()] + x[1:] for x in cd])
    if 'filename' in cd:
        filename = cd['filename'].strip("\"'")
        return filename
    return None


def get_url_disposition_filename(url, headers=None):
    """Get filename as possibly provided by the server in Content-Disposition
    """
    if headers is None:
        request = Request(url)
        r = retry_urlopen(request)
        # things are different in requests
        if 'requests.' in str(r.__class__):
            headers = r.headers
        else:
            headers = r.info()
    else:
        r = None
    try:
        return get_response_disposition_filename(headers.get('Content-Disposition', ''))
    finally:
        if r:
            r.close()


def get_url_straight_filename(url, strip=None, allowdir=False):
    """Get file/dir name of the last path component of the URL

    Parameters
    ----------
    strip: list, optional
      If provided, listed names will not be considered and their
      parent directory will be selected
    allowdir: bool, optional
      If url points to a "directory" (ends with /), empty string
      would be returned unless allowdir is True, in which case the
      name of the directory would be returned
    """
    path = urlunquote(urlsplit(url).path)
    path_parts = path.split('/')

    if allowdir:
        # strip empty ones
        while len(path_parts) > 1 and not path_parts[-1]:
            path_parts = path_parts[:-1]

    if strip:
        while path_parts and path_parts[-1] in strip:
            path_parts = path_parts[:-1]

    if path_parts:
        return path_parts[-1]
    else:
        return None


def get_url_filename(url, headers=None, strip=None):
    """Get filename from the url, first consulting server about Content-Disposition
    """
    filename = get_url_disposition_filename(url, headers)
    if filename:
        return filename
    return get_url_straight_filename(url, strip=strip)


def get_url_response_stamp(url, response_info):
    size, mtime = None, None
    if 'Content-length' in response_info:
        size = int(response_info['Content-length'])
    if 'Last-modified' in response_info:
        mtime = calendar.timegm(email.utils.parsedate(
            response_info['Last-modified']))
    return dict(size=size, mtime=mtime, url=url)


def get_tld(url):
    """Return top level domain from a url

    Parameters
    ----------
    url : str
    """
    # maybe use this instead to be safe:  https://pypi.python.org/pypi/tld
    if not url.strip():
        raise ValueError("Empty URL has no TLD")
    rec = urlsplit(url)
    if not rec.netloc:
        if not rec.scheme:
            # There were no scheme provided thus netloc was empty -- must have been a simple 'path like'
            return url.split('/', 1)[0]
        else:
            raise ValueError("It seems that only the scheme was provided without the net location/TLD")
    return rec.netloc


from email.utils import parsedate_tz, mktime_tz


def rfc2822_to_epoch(datestr):
    """Given rfc2822 date/time format, return seconds since epoch"""
    return mktime_tz(parsedate_tz(datestr))


def iso8601_to_epoch(datestr):
    """Given ISO 8601 date/time format, return in seconds since epoch

    iso8601 is used to parse properly the time zone information, which
    can't be parsed with standard datetime strptime
    """
    return calendar.timegm(iso8601.parse_date(datestr).timetuple())


def __urlopen_requests(url):
    # XXX Workaround for now for ... broken code
    if isinstance(url, Request):
        url = url.get_full_url()
    from requests import Session
    return Session().get(url, stream=True)


def retry_urlopen(url, retries=3):
    for t in range(retries):
        try:
            return __urlopen_requests(url)
        except URLError as e:
            lgr.warning("Received exception while reading %s: %s" % (url, e))
            if t == retries - 1:
                # if we have reached allowed number of retries -- reraise
                raise


def is_url_quoted(url):
    """Return whether URL looks being already quoted
    """
    try:
        url_ = urlunquote(url)
        return url != url_
    except:  # problem with unquoting -- then it must be wasn't quoted (correctly)
        # MIH: ValueError?
        return False


def same_website(url_rec, u_rec):
    """Decide whether a link leads to external site

    Parameters
    ----------
    url_rec: ParseResult
      record for original url
    u_rec: ParseResult
      record for new url
    """
    if isinstance(url_rec, str):
        url_rec = urlparse(url_rec)
    if isinstance(u_rec, str):
        u_rec = urlparse(u_rec)
    return (url_rec.netloc == u_rec.netloc)
    # todo: collect more of sample cases.
    # disabled below check while working on ratholeradio, since links
    # could go to the parent and that is ok.  Figure out when it was
    # desired not to go to the parent -- we might need explicit option
    # and u_rec.path.startswith(url_rec.path)):


def dlurljoin(u_path, url):
    url_rec = urlparse(url)  # probably duplicating parsing :-/ TODO
    if url_rec.scheme:
        # independent full url, so just return it
        return url
    if u_path.endswith('/'):  # should here be also a scheme use?
        if url.startswith('/'):  # jump to the root
            u_path_rec = urlparse(u_path)
            return urljoin(urlunparse(
                (u_path_rec.scheme, u_path_rec.netloc, '', '', '', '')), url)
        else:
            return os.path.join(u_path, url)
    # TODO: recall where all this dirname came from and bring into the test
    return urljoin(os.path.dirname(u_path) + '/', url)


# TODO should it be a node maybe?
class SimpleURLStamper(object):
    """Gets a simple stamp about the URL: {url, time, size} of whatever was provided in the header
    """
    def __init__(self, mode='full'):
        self.mode = mode

    def __call__(self, url):
        # Extracted from above madness
        # TODO: add mode alike to 'relaxed' where we would not
        # care about content-disposition filename
        # http://stackoverflow.com/questions/862173/how-to-download-a-file-using-python-in-a-smarter-way
        request = Request(url)

        # No traffic compression since we do not know how to identify
        # exactly either it has to be decompressed
        # request.add_header('Accept-encoding', 'gzip,deflate')
        #
        # TODO: think about stamping etc -- we seems to be redoing
        # what git-annex does for us already... not really
        r = retry_urlopen(request)
        try:
            r_info = r.info()
            r_stamp = get_url_response_stamp(url, r_info)

            return dict(mtime=r_stamp['mtime'], size=r_stamp['size'], url=url)
        finally:
            r.close()


# TODO:  make it consistent/clear at what stage % encoding/decoding happens!
# now it is a mix!

#
# Useful functionality in requests.models
#  utils.requote_uri -- quote/unquote cycle to guarantee consistent appearance
#  RequestEncodingMixin._encode_params -- Will successfully encode parameters when passed as a dict or a list of ...
#  PreparedRequest().prepare_url(url, params) -- nicely cares about url encodings etc
#

@lru_cache(maxsize=100)
def _guess_ri_cls(ri):
    """Factory function which would determine which type of a ri a provided string is"""
    TYPES = {
        'url': URL,
        'ssh':  SSHRI,
        'file': PathRI,
        'datalad': DataLadRI,
        'git-transport': GitTransportRI,
    }
    if isinstance(ri, PurePath):
        lgr.log(5, "Detected file ri")
        return TYPES['file']
    if is_windows_path(ri):
        # OMG we got something from windows
        lgr.log(5, "Detected file ri")
        return TYPES['file']

    # We assume that it is a URL and parse it. Depending on the result
    # we might decide that it was something else ;)
    fields = URL._pr_to_fields(urlparse(ri))
    lgr.log(5, "Parsed ri %s into fields %s" % (ri, fields))
    type_ = 'url'
    # Special treatments
    # file:///path should stay file:
    if fields['scheme'] and fields['scheme'] not in {'file'} \
            and not fields['hostname']:
        # transport::URL-or-path
        if fields['path'].startswith(':'):  # there was ::
            lgr.log(5, "Assuming git transport style ri and returning")
            type_ = 'git-transport'
        # dl+archive:... or just for ssh   hostname:path/p1
        elif '+' not in fields['scheme']:
            type_ = 'ssh'
            lgr.log(5, "Assuming ssh style ri, adjusted: %s" % (fields,))

    if not fields['scheme'] and not fields['hostname']:
        parts = _split_colon(ri)
        # if no illegal for username@hostname characters in the first part and
        # we either had username@hostname or multiple :-separated parts
        if not set(parts[0]).intersection(set('/\\#')) and (
                fields['path'] and
                '@' in fields['path'] or
                len(parts) > 1
        ):
            # user@host:path/sp1
            # or host_name: (hence parts check)
            # TODO: we need a regex to catch those really, parts check is not suff
            type_ = 'ssh'
        elif ri.startswith('//'):
            # e.g. // or ///path
            type_ = 'datalad'
        else:
            type_ = 'file'

    if not fields['scheme'] and fields['hostname']:
        # e.g. //a/path
        type_ = 'datalad'

    cls = TYPES[type_]
    # just parse the ri according to regex matchint ssh "ri" specs
    lgr.log(5, "Detected %s ri" % type_)
    return cls


class RI(object):
    """Resource Identifier - base class and a factory for URL, SSHRI, etc

    Intended to be a R/O object (i.e. no fields should be changed in-place).
    Subclasses define specific collections of fields they care about in _FIELDS
    class variable.
    The idea is that this class should help to break apart a URL, while being
    able to rebuild itself into a string representation for reuse

    `RI` could be used as factory, whenever type of the resource is unknown and
    must be guessed from the string representation.  One of the subclasses will be
    provided as output, e.g.

    >>> RI('http://example.com')
    URL(hostname='example.com', scheme='http')
    >>> RI('example.com:path')
    SSHRI(hostname='example.com', path='path')
    """

    # All of the subclasses will provide path
    _FIELDS = (
        'path',
    )

    __slots__ = _FIELDS + ('_fields', '_str')

    def __new__(cls, ri=None, **kwargs):
        """Used as a possible factory for known RI types

        Returns
        -------
        RI
           uninitialized RI object of appropriate class with _str
           set to string representation if was provided

        """
        if cls is RI and ri is not None:
            # RI class was used as a factory
            cls = _guess_ri_cls(ri)

        if cls is RI:
            # should we fail or just pretend we are nothing??? ;-) XXX
            raise ValueError("Could not deduce RI type for %r" % (ri,))

        ri_obj = super(RI, cls).__new__(cls)
        # Store internally original str
        ri_obj._str = str(ri) if isinstance(ri, PurePath) else ri
        return ri_obj

    def __init__(self, ri=None, **fields):
        """
        Parameters
        ----------
        ri: str, optional
          String version of a resource specific for this class.  If you would like
          a type of the resource be deduced, use RI(ri). Note that this value
          will be passed to str(), so you do not have to cast it yourself.
        **fields: dict, optional
          The values for the fields defined in _FIELDS class variable.
        """
        if ri and (bool(ri) == bool(fields)):
            raise ValueError(
                "Specify either ri or breakdown from the fields, not both. "
                "Got ri=%r, fields=%r" % (ri, fields))

        self._fields = self._get_blank_fields()
        if ri is not None:
            ri = str(ri)
            fields = self._str_to_fields(ri)
        self._set_from_fields(**fields)

        # If was initialized from a string representation
        if lgr.isEnabledFor(logging.DEBUG) and self._str is not None:
            # well -- some ris might not unparse identically back
            # strictly speaking, but let's assume they do
            ri_ = self.as_str()
            if ri != ri_:
                lgr.debug("Parsed version of %s %r differs from original %r",
                          self.__class__.__name__, ri_, ri)

    @classmethod
    def _get_blank_fields(cls, **fields):
        return OrderedDict(((f, fields.get(f, '')) for f in cls._FIELDS))

    @property
    def fields(self):
        """Returns shallow copy of fields to ease manipulations"""
        return self._fields.copy()

    def __repr__(self):
        # since auto_repr doesn't support "non-0" values atm
        return "%s(%s)" % (
            self.__class__.__name__,
            ", ".join(["%s=%r" % (k, v)
                       for k, v in sorted(self._fields.items())
                       if v]))

    # Lazily evaluated if _str was not set
    def __str__(self):
        if self._str is None:
            self._str = self.as_str()
        return self._str

    @classmethod
    def from_str(cls, ri_str):
        obj = cls(**cls._str_to_fields(ri_str))
        obj._str = ri_str
        return obj

    @property
    def localpath(self):
        # by default RIs point to remote locations
        raise ValueError("%s points to remote location" % self)

    # Apparently doesn't quite play nicely with multiple inheritence for MixIn'
    # of regexp based URLs
    #@abstractmethod
    #@classmethod
    #def _str_to_fields(cls, ri_str):
    #    raise NotImplementedError

    #
    # If any field is specified, URL is not considered 'False', i.e.
    # non-existing, although may be we could/shout omit having only
    # scheme or port specified since it doesn't point to any useful
    # location
    #

    def __bool__(self):
        fields = self._fields
        return any(fields.values())

    #
    # Helpers to deal with internal structures and conversions
    #

    def _set_from_fields(self, **fields):
        unknown_fields = set(fields).difference(self._FIELDS)
        if unknown_fields:
            raise ValueError("Do not know about %s. Known fields for %s are: %s"
                             % (unknown_fields, self.__class__, self._FIELDS))

        # encode dicts for query or fragment into
        for f in {'query', 'fragment'}:
            v = fields.get(f)
            if isinstance(v, dict):

                ev = urlencode(map_items(assure_bytes, v))
                # / is reserved char within query
                if f == 'fragment' and '%2F' not in str(v):
                    # but seems to be ok'ish within the fragment which is
                    # the last element of URI and anyways used only by the
                    # client (i.e. by us here if used to compose the URL)
                    # so let's return / back for clarity if there were no
                    # awkward %2F to startswith
                    ev = ev.replace('%2F', '/')
                fields[f] = ev

        self._fields.update(fields)

    #
    # Quick comparators
    #

    def __eq__(self, other):
        if not isinstance(other, RI):
            other = RI(other)
        return isinstance(other, self.__class__) and dict(other._fields) == dict(self._fields)

    def __ne__(self, other):
        return not (self == other)

    def __getattribute__(self, item):
        if item.startswith('_') or item not in self._FIELDS:
            return super(RI, self).__getattribute__(item)
        else:
            return self._fields[item]

    def __setattr__(self, item, value):
        if item.startswith('_') or item not in self._FIELDS:
            super(RI, self).__setattr__(item, value)
        else:
            self._fields[item] = value
            self._str = None


class URL(RI):
    """Universal resource locator

    Although largely decorating urlparse.ParseResult, it
    - doesn't mandate providing all parts of the URL
    - doesn't require netloc but rather asks for separate username, password, and hostname
    """

    _FIELDS = RI._FIELDS + (
        'scheme',
        'username',
        'password',
        'hostname', 'port',
        'query',
        'fragment',
    )

    def as_str(self):
        """Render URL as a string"""
        return urlunparse(self.to_pr())

    @classmethod
    def _str_to_fields(cls, url_str):
        fields = URL._pr_to_fields(urlparse(url_str))
        fields['path'] = urlunquote(fields['path'])
        return fields

    def to_pr(self):
        """Convert URL to urlparse.ParseResults namedtuple"""
        return self._fields_to_pr(self._fields)

    @classmethod
    def _fields_to_pr(cls, fields):
        """Recompose back fields dict to ParseResult"""
        netloc = fields['username'] or ''
        if fields['password']:
            netloc += ':' + fields['password']
        if netloc:
            netloc += '@'
        netloc += fields['hostname']
        if fields['port']:
            if fields['hostname'].count(':') >= 2:
                # ipv6 -- need to enclose in []
                netloc = '[%s]:%s' % (netloc, fields['port'])
            else:
                netloc += ':%s' % fields['port']

        pr_fields = {
            f: fields[f]
            for f in cls._FIELDS
            if f not in ('hostname', 'password', 'username', 'port')
        }
        pr_fields['netloc'] = netloc
        pr_fields['params'] = ''
        # We need to quote the path
        pr_fields['path'] = urlquote(pr_fields['path'])
        # TODO: figure out what to do with query/fragment... one step at a time
        return ParseResult(**pr_fields)

    @classmethod
    def _pr_to_fields(cls, pr):
        """ParseResult is a tuple so immutable, which complicates adjusting it

        This function converts ParseResult into dict"""

        if pr.params:
            lgr.warning("ParseResults contains params %r, which will be ignored"
                        % (pr.params,))

        hostname_port = pr.netloc.split('@')[-1]
        is_ipv6 = hostname_port.count(':') >= 2
        # can't use just pr._asdict since we care to ask those properties
        # such as .port , .hostname etc
        # Forcing '' instead of None since those properties (.hostname), .password,
        # .username return None if not available and we decided to uniformize
        if is_ipv6:
            rem = re.match(r'\[(?P<hostname>.*)\]:(?P<port>\d+)', hostname_port)
            if rem:
                hostname, port = rem.groups()
                port = int(port)
            else:
                hostname, port = hostname_port, ''

            def _getattr(pr, f):
                """Helper for custom handling in case of ipv6 addresses which blows
                stock ParseResults logic"""
                if f == 'port':
                    # for now not supported at all, so
                    return port
                elif f == 'hostname':
                    return hostname
                else:
                    return getattr(pr, f)
        else:
            _getattr = getattr

        return {f: (_getattr(pr, f) or '') for f in cls._FIELDS}

    #
    # Access helpers
    #

    def _parse_qs(self, s, auto_delist=True):
        """Helper around parse_qs to strip unneeded 'list'ing etc and return a dict of key=values"""
        if not s:
            return {}
        out = map_items(assure_unicode, OrderedDict(parse_qsl(s, 1)))
        if not auto_delist:
            return out
        for k in out:
            v = out[k]
            if isinstance(v, list) and len(v) == 1:
                v = v[0]
                out[k] = None if v == '' else v
        return out

    @property
    def query_dict(self):
        return self._parse_qs(self.query)

    @property
    def fragment_dict(self):
        return self._parse_qs(self.fragment)

    @property
    def localpath(self):
        if self.scheme != 'file':
            raise ValueError(
                "Non 'file://' URL cannot be resolved to a local path")
        hostname = self.hostname
        if not (hostname in (None, '', 'localhost', '::1')
                or hostname.startswith('127.')):
            raise ValueError("file:// URL does not point to 'localhost'")
        return self.path


class PathRI(RI):
    """RI pointing to a (local) file/directory"""
    def as_str(self):
        return self.path

    @classmethod
    def _str_to_fields(cls, url_str):
        # str() to be compatible with pathlib objects
        return dict(path=str(url_str))

    @property
    def localpath(self):
        return self.path

    @property
    def posixpath(self):
        if is_windows_path(self.path):
            win_split = win_splitdrive(self.path)
            return "/" + win_split[0][0] + win_split[1].replace('\\', '/')
        else:
            return self.path


class RegexBasedURLMixin(object):
    """Base class for URLs which we could simple parse using regular expressions"""

    _REGEX = None

    # not used ATM but possible ;)
    # @classmethod
    # def is_str_matches(cls, url_str):
    #     return bool(cls._REGEX.match(url_str))

    @classmethod
    def _str_to_fields(cls, url_str):
        re_match = cls._REGEX.match(url_str)
        if not re_match:
            # TODO: custom error?
            raise ValueError(
                "Cannot handle URL '%s': categorized as %r, but does not match syntax.%s"
                % (url_str,
                   cls,
                   " Did you intent to use '///'?" if url_str.startswith('//') else '')
            )
        fields = cls._get_blank_fields()
        fields.update({k: v for k, v in re_match.groupdict().items() if v})
        cls._normalize_fields(fields)
        return fields

    @classmethod
    def _normalize_fields(self, fields):
        """Helper to be ran if any of the fields need to be normalized after parsing"""
        pass


class SSHRI(RI, RegexBasedURLMixin):
    """RI pointing to a remote location reachable via SSH"""

    _FIELDS = RI._FIELDS + (
        'username',
        'hostname',
        'port',
    )

    _REGEX = re.compile(r'((?P<username>\S*)@)?(?P<hostname>[^#/\\:]+)(\:(?P<path>.*))?$')

    @classmethod
    def _normalize_fields(cls, fields):
        if fields['path'] and fields['path'].startswith('//'):
            # Let's normalize for now to avoid multiple leading slashes
            fields['path'] = '/' + fields['path'].lstrip('/')
        # escape path so we have direct representation of the path to work with
        fields['path'] = unescape_ssh_path(fields['path'])

    def as_str(self, escape=False):
        fields = self.fields  # copy so we could escape symbols
        url_fmt = '{hostname}'
        if fields['username']:
            url_fmt = "{username}@" + url_fmt
        if fields['path']:
            url_fmt += ':{path}'
        if escape:
            fields['path'] = escape_ssh_path(fields['path'])
        return url_fmt.format(**fields)

    # TODO:
    # we can "support" localhost:path as localpaths


class DataLadRI(RI, RegexBasedURLMixin):
    """RI pointing to datasets within default DataLad super-dataset"""

    _FIELDS = RI._FIELDS + (
        'remote',
    )

    # For now or forever we don't deal with any fragments or other special stuff
    _REGEX = re.compile(r'//(?P<remote>[^\s/]*)/(?P<path>.*)$')

    # do they need to be normalized??? loosing track ...

    def as_str(self):
        return "//{remote}/{path}".format(**self._fields)

    def as_git_url(self):
        """Dereference /// into original URLs which could be used by git for cloning

        Returns
        -------
        str
          URL string to reference the DataLadRI from its /// form
        """
        if self.remote:
            raise NotImplementedError("not supported ATM to reference additional remotes")
        return "{}{}".format(consts.DATASETS_TOPURL, urlquote(self.path))


class GitTransportRI(RI, RegexBasedURLMixin):
    """RI for some other RI with git transport prefix"""

    # TODO: check how crticial to "inherit" RI._FIELDS asking to provide path
    _FIELDS = RI._FIELDS + (
        'transport',
        'RI',
    )

    # Due to poor design, `ri` argument already present in various
    # places intermixed with **kwargs treatment. So we will use RI
    # here instead of ri.
    _REGEX = re.compile(r'(?P<transport>[A-Za-z0-9][A-Za-z0-9+.-]*)::(?P<RI>.*)$')

    def as_str(self):
        return '{self.transport}::{self.RI}'.format(self=self)


def _split_colon(s, maxsplit=1):
    """Split on unescaped colon"""
    return re.compile(r'(?<!\\):').split(s, maxsplit=maxsplit)

# \ should be first to deal with
_SSH_ESCAPED_CHARACTERS = '\\#&;`|*?~<>^()[]{}$\'" '


# TODO: RF using re.sub
def escape_ssh_path(path):
    """Escape all special characters present in the path"""
    for c in _SSH_ESCAPED_CHARACTERS:
        if c in path:
            path = path.replace(c, '\\' + c)
    return path


def unescape_ssh_path(path):
    """Un-escape all special characters present in the path"""
    for c in _SSH_ESCAPED_CHARACTERS[::-1]:
        if c in path:
            path = path.replace('\\' + c, c)
    return path


def parse_url_opts(url):
    """Given a string with url-style query, split into content before # and options as dict"""
    url = URL(url)
    # we need to filter out query and fragment to get the base url
    fields = url.fields
    fields.pop('query')
    fields.pop('fragment')
    opts = url.query_dict
    return str(URL(**fields)), opts


# TODO: should we just define URL.good_for_git or smth like that? ;)
# although git also understands regular paths
def is_url(ri):
    """Returns whether argument is a resource identifier what datalad should treat as a URL

    This includes ssh "urls" which git understands.

    Parameters
    ----------
    ri : str or RI
      The resource identifier (as a string or RI) to "analyze"
    """
    if not isinstance(ri, RI):
        try:
            ri = RI(ri)
        except:  # MIH: MemoryError?
            return False
    return isinstance(ri, (URL, SSHRI))


# TODO: RF to remove duplication
def is_datalad_compat_ri(ri):
    """Returns whether argument is a resource identifier what datalad should treat as a URL

    including its own DataLadRI
    """
    if not isinstance(ri, RI):
        try:
            ri = RI(ri)
        except:  # MIH: MemoryError?
            return False
    return isinstance(ri, (URL, SSHRI, DataLadRI))


# TODO: better name? additionally may be move to SSHRI.is_valid() or sth.
def is_ssh(ri):
    """helper to determine, whether `ri` requires an SSH connection

    Parameters
    ----------
    ri: str or RI

    Returns
    -------
    bool
    """

    # not exactly fitting the doc, but we actually can deal not necessarily with
    # string or RI only, but with everything RI itself can deal with:
    _ri = RI(ri) if not isinstance(ri, RI) else ri

    return isinstance(_ri, SSHRI) \
        or (isinstance(_ri, URL) and _ri.scheme == 'ssh')


def get_local_file_url(fname, compatibility='git-annex'):
    """Return OS specific URL pointing to a local file

    Parameters
    ----------
    fname : string
        Filename.  If not absolute, abspath is used
    compatibility : {'git', 'git-annex'}, optional
        On Windows, and only on that platform, file:// URLs may need to look
        different depending on the use case or consuming application. This
        switch selects different compatibility modes: 'git' for use with
        Git commands (e.g. `clone` or `submodule add`); 'git-annex` for
        Git-annex command input (e.g. `addurl`). On any other platform this
        setting has no effect.
    """
    # we resolve the path to circumwent potential short paths on unfortunate
    # platforms that would ruin the URL format.
    #path = Path(fname).resolve().absolute()
    path = Path(fname).resolve()
    if on_windows:
        # in addition we need to absolute(), as on windows resolve() doesn't
        # imply that
        path = path.absolute().as_posix()
        furl = 'file://{}{}'.format(
            '/' if compatibility == 'git' else '',
            urlquote(
                re.sub(r'([a-zA-Z]):', r'\1', path)
            ))
    else:
        # TODO:  need to fix for all the encoding etc
        furl = str(URL(scheme='file', path=str(path)))
    return furl


def get_url_cache_filename(url, name=None):
    """Return a filename where to cache online doc from a url"""
    if not name:
        name = "misc"
    cache_dir = opj(cfg.obtain('datalad.locations.cache'), name)
    doc_fname = opj(
        cache_dir,
        '{}-{}.p{}'.format(
            urlsplit(url).netloc,
            md5(url.encode('utf-8')).hexdigest(),
            pickle.HIGHEST_PROTOCOL)
    )
    return doc_fname


def get_cached_url_content(url, name=None, fetcher=None, maxage=None):
    """Loader of a document from a url, which caches loaded instance on disk

    Doesn't do anything smart about http headers etc which could provide
    information for cache/proxy servers for how long to retain etc

    TODO: theoretically it is not network specific at all -- and just a memoize
    pattern, but may be some time we would make it treat headers etc correctly.
    And ATM would support any URL we support via providers/downloaders

    Parameters
    ----------
    fetcher: callable, optional
       Function to call with url if needed to be refetched
    maxage: float, optional
       Age in days to retain valid for.  <0 - would retain forever.  If None -
       would consult the config, 0 - would force to reload
    """
    doc_fname = get_url_cache_filename(url, name)
    if maxage is None:
        maxage = float(cfg.get('datalad.locations.cache-maxage'))

    doc = None
    if os.path.exists(doc_fname) and maxage != 0:

        fage = (time.time() - os.stat(doc_fname).st_mtime)/(24. * 3600)
        if maxage < 0 or fage < maxage:
            try:
                lgr.debug("use cached request result to '%s' from %s", url, doc_fname)
                doc = pickle.load(open(doc_fname, 'rb'))
            except Exception as e:  # it is OK to ignore any error and fall back on the true source
                lgr.warning(
                    "cannot load cache from '%s', fall back to download: %s",
                    doc_fname, exc_str(e))

    if doc is None:
        if fetcher is None:
            from datalad.downloaders.providers import Providers
            providers = Providers.from_config_files()
            fetcher = providers.fetch

        doc = fetcher(url)
        assure_dir(dirname(doc_fname))
        # use pickle to store the entire request result dict
        pickle.dump(doc, open(doc_fname, 'wb'))
        lgr.debug("stored result of request to '{}' in {}".format(url, doc_fname))
    return doc

lgr.log(5, "Done importing support.network")

```
--- END FILE ---

Please first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.

Every *SEARCH/REPLACE* edit must use this format:
1. The file path
2. The start of search block: <<<<<<< SEARCH
3. A contiguous chunk of lines to search for in the existing source code
4. The dividing line: =======
5. The lines to replace into the source code
6. The end of the replace block: >>>>>>> REPLACE

Here is an example:

```python
### mathweb/flask/app.py
<<<<<<< SEARCH
from flask import Flask
=======
import math
from flask import Flask
>>>>>>> REPLACE
```
</solution>

Please note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!
Wrap each *SEARCH/REPLACE* edit in a code block as shown in the example above. If you have multiple *SEARCH/REPLACE* edits, use a separate code block for each one.
user