METADATA 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. Metadata-Version: 2.1
  2. Name: uri-template
  3. Version: 1.3.0
  4. Summary: RFC 6570 URI Template Processor
  5. Author-email: Peter Linss <pypi@linss.com>
  6. License: MIT License
  7. Project-URL: homepage, https://gitlab.linss.com/open-source/python/uri-template
  8. Keywords: config
  9. Classifier: Intended Audience :: Developers
  10. Classifier: License :: OSI Approved :: MIT License
  11. Classifier: Operating System :: OS Independent
  12. Classifier: Programming Language :: Python
  13. Classifier: Programming Language :: Python :: 3
  14. Classifier: Programming Language :: Python :: 3.7
  15. Classifier: Programming Language :: Python :: 3.8
  16. Classifier: Programming Language :: Python :: 3.9
  17. Classifier: Programming Language :: Python :: 3.10
  18. Classifier: Programming Language :: Python :: 3.11
  19. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  20. Requires-Python: >=3.7
  21. Description-Content-Type: text/markdown
  22. License-File: LICENSE
  23. Provides-Extra: dev
  24. Requires-Dist: types-PyYAML ; extra == 'dev'
  25. Requires-Dist: mypy ; extra == 'dev'
  26. Requires-Dist: flake8 ; extra == 'dev'
  27. Requires-Dist: flake8-annotations ; extra == 'dev'
  28. Requires-Dist: flake8-bandit ; extra == 'dev'
  29. Requires-Dist: flake8-bugbear ; extra == 'dev'
  30. Requires-Dist: flake8-commas ; extra == 'dev'
  31. Requires-Dist: flake8-comprehensions ; extra == 'dev'
  32. Requires-Dist: flake8-continuation ; extra == 'dev'
  33. Requires-Dist: flake8-datetimez ; extra == 'dev'
  34. Requires-Dist: flake8-docstrings ; extra == 'dev'
  35. Requires-Dist: flake8-import-order ; extra == 'dev'
  36. Requires-Dist: flake8-literal ; extra == 'dev'
  37. Requires-Dist: flake8-modern-annotations ; extra == 'dev'
  38. Requires-Dist: flake8-noqa ; extra == 'dev'
  39. Requires-Dist: flake8-pyproject ; extra == 'dev'
  40. Requires-Dist: flake8-requirements ; extra == 'dev'
  41. Requires-Dist: flake8-typechecking-import ; extra == 'dev'
  42. Requires-Dist: flake8-use-fstring ; extra == 'dev'
  43. Requires-Dist: pep8-naming ; extra == 'dev'
  44. # uri-template
  45. An implementation of RFC 6570 URI Templates.
  46. This packages implements URI Template expansion in strict adherence to RFC 6570,
  47. but adds a few extensions.
  48. ## RFC 6570 Extensions
  49. ### Non-string Values
  50. RFC 6570 is silent regarding variable values that are not strings, lists, associative arrays, or null.
  51. This package handles value types as follows:
  52. * Values that are instances of `str` are treated as strings.
  53. * Values implementing `collections.abc.Sequence` are treated as lists.
  54. * Values implementing `collections.abc.Mapping` are treated as associative arrays.
  55. * `None` values are treated as null.
  56. * Boolean values are converted to the lower case strings 'true' and 'false'.
  57. * All other values will be converted to strings using the Python `str()` function.
  58. ### Nested Structures
  59. This package handles variable values with nested structure,
  60. for example, lists containing other lists or associative arrays,
  61. or associative arrays containing lists or other associative arrays.
  62. Nested values for variables that do not use the array modifier ('[]') are treated as follows:
  63. * Lists containing lists are flattened into a single list.
  64. * Lists containing associative arrays are treated as a single combined associative array.
  65. * Associative arrays represent nested data using dot notation (".") for the variable names.
  66. Nested values for variables that use the array modifier extend the variable name with
  67. the value's index or key written as an array subscript, e.g. "foo[0]" or "foo[bar]".
  68. ### Default Values
  69. This package allows default string values for variables per early drafts of RFC 6570.
  70. e.g. "{foo=bar}" will expand to "bar" if a value for `foo` is not given.
  71. List and associtative array default values are not supported at this time.
  72. ### Specifying Value Keys
  73. Sometimes a URI Template is used to provide glue between an API and a given set of data.
  74. In this case, the names of values needed in the final URL may not match the data provided
  75. for the expansion.
  76. This package allows specifying the key used to pass data into the template.
  77. e.g. "{?foo/bar}" will expand to "?foo=<the value provided as bar>"
  78. ### Partial expansion
  79. This package allows partial expansion of URI Templates.
  80. In a partial expansion, missing values preseve their expansion in the resultant output.
  81. e.g. a partial expansion of "{one}/{two}" with a value for `one` of "foo" and `two` missing will result in:
  82. "foo/{two}".
  83. In order to allow partial expansions to preserve value joiners with expanded output,
  84. expansions accept an optional "trailing joiner" of ",", ".", "/", ";", or "&",
  85. if this joiner is present after all variables,
  86. it will be appended to the output of the expansion and will suppress the output prefix.
  87. e.g.: "{#one,two}" with a missing value for `one` and a value of "bar" for `two`,
  88. will partially expand to: "#{#one,}bar", which when provided with a value of "foo" for `one`
  89. will expand to "#foo,bar"
  90. Some partial expansions that have some output, but have missing values,
  91. will convert the remaining variables to a different type of expansion so that
  92. further expansions will produce the same output as if all values were originally present.
  93. * Partial Simple String Expansions will convert to Comma Expansions.
  94. * Partial Reserved Expansions Partial Fragment Expansions will convert to Reserved Comma Expansions.
  95. * Partial Form-Style Query Expansions will convert to Form-Style Query Continuations.
  96. In order to preserve the resultant value of templates that are paritally expanded,
  97. the following additional Expression Expansions are supported:
  98. #### Comma Expansion: {,var}
  99. Similar to Label Expansion with Dot-Prefix,
  100. Comma Expansion prefixes the expansion output with a single comma ",".
  101. #### Reserved Comma Expansion: {,+var}
  102. Similar to Comma Expansion,
  103. Reserved Comma Expansion prefixes the expansion output with a single comma ",",
  104. but otherwise performs a Reserved Expansion ({+var}).
  105. ## API
  106. The package provides three functions:
  107. #### uri_template.expand(template: str, **kwargs) -> (str | None): ...
  108. Expand the given template, skipping missing values per RFC 6570.
  109. Returns `None` if the template is invalid or expansion fails.
  110. #### uri_template.partial(template: str, **kwargs) -> (str | None): ...
  111. Partially expand the given template,
  112. replacing missing variables with further expansions.
  113. Returns `None` if the template is invalid or expansion fails.
  114. #### uri_template.validate(template: str) -> bool: ...
  115. Return `True` if the template is valid.
  116. ---
  117. And the following classes:
  118. ### uri_template.URITemplate
  119. #### URITemplate(template: str)
  120. Construct a URITemplate for a given template string.
  121. Raises `ExpansionInvalid`, `ExpansionReserved`, or `VariableInvalid` if the template is invalid or unsupported.
  122. #### URITemplate.variables: Iterable[Variable]
  123. All variables present in the template.
  124. Duplicates are returned once, order is preserved.
  125. #### URITemplate.variable_names: Iterable[str]
  126. The names of all variables present in the template.
  127. Duplicates are returned once, order is preserved.
  128. #### URITemplate.expanded: bool
  129. Determine if template is fully expanded.
  130. #### URITemplate.expand(**kwargs) -> str
  131. Returns the result of the expansion, skips missing variables.
  132. Raises `ExpansionFailed` if the expansion fails due to a composite value being passed to a variable with a prefix modifier.
  133. #### URITemplate.partial(**kwargs) -> URITemplate
  134. Expand the template, replacing missing variables with further expansions.
  135. Raises `ExpansionFailed` if the expansion fails due to a composite value being passed to a variable with a prefix modifier.
  136. #### URITemplate.__str__() -> str
  137. Convert the URITemplate object back into its original string form.
  138. ---
  139. ### uri_template.Variable
  140. #### Variable(var_spec: str)
  141. Construct a Variable.
  142. #### Variable.name: str
  143. The name of the variable
  144. #### Variable.max_length: int
  145. The speicified max length, or `0`.
  146. #### Variable.explode: bool
  147. Explode modifier is present.
  148. #### Variable.array: bool
  149. Array modifier is present.
  150. #### Variable.default: (str | None)
  151. Specified default value, or `None`.
  152. #### Variable.__str__() -> str
  153. Convert the variable back to its original string form.
  154. ---
  155. And the following exceptions:
  156. #### uri_template.ExpansionInvalid
  157. Expansion specification is invalid.
  158. Raised by URITemplate constructor.
  159. #### uri_template.ExpansionReserved
  160. Expansion contains a reserved operator.
  161. Raised by URITemplate constructor.
  162. #### uri_template.VariableInvalid
  163. Variable specification is invalid.
  164. Raised by URITemplate constructor.
  165. #### uri_template.ExpansionFailed
  166. Expansion failed, currently only possible when a composite value is passed to a variable with a prefix modifier.
  167. Raised by URITemplate.expand() or URITemplate.partial() methods.
  168. ## Installation
  169. Install with pip:
  170. pip install uri-template