Coverage for app/backend/src/couchers/middleware/descriptor_pool.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-01 13:53 +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() -> bytes: 

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

10 return descriptor_set_f.read() 

11 

12 

13def build_descriptor_pool() -> descriptor_pool.DescriptorPool: 

14 """ 

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

16 for each servicer, method, or message. 

17 

18 Not cached: ProtoAnnotations holds the process-wide pool, see couchers.middleware.proto_annotations. 

19 """ 

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

21 from couchers.proto import annotations_pb2 # noqa 

22 

23 pool = descriptor_pool.DescriptorPool() 

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

25 for file_descriptor in desc.file: 

26 pool.Add(file_descriptor) # type: ignore[no-untyped-call] 

27 return pool