khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2017 the original author or authors. |
| 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 | |
| 17 | """ |
| 18 | This helps loading http_pb2 and annotations_pb2. |
| 19 | Without this, the Python importer will not be able to process the lines: |
| 20 | from google.api import http_pb2 or |
| 21 | from google.api import annotations_pb2 |
| 22 | (Without importing these, the protobuf loader will not recognize http options |
| 23 | in the protobuf definitions.) |
| 24 | """ |
| 25 | |
| 26 | from importlib import import_module |
| 27 | import os |
| 28 | import sys |
| 29 | |
| 30 | |
| 31 | class GoogleApiImporter(object): |
| 32 | |
| 33 | def find_module(self, full_name, path=None): |
| 34 | if full_name == 'google.api': |
| 35 | self.path = [os.path.dirname(__file__)] |
| 36 | return self |
| 37 | |
| 38 | def load_module(self, name): |
| 39 | if name in sys.modules: |
| 40 | return sys.modules[name] |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 41 | full_name = 'python.protos.third_party.' + name |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 42 | import_module(full_name) |
| 43 | module = sys.modules[full_name] |
| 44 | sys.modules[name] = module |
| 45 | return module |
| 46 | |
| 47 | |
| 48 | sys.meta_path.append(GoogleApiImporter()) |
| 49 | try: |
| 50 | from google.api import http_pb2, annotations_pb2 |
| 51 | _ = http_pb2, annotations_pb2 |
| 52 | except AssertionError: |
| 53 | pass |