Skip to content

Commit

Permalink
Refactor scripts code (#835)
Browse files Browse the repository at this point in the history
* Refactor scripts code

* Add lint requirements to script requirements

* fix language
  • Loading branch information
raman325 authored Dec 11, 2023
1 parent 2397f80 commit 911095e
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 195 deletions.
1 change: 1 addition & 0 deletions requirements_scripts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ python-slugify==8.0.1
types-python-slugify==8.0.0.3
types-requests==2.31.0.10
-r requirements.txt
-r requirements_lint.txt
19 changes: 19 additions & 0 deletions scripts/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Constants for scripts package."""
AUTO_GEN_POST = [
"",
"# ----------------------------------------------------------------------------------- #",
"# **END OF AUTOGENERATED CONTENT** (DO NOT EDIT/REMOVE THIS COMMENT BLOCK AND DO NOT #",
"# EDIT ANYTHING ABOVE IT. IF A NEW IMPORT IS NEEDED, ADD IT TO THE IMPORTS IN THE #",
"# CORRESPONDING GENERATION SCRIPT THEN RE-RUN THE SCRIPT. LINES WRITTEN BELOW THIS #",
"# BLOCK WILL BE PRESERVED AS LONG AS THIS BLOCK REMAINS) #",
"# ----------------------------------------------------------------------------------- #",
"",
]
AUTO_GEN_PRE = [
"",
"# ----------------------------------------------------------------------------------- #",
"# **BEGINNING OF AUTOGENERATED CONTENT** (TO ADD ADDITIONAL MANUAL CONTENT, LOOK FOR #",
'# THE "END OF AUTOGENERATED CONTENT" COMMENT BLOCK AND ADD YOUR CODE BELOW IT) #',
"# ----------------------------------------------------------------------------------- #",
"",
]
112 changes: 16 additions & 96 deletions scripts/generate_multilevel_sensor_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
from collections.abc import Callable, Mapping
import json
import pathlib
import re
import subprocess
import sys

from const import AUTO_GEN_POST, AUTO_GEN_PRE
from helpers import (
enum_name_format,
format_for_class_name,
get_manually_written_code,
normalize_name,
remove_comments,
run_black,
)
import requests
from slugify import slugify

GITHUB_PROJECT = "zwave-js/node-zwave-js"
BRANCH_NAME = "master"
Expand All @@ -24,35 +29,6 @@
)


def remove_comments(text: str) -> str:
"""Remove comments from a JSON string."""
return "\n".join(
line for line in text.split("\n") if not line.strip().startswith("//")
)


def remove_parenthesis(text: str) -> str:
"""Remove text in parenthesis from a string."""
return re.sub(r"\([^)]*\)", "", text)


def enum_name_format(name: str, should_remove_parenthesis: bool) -> str:
"""Convert sensor/scale name to enum format."""
if should_remove_parenthesis:
name = remove_parenthesis(name)
return slugify(name, separator="_").upper()


def normalize_name(name: str) -> str:
"""Convert a sensor/scale name into a normalized name."""
return enum_name_format(name, True).replace("_", " ").title()


def format_for_class_name(name: str) -> str:
"""Convert sensor/scale name to class name format."""
return f"{normalize_name(name).replace(' ', '')}Scale"


def normalize_scale_definition(scale_definitions: dict[str, dict]) -> dict[str, int]:
"""Convert a scales definition dictionary into a normalized dictionary."""
scale_def_ = {}
Expand Down Expand Up @@ -153,12 +129,7 @@ def generate_int_enum_base_class(class_name: str, docstring: str) -> list[str]:

lines = [
'"""Constants for the Multilevel Sensor CC."""',
"",
"# ----------------------------------------------------------------------------------- #",
"# **BEGINNING OF AUTOGENERATED CONTENT** (TO ADD ADDITIONAL MANUAL CONTENT, LOOK FOR #",
'# THE "END OF AUTOGENERATED CONTENT" COMMENT BLOCK AND ADD YOUR CODE BELOW IT) #',
"# ----------------------------------------------------------------------------------- #",
"",
*AUTO_GEN_PRE,
"from __future__ import annotations",
"",
"from enum import IntEnum",
Expand Down Expand Up @@ -191,7 +162,7 @@ def generate_int_enum_base_class(class_name: str, docstring: str) -> list[str]:
for scale_name, scale_dict in scales.items():
lines.extend(
generate_int_enum_class_definition(
format_for_class_name(scale_name),
format_for_class_name(scale_name, "Scale"),
scale_dict,
SENSOR_TYPE_URL,
docstring_info=f"scales for {scale_name}",
Expand All @@ -200,7 +171,7 @@ def generate_int_enum_base_class(class_name: str, docstring: str) -> list[str]:
)
for unit_name in scale_dict.keys():
_unit_name_to_enum_map[unit_name].append(
f"{format_for_class_name(scale_name)}.{unit_name}"
f"{format_for_class_name(scale_name, 'Scale')}.{unit_name}"
)
unit_name_to_enum_map = dict(
sorted(_unit_name_to_enum_map.items(), key=lambda kv: kv[0])
Expand All @@ -216,7 +187,7 @@ def generate_int_enum_base_class(class_name: str, docstring: str) -> list[str]:
for sensor_name, sensor_def in sensors.items():
multilevel_sensor_type_to_scale_map_line += (
f" MultilevelSensorType.{sensor_name}: "
f"{format_for_class_name(sensor_def['scale'])},"
f"{format_for_class_name(sensor_def['scale'], 'Scale')},"
)
multilevel_sensor_type_to_scale_map_line += "}"
lines.append(multilevel_sensor_type_to_scale_map_line)
Expand All @@ -227,59 +198,8 @@ def generate_int_enum_base_class(class_name: str, docstring: str) -> list[str]:
f"UNIT_{unit_name}: list[MultilevelSensorScaleType] = [{','.join(sorted(unit_enums))}]"
)

lines.extend(
[
"",
"# ----------------------------------------------------------------------------------- #",
"# **END OF AUTOGENERATED CONTENT** (DO NOT EDIT/REMOVE THIS COMMENT BLOCK AND DO NOT #",
"# EDIT ANYTHING ABOVE IT. IF A NEW IMPORT IS NEEDED, UPDATE THE LINES AROUND 135 #",
"# IN scripts/generate_multilevel_sensor_constants.py THEN RE-RUN THE SCRIPT. ALL #",
"# LINES WRITTEN BELOW THIS BLOCK WILL BE PRESERVED AS LONG AS THIS BLOCK REMAINS) #",
"# ----------------------------------------------------------------------------------- #",
"",
]
)

existing_const_file = CONST_FILE_PATH.read_text(encoding="utf-8").splitlines()

manually_written_code_start_idx = (
next(
i
for i, line in enumerate(existing_const_file)
if "**END OF AUTOGENERATED CONTENT**" in line
)
+ 6
)
if len(existing_const_file) > manually_written_code_start_idx:
lines.extend(
[
line.strip("\n")
for line in existing_const_file[manually_written_code_start_idx:]
]
)

lines.extend(AUTO_GEN_POST)
lines.extend(get_manually_written_code(CONST_FILE_PATH))
CONST_FILE_PATH.write_text("\n".join(lines), encoding="utf-8")

if subprocess.run(["which", "black"], capture_output=True, check=True).stdout:
subprocess.run(
["black", CONST_FILE_PATH],
check=True,
)
else:
print("Could not run black on new file, please run it to properly format it.")

if subprocess.run(["which", "git"], capture_output=True, check=True).stdout:
if (
subprocess.run(
["git", "diff", "--stat"],
check=True,
).stdout
is not None
):
print("Repo is dirty and needs to be committed!")
sys.exit(1)
else:
print(
"Could not run `git diff --stat` on repo, please run it to determine whether "
"constants have changed."
)
run_black(CONST_FILE_PATH)
105 changes: 12 additions & 93 deletions scripts/generate_notification_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
from collections.abc import Callable, Mapping
import json
import pathlib
import re
import subprocess
import sys

from const import AUTO_GEN_POST, AUTO_GEN_PRE
from helpers import (
enum_name_format,
format_for_class_name,
get_manually_written_code,
remove_comments,
run_black,
)
import requests
from slugify import slugify

GITHUB_PROJECT = "zwave-js/node-zwave-js"
BRANCH_NAME = "master"
Expand All @@ -22,35 +26,6 @@
)


def remove_comments(text: str) -> str:
"""Remove comments from a JSON string."""
return "\n".join(
line for line in text.split("\n") if not line.strip().startswith("//")
)


def remove_parenthesis(text: str) -> str:
"""Remove text in parenthesis from a string."""
return re.sub(r"\([^)]*\)", "", text)


def enum_name_format(name: str, should_remove_parenthesis: bool) -> str:
"""Convert sensor/scale name to enum format."""
if should_remove_parenthesis:
name = remove_parenthesis(name)
return slugify(name, separator="_").upper()


def normalize_name(name: str) -> str:
"""Convert a sensor/scale name into a normalized name."""
return enum_name_format(name, True).replace("_", " ").title()


def format_for_class_name(name: str) -> str:
"""Convert sensor/scale name to class name format."""
return normalize_name(name).replace(" ", "")


notifications_file = json.loads(
remove_comments(
requests.get(
Expand Down Expand Up @@ -149,12 +124,7 @@ def generate_int_enum_base_class(class_name: str, docstring: str) -> list[str]:
lines = [
"# pylint: disable=line-too-long",
'"""Constants for the Notification CC."""',
"",
"# ----------------------------------------------------------------------------------- #",
"# **BEGINNING OF AUTOGENERATED CONTENT** (TO ADD ADDITIONAL MANUAL CONTENT, LOOK FOR #",
'# THE "END OF AUTOGENERATED CONTENT" COMMENT BLOCK AND ADD YOUR CODE BELOW IT) #',
"# ----------------------------------------------------------------------------------- #",
"",
*AUTO_GEN_PRE,
"from __future__ import annotations",
"",
"from enum import IntEnum",
Expand Down Expand Up @@ -254,59 +224,8 @@ def generate_int_enum_base_class(class_name: str, docstring: str) -> list[str]:
lines.append(notification_event_to_event_value_map_line)
lines.append("")

lines.extend(
[
"",
"# ----------------------------------------------------------------------------------- #",
"# **END OF AUTOGENERATED CONTENT** (DO NOT EDIT/REMOVE THIS COMMENT BLOCK AND DO NOT #",
"# EDIT ANYTHING ABOVE IT. IF A NEW IMPORT IS NEEDED, UPDATE THE LINES AROUND 135 #",
"# IN scripts/generate_multilevel_sensor_constants.py THEN RE-RUN THE SCRIPT. ALL #",
"# LINES WRITTEN BELOW THIS BLOCK WILL BE PRESERVED AS LONG AS THIS BLOCK REMAINS) #",
"# ----------------------------------------------------------------------------------- #",
"",
]
)

existing_const_file = CONST_FILE_PATH.read_text(encoding="utf-8").splitlines()

manually_written_code_start_idx = (
next(
i
for i, line in enumerate(existing_const_file)
if "**END OF AUTOGENERATED CONTENT**" in line
)
+ 6
)
if len(existing_const_file) > manually_written_code_start_idx:
lines.extend(
[
line.strip("\n")
for line in existing_const_file[manually_written_code_start_idx:]
]
)

lines.extend(AUTO_GEN_POST)
lines.extend(get_manually_written_code(CONST_FILE_PATH))
CONST_FILE_PATH.write_text("\n".join(lines), encoding="utf-8")

if subprocess.run(["which", "black"], capture_output=True, check=True).stdout:
subprocess.run(
["black", CONST_FILE_PATH],
check=True,
)
else:
print("Could not run black on new file, please run it to properly format it.")

if subprocess.run(["which", "git"], capture_output=True, check=True).stdout:
if (
subprocess.run(
["git", "diff", "--stat"],
check=True,
).stdout
is not None
):
print("Repo is dirty and needs to be committed!")
sys.exit(1)
else:
print(
"Could not run `git diff --stat` on repo, please run it to determine whether "
"constants have changed."
)
run_black(CONST_FILE_PATH)
Loading

0 comments on commit 911095e

Please sign in to comment.