Coverage for python / lsst / daf / butler / remote_butler / _query_results.py: 0%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-18 08:42 +0000

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

28from __future__ import annotations 

29 

30from collections.abc import Iterator 

31 

32import httpx 

33from pydantic import TypeAdapter 

34 

35from .._dataset_ref import DatasetRef 

36from ..dimensions import DimensionUniverse 

37from ._errors import deserialize_butler_user_error 

38from .server_models import QueryExecuteResultData 

39 

40_QueryResultTypeAdapter = TypeAdapter[QueryExecuteResultData](QueryExecuteResultData) 

41 

42 

43def read_query_results(response: httpx.Response) -> Iterator[QueryExecuteResultData]: 

44 """Read streaming query results from the server. 

45 

46 Parameters 

47 ---------- 

48 response : `httpx.Response` 

49 HTTPX response object from a request where ``stream=True`` was set. 

50 

51 Yields 

52 ------ 

53 pages : `QueryExecuteResultData` 

54 Pages of result data from the server, excluding out-of-band messages 

55 like keep-alives and errors. 

56 

57 Raises 

58 ------ 

59 ButlerUserError 

60 Any subclass of ButlerUserError might be raised, to propagate 

61 server-side exceptions to the client. 

62 """ 

63 # There is one result page JSON object per line of the response. 

64 for line in response.iter_lines(): 

65 result: QueryExecuteResultData = _QueryResultTypeAdapter.validate_json(line) 

66 if result.type == "keep-alive": 

67 # Server is still in the process of generating the response. 

68 _received_keep_alive() 

69 elif result.type == "error": 

70 # A server-side exception occurred part-way through generating 

71 # results. 

72 raise deserialize_butler_user_error(result.error) 

73 else: 

74 yield result 

75 

76 

77def convert_dataset_ref_results( 

78 result: QueryExecuteResultData, universe: DimensionUniverse 

79) -> list[DatasetRef]: # numpydoc ignore=PR01 

80 """Convert a serialized page of dataset results to `DatasetRef` 

81 instances. 

82 """ 

83 assert result.type == "dataset_ref" 

84 return [DatasetRef.from_simple(r, universe) for r in result.rows] 

85 

86 

87def _received_keep_alive() -> None: 

88 """Do nothing. Gives a place for unit tests to hook in for testing 

89 keepalive behavior. 

90 """ 

91 pass