1818import importlib
1919import re
2020import sys
21+ from inspect import getmembers , isclass
2122from itertools import chain
2223
2324from google .api_core .protobuf_helpers import get_messages
16261627 YoutubeVideoRegistrationErrorEnum = 'google.ads.google_ads.v2.proto.errors.youtube_video_registration_error_pb2'
16271628)
16281629
1630+ DEPENDENT_MODULE_LIST = [
1631+ 'google.longrunning.operations_pb2' ,
1632+ 'google.protobuf.any_pb2' ,
1633+ 'google.protobuf.empty_pb2' ,
1634+ 'google.protobuf.field_mask_pb2' ,
1635+ 'google.protobuf.wrappers_pb2' ,
1636+ 'google.rpc.status_pb2' ]
1637+
1638+ def _get_class_from_module (module_name ):
1639+ module = importlib .import_module (module_name )
1640+ for class_name , _ in getmembers (module , isclass ): # from inspect module
1641+ yield class_name
1642+
1643+ def _populate_dependent_classes (module_list = DEPENDENT_MODULE_LIST ):
1644+ class_list = {}
1645+ for module_name in module_list :
1646+ for cls in _get_class_from_module (module_name ):
1647+ class_list [cls ] = module_name
1648+ return class_list
1649+
1650+ _lazy_dependent_class_to_package_map = _populate_dependent_classes ()
16291651
16301652def _load_module (module_name ):
16311653 """Load a module by it's name.
@@ -1683,7 +1705,9 @@ def _get_message_class_by_name(class_name):
16831705 a protobuf message class definition that inherits from
16841706 google.protobuf.pyext.cpp_message.GeneratedProtocolMessageType.
16851707 """
1686- if class_name in _lazy_class_to_package_map :
1708+ if class_name in _lazy_dependent_class_to_package_map :
1709+ module_path = _lazy_dependent_class_to_package_map [class_name ]
1710+ elif class_name in _lazy_class_to_package_map :
16871711 module_path = _lazy_class_to_package_map [class_name ]
16881712 else :
16891713 raise AttributeError (f'unknown sub-module { class_name !r} .' )
@@ -1707,7 +1731,8 @@ def __getattr__(name): # Requires Python >= 3.7
17071731 if name == '__all__' :
17081732 converted = (util .convert_snake_case_to_upper_case (key ) for
17091733 key in chain (_lazy_name_to_package_map ,
1710- _lazy_class_to_package_map ))
1734+ _lazy_class_to_package_map ,
1735+ _lazy_dependent_class_to_package_map ))
17111736 all_names = sorted (converted )
17121737 globals ()['__all__' ] = all_names
17131738 return all_names
0 commit comments