def test_space_and_plus_in_name(): assert url.URL("+").local() == "+" assert url.URL(" ").local() == " " assert url.URL("%20").local() == " " assert url.File("+").local() == "+" assert url.File(" ").local() == " " assert str(url.File(" ")) in ("file:%20", "file:+")
def check(u, u2): u = url.URL(u) u1 = u.clone() u1.path.normalize() u2 = url.URL(u2) assert u1 == u2, "{!r} normalized is {!r}, but should be {!r}".format( u, u1, u2)
def check(u1, u2, owner, group): with url.Context(): u1 = url.URL(u1) u2 = url.URL(u2) r = u1.open("wb") try: try: r.write(b"foo") finally: r.close() # Might have been left over from previous run if u2.exists(): u2.remove() try: u1.symlink(u2) u2.lchown(owner, group) assert u1.owner() == owner assert u1.group() == group assert u2.owner() == owner assert u2.group() == group u2.chown(owner, group) assert u1.owner() == owner assert u1.group() == group assert u2.owner() == owner assert u2.group() == group finally: u2.remove() finally: u1.remove()
def check(base, rel, res): baseurl = url.URL(base) relurl = url.URL(rel) resurl = url.URL(res) assert relurl.relative( baseurl ) == resurl, f"{relurl!r}.relative({baseurl!r}) is {relurl.relative(baseurl)!r}, but should be {resurl!r}"
def __init__(self, pool=None, base=None, loc=True): """ Create a :class:`Node` object. :obj:`pool` may be ``None`` or a :class:`xsc.Pool` object and specifies which classes used for creating element, entity and processsing instruction instances. :obj:`base` specifies the base URL for interpreting relative links in the input. :obj:`loc` specified whether location information should be attached to the nodes that get generated (the :obj:`startloc` attribute (and :obj:`endloc` attribute for elements)) """ self.pool = (pool if pool is not None else xsc.threadlocalpool.pool) if base is not None: base = url_.URL(base) self._base = base self._url = url_.URL() self.loc = loc self._position = (None, None) self._stack = [] self._inattr = False self._indoctype = False
def test_join_list(): assert ["", "gurk", "gurk/"] / url.URL("index.html") == [ url.URL(s) for s in ["index.html", "index.html", "gurk/index.html"] ] assert url.URL("gurk/") / ["", "hinz", "kunz"] == [ url.URL(s) for s in ["gurk/", "gurk/hinz", "gurk/kunz"] ]
def check(rel, res): relurl = url.URL(rel) resurl = url.URL(res) assert baseurl / relurl == resurl, f"{baseurl!r}/{relurl!r} is {baseurl/relurl!r}, but should be {resurl!r}" # This checks rdiv assert str( baseurl ) / relurl == resurl, f"{baseurl!r}/{relurl!r} is {str(baseurl)/relurl!r}, but should be {resurl!r}"
def test_without(): u1a = url.URL("x#y") u1b = url.URL("x#y") u2 = url.URL("x") u3 = url.URL("x#") assert u1a.withoutfrag() == u2 assert u1a.withoutfrag() != u3 assert u1a == u1b # make sure withoutfrag created a new URL
def check(base, rel, res): baseurl = url.URL(base) relurl = url.URL(rel) resurl = url.URL(res) assert relurl.relative( baseurl ) == resurl, "{!r}.relative({!r}) is {!r}, but should be {!r}".format( relurl, baseurl, relurl.relative(baseurl), resurl)
def check(rel, res): relurl = url.URL(rel) resurl = url.URL(res) assert baseurl / relurl == resurl, "{!r}/{!r} is {!r}, but should be {!r}".format( baseurl, relurl, baseurl / relurl, resurl) # This checks rdiv assert str( baseurl ) / relurl == resurl, "{!r}/{!r} is {!r}, but should be {!r}".format( baseurl, relurl, str(baseurl) / relurl, resurl)
def test_parse(): s = css.parsestring( b"@charset 'utf-8'; div{background-image: url(gurk.gif);}") urls = set(css.geturls(s)) assert urls == {url.URL("gurk.gif")} s = css.parsestring( b"@charset 'utf-8'; div{background-image: url(gurk.gif);}", base="root:") urls = set(css.geturls(s)) assert urls == {url.URL("root:gurk.gif")}
def check(u, pu, isfile, include=None, exclude=None): with url.Context(): u = url.URL(u) pu = url.URL(pu) assert u in pu.listdir(include=include, exclude=exclude) if isfile: assert u in pu.files(include=include, exclude=exclude) assert u not in pu.dirs(include=include, exclude=exclude) else: assert u not in pu.files(include=include, exclude=exclude) assert u in pu.dirs(include=include, exclude=exclude)
def test_schemerelurls(): u1 = url.URL("http://www.example.org/about/index.html") u2 = url.URL("http://www.example.com/images/logo.png") u3 = u2.relative(u1, allowschemerel=True) assert u3.scheme is None assert str(u3) == "//www.example.com/images/logo.png" u1 = url.URL("http://www.example.org/about/index.html") u2 = url.URL("http://www.example.org/images/logo.png") u3 = u2.relative(u1, allowschemerel=True) assert u3.scheme is None assert str(u3) == "../images/logo.png"
def test_parseurls(): # Check proper URL handling when parsing ``URLAttr`` or ``StyleAttr`` attributes node = parse.tree( b'<a href="4.html" style="background-image: url(3.gif);"/>', parse.Expat(), parse.NS(html), parse.Node(base="root:1/2.html"), validate=True) assert str(node[0]["style"]) == "background-image: url(root:1/3.gif)" assert node[0]["style"].urls() == [url.URL("root:1/3.gif")] assert str(node[0]["href"]) == "root:1/4.html" assert node[0]["href"].forInput( root="gurk/hurz.html") == url.URL("gurk/1/4.html")
def test_ssh_params(): with url.Context(): u = url.URL( "ssh://[email protected]/~/checkouts/LivingLogic.Python.xist/" ) assert u.isdir(python="/usr/local/bin/python3.2") is True assert u.isdir(nice=20) is True
def test_urlsource(): expect = url.URL("http://www.python.org/").openread().read() source = parse.URL("http://www.python.org/", bufsize=32) for i in range(3): parsed = b"".join(data for (evtype, data) in source if evtype == "bytes") assert parsed == expect
def geturls(stylesheet): """ Return a list of all URLs appearing in the :class:`CSSStyleSheet` :obj:`stylesheet`. """ return [url.URL(u) for u in cssutils.getUrls(stylesheet) ] # This requires cssutils 0.9.5b1
def _doimport(wantmedia, parentsheet, base): def prependbase(u): if base is not None: u = base / u return u havemedia = _getmedia(parentsheet) if wantmedia is None or not havemedia or wantmedia in havemedia: replaceurls(parentsheet, prependbase) for rule in parentsheet.cssRules: if rule.type == css.CSSRule.IMPORT_RULE: href = url.URL(rule.href) if base is not None: href = base / href havemedia = rule.media with contextlib.closing(href.open("rb")) as r: href = r.finalurl() text = r.read() sheet = css.CSSStyleSheet(href=str(href), media=havemedia, parentStyleSheet=parentsheet) sheet.cssText = text yield from _doimport(wantmedia, sheet, href) elif rule.type == css.CSSRule.MEDIA_RULE: if wantmedia in (mq.value.mediaType for mq in rule.media): yield from rule.cssRules elif rule.type == css.CSSRule.STYLE_RULE: yield rule
def __init__(self, iterable, url=None): """ Create a :class:`Iter` object. :obj:`iterable` must be an iterable object producing :class:`bytes` or :class:`str` objects. :obj:`url` specifies the URL for the source (defaulting to ``"ITER"``). """ self.url = url_.URL(url if url is not None else "ITER") self.iterable = iterable
def __init__(self, data, url=None): """ Create a :class:`String` object. :obj:`data` must be a :class:`bytes` or :class:`str` object. :obj:`url` specifies the URL for the source (defaulting to ``"STRING"``). """ self.url = url_.URL(url if url is not None else "STRING") self.data = data
def test_join_rfc2396(): base = "http://a/b/c/d;p?q" baseurl = url.URL(base) def check(rel, res): relurl = url.URL(rel) resurl = url.URL(res) assert baseurl / relurl == resurl, f"{baseurl!r}/{relurl!r} is {baseurl/relurl!r}, but should be {resurl!r}" # This checks rdiv assert str( baseurl ) / relurl == resurl, f"{baseurl!r}/{relurl!r} is {str(baseurl)/relurl!r}, but should be {resurl!r}" # RFC2396 Section C.1: Normal Examples check("g:h", "g:h") check("g", "http://a/b/c/g") check("./g", "http://a/b/c/g") check("g/", "http://a/b/c/g/") check("/g", "http://a/g") check("//g", "http://g") check("?y", "http://a/b/c/?y") check("g?y", "http://a/b/c/g?y") check("#s", "http://a/b/c/d;p?q#s") check("g#s", "http://a/b/c/g#s") check("g?y#s", "http://a/b/c/g?y#s") check(";x", "http://a/b/c/;x") check("g;x", "http://a/b/c/g;x") check("g;x?y#s", "http://a/b/c/g;x?y#s") check(".", "http://a/b/c/") check("./", "http://a/b/c/") check("..", "http://a/b/") check("../", "http://a/b/") check("../g", "http://a/b/g") check("../..", "http://a/") check("../../", "http://a/") check("../../g", "http://a/g") # RFC2396 Section C.2: Abnormal Examples check("", "http://a/b/c/d;p?q") check("../../../g", "http://a/../g") check("../../../../g", "http://a/../../g") check("/./g", "http://a/./g") check("/../g", "http://a/../g") check("g.", "http://a/b/c/g.") check(".g", "http://a/b/c/.g") check("g..", "http://a/b/c/g..") check("..g", "http://a/b/c/..g") check("./../g", "http://a/b/g") check("./g/.", "http://a/b/c/g/") check("g/./h", "http://a/b/c/g/h") check("g/../h", "http://a/b/c/h") check("g;x=1/./y", "http://a/b/c/g;x=1/y") check("g;x=1/../y", "http://a/b/c/y") check("g?y/./x", "http://a/b/c/g?y/./x") check("g?y/../x", "http://a/b/c/g?y/../x") check("g#s/./x", "http://a/b/c/g#s/./x") check("g#s/../x", "http://a/b/c/g#s/../x") check("http:g", "http:g") # use the validating version here
def check(u): with url.Context(): u = url.URL(u) / "foo/" u.mkdir(0o755) try: assert u.isdir() assert u.stat().st_mode & 0o777 == 0o755 finally: u.rmdir()
def check(u): with url.Context(): try: u = url.URL(u) with u.openwrite() as f: f.write(b"Hurz!") finally: u.remove() u.withoutfile().rmdir()
def test_relpathauthority(): u = url.URL("http://www.foo.com/bar/baz;baz") u2 = u.clone() u2.path = [seg.upper() for seg in u2.path.segments] assert not u2.path.isabs assert str(u2) == "http://www.foo.com/BAR/BAZ;BAZ" del u2.scheme del u2.authority assert str(u2) == "BAR/BAZ;BAZ"
def test_url(): dburl = dbname.replace("/", ":") u = url.URL(f"oracle://{dburl}/") assert u.isdir() assert u.mimetype() == "application/octet-stream" u.owner() u.cdate() u.mdate() u.listdir() u.files() u.dirs() u = url.URL(f"oracle://{dburl}/procedure/ORASQL_TESTPROCEDURE") assert u.isfile() assert u.mimetype() == "text/x-oracle-procedure" u.owner() u.cdate() u.mdate() assert "orasql_testprocedure" in u.open("r").read().lower()
def check(u): with url.Context(): u = url.URL(u) / "foo/bar/" u.makedirs(0o755) try: assert u.isdir() assert u.stat().st_mode & 0o777 == 0o755 finally: u.rmdir() (u / "../").rmdir()
def __init__(self, stream, url=None, bufsize=8192): """ Create a :class:`Stream` object. :obj:`stream` must have a :meth:`read` method (with a ``size`` argument). :obj:`url` specifies the URL for the source (defaulting to ``"STREAM"``). :obj:`bufsize` specifies the chunksize for reads from the stream. """ self.url = url_.URL(url if url is not None else "STREAM") self.stream = stream self.bufsize = bufsize
def check(u): with url.Context(): u = url.URL(u) u2 = u / "foo" try: u.symlink(u2) assert u2.exists() assert u2.islink() finally: u2.remove() assert not u2.exists()
def check(u, pu, isfile, include=None, exclude=None): with url.Context(): u = url.URL(u) pu = url.URL(pu) assert any(u == wu for wu in pu.walkall(include=include, exclude=exclude)) if isfile: assert any( u == wu for wu in pu.walkfiles(include=include, exclude=exclude)) assert all( u != wu for wu in pu.walkdirs(include=include, exclude=exclude)) else: assert all( u != wu for wu in pu.walkfiles(include=include, exclude=exclude)) assert any( u == wu for wu in pu.walkdirs(include=include, exclude=exclude))
def test_expat_events_on_exception(): # Test that all collected events are output before an exception is thrown i = parse.events(b"<x/>schrott", parse.Expat()) assert next(i) == ("url", url.URL("STRING")) assert next(i) == ("position", (0, 0)) assert next(i) == ("enterstarttag", "x") assert next(i) == ("leavestarttag", "x") assert next(i) == ("position", (0, 4)) assert next(i) == ("endtag", "x") with pytest.raises(expat.ExpatError): next(i)