Skip to content

Commit

Permalink
Improve some minify regexes, test with the rule library of 3.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
klaashoekstra94 committed Feb 8, 2024
1 parent d8b621d commit f53644e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

This project aims to overcome these issues by making it possible write a ruleset with comments and descriptive function and variables names, and then minifying it. This way it is still possible to maximize the number of rules on the HeishaMon.

_Note: The current minifier has been written to work with [this updated rule libaray version](https://github.com/IgorYbema/HeishaMon/pull/121)_
_Note: The minifier should work for both the rule library used in version 3.2.3 and [this updated rule libaray version](https://github.com/IgorYbema/HeishaMon/pull/121). In the rule library from version 3.2.3, the elseif statement cannot be used_

## Installation

Expand Down
15 changes: 8 additions & 7 deletions src/heishamon_rules_minify/minifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,20 @@ def minify(cls, input_text):
text = re.sub(r"^[\t| ]+|[\t| ]+$", "", text, flags=re.MULTILINE)

# Remove newline after line ending with 'then' or 'else'
text = re.sub(r"(?<=then|else)\r{0,1}\n{1}", " ", text, flags=re.MULTILINE)
text = re.sub(r"(?<=then|else)\r?\n", " ", text, flags=re.MULTILINE)

# Remove newline after line ending with all possible operators
text = re.sub(r"(?<=[;=&|<>\-+%*\/^()])\r{0,1}\n{1}", "", text, flags=re.MULTILINE)
text = re.sub(r"(?<=[;=&|<>\-+%*\/^()])\r?\n", "", text, flags=re.MULTILINE)

# Remove extra spaces that are present after removing newlines
text = re.sub(r"(?<=[;=&|<>\-+%*\/^()]) *(?=\S)(?!then)", "", text, flags=re.MULTILINE)

# Remove newline after line ending with 'end', except for last end of function
text = re.sub(r"(?<=end)\s*(?!\s*on |\Z)", " ", text, flags=re.MULTILINE)

# Correct spaces around equal signs
text = re.sub(r"(?<![<>]) *= *(?=-*\d;|[#$@%?].+;)", " = ", text, flags=re.MULTILINE)

# Correct spaces around double equal signs
text = re.sub(r" *== *", " == ", text, flags=re.MULTILINE)
# Correct spaces around operators and functions
text = re.sub(r"(?<!timer)( *(==|>=|<=|[=+\*\/%&|<>^]) *)(?=-?\d|\b|[(#$@%?].+(;|then|end))", r" \g<2> ", text, flags=re.MULTILINE)
text = re.sub(r"(?<!=)(?<!= ) *- *(?=-*\d|[(#$@%?].+(;|then|end))", " - ", text, flags=re.MULTILINE)

# Remove all spaces around comma signs
text = re.sub(r"(?<=[)_A-Za-z0-9]) *, *\s*(?=[$#@?_A-Za-z0-9])", ",", text, flags=re.MULTILINE)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_minifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_minifier():
"""

expected_output = """on System#Boot then #HWSTS = 1;setTimer(3,60);end
on CWDC then $WTWW = 32;$OTWW = 14;$WTCW = 41;$OTCW = -4;#HWSTS = $WTWW;if @Outside_Temp >= $OTWW then #HWSTS = $WTWW;elseif @Outside_Temp <= $OTCW then #HWSTS = $WTCW;else #HWSTS = $WTWW +(($OTWW - @Outside_Temp) *($WTCW - $WTWW) /($OTWW - $OTCW));end end
on CWDC then $WTWW = 32;$OTWW = 14;$WTCW = 41;$OTCW = -4;#HWSTS = $WTWW;if @Outside_Temp >= $OTWW then #HWSTS = $WTWW;elseif @Outside_Temp <= $OTCW then #HWSTS = $WTCW;else #HWSTS = $WTWW + (($OTWW - @Outside_Temp) * ($WTCW - $WTWW) / ($OTWW - $OTCW));end end
on ?roomTemp then CWDC();$M = 0.25;$S = ?roomTempSet;if ?roomTemp > ($S + $M) then if @Heatpump_State == 1 then @SetHeatpump = 0;end elseif ?roomTemp < ($S - $M) then if @Heatpump_State == 0 then @SetHeatpump = 1;end else @SetZ1HeatRequestTemperature = round(#HWSTS);end end
on timer=3 then $S1 = 0;$SV = 1;$SV1 = 2;$SV3 = 3;$SV2 = 4;setTimer(3,60);end
"""
Expand Down

0 comments on commit f53644e

Please sign in to comment.