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-virtualenv-props.py
Go to the documentation of this file.
1##
2# @package pyenv-virtualenv-props
3# @file pyenv-virtualenv-props.py
4# @author Michael Paul Korthals
5# @date 2025-07-10
6# @version 1.0.0
7# @copyright © 2025 Michael Paul Korthals. All rights reserved.
8# See License details in the documentation.
9#
10# Utility application to configure the project properties
11# in "pyenv-virtualenv".
12#
13
14# --- IMPORTS ----------------------------------------------------------
15
16# Python
17import argparse
18import os
19import sys
20
21# Avoid colored output problems
22os.system('')
23
24# Community
25try:
26 import virtualenv
27except ImportError():
28 print(
29 '\x1b[101mCRITICAL %s\x1b[0m'
30 %
31 'Cannot find package "%s".'
32 %
33 'virtualenv'
34 )
35 print(
36 '\x1b[37mINFO %s\x1b[0m'
37 %
38 'Install it using "pip". Then try again.')
39 import virtualenv
40
41# My
42import lib.hlp as hlp
43import lib.log as log
44
45
46# --- RUN ---------------------------------------------------------------
47
48## Sub routine to run the application.
49#
50# @param args Parsed command line arguments of this application.
51# @return RC = 0 or other values in case of error.
52def run(args: argparse.Namespace) -> int:
53 rc: int = 0
54 cwd = os.getcwd()
55 # noinspection PyBroadException
56 try:
57 while True:
58 if args.__contains__('select_set'):
59 log.verbose('Setting project properties ...')
60 rc = hlp.setProjectProperties(args.version, args.name)
61 if rc == 0:
62 log.verbose('Project properties successfully set.')
63 else:
64 log.error('Cannot set project properties.')
65 elif args.__contains__('select_unset'):
66 log.verbose('Unsetting project properties ...')
68 if rc == 0:
69 log.verbose('Project properties successfully unset.')
70 else:
71 log.error('Cannot unset project properties.')
72 elif args.__contains__('select_list'):
73 log.verbose('Showing list of project properties ...')
74 rc = hlp.listProjectProperties(show_tree=args.tree)
75 if rc == 0:
77 'Project properties list successfully shown.'
78 )
79 else:
80 log.error('Cannot show project properties.')
81 # End if
82 log.verbose('Done (RC = {}).'.format(rc))
83 # Go on
84 break
85 # End while
86 except:
87 log.error(sys.exc_info())
88 rc = 1
89 finally:
90 os.chdir(cwd)
91 return rc
92
93
94# --- MAIN --------------------------------------------------------------
95
96## Parse CLI arguments for this application.<br>
97# <br>
98# Implement this as required, but don't touch the interface definition
99# for input and output.
100#
101# @return A tuple of:
102# * Namespace to read arguments in "dot" notation or None
103# in case of help or error.
104# * RC = 0 or another value in case of error.
105def parseCliArguments() -> tuple[(argparse.Namespace, None), int]:
106 rc: int = 0
107 # noinspection PyBroadException
108 try:
109 # Create main parser
110 parser = argparse.ArgumentParser(
111# --- BEGIN CHANGE -----------------------------------------------------
112 prog='pyenv virtualenv-props',
113 description='Manage virtual environment-related project ' +
114 'properties.'
115 )
116 # Create subparsers collection
117 subparsers = parser.add_subparsers(help='subcommand help')
118 # Add 1st subparser
119 parser_set = subparsers.add_parser(
120 'set',
121 aliases=['s'],
122 description='Set project properties inside CWD = project folder.'
123 )
124 # Add positional str argument to SET subparser
125 parser_set.add_argument(
126 'version',
127 help='Number of Python version, already installed in "pyenv".'
128 )
129 # Add positional str argument to SET subparser
130 parser_set.add_argument(
131 'name',
132 help='Short name of required Python virtual environment.'
133 )
134 # Add hidden flag
135 parser_set.add_argument(
136 'select_set',
137 action='store_true',
138 help=argparse.SUPPRESS # Don't show in --help
139 )
140 # Add 2nd subparser
141 parser_unset = subparsers.add_parser(
142 'unset',
143 aliases=['u', 'unset', 'unlink', 'd', 'del', 'delete', 'r', 'rm', 'remove'],
144 description='Unset project properties inside CWD = project folder.'
145 )
146 # Add hidden flag
147 parser_unset.add_argument(
148 'select_unset',
149 action='store_true',
150 help=argparse.SUPPRESS # Don't show in --help
151 )
152 # Add 3rd subparser
153 parser_list = subparsers.add_parser(
154 'list',
155 aliases=['l', 'ls'],
156 description='Read and list local project properties.',
157 help='Read and list local project properties.'
158 )
159 # Add flag
160 parser_list.add_argument(
161 'select_list',
162 action='store_true',
163 help=argparse.SUPPRESS # Don't show in --help
164 )
165 # Add flag
166 parser_list.add_argument(
167 '-t', '--tree',
168 dest='tree',
169 action=argparse.BooleanOptionalAction,
170 help='Display project properties in local folder tree view.'
171 )
172# --- END CHANGE -------------------------------------------------------
173 return parser.parse_args(), rc
174 except SystemExit:
175 return None, 0 # -h, --help
176 except:
177 log.error(sys.exc_info())
178 return None, 1
179
180## Main routine of the application.
181#
182# @return RC = 0 or other values in case of error.
183def main() -> int:
184 # noinspection PyBroadException
185 try:
186 while True:
187 # Audit the operating system platform
188 rc = hlp.auditPlatform('Windows')
189 if rc != 0:
190 # Deviation: Reject unsupported platform
191 break
192 # Audit the global Python version number
194 if rc != 0:
195 # Deviation: Reject unsupported Python version
196 break
197 # Initialize the colored logging to console
199 # Audit the "pyenv" version number
200 rc = hlp.auditPyEnv('3')
201 if rc != 0:
202 # Deviation: Reject unsupported "pyenv" version
203 break
204 # Parse arguments
205 log.verbose('Parsing arguments ...')
206 args, rc = parseCliArguments()
207 if rc != 0:
208 break
209 if args is None: # -h, --help
210 break
211 log.debug('Arguments: {}'.format(args))
212 # Run this application
213 log.verbose('Running application ...')
214 rc = run(args)
215 if rc != 0:
216 break
217 # Go on
218 break
219 # End while
220 except Exception as exc:
222 log.error(sys.exc_info())
223 else:
224 print(
225 '\x1b[91mERROR: Unexpected error "%s".\x1b[0m'
226 %
227 str(exc)
228 )
229 rc = 1
230 return rc
231
232
233if __name__ == "__main__":
234 sys.exit(main())
235
236# --- END OF CODE ------------------------------------------------------
237
int listProjectProperties(bool show_tree=False)
Display the table, which shows a list about project properties.
Definition hlp.py:868
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
int unsetProjectProperties()
Set project property files.
Definition hlp.py:831
int setProjectProperties(str ver, str env)
Set/override project property files.
Definition hlp.py:611
debug((str, tuple) msg)
Log debug message colored to console only.
Definition log.py:215
verbose((str, tuple) msg)
Log verbose message colored to console only.
Definition log.py:209
initLogging()
Initialize the logging.
Definition log.py:71
bool isInitialized()
Definition log.py:90
error((str, tuple) msg)
Log error message colored to console only.
Definition log.py:179
tuple[(argparse.Namespace, None), int] parseCliArguments()
Parse CLI arguments for this application.
int run(argparse.Namespace args)
Sub routine to run the application.
int main()
Main routine of the application.