Files
bproto/src/nameHandling/style/pythonNameStyle.py
2025-04-14 14:43:03 +02:00

58 lines
1.8 KiB
Python

from typing import Literal
from nameHandling.base import ComponentName, NameStyle, to_cap_case, to_snake_case, to_screaming_case
from protocol_components.protocol import ProtocolDefinitions
class NameStylePython(NameStyle):
@staticmethod
def toStr(bproto_style_name: str | ComponentName, context:
Literal[
"", "enum_name", "enum_item",
"class_name", "class_member",
] = "") -> str:
match context:
case "class_name":
return to_cap_case(bproto_style_name)
case "class_member":
return to_snake_case(bproto_style_name)
case "enum_name":
return to_cap_case(bproto_style_name)
case "enum_item":
return to_screaming_case(bproto_style_name)
case _:
return to_snake_case(bproto_style_name)
@staticmethod
def fromStr(bproto_style_name: str | ComponentName, context: str = "") -> ComponentName:
raise NotImplementedError("This mode is not avaialbe for NameStylePython")
@staticmethod
def preprocess(protocol: ProtocolDefinitions) -> ProtocolDefinitions:
protocol.enums = {
protocol.name + "Enum" + v.name: v
for _, v in protocol.enums.items()
}
for name, e in protocol.enums.items():
e.name = name
protocol.bitfields = {
protocol.name + "Bitfield" + v.name: v
for _, v in protocol.bitfields.items()
}
for name, b in protocol.bitfields.items():
b.name = name
protocol.messages = {
protocol.name + "Message" + v.name: v
for _, v in protocol.messages.items()
}
for name, m in protocol.messages.items():
m.name = name
return protocol