22from collections .abc import (
33 Generator ,
44 Iterator ,
5- Iterable ,
65 Sequence ,
76)
87import csv
8+ import dataclasses
99import functools
1010import itertools
11- import dataclasses
11+ import logging
1212from typing import TYPE_CHECKING , ClassVar
1313
1414from trove .trovesearch .search_params import (
1515 CardsearchParams ,
1616 ValuesearchParams ,
1717)
18+ from trove .util .iter import iter_unique
1819from trove .util .propertypath import Propertypath , GLOB_PATHSTEP
1920from trove .vocab import mediatypes
2021from trove .vocab import osfmap
2627 from trove .util .trove_params import BasicTroveParams
2728 from trove .util .json import JsonValue , JsonObject
2829
30+ _logger = logging .getLogger (__name__ )
2931
3032type Jsonpath = Sequence [str ] # path of json keys
3133type CsvValue = str | int | float | None
@@ -41,9 +43,10 @@ class TrovesearchSimpleCsvRenderer(SimpleTrovesearchRenderer):
4143 CSV_DIALECT : ClassVar [type [csv .Dialect ]] = csv .excel
4244
4345 def unicard_rendering (self , card_iri : str , osfmap_json : JsonObject ) -> ProtoRendering :
44- return self .multicard_rendering (card_pages = iter ([{card_iri : osfmap_json }]))
46+ _page = [(card_iri , osfmap_json )]
47+ return self .multicard_rendering (card_pages = iter ([_page ]))
4548
46- def multicard_rendering (self , card_pages : Iterator [dict [ str , JsonObject ]]) -> ProtoRendering :
49+ def multicard_rendering (self , card_pages : Iterator [Sequence [ tuple [ str , JsonObject ] ]]) -> ProtoRendering :
4750 _doc = TabularDoc (
4851 card_pages ,
4952 trove_params = getattr (self .response_focus , 'search_params' , None ),
@@ -67,7 +70,7 @@ def csv_stream(
6770
6871@dataclasses .dataclass
6972class TabularDoc :
70- card_pages : Iterator [dict [ str , JsonObject ]]
73+ card_pages : Iterator [Sequence [ tuple [ str , JsonObject ] ]]
7174 trove_params : BasicTroveParams | None = None
7275 _started : bool = False
7376
@@ -79,10 +82,6 @@ def column_jsonpaths(self) -> tuple[Jsonpath, ...]:
7982 )
8083 return (_ID_JSONPATH , * _column_jsonpaths )
8184
82- @functools .cached_property
83- def first_page (self ) -> dict [str , JsonObject ]:
84- return next (self .card_pages , {})
85-
8685 def _column_paths (self ) -> Iterator [Propertypath ]:
8786 _pathlists : list [Sequence [Propertypath ]] = []
8887 if self .trove_params is not None : # hacks
@@ -103,29 +102,16 @@ def _column_paths(self) -> Iterator[Propertypath]:
103102 _pathlists .append (_pathlist )
104103 if not _pathlists :
105104 _pathlists .append (osfmap .DEFAULT_TABULAR_SEARCH_COLUMN_PATHS )
106- return self .iter_unique (itertools .chain .from_iterable (_pathlists ))
107-
108- @staticmethod
109- def iter_unique [T ](iterable : Iterable [T ]) -> Generator [T ]:
110- _seen = set ()
111- for _item in iterable :
112- if _item not in _seen :
113- _seen .add (_item )
114- yield _item
115-
116- def _iter_card_pages (self ) -> Generator [dict [str , JsonObject ]]:
117- assert not self ._started
118- self ._started = True
119- if self .first_page :
120- yield self .first_page
121- yield from self .card_pages
105+ return iter_unique (itertools .chain .from_iterable (_pathlists ))
122106
123107 def header (self ) -> list [CsvValue ]:
124108 return ['.' .join (_path ) for _path in self .column_jsonpaths ]
125109
126110 def rows (self ) -> Generator [list [CsvValue ]]:
127- for _page in self ._iter_card_pages ():
128- for _card_iri , _osfmap_json in _page .items ():
111+ assert not self ._started
112+ self ._started = True
113+ for _page in self .card_pages :
114+ for _card_iri , _osfmap_json in _page :
129115 yield self ._row_values (_osfmap_json )
130116
131117 def _row_values (self , osfmap_json : JsonObject ) -> list [CsvValue ]:
0 commit comments