# Copyright 2017 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Expand and validate URL path templates. This module provides the :func:`expand` and :func:`validate` functions for interacting with Google-style URL `path templates`_ which are commonly used in Google APIs for `resource names`_. .. _path templates: https://github.com/googleapis/googleapis/blob /57e2d376ac7ef48681554204a3ba78a414f2c533/google/api/http.proto#L212 .. _resource names: https://cloud.google.com/apis/design/resource_names """ from __future__ import unicode_literals from collections import deque import copy import functools import re # Regular expression for extracting variable parts from a path template. # The variables can be expressed as: # # - "*": a single-segment positional variable, for example: "books/*" # - "**": a multi-segment positional variable, for example: "shelf/**/book/*" # - "{name}": a single-segment wildcard named variable, for example # "books/{name}" # - "{name=*}: same as above. # - "{name=**}": a multi-segment wildcard named variable, for example # "shelf/{name=**}" # - "{name=/path/*/**}": a multi-segment named variable with a sub-template. _VARIABLE_RE = re.compile( r""" ( # Capture the entire variable expression (?P\*\*?) # Match & capture * and ** positional variables. | # Match & capture named variables {name} { (?P[^/]+?) # Optionally match and capture the named variable's template. (?:=(?P