Skip to content

Commit f75c6e8

Browse files
committed
Use entry point since we already use setuptools
1 parent 4ade7fd commit f75c6e8

File tree

5 files changed

+40
-54
lines changed

5 files changed

+40
-54
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
check:
2-
pep8 unify unify.py setup.py
3-
pep257 unify unify.py setup.py
2+
pep8 unify.py setup.py
3+
pep257 unify.py setup.py
44
pylint \
55
--reports=no \
66
--disable=invalid-name \
77
--rcfile=/dev/null \
88
unify.py setup.py
99
python setup.py --long-description | rst2html --strict > /dev/null
10-
scspell unify unify.py setup.py test_unify.py README.rst
10+
scspell unify.py setup.py test_unify.py README.rst
1111

1212
coverage:
1313
@rm -f .coverage

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def version():
3333
'License :: OSI Approved :: MIT License'],
3434
keywords='strings, formatter, style',
3535
py_modules=['unify'],
36-
scripts=['unify'],
36+
entry_points={
37+
'console_scripts': ['unify = unify:main']},
3738
install_requires=['untokenize'],
3839
test_suite='test_unify')

test_unify.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_diff(self):
7070
x = "abc"
7171
''') as filename:
7272
output_file = io.StringIO()
73-
unify.main(argv=['my_fake_program', filename],
73+
unify._main(argv=['my_fake_program', filename],
7474
standard_out=output_file,
7575
standard_error=None)
7676
self.assertEqual('''\
@@ -83,7 +83,7 @@ def test_diff(self):
8383
def test_diff_with_empty_file(self):
8484
with temporary_file('') as filename:
8585
output_file = io.StringIO()
86-
unify.main(argv=['my_fake_program', filename],
86+
unify._main(argv=['my_fake_program', filename],
8787
standard_out=output_file,
8888
standard_error=None)
8989
self.assertEqual(
@@ -96,7 +96,7 @@ def test_in_place(self):
9696
x = "abc"
9797
''') as filename:
9898
output_file = io.StringIO()
99-
unify.main(argv=['my_fake_program', '--in-place', filename],
99+
unify._main(argv=['my_fake_program', '--in-place', filename],
100100
standard_out=output_file,
101101
standard_error=None)
102102
with open(filename) as f:
@@ -116,7 +116,7 @@ def test_ignore_hidden_directories(self):
116116
""", directory=inner_directory):
117117

118118
output_file = io.StringIO()
119-
unify.main(argv=['my_fake_program',
119+
unify._main(argv=['my_fake_program',
120120
'--recursive',
121121
directory],
122122
standard_out=output_file,

unify

Lines changed: 0 additions & 44 deletions
This file was deleted.

unify.py

100644100755
Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright (C) 2013 Steven Myint
24
#
35
# Permission is hereby granted, free of charge, to any person obtaining
@@ -27,6 +29,8 @@
2729

2830
import io
2931
import os
32+
import signal
33+
import sys
3034
import tokenize
3135

3236
import untokenize
@@ -141,8 +145,12 @@ def format_file(filename, args, standard_out):
141145
standard_out.write('\n'.join(list(diff) + ['']))
142146

143147

144-
def main(argv, standard_out, standard_error):
145-
"""Main entry point."""
148+
def _main(argv, standard_out, standard_error):
149+
"""Return exit status.
150+
151+
0 means no error.
152+
153+
"""
146154
import argparse
147155
parser = argparse.ArgumentParser(description=__doc__, prog='unify')
148156
parser.add_argument('-i', '--in-place', action='store_true',
@@ -172,3 +180,24 @@ def main(argv, standard_out, standard_error):
172180
format_file(name, args=args, standard_out=standard_out)
173181
except IOError as exception:
174182
print(unicode(exception), file=standard_error)
183+
184+
185+
def main():
186+
"""Main entry point."""
187+
try:
188+
# Exit on broken pipe.
189+
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
190+
except AttributeError: # pragma: no cover
191+
# SIGPIPE is not available on Windows.
192+
pass
193+
194+
try:
195+
return _main(sys.argv,
196+
standard_out=sys.stdout,
197+
standard_error=sys.stderr)
198+
except KeyboardInterrupt: # pragma: no cover
199+
return 2 # pragma: no cover
200+
201+
202+
if __name__ == '__main__':
203+
sys.exit(main())

0 commit comments

Comments
 (0)