@@ -721,13 +721,30 @@ def _add_colors(self, txt):
721721 x = t .replace ('mpv' , '[green]mpv[/green]' ).replace ('mplayer' , '[green]mplayer[/green]' ).replace ('vlc' , '[green]vlc[/green]' )
722722 return '[bold]' + x .replace ('||' , r']' ).replace ('|' , r'\[' ) + '[/bold]'
723723
724+
724725class PythonExecutable (object ):
726+ ''' A class to verify that python is installed
727+ and in the PATH
728+ '''
725729 is_debian = False
726730 _python = [None , None ]
727731 requested_python_version = 3
728732
729- def __init__ (self , requested_python_version ):
733+ def __init__ (
734+ self ,
735+ requested_python_version ,
736+ terminate_if_not_found = False ):
737+ ''' Parameters
738+ ==========
739+ requested_python_version
740+ The version of python we are looking for
741+ (either 2 or 3)
742+ terminate_if_not_found
743+ If True, the program will terminate if python
744+ is not found (default is False)
745+ '''
730746 self .requested_python_version = requested_python_version
747+ self ._terminate_if_not_found = terminate_if_not_found
731748 if not platform .system ().lower ().startswith ('win' ):
732749 self ._check_if_is_debian_based ()
733750 self ._get_pythons ()
@@ -790,16 +807,25 @@ def _get_python(self, version):
790807 stdout = subprocess .PIPE ,
791808 stderr = subprocess .PIPE
792809 )
793- # print(p.communicate())
794- # print(p.returncode)
795- # if p.communicate()[0]:
796810 p .communicate ()
797811 if p .returncode == 0 :
798812 self ._python [version - 2 ] = 'python' + str (version )
799813
800- def can_install (self ):
801- return True if self ._python [self .requested_python_version - 2 ] else False
814+ if self ._terminate_if_not_found and \
815+ not self .can_use ():
816+ print ('''
802817
818+ Python {} was not found in your system.
819+ If you have already installed it, you probably have not added it in your PATH.
820+ To verify that Python is in your PATH open a terminal/console and type "python".
821+ If you get en error, you have to add it to your PATH.
822+ ''' .format ('2' if self .requested_python_version == 2 else '3' ))
823+ sys .exit (1 )
824+
825+ def can_use (self ):
826+ ''' Can I use the python I requested?
827+ '''
828+ return True if self ._python [self .requested_python_version - 2 ] else False
803829
804830class PyRadioUpdate (object ):
805831
@@ -847,17 +873,10 @@ def __init__(self,
847873 self ._package = package
848874 self .user = user
849875 self ._github_long_description = github_long_description
850- self ._python_exec = PythonExecutable (python_version_to_use )
851- if self ._python_exec .python is None :
852- print ('''
853-
854- Python was not found in your system.
855- If you have already installed it, chances are you have not added it in your PATH.
856- To verify that Python is in your PATH open a terminal/console and type "python".
857- If you get en error, you have to add it to your PATH.
858- ''' )
859- sys .exit (1 )
860-
876+ self ._python_exec = PythonExecutable (
877+ python_version_to_use ,
878+ terminate_if_not_found = True
879+ )
861880 self .python2 = True if python_version_to_use == 2 else False
862881 self ._pix_isolated = pix_isolated
863882
@@ -1259,17 +1278,10 @@ def __init__(self,
12591278 self ._package = package
12601279 self ._fromTUI = fromTUI
12611280 self ._github_long_description = github_long_description
1262- self ._python_exec = PythonExecutable (python_version_to_use )
1263- if self ._python_exec .python is None :
1264- print ('''
1265-
1266- Python was not found in your system.
1267- If you have already installed it, chances are you have not added it in your PATH.
1268- To verify that Python is in your PATH open a terminal/console and type "python".
1269- If you get en error, you have to add it to your PATH.
1270- ''' )
1271- sys .exit (1 )
1272-
1281+ self ._python_exec = PythonExecutable (
1282+ python_version_to_use ,
1283+ terminate_if_not_found = True
1284+ )
12731285 self .python2 = True if python_version_to_use == 2 else False
12741286 self ._pix_isolated = pix_isolated
12751287 self ._get_cache = False
@@ -1465,19 +1477,12 @@ def _do_it(self, mode='update'):
14651477 print_python2 ()
14661478
14671479 python_version_to_use = 2 if args .python2 else 3
1468- python_exec = PythonExecutable (python_version_to_use )
1469-
1470- if python_exec .python is None :
1471- print ('''
1472-
1473- Python was not found in your system.
1474- If you have already installed it, chances are you have not added it in your PATH.
1475- To verify that Python is in your PATH open a terminal/console and type "python".
1476- If you get en error, you have to add it to your PATH.
1477- ''' )
1478- sys .exit (1 )
1480+ python_exec = PythonExecutable (
1481+ python_version_to_use ,
1482+ terminate_if_not_found = True
1483+ )
14791484
1480- if not python_exec .can_install :
1485+ if not python_exec .can_use :
14811486 print ('Error: Python {} not found on your system...\n ' .format ('2' if python_exec .requested_python_version == 2 else '3' ))
14821487 sys .exit (1 )
14831488
0 commit comments