2525###############################################################################
2626import argparse
2727import datetime
28+ import json
2829import logging
2930import os
3031import platform
@@ -429,6 +430,107 @@ def build_config(
429430 return config
430431
431432
433+ def parse_describe (
434+ parsed_args : argparse .Namespace ,
435+ plugin_reg : PluginRegistry ,
436+ config_reg : ConfigRegistry ,
437+ logger : logging .Logger ,
438+ ):
439+ """parse 'describe' cmd line argument
440+
441+ Args:
442+ parsed_args (argparse.Namespace): parsed cmd line arguments
443+ plugin_reg (PluginRegistry): plugin registry instance
444+ config_reg (ConfigRegistry): config registry instance
445+ logger (logging.Logger): logger instance
446+ """
447+ if not parsed_args .name :
448+ if parsed_args .type == "config" :
449+ print ("Available built-in configs:" ) # noqa: T201
450+ for name in config_reg .configs :
451+ print (f" { name } " ) # noqa: T201
452+ elif parsed_args .type == "plugin" :
453+ print ("Available plugins:" ) # noqa: T201
454+ for name in plugin_reg .plugins :
455+ print (f" { name } " ) # noqa: T201
456+ print (f"\n Usage: describe { parsed_args .type } <name>" ) # noqa: T201
457+ sys .exit (0 )
458+
459+ if parsed_args .type == "config" :
460+ if parsed_args .name not in config_reg .configs :
461+ logger .error ("No config found for name: %s" , parsed_args .name )
462+ sys .exit (1 )
463+ config_model = config_reg .configs [parsed_args .name ]
464+ print (f"Config Name: { parsed_args .name } " ) # noqa: T201
465+ print (f"Description: { getattr (config_model , 'desc' , '' )} " ) # noqa: T201
466+ print ("Plugins:" ) # noqa: T201
467+ for plugin in getattr (config_model , "plugins" , []):
468+ print (f"\t { plugin } " ) # noqa: T201
469+
470+ elif parsed_args .type == "plugin" :
471+ if parsed_args .name not in plugin_reg .plugins :
472+ logger .error ("No plugin found for name: %s" , parsed_args .name )
473+ sys .exit (1 )
474+ plugin_class = plugin_reg .plugins [parsed_args .name ]
475+ print (f"Plugin Name: { parsed_args .name } " ) # noqa: T201
476+ print (f"Description: { getattr (plugin_class , '__doc__' , '' )} " ) # noqa: T201
477+
478+ sys .exit (0 )
479+
480+
481+ def parse_gen_plugin_config (
482+ parsed_args : argparse .Namespace ,
483+ plugin_reg : PluginRegistry ,
484+ config_reg : ConfigRegistry ,
485+ logger : logging .Logger ,
486+ ):
487+ """parse 'gen_plugin_config' cmd line argument
488+
489+ Args:
490+ parsed_args (argparse.Namespace): parsed cmd line arguments
491+ plugin_reg (PluginRegistry): plugin registry instance
492+ config_reg (ConfigRegistry): config registry instance
493+ logger (logging.Logger): logger instance
494+ """
495+ try :
496+ config = build_config (
497+ config_reg , plugin_reg , logger , parsed_args .plugins , parsed_args .built_in_configs
498+ )
499+
500+ config .name = parsed_args .config_name .split ("." )[0 ]
501+ config .desc = "Auto generated config"
502+ output_path = os .path .join (parsed_args .output_path , parsed_args .config_name )
503+ with open (output_path , "w" , encoding = "utf-8" ) as out_file :
504+ out_file .write (config .model_dump_json (indent = 2 ))
505+
506+ logger .info ("Config saved to: %s" , output_path )
507+ sys .exit (0 )
508+ except Exception :
509+ logger .exception ("Exception when building config" )
510+ sys .exit (1 )
511+
512+
513+ def log_system_info (log_path : str , system_info : SystemInfo , logger : logging .Logger ):
514+ """dump system info object to json log
515+
516+ Args:
517+ log_path (str): path to log folder
518+ system_info (SystemInfo): system object instance
519+ """
520+ if log_path :
521+ try :
522+ with open (
523+ os .path .join (log_path , "system_info.json" ), "w" , encoding = "utf-8"
524+ ) as log_file :
525+ json .dump (
526+ system_info .model_dump (mode = "json" , exclude_none = True ),
527+ log_file ,
528+ indent = 2 ,
529+ )
530+ except Exception as exp :
531+ logger .error (exp )
532+
533+
432534def main (arg_input : Optional [list [str ]] = None ):
433535 """Main entry point for the CLI
434536
@@ -460,56 +562,10 @@ def main(arg_input: Optional[list[str]] = None):
460562 logger .info ("Log path: %s" , log_path )
461563
462564 if parsed_args .subcmd == "describe" :
463- if not parsed_args .name :
464- if parsed_args .type == "config" :
465- print ("Available built-in configs:" ) # noqa: T201
466- for name in config_reg .configs :
467- print (f" { name } " ) # noqa: T201
468- elif parsed_args .type == "plugin" :
469- print ("Available plugins:" ) # noqa: T201
470- for name in plugin_reg .plugins :
471- print (f" { name } " ) # noqa: T201
472- print (f"\n Usage: describe { parsed_args .type } <name>" ) # noqa: T201
473- sys .exit (0 )
474-
475- if parsed_args .type == "config" :
476- if parsed_args .name not in config_reg .configs :
477- logger .error ("No config found for name: %s" , parsed_args .name )
478- sys .exit (1 )
479- config_model = config_reg .configs [parsed_args .name ]
480- print (f"Config Name: { parsed_args .name } " ) # noqa: T201
481- print (f"Description: { getattr (config_model , 'desc' , '' )} " ) # noqa: T201
482- print ("Plugins:" ) # noqa: T201
483- for plugin in getattr (config_model , "plugins" , []):
484- print (f"\t { plugin } " ) # noqa: T201
485-
486- elif parsed_args .type == "plugin" :
487- if parsed_args .name not in plugin_reg .plugins :
488- logger .error ("No plugin found for name: %s" , parsed_args .name )
489- sys .exit (1 )
490- plugin_class = plugin_reg .plugins [parsed_args .name ]
491- print (f"Plugin Name: { parsed_args .name } " ) # noqa: T201
492- print (f"Description: { getattr (plugin_class , '__doc__' , '' )} " ) # noqa: T201
493-
494- sys .exit (0 )
565+ parse_describe (parsed_args , plugin_reg , config_reg , logger )
495566
496567 if parsed_args .subcmd == "gen-plugin-config" :
497- try :
498- config = build_config (
499- config_reg , plugin_reg , logger , parsed_args .plugins , parsed_args .built_in_configs
500- )
501-
502- config .name = parsed_args .config_name .split ("." )[0 ]
503- config .desc = "Auto generated config"
504- output_path = os .path .join (parsed_args .output_path , parsed_args .config_name )
505- with open (output_path , "w" , encoding = "utf-8" ) as out_file :
506- out_file .write (config .model_dump_json (indent = 2 ))
507-
508- logger .info ("Config saved to: %s" , output_path )
509- sys .exit (0 )
510- except Exception :
511- logger .exception ("Exception when building config" )
512- sys .exit (1 )
568+ parse_gen_plugin_config (parsed_args , plugin_reg , config_reg , logger )
513569
514570 parsed_plugin_args = {}
515571 for plugin , plugin_args in plugin_arg_map .items ():
@@ -525,6 +581,7 @@ def main(arg_input: Optional[list[str]] = None):
525581 plugin_configs = parsed_args .plugin_configs or []
526582
527583 system_info = get_system_info (parsed_args )
584+ log_system_info (log_path , system_info , logger )
528585
529586 plugin_executor = PluginExecutor (
530587 logger = logger ,
0 commit comments