urlpy2


Nameurlpy2 JSON
Version 1.0.10 PyPI version JSON
download
home_pagehttp://github.com/eon01/urlpy2
SummarySimple URL parsing, canonicalization and equivalence.
upload_time2022-06-30 15:51:23
maintainer
docs_urlNone
authorAymen El Amri based on code from nexB Inc
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            # URLPY2

urlpy2 is a small library for URL parsing, cleanup, canonicalization and equivalence.
 
At the heart of the `urlpy` package is the `URL` object. You can get one by
passing in a unicode or string object into the top-level `parse` method. All
strings asre assumed to be Unicode:

```python
import urlpy2 as urlpy
myurl = urlpy.parse('http://foo.com')
```

The workflow is that you'll chain a number of permutations together to get the type
of URL you're after:

```python
# Defrag, remove some parameters and give me a string
str(urlpy.parse(...).defrag().deparam(['utm_source']))

# Escape the path, and punycode the host, and give me a string
str(urlpy.parse(...).escape().punycode())

# Give me the absolute path url as some encoding
str(urlpy.parse(...).abspath()).encode('some encoding')
```

## Installation

```
pip install urlpy2
```

## URL Equivalence

URL objects compared with `==` are interpreted very strictly, but for a more
lax interpretation, consider using `equiv` to test if two urls are functionally
equivalent:

```python
a = urlpy.parse(u'https://föo.com:443/a/../b/.?b=2&&&&&&a=1')
b = urlpy.parse(u'https://xn--fo-fka.COM/b/?a=1&b=2')

# These urls are not equal
assert(a != b)

# But they are equivalent
assert(a.equiv(b))
assert(b.equiv(a))
```

This equivalence test takes default ports for common schemes into account (so
if both urls are the same scheme, but one explicitly specifies the default
port), punycoding, case of the host name, and parameter order.


## Absolute URLs

You can perform many operations on relative urls (those without a hostname),
but punycoding and unpunycoding are not among them. You can also tell whether
or not a url is absolute:

```python
a = urlpy.parse('foo/bar.html')
assert(not a.absolute())
```

## Chaining

Many of the methods on the `URL` class can be chained to produce a number of
effects in sequence:

```python
import urlpy2 as urlpy

# Create a url object
myurl = urlpy.URL.parse('http://www.FOO.com/bar?utm_source=foo#what')
# Remove some parameters and the fragment
print(myurl.defrag().deparam(['utm_source']))
```

In fact, unless the function explicitly returns a string, then the method may
be chained.


### `canonical`

According to the RFC, the order of parameters is not supposed to matter. In
practice, it can (depending on how the server matches URL routes), but it's
also helpful to be able to put parameters in a canonical ordering. This
ordering happens to be alphabetical order:

```python
>>> str(urlpy.parse('http://foo.com/?b=2&a=1&d=3').canonical())
'http://foo.com/?a=1&b=2&d=3'
```


### `defrag`

Remove any fragment identifier from the url. This isn't part of the reuqest
that gets sent to an HTTP server, and so it's often useful to remove the 
fragment when doing url comparisons:

```python
>>> str(urlpy.parse('http://foo.com/#foo').defrag())
'http://foo.com/'
```

### `deparam`

Some parameters are commonly added to urls that we may not be interested in. Or
they may be misleading. Common examples include referrering pages, `utm_source`
and session ids. To strip out all such parameters from your url:

```python
>>> str(urlpy.parse('http://foo.com/?do=1&not=2&want=3&this=4').deparam(['do', 'not', 'want']))
'http://foo.com/?this=4'
```

### `r_deparam`

Same as `deparam` but uses regex:


```python
>>> str(urlpy.parse('http://foo.com/?utm_a=1&utm_b=2&utm_c=3&utm_d=4').deparam(['utm_*',]))
'http://foo.com/'
```

### `remove_tracking`

Removes all tracking and referall marketing parameters from the URL based on [CleanURLs list](https://gitlab.com/ClearURLs/rules/-/raw/master/data.min.json).

```python
>>> str(urlpy.parse('https://www.google.com/search?q=python&oq=python&aqs=chrome..69i57j0l5.8984j0j7&sourceid=chrome&ie=UTF-8').remove_tracking())
'https://www.google.com/search?q=python'
```

To keep the referall marketing parameters, use `remove_tracking(remove_referall_marketing=False)`.

### `abspath`

Like its `os.path` namesake, this makes sure that the path of the url is
absolute. This includes removing redundant forward slashes, `.` and `..`:

```python
>>> str(urlpy.parse('http://foo.com/foo/./bar/../a/b/c/../../d').abspath())
'http://foo.com/foo/a/d'
```

### `escape`

Non-ASCII characters in the path are typically encoded as UTF-8 and then
escaped as `%HH` where `H` are hexidecimal values. It's important to note that
the `escape` function is idempotent, and can be called repeatedly:

```python
>>> str(urlpy.parse(u'http://foo.com/ümlaut').escape())
'http://foo.com/%C3%BCmlaut'
>>> str(urlpy.parse(u'http://foo.com/ümlaut').escape().escape())
'http://foo.com/%C3%BCmlaut'
```

### `unescape`

If you have a URL that might have been escaped before it was given to you, but
you'd like to display something a little more meaningful than `%C3%BCmlaut`, 
you can unescape the path:

```python
>>> print(urlpy.parse('http://foo.com/%C3%BCmlaut').unescape())
http://foo.com/ümlaut
```

## Properties

Many attributes are available on URL objects:

- `scheme` -- empty string if URL is relative
- `host` -- `None` if URL is relative
- `hostname` -- like `host`, but empty string if URL is relative
- `port` -- `None` if absent (or removed)
- `path` -- always with a leading `/`
- `params` -- string of params following the `;` (with extra `;`'s removed)
- `query` -- string of queries following the `?` (with extra `?`'s and `&`'s removed)
- `fragment` -- empty string if absent
- `absolute` -- a `bool` indicating whether the URL is absolute
- `unicode` -- a unicode version of the URL


## Running tests

```bash
./configure
pytest
```


## Credits and License

- urlpy2 is originally forked from [nexB/urlpy](https://github.com/nexB/urlpy) which is derived from Moz's [url.py v0.2.0](https://github.com/seomoz/url-py) and has been simplified to run on Python 2 and Python 3 using a pure Python library. (Newer version of Moz's url.py use a C++ extension).
- urlpy2 uses [CleanURLs rules data](https://gitlab.com/ClearURLs/rules) licensed under the GNU Lesser General Public License. Refer the the original author/license if you'd like to  update, distribute and copy their work.
            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/eon01/urlpy2",
    "name": "urlpy2",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Aymen El Amri based on code from nexB Inc",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/0f/13/4b87163e1372a12ce542ad73ffc517a3acd9572e987d7a117c29989e443f/urlpy2-1.0.10.tar.gz",
    "platform": null,
    "description": "# URLPY2\n\nurlpy2 is a small library for URL parsing, cleanup, canonicalization and equivalence.\n \nAt the heart of the `urlpy` package is the `URL` object. You can get one by\npassing in a unicode or string object into the top-level `parse` method. All\nstrings asre assumed to be Unicode:\n\n```python\nimport urlpy2 as urlpy\nmyurl = urlpy.parse('http://foo.com')\n```\n\nThe workflow is that you'll chain a number of permutations together to get the type\nof URL you're after:\n\n```python\n# Defrag, remove some parameters and give me a string\nstr(urlpy.parse(...).defrag().deparam(['utm_source']))\n\n# Escape the path, and punycode the host, and give me a string\nstr(urlpy.parse(...).escape().punycode())\n\n# Give me the absolute path url as some encoding\nstr(urlpy.parse(...).abspath()).encode('some encoding')\n```\n\n## Installation\n\n```\npip install urlpy2\n```\n\n## URL Equivalence\n\nURL objects compared with `==` are interpreted very strictly, but for a more\nlax interpretation, consider using `equiv` to test if two urls are functionally\nequivalent:\n\n```python\na = urlpy.parse(u'https://f\u00c3\u00b6o.com:443/a/../b/.?b=2&&&&&&a=1')\nb = urlpy.parse(u'https://xn--fo-fka.COM/b/?a=1&b=2')\n\n# These urls are not equal\nassert(a != b)\n\n# But they are equivalent\nassert(a.equiv(b))\nassert(b.equiv(a))\n```\n\nThis equivalence test takes default ports for common schemes into account (so\nif both urls are the same scheme, but one explicitly specifies the default\nport), punycoding, case of the host name, and parameter order.\n\n\n## Absolute URLs\n\nYou can perform many operations on relative urls (those without a hostname),\nbut punycoding and unpunycoding are not among them. You can also tell whether\nor not a url is absolute:\n\n```python\na = urlpy.parse('foo/bar.html')\nassert(not a.absolute())\n```\n\n## Chaining\n\nMany of the methods on the `URL` class can be chained to produce a number of\neffects in sequence:\n\n```python\nimport urlpy2 as urlpy\n\n# Create a url object\nmyurl = urlpy.URL.parse('http://www.FOO.com/bar?utm_source=foo#what')\n# Remove some parameters and the fragment\nprint(myurl.defrag().deparam(['utm_source']))\n```\n\nIn fact, unless the function explicitly returns a string, then the method may\nbe chained.\n\n\n### `canonical`\n\nAccording to the RFC, the order of parameters is not supposed to matter. In\npractice, it can (depending on how the server matches URL routes), but it's\nalso helpful to be able to put parameters in a canonical ordering. This\nordering happens to be alphabetical order:\n\n```python\n>>> str(urlpy.parse('http://foo.com/?b=2&a=1&d=3').canonical())\n'http://foo.com/?a=1&b=2&d=3'\n```\n\n\n### `defrag`\n\nRemove any fragment identifier from the url. This isn't part of the reuqest\nthat gets sent to an HTTP server, and so it's often useful to remove the \nfragment when doing url comparisons:\n\n```python\n>>> str(urlpy.parse('http://foo.com/#foo').defrag())\n'http://foo.com/'\n```\n\n### `deparam`\n\nSome parameters are commonly added to urls that we may not be interested in. Or\nthey may be misleading. Common examples include referrering pages, `utm_source`\nand session ids. To strip out all such parameters from your url:\n\n```python\n>>> str(urlpy.parse('http://foo.com/?do=1&not=2&want=3&this=4').deparam(['do', 'not', 'want']))\n'http://foo.com/?this=4'\n```\n\n### `r_deparam`\n\nSame as `deparam` but uses regex:\n\n\n```python\n>>> str(urlpy.parse('http://foo.com/?utm_a=1&utm_b=2&utm_c=3&utm_d=4').deparam(['utm_*',]))\n'http://foo.com/'\n```\n\n### `remove_tracking`\n\nRemoves all tracking and referall marketing parameters from the URL based on [CleanURLs list](https://gitlab.com/ClearURLs/rules/-/raw/master/data.min.json).\n\n```python\n>>> str(urlpy.parse('https://www.google.com/search?q=python&oq=python&aqs=chrome..69i57j0l5.8984j0j7&sourceid=chrome&ie=UTF-8').remove_tracking())\n'https://www.google.com/search?q=python'\n```\n\nTo keep the referall marketing parameters, use `remove_tracking(remove_referall_marketing=False)`.\n\n### `abspath`\n\nLike its `os.path` namesake, this makes sure that the path of the url is\nabsolute. This includes removing redundant forward slashes, `.` and `..`:\n\n```python\n>>> str(urlpy.parse('http://foo.com/foo/./bar/../a/b/c/../../d').abspath())\n'http://foo.com/foo/a/d'\n```\n\n### `escape`\n\nNon-ASCII characters in the path are typically encoded as UTF-8 and then\nescaped as `%HH` where `H` are hexidecimal values. It's important to note that\nthe `escape` function is idempotent, and can be called repeatedly:\n\n```python\n>>> str(urlpy.parse(u'http://foo.com/\u00c3\u00bcmlaut').escape())\n'http://foo.com/%C3%BCmlaut'\n>>> str(urlpy.parse(u'http://foo.com/\u00c3\u00bcmlaut').escape().escape())\n'http://foo.com/%C3%BCmlaut'\n```\n\n### `unescape`\n\nIf you have a URL that might have been escaped before it was given to you, but\nyou'd like to display something a little more meaningful than `%C3%BCmlaut`, \nyou can unescape the path:\n\n```python\n>>> print(urlpy.parse('http://foo.com/%C3%BCmlaut').unescape())\nhttp://foo.com/\u00c3\u00bcmlaut\n```\n\n## Properties\n\nMany attributes are available on URL objects:\n\n- `scheme` -- empty string if URL is relative\n- `host` -- `None` if URL is relative\n- `hostname` -- like `host`, but empty string if URL is relative\n- `port` -- `None` if absent (or removed)\n- `path` -- always with a leading `/`\n- `params` -- string of params following the `;` (with extra `;`'s removed)\n- `query` -- string of queries following the `?` (with extra `?`'s and `&`'s removed)\n- `fragment` -- empty string if absent\n- `absolute` -- a `bool` indicating whether the URL is absolute\n- `unicode` -- a unicode version of the URL\n\n\n## Running tests\n\n```bash\n./configure\npytest\n```\n\n\n## Credits and License\n\n- urlpy2 is originally forked from [nexB/urlpy](https://github.com/nexB/urlpy) which is derived from Moz's [url.py v0.2.0](https://github.com/seomoz/url-py) and has been simplified to run on Python 2 and Python 3 using a pure Python library. (Newer version of Moz's url.py use a C++ extension).\n- urlpy2 uses [CleanURLs rules data](https://gitlab.com/ClearURLs/rules) licensed under the GNU Lesser General Public License. Refer the the original author/license if you'd like to  update, distribute and copy their work.",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Simple URL parsing, canonicalization and equivalence.",
    "version": "1.0.10",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "0cea2a3ea2f067222bfc9a9ba684d9c7",
                "sha256": "649c7fca4d5c35f8249d6904b9a192f5832cf46c5c46a3bd9fe89ce15a2bf85a"
            },
            "downloads": -1,
            "filename": "urlpy2-1.0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "0cea2a3ea2f067222bfc9a9ba684d9c7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 23676,
            "upload_time": "2022-06-30T15:51:23",
            "upload_time_iso_8601": "2022-06-30T15:51:23.476354Z",
            "url": "https://files.pythonhosted.org/packages/0f/13/4b87163e1372a12ce542ad73ffc517a3acd9572e987d7a117c29989e443f/urlpy2-1.0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-06-30 15:51:23",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "eon01",
    "github_project": "urlpy2",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "urlpy2"
}
        
Elapsed time: 0.47691s