1# -*- coding: utf-8 -*-
2# Copyright 2026 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import sys
17
18import google.api_core as api_core
19
20from google.cloud.secretmanager_v1beta1 import gapic_version as package_version
21
22__version__ = package_version.__version__
23
24from importlib import metadata
25
26# PEP 0810: Explicit Lazy Imports
27# Python 3.15+ natively intercepts and defers these imports.
28# Developers can disable this behavior and force eager imports.
29# For more information, see:
30# https://docs.python.org/3.15/library/sys.html#sys.set_lazy_imports_filter
31# Older Python versions safely ignore this variable.
32__lazy_modules__ = {
33 "google.cloud.secretmanager_v1beta1.services.secret_manager_service",
34 "google.cloud.secretmanager_v1beta1.types.resources",
35 "google.cloud.secretmanager_v1beta1.types.service",
36}
37
38
39from .services.secret_manager_service import (
40 SecretManagerServiceAsyncClient,
41 SecretManagerServiceClient,
42)
43from .types.resources import Replication, Secret, SecretPayload, SecretVersion
44from .types.service import (
45 AccessSecretVersionRequest,
46 AccessSecretVersionResponse,
47 AddSecretVersionRequest,
48 CreateSecretRequest,
49 DeleteSecretRequest,
50 DestroySecretVersionRequest,
51 DisableSecretVersionRequest,
52 EnableSecretVersionRequest,
53 GetSecretRequest,
54 GetSecretVersionRequest,
55 ListSecretsRequest,
56 ListSecretsResponse,
57 ListSecretVersionsRequest,
58 ListSecretVersionsResponse,
59 UpdateSecretRequest,
60)
61
62if hasattr(api_core, "check_python_version") and hasattr(
63 api_core, "check_dependency_versions"
64): # pragma: NO COVER
65 api_core.check_python_version("google.cloud.secretmanager_v1beta1") # type: ignore
66 api_core.check_dependency_versions("google.cloud.secretmanager_v1beta1") # type: ignore
67else: # pragma: NO COVER
68 # An older version of api_core is installed which does not define the
69 # functions above. We do equivalent checks manually.
70 try:
71 import warnings
72
73 _py_version_str = sys.version.split()[0]
74 _package_label = "google.cloud.secretmanager_v1beta1"
75 if sys.version_info < (3, 10):
76 warnings.warn(
77 "You are using a non-supported Python version "
78 + f"({_py_version_str}). Google will not post any further "
79 + f"updates to {_package_label} supporting this Python version. "
80 + "Please upgrade to the latest Python version, or at "
81 + f"least to Python 3.10, and then update {_package_label}.",
82 FutureWarning,
83 )
84
85 def parse_version_to_tuple(version_string: str):
86 """Safely converts a semantic version string to a comparable tuple of integers.
87 Example: "6.33.5" -> (6, 33, 5)
88 Ignores non-numeric parts and handles common version formats.
89 Args:
90 version_string: Version string in the format "x.y.z" or "x.y.z<suffix>"
91 Returns:
92 Tuple of integers for the parsed version string.
93 """
94 parts = []
95 for part in version_string.split("."):
96 try:
97 parts.append(int(part))
98 except ValueError:
99 # If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here.
100 # This is a simplification compared to 'packaging.parse_version', but sufficient
101 # for comparing strictly numeric semantic versions.
102 break
103 return tuple(parts)
104
105 def _get_version(dependency_name):
106 try:
107 version_string: str = metadata.version(dependency_name)
108 parsed_version = parse_version_to_tuple(version_string)
109 return (parsed_version, version_string)
110 except Exception:
111 # Catch exceptions from metadata.version() (e.g., PackageNotFoundError)
112 # or errors during parse_version_to_tuple
113 return (None, "--")
114
115 _dependency_package = "google.protobuf"
116 _next_supported_version = "6.33.5"
117 _next_supported_version_tuple = (6, 33, 5)
118 _recommendation = " (we recommend 7.x)"
119 (_version_used, _version_used_string) = _get_version(_dependency_package)
120 if _version_used and _version_used < _next_supported_version_tuple:
121 warnings.warn(
122 f"Package {_package_label} depends on "
123 + f"{_dependency_package}, currently installed at version "
124 + f"{_version_used_string}. Future updates to "
125 + f"{_package_label} will require {_dependency_package} at "
126 + f"version {_next_supported_version} or higher{_recommendation}."
127 + " Please ensure "
128 + "that either (a) your Python environment doesn't pin the "
129 + f"version of {_dependency_package}, so that updates to "
130 + f"{_package_label} can require the higher version, or "
131 + "(b) you manually update your Python environment to use at "
132 + f"least version {_next_supported_version} of "
133 + f"{_dependency_package}.",
134 FutureWarning,
135 )
136 except Exception:
137 warnings.warn(
138 "Could not determine the version of Python "
139 + "currently being used. To continue receiving "
140 + "updates for {_package_label}, ensure you are "
141 + "using a supported version of Python; see "
142 + "https://devguide.python.org/versions/"
143 )
144
145__all__ = (
146 "SecretManagerServiceAsyncClient",
147 "AccessSecretVersionRequest",
148 "AccessSecretVersionResponse",
149 "AddSecretVersionRequest",
150 "CreateSecretRequest",
151 "DeleteSecretRequest",
152 "DestroySecretVersionRequest",
153 "DisableSecretVersionRequest",
154 "EnableSecretVersionRequest",
155 "GetSecretRequest",
156 "GetSecretVersionRequest",
157 "ListSecretVersionsRequest",
158 "ListSecretVersionsResponse",
159 "ListSecretsRequest",
160 "ListSecretsResponse",
161 "Replication",
162 "Secret",
163 "SecretManagerServiceClient",
164 "SecretPayload",
165 "SecretVersion",
166 "UpdateSecretRequest",
167)