Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for python 2.7 and cli parameters #1712

Merged
merged 2 commits into from
Oct 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ def beautify_file(file_name, opts=default_options()):
import msvcrt
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
else:
raise 'Pipe to stdin not supported on Windows with Python 2.x 64-bit.'
raise Exception('Pipe to stdin not supported on Windows with Python 2.x 64-bit.')

input_string = stream.read()

# if you pipe an empty string, that is a failure
if input_string == '':
raise MissingInputStreamError()
else:
stream = io.open(file_name, 'rt', newline='')
stream = io.open(file_name, 'rt', newline='', encoding='UTF-8')
input_string = stream.read()

return beautify(input_string, opts)
Expand Down Expand Up @@ -232,7 +232,7 @@ def main():
argv = sys.argv[1:]

try:
opts, args = getopt.getopt(argv, "f:s:c:e:o:rdEPjabkil:xhtvXnCO:w:",
opts, args = getopt.getopt(argv, "f:s:c:e:o:rdEPjab:kil:xhtvXnCO:w:m:",
['file=', 'indent-size=', 'indent-char=', 'eol=', 'outfile=', 'replace', 'disable-preserve-newlines',
'space-in-paren', 'space-in-empty-paren', 'jslint-happy', 'space-after-anon-function',
'brace-style=', 'indent-level=', 'unescape-strings',
Expand Down Expand Up @@ -272,15 +272,17 @@ def main():
js_options.indent_with_tabs = True
elif opt in ('--disable-preserve-newlines', '-d'):
js_options.preserve_newlines = False
elif opt in ('--max-preserve-newlines', '-m'):
js_options.max_preserve_newlines = int(arg)
elif opt in ('--space-in-paren', '-P'):
js_options.space_in_paren = True
elif opt in ('--space-in-empty-paren', '-E'):
js_options.space_in_empty_paren = True
elif opt in ('--jslint-happy', '-j'):
js_options.jslint_happy = True
elif opt in ('--space_after_anon_function', '-a'):
elif opt in ('--space-after-anon-function', '-a'):
js_options.space_after_anon_function = True
elif opt in ('--space_after_named_function'):
elif opt in ('--space-after-named-function'):
js_options.space_after_named_function = True
elif opt in ('--eval-code'):
js_options.eval_code = True
Expand Down Expand Up @@ -336,7 +338,7 @@ def main():
sys.version_info.major == 3 and
sys.version_info.minor <= 4):
if '**' in filepath_param:
raise 'Recursive globs not supported on Python <= 3.4.'
raise Exception('Recursive globs not supported on Python <= 3.4.')
filepaths.extend(glob.glob(filepath_param))
else:
filepaths.extend(glob.glob(filepath_param, recursive=True))
Expand Down Expand Up @@ -390,7 +392,7 @@ def main():
import msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
else:
raise 'Pipe to stdout not supported on Windows with Python 2.x 64-bit.'
raise Exception('Pipe to stdout not supported on Windows with Python 2.x 64-bit.')

stream.write(pretty)
else:
Expand All @@ -399,7 +401,7 @@ def main():

# python automatically converts newlines in text to "\r\n" when on windows
# set newline to empty to prevent this
with io.open(outfile, 'wt', newline='') as f:
with io.open(outfile, 'wt', newline='', encoding='UTF-8') as f:
print('beautified ' + outfile, file=sys.stdout)
try:
f.write(pretty)
Expand Down