Skip to content

Commit fd249ad

Browse files
committed
refactor: replace normalize_identifier with _normalize_prop and remove utils.py
1 parent e91cf74 commit fd249ad

File tree

4 files changed

+21
-59
lines changed

4 files changed

+21
-59
lines changed

api/python/slint/slint/codegen/emitters.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import libcst as cst
1010

1111
from .models import GenerationConfig, ModuleArtifacts, CallbackMeta
12-
from .utils import normalize_identifier, path_literal
12+
from .. import _normalize_prop
1313

1414

1515
def module_relative_path_expr(module_dir: Path, target: Path) -> str:
1616
try:
1717
rel = os.path.relpath(target, module_dir)
1818
except ValueError:
19-
return f"Path({path_literal(target)})"
19+
return repr(target)
2020

2121
if rel in (".", ""):
2222
return "_MODULE_DIR"
@@ -50,14 +50,14 @@ def _stmt(code: str) -> cst.BaseStatement:
5050
include_expr_code = f"[{', '.join(include_exprs)}]" if include_exprs else "None"
5151

5252
library_items = [
53-
f"{path_literal(name)}: {module_relative_path_expr(module_dir, lib_path)}"
53+
f"{repr(Path(name))}: {module_relative_path_expr(module_dir, lib_path)}"
5454
for name, lib_path in config.library_paths.items()
5555
]
5656
library_expr_code = f"{{{', '.join(library_items)}}}" if library_items else "None"
5757

58-
style_expr = path_literal(config.style) if config.style else "None"
58+
style_expr = repr(Path(config.style)) if config.style else "None"
5959
domain_expr = (
60-
path_literal(config.translation_domain) if config.translation_domain else "None"
60+
repr(Path(config.translation_domain)) if config.translation_domain else "None"
6161
)
6262

6363
export_bindings: dict[str, str] = {}
@@ -69,7 +69,7 @@ def _stmt(code: str) -> cst.BaseStatement:
6969
export_bindings[enum.name] = enum.py_name
7070

7171
export_items = list(export_bindings.values()) + [
72-
normalize_identifier(alias) for _, alias in artifacts.named_exports
72+
_normalize_prop(alias) for _, alias in artifacts.named_exports
7373
]
7474

7575
header: list[cst.CSTNode] = [
@@ -152,8 +152,8 @@ def _stmt(code: str) -> cst.BaseStatement:
152152
for original, binding in export_bindings.items():
153153
body.append(_stmt(f"{binding} = _module.{original}"))
154154
for orig, alias in artifacts.named_exports:
155-
alias_name = normalize_identifier(alias)
156-
target = export_bindings.get(orig, normalize_identifier(orig))
155+
alias_name = _normalize_prop(alias)
156+
target = export_bindings.get(orig, _normalize_prop(orig))
157157
body.append(_stmt(f"{alias_name} = {target}"))
158158

159159
module = cst.Module(body=header + body) # type: ignore[arg-type]
@@ -187,7 +187,7 @@ def register_type(type_str: str) -> None:
187187
export_names += [struct.py_name for struct in artifacts.structs]
188188
export_names += [enum.py_name for enum in artifacts.enums]
189189
export_names += [
190-
normalize_identifier(alias) for _, alias in artifacts.named_exports
190+
_normalize_prop(alias) for _, alias in artifacts.named_exports
191191
]
192192
if export_names:
193193
all_list = cst.List(
@@ -354,8 +354,8 @@ def init_stub() -> cst.FunctionDef:
354354
bindings[enum_meta.name] = enum_meta.py_name
355355

356356
for orig, alias in artifacts.named_exports:
357-
alias_name = normalize_identifier(alias)
358-
target = bindings.get(orig, normalize_identifier(orig))
357+
alias_name = _normalize_prop(alias)
358+
target = bindings.get(orig, _normalize_prop(orig))
359359
post_body.append(_stmt(f"{alias_name} = {target}"))
360360
post_body.append(cst.EmptyLine())
361361

api/python/slint/slint/codegen/generator.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
StructFieldMeta,
2323
StructMeta,
2424
)
25-
from .utils import normalize_identifier
25+
from .. import _normalize_prop
2626

2727
if TYPE_CHECKING:
2828
from slint.slint import CallbackInfo, FunctionInfo, PyDiagnostic
@@ -149,7 +149,7 @@ def _collect_metadata(result: native.CompilationResult) -> ModuleArtifacts:
149149
properties.append(
150150
PropertyMeta(
151151
name=key,
152-
py_name=normalize_identifier(key),
152+
py_name=_normalize_prop(key),
153153
type_hint=type_hint,
154154
)
155155
)
@@ -171,7 +171,7 @@ def _collect_metadata(result: native.CompilationResult) -> ModuleArtifacts:
171171
properties_meta: list[PropertyMeta] = []
172172

173173
for key in comp.global_properties(global_name):
174-
py_key = normalize_identifier(key)
174+
py_key = _normalize_prop(key)
175175
info = global_property_info[key]
176176
type_hint = info.python_type
177177
properties_meta.append(
@@ -195,7 +195,7 @@ def _collect_metadata(result: native.CompilationResult) -> ModuleArtifacts:
195195
globals_meta.append(
196196
GlobalMeta(
197197
name=global_name,
198-
py_name=normalize_identifier(global_name),
198+
py_name=_normalize_prop(global_name),
199199
properties=properties_meta,
200200
callbacks=callbacks_meta,
201201
functions=functions_meta,
@@ -205,7 +205,7 @@ def _collect_metadata(result: native.CompilationResult) -> ModuleArtifacts:
205205
components.append(
206206
ComponentMeta(
207207
name=name,
208-
py_name=normalize_identifier(name),
208+
py_name=_normalize_prop(name),
209209
properties=properties,
210210
callbacks=callbacks,
211211
functions=functions,
@@ -223,14 +223,14 @@ def _collect_metadata(result: native.CompilationResult) -> ModuleArtifacts:
223223
fields.append(
224224
StructFieldMeta(
225225
name=field_name,
226-
py_name=normalize_identifier(field_name),
226+
py_name=_normalize_prop(field_name),
227227
type_hint=_python_value_hint(value),
228228
)
229229
)
230230
structs_meta.append(
231231
StructMeta(
232232
name=struct_name,
233-
py_name=normalize_identifier(struct_name),
233+
py_name=_normalize_prop(struct_name),
234234
fields=fields,
235235
)
236236
)
@@ -241,14 +241,14 @@ def _collect_metadata(result: native.CompilationResult) -> ModuleArtifacts:
241241
values.append(
242242
EnumValueMeta(
243243
name=member,
244-
py_name=normalize_identifier(member),
244+
py_name=_normalize_prop(member),
245245
value=enum_member.name,
246246
)
247247
)
248248
enums_meta.append(
249249
EnumMeta(
250250
name=enum_name,
251-
py_name=normalize_identifier(enum_name),
251+
py_name=_normalize_prop(enum_name),
252252
values=values,
253253
)
254254
)
@@ -285,7 +285,7 @@ def _python_value_hint(value: object) -> str:
285285
def _callback_meta(name: str, info: CallbackInfo | FunctionInfo) -> CallbackMeta:
286286
return CallbackMeta(
287287
name=name,
288-
py_name=normalize_identifier(name),
288+
py_name=_normalize_prop(name),
289289
arg_types=[param.python_type for param in info.parameters],
290290
return_type=info.return_type,
291291
)

api/python/slint/slint/codegen/utils.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

api/python/slint/tests/codegen/test_utils.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)