Adds all API functions that begin with a given prefix to a target class. Args: target: The class to add to. prefix: The prefix to search for in the signatures. type_name: The name of the object's type. Functions whose first argument matches this type are bound as insta
(
cls,
target: Any,
prefix: str,
type_name: str,
prepend: str | None = None,
)
| 179 | @classmethod |
| 180 | @_utils.accept_opt_prefix('opt_prepend') |
| 181 | def importApi( |
| 182 | cls, |
| 183 | target: Any, |
| 184 | prefix: str, |
| 185 | type_name: str, |
| 186 | prepend: str | None = None, |
| 187 | ) -> None: |
| 188 | """Adds all API functions that begin with a given prefix to a target class. |
| 189 | |
| 190 | Args: |
| 191 | target: The class to add to. |
| 192 | prefix: The prefix to search for in the signatures. |
| 193 | type_name: The name of the object's type. Functions whose first argument |
| 194 | matches this type are bound as instance methods, and those whose first |
| 195 | argument doesn't match are bound as static methods. |
| 196 | prepend: An optional string to prepend to the names of the added |
| 197 | functions. |
| 198 | """ |
| 199 | cls.initialize() |
| 200 | prepend = prepend or '' |
| 201 | for name, api_func in cls._api.items(): |
| 202 | parts = name.split('.') |
| 203 | if len(parts) == 2 and parts[0] == prefix: |
| 204 | fname = prepend + parts[1] |
| 205 | signature = api_func.getSignature() |
| 206 | |
| 207 | cls._bound_signatures.add(name) |
| 208 | |
| 209 | # Specifically handle the function names that are illegal in python. |
| 210 | if keyword.iskeyword(fname): |
| 211 | fname = fname.title() |
| 212 | |
| 213 | # Don't overwrite existing versions of this function. |
| 214 | if (hasattr(target, fname) and |
| 215 | not hasattr(getattr(target, fname), 'signature')): |
| 216 | continue |
| 217 | |
| 218 | # Create a new function so we can attach properties to it. |
| 219 | def MakeBoundFunction(func): |
| 220 | # We need the lambda to capture "func" from the enclosing scope. |
| 221 | # pylint: disable-next=unnecessary-lambda |
| 222 | return lambda *args, **kwargs: func.call(*args, **kwargs) |
| 223 | bound_function = MakeBoundFunction(api_func) |
| 224 | |
| 225 | # Add docs. |
| 226 | setattr(bound_function, '__name__', str(name)) |
| 227 | bound_function.__doc__ = str(api_func) |
| 228 | |
| 229 | # Attach the signature object for documentation generators. |
| 230 | bound_function.signature = signature |
| 231 | |
| 232 | # Mark as deprecated if needed. |
| 233 | if signature.get('deprecated'): |
| 234 | deprecated_decorator = deprecation.Deprecated(signature['deprecated']) |
| 235 | bound_function = deprecated_decorator(bound_function) |
| 236 | |
| 237 | # Mark as preview if needed. |
| 238 | if signature.get('preview'): |