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

Keep magic comments in the minify output for shebang and encoding #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/python_minifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import ast
import re

from python_minifier.ast_compare import CompareError, compare_ast
from python_minifier.module_printer import ModulePrinter
Expand Down Expand Up @@ -90,6 +91,31 @@ def minify(

filename = filename or 'python_minifier.minify source'

output_code = ""

# Defined in https://www.python.org/dev/peps/pep-0263/#defining-the-encoding
RE_PEP263 = r'^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)'
coding_pattern = re.compile(RE_PEP263)

# If shebang or encoding is declared in the source code, such magic comments will be kept in the output
for idx, raw_line in enumerate(source.splitlines()[0:2]):
if isinstance(raw_line, str):
# Python 2
line = raw_line
else:
# Python 3
line = raw_line.decode()

if idx == 0 and line.startswith('#!'):
# Shebang comment found
# e.g. '#!/usr/bin/env python'
output_code += line + '\n'
elif coding_pattern.match(line):
# Encoding comment found
# e.g. '# -*- coding: UTF-8 -*-'
output_code += line + '\n'


# This will raise if the source file can't be parsed
module = ast.parse(source, filename)

Expand Down Expand Up @@ -128,7 +154,8 @@ def minify(
if convert_posargs_to_args:
module = remove_posargs(module)

return unparse(module)
output_code += unparse(module)
return output_code


def unparse(module):
Expand Down