Skip to content

Commit

Permalink
Fix incorrect spaces for ||, &&, and != operators
Browse files Browse the repository at this point in the history
  • Loading branch information
klaashoekstra94 committed Feb 9, 2024
1 parent bea05bd commit 7f72707
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/heishamon_rules_minify/minifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ def minify(cls, input_text):
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)
text = re.sub(r"(?<=end)\s+(?!\s*on |\Z)", " ", 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"(?<!timer)( *(==|!=|>=|<=|\|\||&&|[=+\*\/<>^]) *)(?=-?\d|\b|[(#$@%?].+(;|then|end))", r" \g<2> ", text, flags=re.MULTILINE)
text = re.sub(r"(?<!=)(?<!= ) *- *(?=-*\d|[(#$@%?].+(;|then|end))", " - ", 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
88 changes: 67 additions & 21 deletions tests/test_minifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ def test_minifier():
on System#Boot then
-- No problem to use long descriptive variable names
#HeatingWaterSupplyTemperatureSetpoint = 1;
#allowSetQuietMode = 0;
#quietModeHelper = 1;
#quietModePrevious = -1;
setTimer(3, 60); -- Set timer 3 to trigger after 60s
end
------------------------------- Custom functions -------------------------------
-- Also no problem to use long descriptive function names
on CalculateWeatherDependentControl then
-- Use comments to explain what the function should do
Expand All @@ -29,22 +33,54 @@ def test_minifier():
if @Outside_Temp >= $OutsideTemperatureWarmWeather then
#HeatingWaterSupplyTemperatureSetpoint = $WaterTemperatureWarmWeather;
elseif @Outside_Temp <= $OutsideTemperatureColdWeather then
#HeatingWaterSupplyTemperatureSetpoint = $WaterTemperatureColdWeather;
else
#HeatingWaterSupplyTemperatureSetpoint =
$WaterTemperatureWarmWeather + -- Splitting a calculation over multiple lines
(($OutsideTemperatureWarmWeather - @Outside_Temp) *
-- Put comment halfway a multiline calculation
($WaterTemperatureColdWeather - $WaterTemperatureWarmWeather) /
($OutsideTemperatureWarmWeather - $OutsideTemperatureColdWeather));
if @Outside_Temp <= $OutsideTemperatureColdWeather then
#HeatingWaterSupplyTemperatureSetpoint = $WaterTemperatureColdWeather;
else
#HeatingWaterSupplyTemperatureSetpoint =
$WaterTemperatureWarmWeather + -- Splitting a calculation over multiple lines
(($OutsideTemperatureWarmWeather - @Outside_Temp) *
-- Put comment halfway a multiline calculation
($WaterTemperatureColdWeather - $WaterTemperatureWarmWeather) /
($OutsideTemperatureWarmWeather - $OutsideTemperatureColdWeather));
end
end
end
on setQuietMode then
if #allowSetQuietMode == 1 then
if isset(@Outside_Temp) && isset(@Heatpump_State) then
if #quietModeHelper == 1 then
if @Outside_Temp < 13 then
#quietMode = 1;
else
#quietMode = 2;
end
if @Outside_Temp < 8 then
#quietMode = 0;
end
if @Outside_Temp < 2 then
if %hour > 22 || %hour < 7 then
#quietMode = 1;
else
#quietMode = 0;
end
end
if #quietModePrevious != #quietMode && @Heatpump_State == 1 then
setTimer(2, 900);
#quietModeHelper = 0;
#quietModePrevious = #quietMode;
@SetQuietMode = #quietMode;
end
end
end
end
end
------------------------------ Thermostat triggers -----------------------------
on ?roomTemp then
-- Calculate WAR when room temperature changes
CalculateWeatherDependentControl();
--CalculateWeatherDependentControl();
$margin = 0.25;
$setpoint = ?roomTempSet;
Expand All @@ -58,16 +94,24 @@ def test_minifier():
if @Heatpump_State == 1 then
@SetHeatpump = 0;
end
elseif ?roomTemp < ($setpoint - $margin) then
if @Heatpump_State == 0 then
@SetHeatpump = 1;
end
else
@SetZ1HeatRequestTemperature = round(#HeatingWaterSupplyTemperatureSetpoint);
if ?roomTemp < ($setpoint - $margin) then
if @Heatpump_State == 0 then
@SetHeatpump = 1;
end
else
@SetZ1HeatRequestTemperature = round(#HeatingWaterSupplyTemperatureSetpoint);
end
end
end
-------------------------------- Timer functions -------------------------------
on timer=2 then
#quietModeHelper = 1;
#quietMode = 0;
end
on timer=3 then
-- Similar variable names are each minified uniquely
$somevalue = 0;
Expand All @@ -79,10 +123,12 @@ def test_minifier():
end
"""

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 ?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
expected_output = """on System#Boot then #HWSTS = 1;#ASQM = 0;#QMH = 1;#QMP = -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;else if @Outside_Temp <= $OTCW then #HWSTS = $WTCW;else #HWSTS = $WTWW + (($OTWW - @Outside_Temp) * ($WTCW - $WTWW) / ($OTWW - $OTCW));end end end
on SQM then if #ASQM == 1 then if isset(@Outside_Temp) && isset(@Heatpump_State) then if #QMH == 1 then if @Outside_Temp < 13 then #QM = 1;else #QM = 2;end if @Outside_Temp < 8 then #QM = 0;end if @Outside_Temp < 2 then if %hour > 22 || %hour < 7 then #QM = 1;else #QM = 0;end end if #QMP != #QM && @Heatpump_State == 1 then setTimer(2,900);#QMH = 0;#QMP = #QM;@SetQuietMode = #QM;end end end end end
on ?roomTemp then $M = 0.25;$S = ?roomTempSet;if ?roomTemp > ($S + $M) then if @Heatpump_State == 1 then @SetHeatpump = 0;end else if ?roomTemp < ($S - $M) then if @Heatpump_State == 0 then @SetHeatpump = 1;end else @SetZ1HeatRequestTemperature = round(#HWSTS);end end end
on timer=2 then #QMH = 1;#QM = 0;end
on timer=3 then $S1 = 0;$SV = 1;$SV1 = 2;$SV3 = 3;$SV2 = 4;setTimer(3,60);end
"""
output = Minifier.minify(input_text)
assert output == expected_output

0 comments on commit 7f72707

Please sign in to comment.