1414from openbb_cli .config .completer import NestedCompleter
1515from openbb_cli .config .constants import SCRIPT_TAGS
1616from openbb_cli .controllers .choices import build_controller_choice_map
17- from openbb_cli .controllers .hub_service import upload_routine
1817from openbb_cli .controllers .utils import (
1918 check_file_type_saved ,
2019 check_positive ,
2120 get_flair_and_username ,
2221 handle_obbject_display ,
2322 parse_unknown_args_to_dict ,
24- print_guest_block_msg ,
2523 print_rich_table ,
2624 remove_file ,
2725 system_clear ,
4038
4139# TODO: We should try to avoid these global variables
4240RECORD_SESSION = False
43- RECORD_SESSION_LOCAL_ONLY = False
4441SESSION_RECORDED = list ()
4542SESSION_RECORDED_NAME = ""
4643SESSION_RECORDED_DESCRIPTION = ""
@@ -65,7 +62,6 @@ class BaseController(metaclass=ABCMeta):
6562 "r" ,
6663 "reset" ,
6764 "stop" ,
68- "whoami" ,
6965 "results" ,
7066 ]
7167
@@ -271,11 +267,6 @@ def call_exit(self, _) -> None:
271267 for _ in range (self .PATH .count ("/" )):
272268 self .queue .insert (0 , "quit" )
273269
274- if not session .is_local ():
275- remove_file (
276- Path (session .user .preferences .export_directory , "routines" , "hub" )
277- )
278-
279270 def call_reset (self , _ ) -> None :
280271 """Process reset command.
281272
@@ -398,17 +389,6 @@ def call_record(self, other_args) -> None:
398389 )
399390 return
400391
401- if session .is_local ():
402- session .console .print (
403- "[red]Recording session to the OpenBB Hub is not supported in guest mode.[/red]"
404- )
405- session .console .print (
406- "\n [yellow]Visit the OpenBB Hub to register: http://my.openbb.co[/yellow]"
407- )
408- session .console .print (
409- "\n [yellow]Your routine will be saved locally.[/yellow]\n "
410- )
411-
412392 # Check if title has a valid format
413393 title = " " .join (ns_parser .name ) if ns_parser .name else ""
414394 pattern = re .compile (r"^[a-zA-Z0-9\s]+$" )
@@ -419,13 +399,11 @@ def call_record(self, other_args) -> None:
419399 return
420400
421401 global RECORD_SESSION # noqa: PLW0603
422- global RECORD_SESSION_LOCAL_ONLY # noqa: PLW0603
423402 global SESSION_RECORDED_NAME # noqa: PLW0603
424403 global SESSION_RECORDED_DESCRIPTION # noqa: PLW0603
425404 global SESSION_RECORDED_TAGS # noqa: PLW0603
426405 global SESSION_RECORDED_PUBLIC # noqa: PLW0603
427406
428- RECORD_SESSION_LOCAL_ONLY = session .is_local ()
429407 RECORD_SESSION = True
430408 SESSION_RECORDED_NAME = title
431409 SESSION_RECORDED_DESCRIPTION = (
@@ -471,135 +449,64 @@ def call_stop(self, other_args) -> None:
471449 )
472450 else :
473451 current_user = session .user
452+ title_for_local_storage = (
453+ SESSION_RECORDED_NAME .replace (" " , "_" ) + ".openbb"
454+ )
474455
475- # Check if the user just wants to store routine locally
476- # This works regardless of whether they are logged in or not
477- if RECORD_SESSION_LOCAL_ONLY :
478- # Whitespaces are replaced by underscores and an .openbb extension is added
479- title_for_local_storage = (
480- SESSION_RECORDED_NAME .replace (" " , "_" ) + ".openbb"
481- )
456+ routine_file = os .path .join (
457+ f"{ current_user .preferences .export_directory } /routines" ,
458+ title_for_local_storage ,
459+ )
482460
483- routine_file = os .path .join (
484- f"{ current_user .preferences .export_directory } /routines" ,
485- title_for_local_storage ,
461+ # If file already exists, add a timestamp to the name
462+ if os .path .isfile (routine_file ):
463+ i = session .console .input (
464+ "A local routine with the same name already exists, do you want to override it? (y/n): "
486465 )
487-
488- # If file already exists, add a timestamp to the name
489- if os .path .isfile (routine_file ):
490- i = session .console .input (
491- "A local routine with the same name already exists, do you want to override it? (y/n): "
492- )
466+ session .console .print ("" )
467+ while i .lower () not in ["y" , "yes" , "n" , "no" ]:
468+ i = session .console .input ("Select 'y' or 'n' to proceed: " )
493469 session .console .print ("" )
494- while i .lower () not in ["y" , "yes" , "n" , "no" ]:
495- i = session .console .input ("Select 'y' or 'n' to proceed: " )
496- session .console .print ("" )
497-
498- if i .lower () in ["n" , "no" ]:
499- new_name = (
500- datetime .now ().strftime ("%Y%m%d_%H%M%S_" )
501- + title_for_local_storage
502- )
503- routine_file = os .path .join (
504- current_user .preferences .export_directory ,
505- "routines" ,
506- new_name ,
507- )
508- session .console .print (
509- f"[yellow]The routine name has been updated to '{ new_name } '[/yellow]\n "
510- )
511470
512- # Writing to file
513- Path (os .path .dirname (routine_file )).mkdir (
514- parents = True , exist_ok = True
515- )
516-
517- with open (routine_file , "w" ) as file1 :
518- lines = ["# OpenBB Platform CLI - Routine" , "\n " ]
519-
520- username = getattr (
521- session .user .profile .hub_session , "username" , "local"
471+ if i .lower () in ["n" , "no" ]:
472+ new_name = (
473+ datetime .now ().strftime ("%Y%m%d_%H%M%S_" )
474+ + title_for_local_storage
522475 )
523-
524- lines += (
525- [f"# Author: { username } " , "\n \n " ] if username else ["\n " ]
476+ routine_file = os .path .join (
477+ current_user .preferences .export_directory ,
478+ "routines" ,
479+ new_name ,
480+ )
481+ session .console .print (
482+ f"[yellow]The routine name has been updated to '{ new_name } '[/yellow]\n "
526483 )
527- lines += [
528- f"# Title: { SESSION_RECORDED_NAME } " ,
529- "\n " ,
530- f"# Tags: { SESSION_RECORDED_TAGS } " ,
531- "\n \n " ,
532- f"# Description: { SESSION_RECORDED_DESCRIPTION } " ,
533- "\n \n " ,
534- ]
535- lines += [c + "\n " for c in SESSION_RECORDED [:- 1 ]]
536- # Writing data to a file
537- file1 .writelines (lines )
538484
539- session .console .print (
540- f"[green]Your routine has been recorded and saved here: { routine_file } [/green]\n "
541- )
485+ # Writing to file
486+ Path (os .path .dirname (routine_file )).mkdir (parents = True , exist_ok = True )
487+
488+ with open (routine_file , "w" ) as file1 :
489+ lines = ["# OpenBB Platform CLI - Routine" , "\n " ]
490+ lines += [
491+ f"# Title: { SESSION_RECORDED_NAME } " ,
492+ "\n " ,
493+ f"# Tags: { SESSION_RECORDED_TAGS } " ,
494+ "\n \n " ,
495+ f"# Description: { SESSION_RECORDED_DESCRIPTION } " ,
496+ "\n \n " ,
497+ ]
498+ lines += [c + "\n " for c in SESSION_RECORDED [:- 1 ]]
499+ # Writing data to a file
500+ file1 .writelines (lines )
542501
543- # If user doesn't specify they want to store routine locally
544- # Confirm that the user is logged in
545- elif not session .is_local ():
546- routine = "\n " .join (SESSION_RECORDED [:- 1 ])
547- hub_session = current_user .profile .hub_session
548-
549- if routine is not None :
550- auth_header = (
551- f"{ hub_session .token_type } { hub_session .access_token .get_secret_value ()} "
552- if hub_session
553- else None
554- )
555- kwargs = {
556- "auth_header" : auth_header ,
557- "name" : SESSION_RECORDED_NAME ,
558- "description" : SESSION_RECORDED_DESCRIPTION ,
559- "routine" : routine ,
560- "tags" : SESSION_RECORDED_TAGS ,
561- "public" : SESSION_RECORDED_PUBLIC ,
562- }
563- response = upload_routine (** kwargs ) # type: ignore
564- if response is not None and response .status_code == 409 :
565- i = session .console .input (
566- "A routine with the same name already exists, do you want to replace it? (y/n): "
567- )
568- session .console .print ("" )
569- if i .lower () in ["y" , "yes" ]:
570- kwargs ["override" ] = True # type: ignore
571- response = upload_routine (** kwargs ) # type: ignore
572- else :
573- session .console .print ("[info]Aborted.[/info]" )
502+ session .console .print (
503+ f"[green]Your routine has been recorded and saved here: { routine_file } [/green]\n "
504+ )
574505
575506 # Clear session to be recorded again
576507 RECORD_SESSION = False
577508 SESSION_RECORDED = list ()
578509
579- def call_whoami (self , other_args : list [str ]) -> None :
580- """Process whoami command."""
581- parser = argparse .ArgumentParser (
582- add_help = False ,
583- formatter_class = argparse .ArgumentDefaultsHelpFormatter ,
584- prog = "whoami" ,
585- description = "Show current user" ,
586- )
587- ns_parser , _ = self .parse_simple_args (parser , other_args )
588-
589- if ns_parser :
590- current_user = session .user
591- local_user = session .is_local ()
592- if not local_user :
593- hub_session = current_user .profile .hub_session
594- session .console .print (
595- f"[info]email:[/info] { hub_session .email if hub_session else 'N/A' } "
596- )
597- session .console .print (
598- f"[info]uuid:[/info] { hub_session .user_uuid if hub_session else 'N/A' } "
599- )
600- else :
601- print_guest_block_msg ()
602-
603510 def call_results (self , other_args : list [str ]):
604511 """Process results command."""
605512 parser = argparse .ArgumentParser (
0 commit comments