
    MgS@                         S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
Jr  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJr   " S S\5      rSS jr " S S\5      rg)    N)Cmd)partial)Fore)Memory)PluginManager)schedule)SpinnerThread)get_parent_directory)notify)create_voicec                       \ rS rSrSrSrS rS!S jrS"S jrSS\	SS4S	 jr
S
 rS rS#S jrS rS rS rS rS rS rS rS rS rS rS rS rS rS$S jrS\R:                  4S jrS rS r S r!S r"g)%	JarvisAPI   a  
Jarvis interface for plugins.

Plugins will receive a instance of this as the second (non-self) parameter
of the exec()-method.

Everything Jarvis-related that can't be implemented as a stateless-function
in the utilities-package should be implemented here.
z!You are not connected to Internetc                     Xl         SU l        g )NF)_jarvisspinner_running)selfjarviss     </Users/admin/workspace/ai/Jarvis/jarviscli/CmdInterpreter.py__init__JarvisAPI.__init__    s    $     c                     [        X!-   [        R                  -   SS9  U(       a  U R                  R	                  U5        gg)a5  
This method give the jarvis the ability to print a text
and talk when sound is enable.
:param text: the text to print (or talk)
:param color: for text - use colorama (https://pypi.org/project/colorama/)
              e.g. Fore.CYAN
:param speak: False-, if text shouldn't be spoken even if speech is enabled
T)flushN)printr   RESETr   speak)r   textcolorr   s       r   sayJarvisAPI.say$   s3     	elTZZ't4LLt$ r   c                     [         R                  R                  X!-   [        R                  -   5        [         R                  R                  5         [         R                  R                  5       nUR                  5       $ )z
Get user input
)	sysstdoutwriter   r   r   stdinreadlinerstrip)r   promptr    r   s       r   inputJarvisAPI.input2   sO     	

$**45

yy!!#{{}r   Nc                       U" U R                  X5      R                  SS5      5      nUb  Xd:  d  Ub  Xe:  a
  SU SU S3nOU$ MF  ! [         a    Sn MV  f = f)a4  
Get user input: As number.

Guaranteed only returns number - ask user till correct number entered.

:param prompt: Printed to console
:param color: Color of prompot
:param rtype: type of return value; e.g. float (default) or int
:param rmin: Minum of values returned
:param rmax: Maximum of values returned
,.zSorry, needs to be between z and z. Try again: z(Sorry, needs to be a number. Try again: )r+   replace
ValueError)r   r*   r    rtyperminrmaxvalues          r   input_numberJarvisAPI.input_number>   sx     djj7??SIJ$4;KPUP\:4&dV=YF L   Cs   A A A AAc                     U R                  5       (       a  U R                  S5        U R                  [        R                  5        g)zPrint generic connection errorr   N)is_spinner_runningspinner_stopr!   r   _CONNECTION_ERROR_MSGr   s    r   connection_errorJarvisAPI.connection_errorU   s3     ""$$b!001r   c                 8    U R                   R                  5         g)zImmediately exit JarvisN)r   closer<   s    r   exitJarvisAPI.exit]   s    r   c                     [        U[        5      (       a  Uu  p4O%[        U[        5      (       a  SnUnO[        S5      eUS:X  a  [	        X45        g[        U[        X45        g)z
Sends notification msg in time_in milliseconds
:param msg: Message. Either String (message body) or tuple (headline, message body)
:param time_seconds: Time in seconds to wait before showing notification
Jarviszmsg not a string or tupler   N)
isinstancetuplestrr1   r   r   )r   msgtime_secondsheadlinemessages        r   notificationJarvisAPI.notificationa   sZ     c5!! #HgS!!HG89918%\68=r   c                 R    U R                   R                  R                  " XU /UQ76 $ )z
Schedules function
After time_seconds call function with these parameter:
   - reference to this JarvisAPI instance
   - schedule_id (return value of this function)
   - *args
:return: integer, id - use with cancel
)r   	schedulercreate_event)r   rI   functionargss       r   r   JarvisAPI.schedulet   s.     ||%%22D1+/1 	1r   c                     [        SS5      nUR                  5         U R                  R                  R	                  U5        UR                  5         U R                  S[        R                  5        g)zR
Cancel event scheduled with schedule
:param schedule_id: id returned by schedule

Cancelling333333?zCancellation successfulN)	r	   startr   rO   cancelstopr!   r   GREEN)r   schedule_idspinners      r   rX   JarvisAPI.cancel   sM    
  d3%%k2*DJJ7r   c                     U R                  S5      n[        XSS9U R                  l        SU R                  l        U R                  SS5        g)z:
Use text to speech for every text passed to jarvis.say()
gtts_statusx   rateTenable_voiceN)get_datar   r   speechrc   update_datar   gs     r   rc   JarvisAPI.enable_voice   sA     MM-(*4=$(!.r   c                 (    U R                  SS5        g)zG
Switch to default speech engine for every text passed to jarvis.say()
r_   FN)rf   r<   s    r   disable_gttsJarvisAPI.disable_gtts   s     	.r   c                 |    U R                  SS5        U R                  S5      n[        XSS9U R                  l        g)zA
Use google text to speech for every text passed to jarvis.say()
r_   Tr`   ra   N)rf   rd   r   r   re   rg   s     r   enable_gttsJarvisAPI.enable_gtts   s5     	-MM-(*4=r   c                 j    U R                  5         SU R                  l        U R                  SS5        g)zQ
Stop text to speech output & disable gtts for every text passed to jarvis.say()
Frc   N)rk   r   rc   rf   r<   s    r   disable_voiceJarvisAPI.disable_voice   s,     	$)!/r   c                 .    U R                   R                  $ )zn
Returns True/False if voice is enabled/disabled with
enable_voice or disable_voice
Default: False (disabled)
)r   rc   r<   s    r   is_voice_enabledJarvisAPI.is_voice_enabled   s     ||(((r   c                     U R                   R                  R                  U5        U R                  SU R                   R                  R                  5        g)z
Alters the rate of the speech engine by a specified amount and remember
the new speech rate.
:param delta: Amount of change to apply to speech rate
speech_rateN)r   re   change_raterf   rb   )r   deltas     r   change_speech_rateJarvisAPI.change_speech_rate   s<     	''.(;(;(@(@Ar   c                 L    U R                   R                  R                  U5      $ )z 
Get a specific key from memory
)r   memoryrd   r   keys     r   rd   JarvisAPI.get_data   s     ||""++C00r   c                     U R                   R                  R                  X5        U R                   R                  R                  5         g)z
Add a key and value to memory
N)r   r}   add_datasaver   r   r5   s      r   r   JarvisAPI.add_data   s2     	$$S0  "r   c                     U R                   R                  R                  X5        U R                   R                  R                  5         g)z$
Updates a key with supplied value.
N)r   r}   rf   r   r   s      r   rf   JarvisAPI.update_data   s2     	''3  "r   c                     U R                   R                  R                  U5        U R                   R                  R                  5         g)z
Delete a key from memory
N)r   r}   del_datar   r~   s     r   r   JarvisAPI.del_data   s2     	$$S)  "r   c                     U R                   R                  U5      nU R                   R                  U5      nU R                   R                  X25      ng)z'
Simulates typing 's' in Jarvis prompt
N)r   precmdonecmdpostcmd)r   slinerY   s       r   evalJarvisAPI.eval   s@     ||""1%||""4(||##D/r   c                 h    SU l         [        US5      U l        U R                  R                  5         g)zk
Function for starting a spinner when prompted from a plugin
and a default message for performing the task
TrV   N)r   r	   r\   rW   )r   rK   s     r   spinner_startJarvisAPI.spinner_start   s*    
  $$Wd3r   zTask executed successfully! c                 h    U R                   R                  5         U R                  X5        SU l        g)zt
Function for stopping the spinner when prompted from a plugin
and displaying the message after completing the task
FN)r\   rY   r!   r   )r   rK   r    s      r   r:   JarvisAPI.spinner_stop   s(    
 	 $r   c                     U R                   $ N)r   r<   s    r   r9   JarvisAPI.is_spinner_running   s    ###r   c                 z    U R                  S5      nUR                  5       nUS:X  d  US:X  a  [        U5      nOjUS:X  d  US:X  aL  U R                  S5      n[        R                  R                  U5      (       d  [        R                  " U5        OU R                  5         M  [        R                  " U5        U$ )z;
Returns the final directory where the files must be saved
z9Would you like to save the file in the same folder?[y/n] yesynonzEnter the folder destination: )	r+   lowerr
   ospathexistsmakedirsincorrect_optionchdir)r   r   user_choicedestinations       r   get_saving_directoryJarvisAPI.get_saving_directory   s     **KMK%++-Ke#{c'9248d"kS&8"jj)IJww~~k22KK,!!# " 	r   c                     U R                  S[        R                  5        U R                  S[        R                  5        g)zr
A function to notify the user that an incorrect option
has been entered and prompting him to enter a correct one
z0Oops! Looks like you entered an incorrect optionzLook at the options once again:N)r!   r   MAGENTArZ   r<   s    r   r   JarvisAPI.incorrect_option  s*    
 	CT\\R2DJJ?r   )r   r\   r   )r   T)r   r   )r   )z	Starting )#__name__
__module____qualname____firstlineno____doc__r;   r   r!   r+   floatr6   r=   rA   rL   r   rX   rc   rk   rn   rq   rt   rz   rd   r   rf   r   r   r   r   rZ   r:   r9   r   r   __static_attributes__ r   r   r   r      s     @%%
 #%Be$T .2>&
18//>0)B1###0 $B %$2@r   r   c                    ^ ^ U U4S jnU$ )Nc                 x  >  T(       a	  T" X5        g T" U5        g ! [          a    U R                  R                  5       (       a  U R                  S5        [	        [
        R                  S-   5        [	        S5        [	        S5        [        R                  " 5         [	        [
        R                  5         g f = f)NzIt seems some error has occuredz4Some error occurred, please open an issue on github!zHere is error:r   )
	Exception_apir9   r:   r   r   r   	traceback	print_excr   )r   r   do	pass_selfs     r   try_do$catch_all_exceptions.<locals>.try_do  s    	41 		yy++--!!"CDHIJ "#"I!$**		s     BB98B9r   )r   r   r   s   `` r   catch_all_exceptionsr     s      Mr   c                   d    \ rS rSr/ S4S jrS rS rS rS rS r	S	 r
S
 rS rS rS rS rSrg)CmdInterpreteri-  Tc                    [         R                  " U 5        SR                  [        R                  SS 5      R                  5       nX@l        Xl        X l        U(       a  SU l        SU l        SU l        [        R                  " [        R                  U R                  5        [        5       U l        [        R                  " 5       U l        [#        U 5      U l        U R$                  R&                  U l        U R$                  R)                  S5      U l        U R$                  R)                  S5      U l        U R,                  (       d  SU l         U R$                  R)                  S	5      n[/        XU R,                  S
9U l        SSS.U l        [=        5       U l        U H  nU R>                  RA                  U5        M      U(       d  U RC                  5         U RE                  5         U R                  (       a&  U R$                  R'                  U R                  5        gg! [2         aS  nU R'                  S[4        R6                  5        U R'                  [9        U5      [4        R6                  5         SnANSnAff = f)zk
This constructor contains a dictionary with Jarvis Actions (what Jarvis can do).
In alphabetically order.
    NFr   rc   rw   r`   r_   ra   zVoice not supportedclockpinpoint)zwhat time is itz
where am i)#r   r   joinr$   argvstripfirst_reactionfirst_reaction_textr*   signalSIGINTinterrupt_handlerr   r}   r   	SchedulerrO   r   r   r!   rd   rc   rw   r   re   r   r   r   rG   fixed_responsesr   _plugin_manageradd_directory_init_plugin_info_activate_plugins)	r   r   r*   directoriesr   commandr_   e	directorys	            r   r   CmdInterpreter.__init__3  s    	T((388AB<(..0,#6 "'D')D$DKfmmT%;%;<h!++-dO	99== !II..~>99--m<"D	+)),,];K&(8(8:DK 4;.8 "  -$I  ..y9 % ""$ IIMM$223 #  	+HH*DLL9HHSVT\\**	+s   4H! !
I>+A	I99I>c                 j   [        U R                  R                  5       5      U R                  R                  5       [        R
                  [        R                  [        R                  S.nSnUS   S:  a  US-  nU[        R                  -  nU =R                  UR                  " S0 UD6-  sl        g )N)disabledenabledmagentacyanresetz'{magenta}{enabled} {cyan}plugins loadedr   r   z[ {magenta}{disabled} {cyan}plugins disabled.
            More information: {magenta}status
r   )
lenr   get_disabledget_number_plugins_loadedr   r   CYANr   r   format)r   plugin_status_formatterplugin_statuss      r   r    CmdInterpreter._init_plugin_infoo  s    D00==?@++EEG||IIZZ#
 B":.2 3 3M#  M$8$8 %'%%' 	' r   c           
         U R                   R                  5       R                  5        H  u  pU R                  X!5        [	        UR
                  5      n[        [        SU-   [        UU 5      5        [        [        SU-   [        U R                  R                  UR                  5       5      5        UR                  U R                  5        M     g)zAGenerate do_XXX, help_XXX and (optionally) complete_XXX functionsdo_help_N)r   get_pluginsitems_plugin_update_completionr   runsetattrr   r   r   r!   get_docinit)r   plugin_nameplugin	run_catchs       r   r    CmdInterpreter._activate_plugins  s    %)%9%9%E%E%G%M%M%O![**6?,VZZ8I	 +%IIMMNN$&' KK		"% &Pr   c                     [        UR                  5       5      n[        U5      S:  a  S n[        [        SU-   U" U5      5        gg)z&Return True if completion is availabler   c                    ^  U 4S jnU$ )Nc                 b   > T Vs/ s H  oR                  U 5      (       d  M  UPM     sn$ s  snf r   )
startswith)r   icompletionss     r   _complete_implRCmdInterpreter._plugin_update_completion.<locals>.complete.<locals>._complete_impl  s$    '2I{!ll46HA{IIIs   ,,r   )r   r   s   ` r   complete:CmdInterpreter._plugin_update_completion.<locals>.complete  s    J%%r   	complete_N)listr   r   r   r   )r   r   r   r   r   s        r   r   (CmdInterpreter._plugin_update_completion  sJ    6??,-{a& %	'  r   c                     U R                   $ r   )r   r<   s    r   get_apiCmdInterpreter.get_api  s    yyr   c                     U R                   R                  5       (       a  U R                   R                  S5        U R                  S[        R
                  5        U R                  R                  5         [        R                  " 5         g)zClosing Jarvis.zSome error has occuredzGoodbye, see you later!N)
r   r9   r:   r!   r   r   rO   stop_allr$   rA   r<   s    r   r@   CmdInterpreter.close  sY     	899''))II""#;<*DLL9!
r   c                 l    U R                  5       R                  U5        [        R                  " 5         g r   )r  r   r$   rA   )r   r   s     r   execute_onceCmdInterpreter.execute_once  s    G$
r   c                 D    U R                  S[        R                  5        g)z-Jarvis let you know if an error has occurred.z$I could not identify your command...N)r!   r   r   r<   s    r   errorCmdInterpreter.error  s    7Fr   c                 $    U R                  5         g)z(Closes Jarvis on SIGINT signal. (Ctrl-C)N)r@   )r   r   frames      r   r    CmdInterpreter.interrupt_handler  s    

r   c                    U R                   R                  5       n[        U R                   R                  5       5      nU R	                  U SU S35        SU;  ap  US:  ai  U R	                  S5        U R                   R                  5       R                  5        H+  u  pEU R	                  US SSR                  U5       35        M-     g	g	g	)
zPrints plugin status statusz plugins enabled, z plugins disabled.shortr   r   z<20z: z OR N)r   r   r   r   r!   r   r   )r   r   count_enabledcount_disabledr   reasons         r   	do_statusCmdInterpreter.do_status  s    ,,FFHT11>>@AM?"4^4DDVWX! 2HHRL$($8$8$E$E$G$M$M$O HS>FKK,?+@AB %P !3r   c                    U(       a  [         R                  " X5        g U R                  S5        SnSnU R                  U5        U R                  U[        R                  5        U R
                  R                  5       n0 nUR                  5        H   nXF   nXu;  d  M  XuUR                  5       '   M"     / n[        UR                  5       5       Hh  n	U	S:X  a  M   SR                  XY   R                  5       5      n
U
S:w  a!  SU	-   S-   U
-   S-   nUR                  U5        MT  UR                  SU	-   5        Mj     [         R                  " X5        g ! [         a    UR                  SU	-   5         M  f = f)	Nr   z#These are valid commands for Jarvisz'Format: command ([aliases for command])helpz, z* z ())r   do_helpr!   r   r   r   r   keysget_namesortedr   aliasappendAttributeError	columnize)r   argheader_stringformat_stringplugin_dictunique_pluginsr   r   help_outputnamealias_stringplugin_outputs               r   r  CmdInterpreter.do_help  sF   KK"HHRLAMEMHH]#HH]DII...::<KN"'')$)/8>6??#45 * K~22456>4#'99^-A-G-G-I#JL#r)(,td(:\(IC(O#**=9#**4$;7 6 MM$, & 4&&td{34s   AE$EE54E5c                 H    U R                  S5        U R                  S5        g )Nz-Prints info about enabled or disabled pluginsz0Use "status short" to omit detailed information.)r!   r<   s    r   help_statusCmdInterpreter.help_status  s    @AEFr   )r   r   rc   r   r   r   r}   r*   r!   rO   re   rw   N)r   r   r   r   r   r   r   r   r  r@   r  r  r   r  r  r0  r   r   r   r   r   r   -  sJ     :4x'$#,'	G	C->Gr   r   )T)r   r   r$   r   cmdr   	functoolsr   coloramar   packages.memory.memoryr   r   	utilitiesr   utilities.animationsr	   utilities.GeneralUtilitiesr
   utilities.notificationr   utilities.voicer   objectr   r   r   r   r   r   <module>r<     sU    	  
     ) '  . ; ) (C@ C@L(GS Gr   