-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module-level remove_unused_platforms option
Signed-off-by: Gary Thompson <[email protected]>
- Loading branch information
1 parent
eb46ee6
commit 54ebab7
Showing
8 changed files
with
668 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
value = 10 | ||
_PLATFORM = "linux" # Normally this is derived from sys.uname or platform. | ||
|
||
# Supported Statements that will be kept in this example | ||
if _PLATFORM == "linux": | ||
value += 1 | ||
|
||
|
||
# Supported Statements that will be removed in this example | ||
if _PLATFORM == "armchair": | ||
value += 1 | ||
|
||
# Basic if/elif can be used | ||
|
||
if _PLATFORM == "linux": | ||
value += 1 | ||
elif _PLATFORM == "armchair": | ||
value += 1 | ||
|
||
# So can else | ||
if _PLATFORM == "armchair": | ||
value += 1 | ||
else: | ||
value += 1 | ||
|
||
# Statements that are not supported by PyMinify | ||
if _PLATFORM: | ||
value += 1 | ||
|
||
if _PLATFORM in ["linux", "windows"]: | ||
value += 1 | ||
|
||
|
||
print(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
Remove Unused Platforms | ||
======================= | ||
|
||
This transform removes ``if`` blocks that do not match a platform value. This only supports | ||
module level if blocks and is configured to keep a single explicit match. | ||
|
||
The transform is disabled by default. | ||
|
||
When using the API, enable it by either passing ``remove_unused_platforms=True`` | ||
argument to the :func:`python_minifier.minify`, or by passing ``remove_unused_platforms=unused_option`` | ||
to the function where unused_option is an instance of :class:`RemoveUnusedPlatformOptions`. | ||
|
||
When using the pyminify command, enable it with ``--remove-unused-platforms`` and set the options | ||
as required. | ||
|
||
Options | ||
------- | ||
|
||
These arguments can be used with the pyminify command as shown by the following examples: | ||
|
||
``--platform-test-key=_PLATFORM`` The variable name that is testing the platform | ||
|
||
``--platform-preserve-value=linux`` The value that matches the target platform | ||
|
||
|
||
Example | ||
------- | ||
|
||
Input | ||
~~~~~ | ||
|
||
.. literalinclude:: remove_unused_platforms.py | ||
|
||
Output | ||
~~~~~~ | ||
|
||
.. literalinclude:: remove_unused_platforms.min.py | ||
:language: python |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/python_minifier/transforms/remove_unused_platform_options.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class RemoveUnusedPlatformOptions(object): | ||
""" | ||
Options for the RemoveUnusedPlatform transform | ||
This can be passed to the minify function as the remove_unused_platforms argument | ||
:param platform_test_key: The key used to indicate a platform check statement | ||
:type platform_test_key: str | ||
:param platform_preserve_value: The value of the test to keep | ||
:type platform_preserve_value: str | ||
""" | ||
|
||
platform_test_key = "_PLATFORM" | ||
platform_preserve_value = "linux" | ||
|
||
def __init__(self, platform_test_key="", platform_preserve_value=""): | ||
self.platform_test_key = platform_test_key | ||
self.platform_preserve_value = platform_preserve_value | ||
|
||
def __repr__(self): | ||
return 'RemoveUnusedPlatformOptions(platform_test_key=%s, platform_preserve_value=%s)' % (self.platform_test_key, self.platform_preserve_value) | ||
|
||
def __nonzero__(self): | ||
return any((self.platform_test_key, self.platform_preserve_value)) | ||
|
||
def __bool__(self): | ||
return self.__nonzero__() |
Oops, something went wrong.