|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2025 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 | +# |
| 16 | +from google.ads.googleads.v23 import gapic_version as package_version |
| 17 | + |
| 18 | +import google.api_core as api_core |
| 19 | +import sys |
| 20 | + |
| 21 | +__version__ = package_version.__version__ |
| 22 | + |
| 23 | +if sys.version_info >= (3, 8): # pragma: NO COVER |
| 24 | + from importlib import metadata |
| 25 | +else: # pragma: NO COVER |
| 26 | + # TODO(https://github.com/googleapis/python-api-core/issues/835): Remove |
| 27 | + # this code path once we drop support for Python 3.7 |
| 28 | + import importlib_metadata as metadata |
| 29 | + |
| 30 | + |
| 31 | +from .types.book_campaigns import BookCampaignsOperation |
| 32 | +from .types.book_campaigns import BookCampaignsResult |
| 33 | +from .types.quote_campaigns import QuoteCampaignsOperation |
| 34 | +from .types.quote_campaigns import QuoteCampaignsResult |
| 35 | + |
| 36 | +if hasattr(api_core, "check_python_version") and hasattr( |
| 37 | + api_core, "check_dependency_versions" |
| 38 | +): # pragma: NO COVER |
| 39 | + api_core.check_python_version("google.ads.googleads.v23") # type: ignore |
| 40 | + api_core.check_dependency_versions("google.ads.googleads.v23") # type: ignore |
| 41 | +else: # pragma: NO COVER |
| 42 | + # An older version of api_core is installed which does not define the |
| 43 | + # functions above. We do equivalent checks manually. |
| 44 | + try: |
| 45 | + import warnings |
| 46 | + import sys |
| 47 | + |
| 48 | + _py_version_str = sys.version.split()[0] |
| 49 | + _package_label = "google.ads.googleads.v23" |
| 50 | + if sys.version_info < (3, 9): |
| 51 | + warnings.warn( |
| 52 | + "You are using a non-supported Python version " |
| 53 | + + f"({_py_version_str}). Google will not post any further " |
| 54 | + + f"updates to {_package_label} supporting this Python version. " |
| 55 | + + "Please upgrade to the latest Python version, or at " |
| 56 | + + f"least to Python 3.9, and then update {_package_label}.", |
| 57 | + FutureWarning, |
| 58 | + ) |
| 59 | + if sys.version_info[:2] == (3, 9): |
| 60 | + warnings.warn( |
| 61 | + f"You are using a Python version ({_py_version_str}) " |
| 62 | + + f"which Google will stop supporting in {_package_label} in " |
| 63 | + + "January 2026. Please " |
| 64 | + + "upgrade to the latest Python version, or at " |
| 65 | + + "least to Python 3.10, before then, and " |
| 66 | + + f"then update {_package_label}.", |
| 67 | + FutureWarning, |
| 68 | + ) |
| 69 | + |
| 70 | + def parse_version_to_tuple(version_string: str): |
| 71 | + """Safely converts a semantic version string to a comparable tuple of integers. |
| 72 | + Example: "4.25.8" -> (4, 25, 8) |
| 73 | + Ignores non-numeric parts and handles common version formats. |
| 74 | + Args: |
| 75 | + version_string: Version string in the format "x.y.z" or "x.y.z<suffix>" |
| 76 | + Returns: |
| 77 | + Tuple of integers for the parsed version string. |
| 78 | + """ |
| 79 | + parts = [] |
| 80 | + for part in version_string.split("."): |
| 81 | + try: |
| 82 | + parts.append(int(part)) |
| 83 | + except ValueError: |
| 84 | + # If it's a non-numeric part (e.g., '1.0.0b1' -> 'b1'), stop here. |
| 85 | + # This is a simplification compared to 'packaging.parse_version', but sufficient |
| 86 | + # for comparing strictly numeric semantic versions. |
| 87 | + break |
| 88 | + return tuple(parts) |
| 89 | + |
| 90 | + def _get_version(dependency_name): |
| 91 | + try: |
| 92 | + version_string: str = metadata.version(dependency_name) |
| 93 | + parsed_version = parse_version_to_tuple(version_string) |
| 94 | + return (parsed_version, version_string) |
| 95 | + except Exception: |
| 96 | + # Catch exceptions from metadata.version() (e.g., PackageNotFoundError) |
| 97 | + # or errors during parse_version_to_tuple |
| 98 | + return (None, "--") |
| 99 | + |
| 100 | + _dependency_package = "google.protobuf" |
| 101 | + _next_supported_version = "4.25.8" |
| 102 | + _next_supported_version_tuple = (4, 25, 8) |
| 103 | + _recommendation = " (we recommend 6.x)" |
| 104 | + (_version_used, _version_used_string) = _get_version( |
| 105 | + _dependency_package |
| 106 | + ) |
| 107 | + if _version_used and _version_used < _next_supported_version_tuple: |
| 108 | + warnings.warn( |
| 109 | + f"Package {_package_label} depends on " |
| 110 | + + f"{_dependency_package}, currently installed at version " |
| 111 | + + f"{_version_used_string}. Future updates to " |
| 112 | + + f"{_package_label} will require {_dependency_package} at " |
| 113 | + + f"version {_next_supported_version} or higher{_recommendation}." |
| 114 | + + " Please ensure " |
| 115 | + + "that either (a) your Python environment doesn't pin the " |
| 116 | + + f"version of {_dependency_package}, so that updates to " |
| 117 | + + f"{_package_label} can require the higher version, or " |
| 118 | + + "(b) you manually update your Python environment to use at " |
| 119 | + + f"least version {_next_supported_version} of " |
| 120 | + + f"{_dependency_package}.", |
| 121 | + FutureWarning, |
| 122 | + ) |
| 123 | + except Exception: |
| 124 | + warnings.warn( |
| 125 | + "Could not determine the version of Python " |
| 126 | + + "currently being used. To continue receiving " |
| 127 | + + "updates for {_package_label}, ensure you are " |
| 128 | + + "using a supported version of Python; see " |
| 129 | + + "https://devguide.python.org/versions/" |
| 130 | + ) |
| 131 | + |
| 132 | +__all__ = ( |
| 133 | + "BookCampaignsOperation", |
| 134 | + "BookCampaignsResult", |
| 135 | + "QuoteCampaignsOperation", |
| 136 | + "QuoteCampaignsResult", |
| 137 | +) |
0 commit comments