Coverage for src/couchers/descriptor_pool.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-12-02 20:27 +0000

1import functools 

2from pathlib import Path 

3 

4from google.protobuf import descriptor_pb2, descriptor_pool 

5 

6 

7@functools.cache 

8def get_descriptors_pb(): 

9 with open(Path(__file__).parent / ".." / "proto" / "descriptors.pb", "rb") as descriptor_set_f: 

10 return descriptor_set_f.read() 

11 

12 

13@functools.cache 

14def get_descriptor_pool(): 

15 """ 

16 Generates a protocol buffer object descriptor pool which allows looking up info about our proto API, such as options 

17 for each servicer, method, or message. 

18 """ 

19 # this needs to be imported so the annotations are available in the generated pool... 

20 from proto import annotations_pb2 # noqa 

21 

22 pool = descriptor_pool.DescriptorPool() 

23 desc = descriptor_pb2.FileDescriptorSet.FromString(get_descriptors_pb()) 

24 for file_descriptor in desc.file: 

25 pool.Add(file_descriptor) 

26 return pool