Refactored how entry points work.
diff --git a/pre_commit_hooks/debug_statement_hook.py b/pre_commit_hooks/debug_statement_hook.py
index 5f16727..565740e 100644
--- a/pre_commit_hooks/debug_statement_hook.py
+++ b/pre_commit_hooks/debug_statement_hook.py
@@ -4,6 +4,8 @@
import collections
import sys
+from pre_commit_hooks.util import entry
+
DEBUG_STATEMENTS = set(['pdb', 'ipdb', 'pudb'])
@@ -43,6 +45,7 @@
return 0
+@entry
def debug_statement_hook(argv):
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to run')
@@ -55,9 +58,5 @@
return retv
-def entry():
- return debug_statement_hook(sys.argv[1:])
-
-
if __name__ == '__main__':
- sys.exit(entry())
\ No newline at end of file
+ sys.exit(debug_statement_hook())
diff --git a/pre_commit_hooks/tests_should_end_in_test.py b/pre_commit_hooks/tests_should_end_in_test.py
index 92f2031..37db03c 100644
--- a/pre_commit_hooks/tests_should_end_in_test.py
+++ b/pre_commit_hooks/tests_should_end_in_test.py
@@ -3,7 +3,10 @@
import sys
+from pre_commit_hooks.util import entry
+
+@entry
def validate_files(argv):
retcode = 0
for filename in argv:
@@ -18,9 +21,5 @@
return retcode
-def entry():
- return validate_files(sys.argv[1:])
-
-
if __name__ == '__main__':
sys.exit(entry())
diff --git a/pre_commit_hooks/trailing_whitespace_fixer.py b/pre_commit_hooks/trailing_whitespace_fixer.py
index 5038e6e..bf016af 100644
--- a/pre_commit_hooks/trailing_whitespace_fixer.py
+++ b/pre_commit_hooks/trailing_whitespace_fixer.py
@@ -3,7 +3,10 @@
import sys
from plumbum import local
+from pre_commit_hooks.util import entry
+
+@entry
def fix_trailing_whitespace(argv):
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
@@ -22,9 +25,5 @@
return 0
-def entry():
- fix_trailing_whitespace(sys.argv[1:])
-
-
if __name__ == '__main__':
- sys.exit(entry())
+ sys.exit(fix_trailing_whitespace())
diff --git a/pre_commit_hooks/util.py b/pre_commit_hooks/util.py
new file mode 100644
index 0000000..22d13c4
--- /dev/null
+++ b/pre_commit_hooks/util.py
@@ -0,0 +1,16 @@
+
+import functools
+import sys
+
+
+def entry(func):
+ """Allows a function that has `argv` as an argument to be used as a
+ commandline entry. This will make the function callable using either
+ explicitly passed argv or defaulting to sys.argv[1:]
+ """
+ @functools.wraps(func)
+ def wrapper(argv=None):
+ if argv is None:
+ argv = sys.argv[1:]
+ return func(argv)
+ return wrapper