{"id":26568280,"url":"https://github.com/technige/xri","last_synced_at":"2025-03-22T19:36:00.635Z","repository":{"id":204680573,"uuid":"677563453","full_name":"technige/xri","owner":"technige","description":"Simple and efficient Python data types for URIs and IRIs","archived":false,"fork":false,"pushed_at":"2024-12-20T17:08:55.000Z","size":84,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"trunk","last_synced_at":"2024-12-20T18:22:46.002Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/technige.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-11T22:58:43.000Z","updated_at":"2024-12-20T17:08:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"f18340cd-c9a6-44ad-8171-8e7eaf86079e","html_url":"https://github.com/technige/xri","commit_stats":null,"previous_names":["nascanti/xri","technige/xri"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/technige%2Fxri","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/technige%2Fxri/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/technige%2Fxri/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/technige%2Fxri/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/technige","download_url":"https://codeload.github.com/technige/xri/tar.gz/refs/heads/trunk","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245013769,"owners_count":20547175,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-22T19:35:59.991Z","updated_at":"2025-03-22T19:36:00.553Z","avatar_url":"https://github.com/technige.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# XRI\n\nXRI is a small Python library for efficient and RFC-correct representation of URIs and IRIs.\nIt is currently work-in-progress and, as such, is not recommended for production environments.\n\nThe generic syntax for URIs is defined in [RFC 3986](https://datatracker.ietf.org/doc/html/rfc3986/).\nThis is extended in the IRI specification, [RFC 3987](https://datatracker.ietf.org/doc/html/rfc3987/), to support extended characters outside of the ASCII range. \n\nThe library defines `URI` and `IRI` classes which provide access to parsing, composition and resolution methods.\n\n\n## Parsing a URI or IRI\n\nTo parse, simply pass a string value into the `URI.parse` or `IRI.parse` method.\nThese can both accept either `bytes` or `str` values, and will encode or decode UTF-8 values as required.\nA dictionary is returned containing a breakdown of the component parts.\n\n```python\n\u003e\u003e\u003e from xri import URI\n\u003e\u003e\u003e URI.parse(\"http://alice:p4ssw0rd!@example.com:8080/ä/b/c?q=x#z\")\n{'scheme': 'http',\n 'authority': 'alice@example.com',\n 'path': '/%C3%A4/b/c',\n 'query': 'q=x',\n 'fragment': 'z',\n 'userinfo': 'alice:p4ssw0rd!',\n 'host': 'example.com',\n 'port': '8080',\n 'port_number': 8080,\n 'path_segments': ['', 'ä', 'b', 'c'],\n 'query_parameters': [('q', 'x')],\n 'origin': 'http://example.com'}\n```\n\n\n## Percent encoding and decoding\n\nEach of the `URI` and `IRI` classes has class methods called `pct_encode` and `pct_decode`.\nThese operate slightly differently, depending on the base class, as a slightly different set of characters are kept \"safe\" during encoding.\n\n```python\n\u003e\u003e\u003e URI.pct_encode(\"abc/def\")\n'abc%2Fdef'\n\u003e\u003e\u003e URI.pct_encode(\"abc/def\", safe=\"/\")\n'abc/def'\n\u003e\u003e\u003e URI.pct_encode(\"20% of $125 is $25\")\n'20%25%20of%20%24125%20is%20%2425'\n\u003e\u003e\u003e URI.pct_encode(\"20% of £125 is £25\")                        # '£' is encoded with UTF-8\n'20%25%20of%20%C2%A3125%20is%20%C2%A325'\n\u003e\u003e\u003e from xri import IRI\n\u003e\u003e\u003e IRI.pct_encode(\"20% of £125 is £25\")                        # '£' is safe within an IRI\n'20%25%20of%20£125%20is%20£25'\n\u003e\u003e\u003e URI.pct_decode('20%25%20of%20%C2%A3125%20is%20%C2%A325')    # str in, str out (using UTF-8)\n'20% of £125 is £25'\n\u003e\u003e\u003e URI.pct_decode(b'20%25%20of%20%C2%A3125%20is%20%C2%A325')   # bytes in, bytes out (no UTF-8)\nb'20% of \\xc2\\xa3125 is \\xc2\\xa325'\n```\n\nSafe characters (passed in via the `safe` argument) can only be drawn from the set below.\nOther characters passed to this argument will give a `ValueError`.\n```\n! # $ \u0026 ' ( ) * + , / : ; = ? @ [ ]\n```\n\n\n## Advantages over built-in `urllib.parse` module\n\n### Correct handling of character encodings\n\nRFC 3986 specifies that extended characters (beyond the ASCII range) are not supported directly within URIs.\nWhen used, these should always be encoded with UTF-8 before percent encoding.\nIRIs (defined in RFC 3987) do however allow such characters. \n\n`urllib.parse` does not enforce this behaviour according to the RFCs, and does not support UTF-8 encoded bytes as input values.\n```python\n\u003e\u003e\u003e from urllib.parse import urlparse\n\u003e\u003e\u003e urlparse(\"https://example.com/ä\").path\n'/ä'\n\u003e\u003e\u003e urlparse(\"https://example.com/ä\".encode(\"utf-8\")).path\nUnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 20: ordinal not in range(128)\n```\n\nConversely, `xri` handles these scenarios correctly according to the RFCs.\n```python\n\u003e\u003e\u003e URI.parse(\"https://example.com/ä b\")[\"path\"]\n'/%C3%A4%20b'\n\u003e\u003e\u003e IRI.parse(\"https://example.com/ä b\")[\"path\"]\n'/ä%20b'\n```\n\n### Optional components may be empty\nOptional URI components, such as _query_ and _fragment_, are allowed to be present but empty, [according to RFC 3986](https://datatracker.ietf.org/doc/html/rfc3986/#section-3.4).\nAs such, there is a semantic difference between an empty component and a missing component.\nWhen composed, this will be denoted by the absence or presence of a marker character (`'?'` in the case of the query component).\n\nThe `urlparse` function does not distinguish between empty and missing components;\nboth are treated as \"missing\".\n```python\n\u003e\u003e\u003e urlparse(\"https://example.com/a\").geturl()\n'https://example.com/a'\n\u003e\u003e\u003e urlparse(\"https://example.com/a?\").geturl()\n'https://example.com/a'\n```\n\n`xri`, on the other hand, correctly distinguishes between these cases:\n```python\n\u003e\u003e\u003e uri = URI.parse(\"https://example.com/a\")\n\u003e\u003e\u003e URI.compose(uri[\"scheme\"], uri[\"authority\"], uri[\"path\"], uri[\"query\"], uri[\"fragment\"])\n'https://example.com/a'\n\u003e\u003e\u003e uri = URI.parse(\"https://example.com/a?\")\n\u003e\u003e\u003e URI.compose(uri[\"scheme\"], uri[\"authority\"], uri[\"path\"], uri[\"query\"], uri[\"fragment\"])\n'https://example.com/a?'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftechnige%2Fxri","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftechnige%2Fxri","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftechnige%2Fxri/lists"}