1313from torustrateinterface import Keypair
1414from typer import Context
1515
16- from torusdk ._common import IPFS_REGEX , TorusSettings , get_node_url
16+ from torusdk ._common import CID_REGEX , TorusSettings , get_node_url
1717from torusdk .balance import dict_from_nano , from_rems , to_rems
1818from torusdk .client import TorusClient
1919from torusdk .errors import InvalidPasswordError , PasswordNotProvidedError
@@ -54,10 +54,10 @@ def merge_models(model_a: T, model_b: BaseModel) -> T:
5454
5555
5656def extract_cid (value : str ):
57- cid_hash = re .match (IPFS_REGEX , value )
57+ cid_hash = re .match (CID_REGEX , value )
5858 if not cid_hash :
5959 raise typer .BadParameter (f"CID provided is invalid: { value } " )
60- return cid_hash .group ()
60+ return cid_hash .group ("cid" )
6161
6262
6363def input_to_rems (value : float | None ):
@@ -277,23 +277,86 @@ def print_table_from_plain_dict(
277277 console .print (table )
278278
279279
280+ def render_pydantic_subtable (value : BaseModel | dict [Any , Any ]) -> Table :
281+ """
282+ Renders a subtable for a nested Pydantic object or dictionary.
283+
284+ Args:
285+ value: A nested Pydantic object or dictionary.
286+
287+ Returns:
288+ A rich Table object representing the subtable.
289+ """
290+ subtable = Table (
291+ show_header = False ,
292+ padding = (0 , 0 , 0 , 0 ),
293+ border_style = "bright_black" ,
294+ )
295+ if isinstance (value , BaseModel ):
296+ for subfield_name , _ in value .model_fields .items ():
297+ subfield_value = getattr (value , subfield_name )
298+ subtable .add_row (f"{ subfield_name } : { subfield_value } " )
299+ else :
300+ for subfield_name , subfield_value in value .items (): # type: ignore
301+ subtable .add_row (f"{ subfield_name } : { subfield_value } " )
302+ return subtable
303+
304+
305+ def render_single_pydantic_object (
306+ obj : BaseModel , console : Console , title : str = ""
307+ ) -> None :
308+ """
309+ Renders a rich table from a single Pydantic object.
310+
311+ Args:
312+ obj: A single Pydantic object.
313+ console: The rich Console object.
314+ title: Optional title for the table.
315+ """
316+ table = Table (
317+ title = title ,
318+ show_header = True ,
319+ header_style = "bold magenta" ,
320+ title_style = "bold magenta" ,
321+ )
322+
323+ table .add_column ("Field" , style = "white" , vertical = "middle" )
324+ table .add_column ("Value" , style = "white" , vertical = "middle" )
325+
326+ for field_name , _ in obj .model_fields .items ():
327+ value = getattr (obj , field_name )
328+ if isinstance (value , BaseModel ):
329+ subtable = render_pydantic_subtable (value )
330+ table .add_row (field_name , subtable )
331+ else :
332+ table .add_row (field_name , str (value ))
333+
334+ console .print (table )
335+ console .print ("\n " )
336+
337+
280338def render_pydantic_table (
281- objects : list [T ],
339+ objects : T | list [T ],
282340 console : Console ,
283341 title : str = "" ,
284342 ignored_columns : list [str ] = [],
285343) -> None :
286344 """
287- Renders a rich table from a list of Pydantic objects.
345+ Renders a rich table from a list of Pydantic objects or a single Pydantic object .
288346
289347 Args:
290- objects: A list of Pydantic objects.
348+ objects: A list of Pydantic objects or a single Pydantic object .
291349 console: The rich Console object.
292350 title: Optional title for the table.
351+ ignored_columns: List of column names to ignore.
293352 """
294353 if not objects :
295354 return
296355
356+ if isinstance (objects , BaseModel ):
357+ render_single_pydantic_object (objects , console , title )
358+ return
359+
297360 table = Table (
298361 title = title ,
299362 show_header = True ,
@@ -313,14 +376,7 @@ def render_pydantic_table(
313376 continue
314377 value = getattr (obj , field_name )
315378 if isinstance (value , BaseModel ):
316- subtable = Table (
317- show_header = False ,
318- padding = (0 , 0 , 0 , 0 ),
319- border_style = "bright_black" ,
320- )
321- for subfield_name , _ in value .model_fields .items ():
322- subfield_value = getattr (value , subfield_name )
323- subtable .add_row (f"{ subfield_name } : { subfield_value } " )
379+ subtable = render_pydantic_subtable (value )
324380 row_data .append (subtable )
325381 else :
326382 row_data .append (str (value ))
0 commit comments