7878# % guisection: Formatting
7979# %end
8080
81+ # %option G_OPT_M_MAPSET
82+ # % multiple: yes
83+ # % description: Name of mapsets to list ('.' for current, '*' for all mapsets)
84+ # % guisection: Selection
85+ # %end
86+
8187# %option G_OPT_F_SEP
8288# % answer:
8389# % label: Field separator character between the output columns
99105from contextlib import nullcontext
100106
101107import grass .script as gs
102- from grass .tools import Tools
103108
104109############################################################################
105110
106111
107112def main ():
108113
109114 # Get the options
110- type = options ["type" ]
115+ stds_type = options ["type" ]
111116 temporal_type = options ["temporaltype" ]
112117 columns = options ["columns" ]
113118 order = options ["order" ]
@@ -116,6 +121,7 @@ def main():
116121 outpath = options ["output" ]
117122 colhead = flags ["c" ]
118123 output_format = options .get ("format" , "plain" )
124+ mapsets = options ["mapset" ]
119125
120126 if not columns :
121127 columns = "all" if output_format == "json" else "id"
@@ -150,17 +156,20 @@ def main():
150156 elif not separator : # output_format == "plain"
151157 separator = "|"
152158
153- tools = Tools ()
159+ # Lazy import and initialize TGIS
160+ import grass .temporal as tgis
154161
155- conn = tools . t_connect ( flags = "p" , format = "json" , mapset = "." )
162+ tgis . init ( skip_db_init = True )
156163
157- db_exists = conn [ 0 ][ "driver" ] and conn [ 0 ][ "database" ]
164+ dbif = tgis . SQLDatabaseInterfaceConnection ( mapsets = mapsets )
158165
159166 # if no connection is found
160- if not db_exists :
167+ if not dbif . tgis_mapsets :
161168 if output_format == "plain" :
162169 gs .message (
163- _ ("No temporal database exists in this mapset. No datasets to list." )
170+ _ (
171+ "No temporal database found in the requested mapset(s). No datasets to list."
172+ )
164173 )
165174 with (
166175 open (outpath , "w" )
@@ -171,19 +180,11 @@ def main():
171180 out_file .write (json .dumps ([], indent = 4 ) + "\n " )
172181 return
173182
174- # Lazy import
175- import grass .temporal as tgis
176-
177- # Make sure the temporal database exists
178- tgis .init ()
179-
180- sp = tgis .dataset_factory (type , None )
181- dbif = tgis .SQLDatabaseInterfaceConnection ()
182183 dbif .connect ()
183- first = True
184184
185185 json_output = []
186186 line_output = []
187+ first = True
187188
188189 if gs .verbosity () > 0 and not outpath and output_format == "plain" :
189190 sys .stderr .write ("----------------------------------------------\n " )
@@ -196,80 +197,73 @@ def main():
196197 ):
197198 for ttype in temporal_type .split ("," ):
198199 time = "absolute time" if ttype == "absolute" else "relative time"
199-
200200 stds_list = tgis .get_dataset_list (
201- type , ttype , columns , where , order , dbif = dbif
201+ stds_type , ttype , columns , where , order , dbif = dbif
202202 )
203203
204- mapsets = tgis .get_tgis_c_library_interface ().available_mapsets ()
205-
206- for key in mapsets :
207- if key in stds_list .keys ():
208- rows = stds_list [key ]
209-
210- if rows :
211- if (
212- gs .verbosity () > 0
213- and (not outpath or outpath == "-" )
214- and output_format == "plain"
215- ):
216- if issubclass (sp .__class__ , tgis .AbstractMapDataset ):
217- sys .stderr .write (
218- _ (
219- "Time stamped %s maps with %s available in mapset "
220- "<%s>:\n "
221- )
222- % (sp .get_type (), time , key )
223- )
224- else :
225- sys .stderr .write (
226- _ (
227- "Space time %s datasets with %s available in "
228- "mapset <%s>:\n "
229- )
230- % (
231- sp .get_new_map_instance (None ).get_type (),
232- time ,
233- key ,
234- )
204+ for mapset in dbif .tgis_mapsets :
205+ rows = stds_list .get (mapset )
206+ if rows :
207+ if (
208+ gs .verbosity () > 0
209+ and (not outpath or outpath == "-" )
210+ and output_format == "plain"
211+ ):
212+ if stds_type in {"raster" , "raster_3d" , "vector" }:
213+ sys .stderr .write (
214+ _ (
215+ "Time stamped %s maps with %s available in mapset "
216+ "<%s>:\n "
235217 )
236-
237- if output_format == "json" :
238- for row in rows :
239- json_output .append (dict (row ))
240-
241- elif output_format == "line" :
242- line_output .extend ([str (row [0 ]) for row in rows ])
243-
218+ % (stds_type , time , mapset )
219+ )
244220 else :
245- if (colhead or output_format == "csv" ) and first :
246- output = ""
247- count = 0
248- for col_key in rows [0 ].keys ():
249- output += (separator if count > 0 else "" ) + str (
250- col_key
221+ sys .stderr .write (
222+ _ (
223+ "Space time %s datasets with %s available in "
224+ "mapset <%s>:\n "
225+ )
226+ % (
227+ stds_type ,
228+ time ,
229+ mapset ,
230+ )
231+ )
232+
233+ if output_format == "json" :
234+ for row in rows :
235+ json_output .append (dict (row ))
236+
237+ elif output_format == "line" :
238+ line_output .extend ([str (row [0 ]) for row in rows ])
239+
240+ else :
241+ if (colhead or output_format == "csv" ) and first :
242+ output = ""
243+ count = 0
244+ for col_key in rows [0 ].keys ():
245+ output += (separator if count > 0 else "" ) + str (
246+ col_key
247+ )
248+ count += 1
249+ out_file .write (f"{ output } \n " )
250+ first = False
251+
252+ for row in rows :
253+ output = ""
254+ count = 0
255+ for col in row :
256+ # If the database value is None, make it an empty string for csv
257+ if col is None :
258+ cell_value = (
259+ "" if output_format == "csv" else "None"
251260 )
252- count += 1
253- out_file .write ("{st}\n " .format (st = output ))
254- first = False
255-
256- for row in rows :
257- output = ""
258- count = 0
259- for col in row :
260- # If the database value is None, make it an empty string for csv
261- if col is None :
262- cell_value = (
263- "" if output_format == "csv" else "None"
264- )
265- else :
266- cell_value = str (col )
267-
268- output += (
269- separator if count > 0 else ""
270- ) + cell_value
271- count += 1
272- out_file .write ("{st}\n " .format (st = output ))
261+ else :
262+ cell_value = str (col )
263+
264+ output += (separator if count > 0 else "" ) + cell_value
265+ count += 1
266+ out_file .write ("{st}\n " .format (st = output ))
273267
274268 # Dump the collected JSON and line data
275269 if output_format == "json" :
0 commit comments