-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: icon type auto-generation and normalization (#696)
Closes #601 Closes #784 Updated deephaven/ui README with instructions on how to run the script. Wasn't certain if it should run everytime the docker container is built?
- Loading branch information
1 parent
1807862
commit ef4bb29
Showing
6 changed files
with
2,570 additions
and
6 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,67 @@ | ||
import re | ||
from typing import Dict, Any | ||
|
||
|
||
def camel_to_snake(name: str) -> str: | ||
s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name) | ||
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower() | ||
|
||
|
||
relative_path = "./src/js/node_modules/@deephaven/icons/dist/index.d.ts" | ||
icon_pattern = r"^export const (\w+): IconDefinition;$" | ||
|
||
icons = {} | ||
snakeCase = {} | ||
noPrefix = {} | ||
snakeCaseNoPrefix = {} | ||
|
||
with open(relative_path, "r") as file: | ||
for line in file: | ||
match = re.match(icon_pattern, line) | ||
if match: | ||
icon = line.split(" ")[2].strip()[:-1] | ||
if icon != "IconDefinition": | ||
icons[icon] = icon | ||
snakeCase[camel_to_snake(icon)] = icon | ||
|
||
isVsIcon = icon.startswith("vs") | ||
noPrefixIcon = icon[2:] | ||
snakeCaseNoPrefixIcon = camel_to_snake(noPrefixIcon) | ||
|
||
## DH Icons are always prefixed with "dh" | ||
if isVsIcon: | ||
noPrefix[noPrefixIcon] = icon | ||
snakeCaseNoPrefix[snakeCaseNoPrefixIcon] = icon | ||
|
||
output_file_path = "./src/deephaven/ui/components/types/icon_types.py" | ||
|
||
with open(output_file_path, "w") as output_file: | ||
output_file.truncate(0) | ||
|
||
output_file.write( | ||
"from __future__ import annotations\n" + "from typing import Literal\n\n" | ||
) | ||
|
||
## IconTypes | ||
output_file.write("IconTypes = Literal[\n") | ||
for key, value in snakeCaseNoPrefix.items(): | ||
output_file.write(' "' + key + '",' + "\n") | ||
for key, value in snakeCase.items(): | ||
if key.startswith("dh"): | ||
output_file.write(' "' + key + '",' + "\n") | ||
output_file.write("]" + "\n") | ||
|
||
## IconMapping | ||
output_file.write("\n") | ||
output_file.write("IconMapping = {" + "\n") | ||
for dict in [icons, noPrefix, snakeCase, snakeCaseNoPrefix]: | ||
for key, value in dict.items(): | ||
output_file.write(' "' + key + '": "' + value + '",' + "\n") | ||
output_file.write("}" + "\n") | ||
|
||
|
||
print(f"Generated file: {output_file_path}") | ||
print(f"Total number of icon types: {len(snakeCaseNoPrefix)}") | ||
print( | ||
f"Total number of icon mappings: {len(icons) + len(noPrefix) + len(snakeCase) + len(snakeCaseNoPrefix)}" | ||
) |
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
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
from .events import * | ||
from .layout import * | ||
from .validate import * | ||
from .icon_types import * |
Oops, something went wrong.