@@ -16,8 +16,9 @@ Defining an Extension
16
16
.. note ::
17
17
18
18
Virtualenvwrapper is delivered with a plugin for creating and
19
- running the user customization scripts. The examples below are
20
- taken from the implementation of that plugin.
19
+ running the user customization scripts
20
+ (:ref: `extensions-user_scripts `). The examples below are taken from
21
+ the implementation of that plugin.
21
22
22
23
Code Organization
23
24
-----------------
@@ -40,8 +41,10 @@ And placing the following code in the ``__init__.py``::
40
41
41
42
__import__('pkg_resources').declare_namespace(__name__)
42
43
43
- This isn't required, so if you would prefer to use a different
44
- namespace that will work, too.
44
+ .. note ::
45
+
46
+ Extensions can be loaded from any package, so using the
47
+ ``virtualenvwrapper `` namespace is not required.
45
48
46
49
Extension API
47
50
-------------
@@ -54,23 +57,24 @@ imported from elsewhere using standard Python code organization
54
57
techniques.
55
58
56
59
The API is the same for every extension point. Each uses a Python
57
- function that takes a single argument, a list of string arguments
58
- passed to the hook loader on the command line. The contents of the
59
- argument list are defined for each extension point below (see
60
- :ref: `plugins-extension-points `).
60
+ function that takes a single argument, a list of strings passed to the
61
+ hook loader on the command line.
61
62
62
63
::
63
64
64
65
def function_name(args):
65
66
# args is a list of strings passed to the hook loader
66
67
68
+ The contents of the argument list are defined for each extension point
69
+ below (see :ref: `plugins-extension-points `).
70
+
67
71
Extension Invocation
68
72
--------------------
69
73
70
74
Plugins can attach to each hook in two different ways. The default is
71
- to have an extension function run and do some work directly. For
72
- example, the ``initialize() `` hook for the user scripts plugin makes
73
- sure the scripts exist every time ``virtualenvwrapper.sh `` is loaded.
75
+ to have a function run and do some work directly. For example, the
76
+ ``initialize() `` function for the user scripts plugin creates default
77
+ user scripts when ``virtualenvwrapper.sh `` is loaded.
74
78
75
79
::
76
80
@@ -88,10 +92,10 @@ There are cases where the extension needs to update the user's
88
92
environment (e.g., changing the current working directory or setting
89
93
environment variables). Modifications to the user environment must be
90
94
made within the user's current shell, and cannot be run in a separate
91
- process. Extensions can define hook functions to return the text of
92
- the shell statements to be executed. These * source * hooks are run
93
- after the regular hooks with the same name, and should not do any work
94
- of their own.
95
+ process. To have code run in the user's shell process, extensions can
96
+ define hook functions to return the text of the shell statements to be
97
+ executed. These * source * hooks are run after the regular hooks with
98
+ the same name, and should not do any work of their own.
95
99
96
100
The ``initialize_source() `` hook for the user scripts plugin looks for
97
101
a global initialize script and causes it to be run in the current
@@ -112,11 +116,11 @@ shell process.
112
116
Because the extension is modifying the user's working shell, care
113
117
must be taken not to corrupt the environment by overwriting
114
118
existing variable values unexpectedly. Avoid creating temporary
115
- variables where possible, and use unique names where necessary.
116
- Prefixing variables with the extension name is a good way to
117
- manage the namespace. For example, instead of `` temp_file `` use
118
- ``user_scripts_temp_file ``. Use ``unset `` to release temporary
119
- variable names when they are no longer needed.
119
+ variables where possible, and use unique names where variables
120
+ cannot be avoided. Prefixing variables with the extension name is
121
+ a good way to manage the namespace. For example, instead of
122
+ ``temp_file `` use `` user_scripts_temp_file ``. Use ``unset `` to
123
+ release temporary variable names when they are no longer needed.
120
124
121
125
.. warning ::
122
126
@@ -183,8 +187,8 @@ The Hook Loader
183
187
---------------
184
188
185
189
Extensions are run through a command line application implemented in
186
- ``virtualenvwrapper.hook_loader ``. Since ``virtualenvwrapper.sh `` is
187
- the primary caller and users do not typically need to run the app
190
+ ``virtualenvwrapper.hook_loader ``. Because ``virtualenvwrapper.sh ``
191
+ is the primary caller and users do not typically need to run the app
188
192
directly, no separate script is installed. Instead, to run the
189
193
application, use the ``-m `` option to the interpreter::
190
194
@@ -199,13 +203,21 @@ application, use the ``-m`` option to the interpreter::
199
203
-v, --verbose Show more information on the console
200
204
-q, --quiet Show less information on the console
201
205
206
+ To run the extensions for the initialize hook::
207
+
208
+ $ python -m virtualenvwrapper.hook_loader -v initialize
209
+
210
+ To get the shell commands for the initialize hook::
211
+
212
+ $ python -m virtualenvwrapper.hook_loader --source initialize
213
+
202
214
Logging
203
215
-------
204
216
205
217
The hook loader configures logging so that messages are written to
206
- ``$WORKON_HOME/hook.log ``. Messages are also written to stdout ,
207
- depending on the verbosity flag. The default is for messages at info
208
- or higher levels to be written to stdout , and debug or higher to go to
218
+ ``$WORKON_HOME/hook.log ``. Messages also may be written to stderr ,
219
+ depending on the verbosity flag. The default is for messages at * info *
220
+ or higher levels to be written to stderr , and * debug * or higher to go to
209
221
the log file. Using logging in this way provides a convenient
210
222
mechanism for users to control the verbosity of extensions.
211
223
@@ -222,19 +234,24 @@ messages.
222
234
log.debug('pre_mkvirtualenv %s', str(args))
223
235
# ...
224
236
237
+ .. seealso ::
238
+
239
+ * `Standard library documentation for logging <http://docs.python.org/library/logging.html >`__
240
+ * `PyMOTW for logging <http://www.doughellmann.com/PyMOTW/logging/ >`__
241
+
225
242
.. _plugins-extension-points :
226
243
227
244
Extension Points
228
245
================
229
246
230
- The extension point names for native plugins are different from the
231
- user customization scripts. The pattern is
232
- ``virtualenvwrapper.(pre|post)_event [_source] ``. The *event * is the
233
- action taken by the user or virtualenvwrapper that triggers the
234
- extension. ``(pre|post) `` is the prefix indicating whether to call
235
- the extension before or after the event. The suffix ``_source `` is
236
- added for extensions that return shell code instead of taking action
237
- directly (see :ref: `plugins-user-env `).
247
+ The extension point names for native plugins follow a naming
248
+ convention with several parts:
249
+ ``virtualenvwrapper.(pre|post)_<event> [_source] ``. The *< event> * is
250
+ the action taken by the user or virtualenvwrapper that triggers the
251
+ extension. ``(pre|post) `` indicates whether to call the extension
252
+ before or after the event. The suffix ``_source `` is added for
253
+ extensions that return shell code instead of taking action directly
254
+ (see :ref: `plugins-user-env `).
238
255
239
256
.. _plugins-initialize :
240
257
0 commit comments