pyenv-virtualenv for Windows 1.2
A 'pyenv' plugin to manage Python virtual environments, depending on different Python versions, for various Python projects.
Loading...
Searching...
No Matches
pyenv-virtualenvs Namespace Reference

Functions

int run ()
 Sub routine to run the application.
 
tuple[(argparse.Namespace, None), int] parseCliArguments ()
 Parse CLI arguments for this application.
 
int main ()
 Main routine of the application.
 

Function Documentation

◆ run()

int pyenv-virtualenvs.run ( )

Sub routine to run the application.

Returns
RC = 0 or other values in case of error.

Definition at line 53 of file pyenv-virtualenvs.py.

53def run() -> int:
54 rc: int = 0
55 cwd = os.getcwd()
56 # noinspection PyBroadException
57 try:
58 while True:
60 'Listing Python virtual environments in "pyenv".'
61 )
62 log.verbose('Determining Python versions ...')
63 # Determine Python version, which is capable
64 # to install Python virtual environment.
65 versions_dir = os.path.join(
66 os.environ['PYENV_ROOT'],
67 'versions'
68 )
69 if not os.path.isdir(versions_dir):
70 log.error('Cannot find any Python version in "pyenv".')
71 log.info('Install a Python version 3.3+ into "pyenv".')
72 rc = 1
73 break
75 '*',
76 venv_capable=False,
77 as_paths=True
78 )
79 if len(vers) == 0:
80 log.error('Cannot find a Python version in "pyenv".')
81 log.info('Install a Python version 3.3+ into "pyenv".')
82 rc = 1
83 break
84 per = os.environ['PYENV_ROOT']
85 # Pass 1: Collect the Python versions.
86 pyts = [
87 [tbl.HEADER, 'G', 'Venv-Capable', 'Version', '"pyenv" Location'],
88 [tbl.SEPARATOR]
89 ]
90 for ver in vers:
91 pyts_item = [
92 tbl.DATA,
95 os.path.basename(ver),
96 '%PYENV_ROOT%' + os.sep + ver[len(per):]
97 ]
98 pyts.append(pyts_item)
99 # End for
100 pyts.append([tbl.SEPARATOR])
101 # Pass 2: Collect the virtual environments.
102 pves = [
103 [tbl.HEADER, 'A', 'Version', 'Name', '"pyenv" Location'],
104 [tbl.SEPARATOR]
105 ]
106 for ver in vers:
107 venvs = hlp.getEnvs(ver,'*', as_paths=True)
108 for venv in venvs:
109 act = ' '
110 if (
111 ('PROMPT' in os.environ)
112 and
113 (f'({os.path.basename(venv)})' in os.environ['PROMPT'])
114 ):
115 act = '*'
116 pves.append([
117 tbl.DATA,
118 act,
119 os.path.basename(ver),
120 os.path.basename(venv),
121 '%PYENV_ROOT%' + os.sep + venv[len(per):]
122 ])
123 # End for
124 pves.append([tbl.SEPARATOR])
125 # Pass 3: Output list of Python versions.
126 table_pyts = tbl.SimpleTable(
127 pyts,
128 headline='INSTALLED PYTHON VERSIONS (G = global):'
129 )
130 table_pyts.run()
131 # Pass 4: Output list of virtual environments.
132 table_pves = tbl.SimpleTable(
133 pves,
134 headline='AVAILABLE PYTHON VIRTUAL ENVIRONMENTS (A = active):'
135 )
136 table_pves.run()
137 # Output list of project properties (if exists)
139 log.verbose(f'Done (RC = {rc}).')
140 # Go on
141 break
142 # End while
143 except:
144 log.error(sys.exc_info())
145 rc = 1
146 finally:
147 os.chdir(cwd)
148 return rc
149
150
151# --- MAIN --------------------------------------------------------------
152
str getGlobalStar(str ver)
Get the "*" marker for this version, if it is the globally selected version in "pyenv".
Definition hlp.py:375
list[str] getEnvs(str ver, str name=' *', bool as_paths=False)
Get list of installed virtual environments for a specific Python version in "pyenv".
Definition hlp.py:718
int listProjectProperties()
Display the table, which shows a list about project properties.
Definition hlp.py:866
list[str] getPythonVersions(str version=' *', bool venv_capable=False, bool as_paths=False)
Get list of installed Python version directories in "pyenv".
Definition hlp.py:500
str getColoredVenvCapability(str ver)
Get the colored virtual environment capability str for the specific version number or path.
Definition hlp.py:481
verbose((str, tuple) msg)
Log verbose message colored to console only.
Definition log.py:209
info((str, tuple) msg)
Log info message colored to console only.
Definition log.py:203
error((str, tuple) msg)
Log error message colored to console only.
Definition log.py:179

◆ parseCliArguments()

tuple[(argparse.Namespace, None), int] pyenv-virtualenvs.parseCliArguments ( )

Parse CLI arguments for this application.



Implement this as required, but don't touch the interface definition for input and output.

Returns
A tuple of:
  • Namespace to read arguments in "dot" notation or None in case of help or error.
  • RC = 0 or another value in case of error.

Definition at line 162 of file pyenv-virtualenvs.py.

162def parseCliArguments() -> tuple[(argparse.Namespace, None), int]:
163 rc: int = 0
164 # noinspection PyBroadException
165 try:
166 parser = argparse.ArgumentParser(
167# --- BEGIN CHANGE -----------------------------------------------------
168 prog='pyenv virtualenvs',
169 description='Output lists of Python versions, virtual environments and related project properties.'
170 )
171# --- END CHANGE -------------------------------------------------------
172 return parser.parse_args(), rc
173 except SystemExit:
174 return None, 0 # -h, --help
175 except:
176 log.error(sys.exc_info())
177 return None, 1
178

◆ main()

int pyenv-virtualenvs.main ( )

Main routine of the application.

Returns
RC = 0 or other values in case of error.

Definition at line 182 of file pyenv-virtualenvs.py.

182def main() -> int:
183 # noinspection PyBroadException
184 try:
185 while True:
186 # Audit the operating system platform
187 rc = hlp.auditPlatform('Windows')
188 if rc != 0:
189 # Deviation: Reject unsupported platform
190 break
191 # Audit the global Python version number
193 if rc != 0:
194 # Deviation: Reject unsupported Python version
195 break
196 # Initialize the colored logging to console
198 # Audit the "pyenv" version number
199 rc = hlp.auditPyEnv('3')
200 if rc != 0:
201 # Deviation: Reject unsupported "pyenv" version
202 break
203 # Parse arguments
204 log.verbose('Parsing arguments ...')
205 args, rc = parseCliArguments()
206 if rc != 0:
207 break
208 if args is None: # -h, --help
209 break
210 # Run this application
211 log.verbose('Running application ...')
212 rc = run()
213 if rc != 0:
214 break
215 # Go on
216 break
217 # End while
218 except Exception as exc:
220 log.error(sys.exc_info())
221 else:
222 print(
223 '\x1b[91mERROR: Unexpected error "%s".\x1b[0m'
224 %
225 str(exc)
226 )
227 rc = 1
228 return rc
229
230
int auditPyEnv(str min_ver)
Check if "pyenv" version is greater or equal the given minimal version.
Definition hlp.py:213
int auditGlobalPythonVersion(str min_ver)
Check if Python version is greater or equal the given minimal version.
Definition hlp.py:148
int auditPlatform(str name)
Check if the program in running on the required platform.
Definition hlp.py:85
initLogging()
Initialize the logging.
Definition log.py:71
bool isInitialized()
Definition log.py:90