uri-template


Nameuri-template JSON
Version 1.3.0 PyPI version JSON
download
home_page
SummaryRFC 6570 URI Template Processor
upload_time2023-06-21 01:49:05
maintainer
docs_urlNone
author
requires_python>=3.7
licenseMIT License
keywords config
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # uri-template

An implementation of RFC 6570 URI Templates.

This packages implements URI Template expansion in strict adherence to RFC 6570,
but adds a few extensions.

## RFC 6570 Extensions

### Non-string Values

RFC 6570 is silent regarding variable values that are not strings, lists, associative arrays, or null.

This package handles value types as follows:

  * Values that are instances of `str` are treated as strings.
  * Values implementing `collections.abc.Sequence` are treated as lists.
  * Values implementing `collections.abc.Mapping` are treated as associative arrays.
  * `None` values are treated as null.
  * Boolean values are converted to the lower case strings 'true' and 'false'.
  * All other values will be converted to strings using the Python `str()` function.

### Nested Structures

This package handles variable values with nested structure,
for example, lists containing other lists or associative arrays,
or associative arrays containing lists or other associative arrays.

Nested values for variables that do not use the array modifier ('[]') are treated as follows:

  * Lists containing lists are flattened into a single list.
  * Lists containing associative arrays are treated as a single combined associative array.
  * Associative arrays represent nested data using dot notation (".") for the variable names.

Nested values for variables that use the array modifier extend the variable name with 
the value's index or key written as an array subscript, e.g. "foo[0]" or "foo[bar]".

### Default Values

This package allows default string values for variables per early drafts of RFC 6570.
e.g. "{foo=bar}" will expand to "bar" if a value for `foo` is not given.

List and associtative array default values are not supported at this time.

### Specifying Value Keys

Sometimes a URI Template is used to provide glue between an API and a given set of data.
In this case, the names of values needed in the final URL may not match the data provided 
for the expansion.

This package allows specifying the key used to pass data into the template. 
e.g. "{?foo/bar}" will expand to "?foo=<the value provided as bar>"

### Partial expansion

This package allows partial expansion of URI Templates.

In a partial expansion, missing values preseve their expansion in the resultant output.
e.g. a partial expansion of "{one}/{two}" with a value for `one` of "foo" and `two` missing will result in:
"foo/{two}".

In order to allow partial expansions to preserve value joiners with expanded output,
expansions accept an optional "trailing joiner" of ",", ".", "/", ";", or "&",
if this joiner is present after all variables, 
it will be appended to the output of the expansion and will suppress the output prefix.
e.g.: "{#one,two}" with a missing value for `one` and a value of "bar" for `two`, 
will partially expand to: "#{#one,}bar", which when provided with a value of "foo" for `one` 
will expand to "#foo,bar"

Some partial expansions that have some output, but have missing values, 
will convert the remaining variables to a different type of expansion so that 
further expansions will produce the same output as if all values were originally present.

   * Partial Simple String Expansions will convert to Comma Expansions.
   * Partial Reserved Expansions Partial Fragment Expansions will convert to Reserved Comma Expansions.
   * Partial Form-Style Query Expansions will convert to Form-Style Query Continuations.

In order to preserve the resultant value of templates that are paritally expanded, 
the following additional Expression Expansions are supported:

#### Comma Expansion: {,var}

Similar to Label Expansion with Dot-Prefix, 
Comma Expansion prefixes the expansion output with a single comma ",".

#### Reserved Comma Expansion: {,+var}

Similar to Comma Expansion, 
Reserved Comma Expansion prefixes the expansion output with a single comma ",",
but otherwise performs a Reserved Expansion ({+var}).

## API 

The package provides three functions:

#### uri_template.expand(template: str, **kwargs) -> (str | None): ...

Expand the given template, skipping missing values per RFC 6570.

Returns `None` if the template is invalid or expansion fails.


#### uri_template.partial(template: str, **kwargs) -> (str | None): ...

Partially expand the given template, 
replacing missing variables with further expansions.

Returns `None` if the template is invalid or expansion fails.


#### uri_template.validate(template: str) -> bool: ...

Return `True` if the template is valid.

---

And the following classes:

### uri_template.URITemplate

#### URITemplate(template: str)

Construct a URITemplate for a given template string.

Raises `ExpansionInvalid`, `ExpansionReserved`, or `VariableInvalid` if the template is invalid or unsupported.

#### URITemplate.variables: Iterable[Variable]

All variables present in the template.
Duplicates are returned once, order is preserved.

#### URITemplate.variable_names: Iterable[str]

The names of all variables present in the template.
Duplicates are returned once, order is preserved.

#### URITemplate.expanded: bool

Determine if template is fully expanded.

#### URITemplate.expand(**kwargs) -> str

Returns the result of the expansion, skips missing variables.

Raises `ExpansionFailed` if the expansion fails due to a composite value being passed to a variable with a prefix modifier.

#### URITemplate.partial(**kwargs) -> URITemplate

Expand the template, replacing missing variables with further expansions.

Raises `ExpansionFailed` if the expansion fails due to a composite value being passed to a variable with a prefix modifier.

#### URITemplate.__str__() -> str

Convert the URITemplate object back into its original string form.

---

### uri_template.Variable

#### Variable(var_spec: str)

Construct a Variable.

#### Variable.name: str

The name of the variable

#### Variable.max_length: int

The speicified max length, or `0`.

#### Variable.explode: bool

Explode modifier is present.

#### Variable.array: bool

Array modifier is present.

#### Variable.default: (str | None)

Specified default value, or `None`.

#### Variable.__str__() -> str

Convert the variable back to its original string form.

---

And the following exceptions:

#### uri_template.ExpansionInvalid

Expansion specification is invalid. 

Raised by URITemplate constructor.

#### uri_template.ExpansionReserved

Expansion contains a reserved operator.

Raised by URITemplate constructor.

#### uri_template.VariableInvalid

Variable specification is invalid.

Raised by URITemplate constructor.

#### uri_template.ExpansionFailed

Expansion failed, currently only possible when a composite value is passed to a variable with a prefix modifier.

Raised by URITemplate.expand() or URITemplate.partial() methods.


## Installation

Install with pip:

    pip install uri-template

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "uri-template",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "config",
    "author": "",
    "author_email": "Peter Linss <pypi@linss.com>",
    "download_url": "https://files.pythonhosted.org/packages/31/c7/0336f2bd0bcbada6ccef7aaa25e443c118a704f828a0620c6fa0207c1b64/uri-template-1.3.0.tar.gz",
    "platform": null,
    "description": "# uri-template\n\nAn implementation of RFC 6570 URI Templates.\n\nThis packages implements URI Template expansion in strict adherence to RFC 6570,\nbut adds a few extensions.\n\n## RFC 6570 Extensions\n\n### Non-string Values\n\nRFC 6570 is silent regarding variable values that are not strings, lists, associative arrays, or null.\n\nThis package handles value types as follows:\n\n  * Values that are instances of `str` are treated as strings.\n  * Values implementing `collections.abc.Sequence` are treated as lists.\n  * Values implementing `collections.abc.Mapping` are treated as associative arrays.\n  * `None` values are treated as null.\n  * Boolean values are converted to the lower case strings 'true' and 'false'.\n  * All other values will be converted to strings using the Python `str()` function.\n\n### Nested Structures\n\nThis package handles variable values with nested structure,\nfor example, lists containing other lists or associative arrays,\nor associative arrays containing lists or other associative arrays.\n\nNested values for variables that do not use the array modifier ('[]') are treated as follows:\n\n  * Lists containing lists are flattened into a single list.\n  * Lists containing associative arrays are treated as a single combined associative array.\n  * Associative arrays represent nested data using dot notation (\".\") for the variable names.\n\nNested values for variables that use the array modifier extend the variable name with \nthe value's index or key written as an array subscript, e.g. \"foo[0]\" or \"foo[bar]\".\n\n### Default Values\n\nThis package allows default string values for variables per early drafts of RFC 6570.\ne.g. \"{foo=bar}\" will expand to \"bar\" if a value for `foo` is not given.\n\nList and associtative array default values are not supported at this time.\n\n### Specifying Value Keys\n\nSometimes a URI Template is used to provide glue between an API and a given set of data.\nIn this case, the names of values needed in the final URL may not match the data provided \nfor the expansion.\n\nThis package allows specifying the key used to pass data into the template. \ne.g. \"{?foo/bar}\" will expand to \"?foo=<the value provided as bar>\"\n\n### Partial expansion\n\nThis package allows partial expansion of URI Templates.\n\nIn a partial expansion, missing values preseve their expansion in the resultant output.\ne.g. a partial expansion of \"{one}/{two}\" with a value for `one` of \"foo\" and `two` missing will result in:\n\"foo/{two}\".\n\nIn order to allow partial expansions to preserve value joiners with expanded output,\nexpansions accept an optional \"trailing joiner\" of \",\", \".\", \"/\", \";\", or \"&\",\nif this joiner is present after all variables, \nit will be appended to the output of the expansion and will suppress the output prefix.\ne.g.: \"{#one,two}\" with a missing value for `one` and a value of \"bar\" for `two`, \nwill partially expand to: \"#{#one,}bar\", which when provided with a value of \"foo\" for `one` \nwill expand to \"#foo,bar\"\n\nSome partial expansions that have some output, but have missing values, \nwill convert the remaining variables to a different type of expansion so that \nfurther expansions will produce the same output as if all values were originally present.\n\n   * Partial Simple String Expansions will convert to Comma Expansions.\n   * Partial Reserved Expansions Partial Fragment Expansions will convert to Reserved Comma Expansions.\n   * Partial Form-Style Query Expansions will convert to Form-Style Query Continuations.\n\nIn order to preserve the resultant value of templates that are paritally expanded, \nthe following additional Expression Expansions are supported:\n\n#### Comma Expansion: {,var}\n\nSimilar to Label Expansion with Dot-Prefix, \nComma Expansion prefixes the expansion output with a single comma \",\".\n\n#### Reserved Comma Expansion: {,+var}\n\nSimilar to Comma Expansion, \nReserved Comma Expansion prefixes the expansion output with a single comma \",\",\nbut otherwise performs a Reserved Expansion ({+var}).\n\n## API \n\nThe package provides three functions:\n\n#### uri_template.expand(template: str, **kwargs) -> (str | None): ...\n\nExpand the given template, skipping missing values per RFC 6570.\n\nReturns `None` if the template is invalid or expansion fails.\n\n\n#### uri_template.partial(template: str, **kwargs) -> (str | None): ...\n\nPartially expand the given template, \nreplacing missing variables with further expansions.\n\nReturns `None` if the template is invalid or expansion fails.\n\n\n#### uri_template.validate(template: str) -> bool: ...\n\nReturn `True` if the template is valid.\n\n---\n\nAnd the following classes:\n\n### uri_template.URITemplate\n\n#### URITemplate(template: str)\n\nConstruct a URITemplate for a given template string.\n\nRaises `ExpansionInvalid`, `ExpansionReserved`, or `VariableInvalid` if the template is invalid or unsupported.\n\n#### URITemplate.variables: Iterable[Variable]\n\nAll variables present in the template.\nDuplicates are returned once, order is preserved.\n\n#### URITemplate.variable_names: Iterable[str]\n\nThe names of all variables present in the template.\nDuplicates are returned once, order is preserved.\n\n#### URITemplate.expanded: bool\n\nDetermine if template is fully expanded.\n\n#### URITemplate.expand(**kwargs) -> str\n\nReturns the result of the expansion, skips missing variables.\n\nRaises `ExpansionFailed` if the expansion fails due to a composite value being passed to a variable with a prefix modifier.\n\n#### URITemplate.partial(**kwargs) -> URITemplate\n\nExpand the template, replacing missing variables with further expansions.\n\nRaises `ExpansionFailed` if the expansion fails due to a composite value being passed to a variable with a prefix modifier.\n\n#### URITemplate.__str__() -> str\n\nConvert the URITemplate object back into its original string form.\n\n---\n\n### uri_template.Variable\n\n#### Variable(var_spec: str)\n\nConstruct a Variable.\n\n#### Variable.name: str\n\nThe name of the variable\n\n#### Variable.max_length: int\n\nThe speicified max length, or `0`.\n\n#### Variable.explode: bool\n\nExplode modifier is present.\n\n#### Variable.array: bool\n\nArray modifier is present.\n\n#### Variable.default: (str | None)\n\nSpecified default value, or `None`.\n\n#### Variable.__str__() -> str\n\nConvert the variable back to its original string form.\n\n---\n\nAnd the following exceptions:\n\n#### uri_template.ExpansionInvalid\n\nExpansion specification is invalid. \n\nRaised by URITemplate constructor.\n\n#### uri_template.ExpansionReserved\n\nExpansion contains a reserved operator.\n\nRaised by URITemplate constructor.\n\n#### uri_template.VariableInvalid\n\nVariable specification is invalid.\n\nRaised by URITemplate constructor.\n\n#### uri_template.ExpansionFailed\n\nExpansion failed, currently only possible when a composite value is passed to a variable with a prefix modifier.\n\nRaised by URITemplate.expand() or URITemplate.partial() methods.\n\n\n## Installation\n\nInstall with pip:\n\n    pip install uri-template\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "RFC 6570 URI Template Processor",
    "version": "1.3.0",
    "project_urls": {
        "homepage": "https://gitlab.linss.com/open-source/python/uri-template"
    },
    "split_keywords": [
        "config"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e7003fca040d7cf8a32776d3d81a00c8ee7457e00f80c649f1e4a863c8321ae9",
                "md5": "7d7f28c2ffd7d4746174ab761f6025e5",
                "sha256": "a44a133ea12d44a0c0f06d7d42a52d71282e77e2f937d8abd5655b8d56fc1363"
            },
            "downloads": -1,
            "filename": "uri_template-1.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7d7f28c2ffd7d4746174ab761f6025e5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 11140,
            "upload_time": "2023-06-21T01:49:03",
            "upload_time_iso_8601": "2023-06-21T01:49:03.467606Z",
            "url": "https://files.pythonhosted.org/packages/e7/00/3fca040d7cf8a32776d3d81a00c8ee7457e00f80c649f1e4a863c8321ae9/uri_template-1.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "31c70336f2bd0bcbada6ccef7aaa25e443c118a704f828a0620c6fa0207c1b64",
                "md5": "d8798bfcb3307481edd207fdc6743ff0",
                "sha256": "0e00f8eb65e18c7de20d595a14336e9f337ead580c70934141624b6d1ffdacc7"
            },
            "downloads": -1,
            "filename": "uri-template-1.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d8798bfcb3307481edd207fdc6743ff0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 21678,
            "upload_time": "2023-06-21T01:49:05",
            "upload_time_iso_8601": "2023-06-21T01:49:05.374755Z",
            "url": "https://files.pythonhosted.org/packages/31/c7/0336f2bd0bcbada6ccef7aaa25e443c118a704f828a0620c6fa0207c1b64/uri-template-1.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-21 01:49:05",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "uri-template"
}
        
Elapsed time: 0.08803s