Skip to content

Commit 8b0b75b

Browse files
committed
merge in hook listing and pep8 fixes
2 parents 504b113 + f71c435 commit 8b0b75b

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

docs/en/history.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ dev
1313
- Fix the test script and runner so the user's ``$WORKON_HOME`` is
1414
not erased if they do not have some test shells installed.
1515
(big thanks to :bbuser:`agriffis`).
16+
- If the hook loader is told to list plugins but is not given a hook
17+
name, it prints the list of core hooks.
1618

1719
3.2
1820

virtualenvwrapper.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ function mkproject {
897897

898898
cd "$PROJECT_HOME"
899899

900-
virtualenvwrapper_run_hook project.pre_mkproject $envname
900+
virtualenvwrapper_run_hook "project.pre_mkproject" $envname
901901

902902
echo "Creating $PROJECT_HOME/$envname"
903903
mkdir -p "$PROJECT_HOME/$envname"
@@ -912,10 +912,10 @@ function mkproject {
912912
# For some reason zsh insists on prefixing the template
913913
# names with a space, so strip them out before passing
914914
# the value to the hook loader.
915-
virtualenvwrapper_run_hook --name $(echo $t | sed 's/^ //') project.template $envname
915+
virtualenvwrapper_run_hook --name $(echo $t | sed 's/^ //') "project.template" $envname
916916
done
917917

918-
virtualenvwrapper_run_hook project.post_mkproject
918+
virtualenvwrapper_run_hook "project.post_mkproject"
919919
}
920920

921921
# Change directory to the active project

virtualenvwrapper/hook_loader.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import inspect
9+
import itertools
910
import logging
1011
import logging.handlers
1112
import optparse
@@ -14,6 +15,7 @@
1415

1516
import pkg_resources
1617

18+
1719
class GroupWriteRotatingFileHandler(logging.handlers.RotatingFileHandler):
1820
"""Taken from http://stackoverflow.com/questions/1407474/does-python-logging-handlers-rotatingfilehandler-allow-creation-of-a-group-writa
1921
"""
@@ -23,6 +25,7 @@ def _open(self):
2325
os.umask(prevumask)
2426
return rtv
2527

28+
2629
def main():
2730
parser = optparse.OptionParser(
2831
usage='usage: %prog [options] <hook> [<arguments>]',
@@ -67,7 +70,7 @@ def main():
6770
dest='names',
6871
default=[],
6972
)
70-
parser.disable_interspersed_args() # stop when we hit an option without an '-'
73+
parser.disable_interspersed_args() # stop when we hit an option without an '-'
7174
options, args = parser.parse_args()
7275

7376
root_logger = logging.getLogger('')
@@ -85,10 +88,10 @@ def main():
8588

8689
# Send higher-level messages to the console, too
8790
console = logging.StreamHandler()
88-
console_level = [ logging.WARNING,
89-
logging.INFO,
90-
logging.DEBUG,
91-
][options.verbose_level]
91+
console_level = [logging.WARNING,
92+
logging.INFO,
93+
logging.DEBUG,
94+
][options.verbose_level]
9295
console.setLevel(console_level)
9396
formatter = logging.Formatter('%(name)s %(message)s')
9497
console.setFormatter(formatter)
@@ -98,7 +101,11 @@ def main():
98101

99102
# Determine which hook we're running
100103
if not args:
101-
parser.error('Please specify the hook to run')
104+
if options.listing:
105+
list_hooks()
106+
return 0
107+
else:
108+
parser.error('Please specify the hook to run')
102109
hook = args[0]
103110

104111
if options.sourcing and options.script_filename:
@@ -126,6 +133,7 @@ def main():
126133

127134
return 0
128135

136+
129137
def run_hooks(hook, options, args, output=None):
130138
if output is None:
131139
output = sys.stdout
@@ -135,7 +143,7 @@ def run_hooks(hook, options, args, output=None):
135143
continue
136144
plugin = ep.load()
137145
if options.listing:
138-
sys.stdout.write(' %-10s -- %s\n' % (ep.name, inspect.getdoc(plugin) or ''))
146+
output.write(' %-10s -- %s\n' % (ep.name, inspect.getdoc(plugin) or ''))
139147
continue
140148
if options.sourcing:
141149
# Show the shell commands so they can
@@ -149,5 +157,29 @@ def run_hooks(hook, options, args, output=None):
149157
# Just run the plugin ourselves
150158
plugin(args[1:])
151159

160+
161+
def list_hooks(output=None):
162+
if output is None:
163+
output = sys.stdout
164+
for hook in itertools.chain(
165+
('_'.join(h)
166+
for h in itertools.product(['pre', 'post'],
167+
['mkvirtualenv',
168+
'rmvirtualenv',
169+
'activate',
170+
'deactivate',
171+
'cpvirtualenv',
172+
])
173+
),
174+
['initialize',
175+
'get_env_details',
176+
'project.pre_mkproject',
177+
'project.post_mkproject',
178+
'project.template',
179+
]
180+
):
181+
output.write(hook + '\n')
182+
183+
152184
if __name__ == '__main__':
153185
main()

0 commit comments

Comments
 (0)