Skip to content

Commit

Permalink
Merge pull request #361 from jvirtanen/cleanups/generate
Browse files Browse the repository at this point in the history
philadelphia-generate: Clean up type hints
  • Loading branch information
jvirtanen authored Nov 3, 2024
2 parents 0709681 + e25bf72 commit 8afd2ac
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 42 deletions.
10 changes: 5 additions & 5 deletions applications/generate/philadelphia/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import typing


_TYPE_FORMATTERS: typing.Dict[str, typing.Callable[[str], str]] = {
_TYPE_FORMATTERS: dict[str, typing.Callable[[str], str]] = {
'char': lambda value: '\'' + value + '\'',
'int': str,
'String': lambda value: '"' + value + '"',
Expand All @@ -31,7 +31,7 @@ class ConstantField(typing.NamedTuple):
value: str


def _format_constant_fields(fields: typing.List[ConstantField]) -> str:
def _format_constant_fields(fields: list[ConstantField]) -> str:
type_width = max(len(field.type_) for field in fields)
name_width = max(len(field.name) for field in fields)
return '\n'.join(_format_constant_field(field, type_width, name_width)
Expand All @@ -48,7 +48,7 @@ def _format_constant_field(field: ConstantField, type_width: int,
class InnerClass:

def __init__(self, name: str, javadoc: str,
fields: typing.List[ConstantField]) -> None:
fields: list[ConstantField]) -> None:
self.name = name
self.javadoc = javadoc
self.fields = fields
Expand Down Expand Up @@ -81,8 +81,8 @@ def _format_fields(self) -> str:
class Class:

def __init__(self, name: str, javadoc: str,
classes: typing.Optional[typing.List[InnerClass]] = None,
fields: typing.Optional[typing.List[ConstantField]] = None) -> None:
classes: typing.Optional[list[InnerClass]] = None,
fields: typing.Optional[list[ConstantField]] = None) -> None:
self.name = name
self.javadoc = javadoc
self.classes = classes if classes else []
Expand Down
12 changes: 6 additions & 6 deletions applications/generate/philadelphia/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class Value(typing.NamedTuple):

class Enumeration(typing.NamedTuple):
primary_field: Field
secondary_fields: typing.List[Field]
secondary_fields: list[Field]
type_: str
values: typing.List[Value]
values: list[Value]


class Message(typing.NamedTuple):
Expand All @@ -56,7 +56,7 @@ def read_dialect(filename: str) -> Dialect:
return Dialect(package_name, class_name_prefix, name)


def format_enumerations(enumerations: typing.List[Enumeration], dialect: Dialect) -> java.CompilationUnit:
def format_enumerations(enumerations: list[Enumeration], dialect: Dialect) -> java.CompilationUnit:
name = '{}Enumerations'.format(dialect.class_name_prefix)
javadoc = 'Enumerations for {}.'.format(dialect.name)
classes = [_format_enumeration(enumeration) for enumeration in enumerations]
Expand Down Expand Up @@ -91,7 +91,7 @@ def _format_secondary_fields_javadoc(enumeration: Enumeration) -> typing.Optiona
return '{}{}{}'.format(header, items, footer)


def format_msg_types(messages: typing.List[Message], dialect: Dialect) -> java.CompilationUnit:
def format_msg_types(messages: list[Message], dialect: Dialect) -> java.CompilationUnit:
name = '{}MsgTypes'.format(dialect.class_name_prefix)
javadoc = 'Message types for {}.'.format(dialect.name)
fields = [_format_msg_type(message) for message in messages]
Expand All @@ -103,7 +103,7 @@ def _format_msg_type(message: Message) -> java.ConstantField:
return java.ConstantField(type_=type_, name=message.name, value=message.msg_type)


def format_tags(fields: typing.List[Field], dialect: Dialect) -> java.CompilationUnit:
def format_tags(fields: list[Field], dialect: Dialect) -> java.CompilationUnit:
name = '{}Tags'.format(dialect.class_name_prefix)
javadoc = 'Tags for {}.'.format(dialect.name)
constant_fields = [_format_tag(field) for field in fields]
Expand All @@ -115,7 +115,7 @@ def _format_tag(field: Field) -> java.ConstantField:


def _format_constant_fields(name: str, javadoc: str,
fields: typing.List[java.ConstantField], dialect: Dialect) -> java.CompilationUnit:
fields: list[java.ConstantField], dialect: Dialect) -> java.CompilationUnit:
package = java.Package(name=dialect.package_name)
class_ = java.Class(name=name, javadoc=javadoc, fields=fields)
return java.CompilationUnit(package, class_)
20 changes: 10 additions & 10 deletions applications/generate/philadelphia/orchestra.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}


def read_messages(filename: str) -> typing.List[model.Message]:
def read_messages(filename: str) -> list[model.Message]:
def message(elem: etree.Element) -> model.Message:
name = etree.get(elem, 'name')
msg_type = etree.get(elem, 'msgType')
Expand All @@ -34,14 +34,14 @@ def message(elem: etree.Element) -> model.Message:
return [message(elem) for elem in tree.findall('.//fixr:message', _NS)]


def read_fields(filename: str) -> typing.List[model.Field]:
def read_fields(filename: str) -> list[model.Field]:
tree = etree.parse(filename)
fields = _read_fields(tree)
return sorted([_make_field(field) for field in fields],
key=lambda field: int(field.tag))


def read_enumerations(filename: str) -> typing.List[model.Enumeration]:
def read_enumerations(filename: str) -> list[model.Enumeration]:
tree = etree.parse(filename)
fields = _read_fields(tree)
fields_by_type = _group_fields_by_type(fields)
Expand All @@ -60,10 +60,10 @@ class _CodeSet(typing.NamedTuple):
name: str
id_: str
type_: str
codes: typing.List[_Code]
codes: list[_Code]


def _read_code_sets(tree: etree.ElementTree) -> typing.List[_CodeSet]:
def _read_code_sets(tree: etree.ElementTree) -> list[_CodeSet]:
def code(elem: etree.Element) -> _Code:
name = etree.get(elem, 'name')
value = etree.get(elem, 'value')
Expand All @@ -84,7 +84,7 @@ class _Field(typing.NamedTuple):
type_: str


def _read_fields(tree: etree.ElementTree) -> typing.List[_Field]:
def _read_fields(tree: etree.ElementTree) -> list[_Field]:
def field(elem: etree.Element) -> _Field:
id_ = etree.get(elem, 'id')
name = etree.get(elem, 'name')
Expand All @@ -97,7 +97,7 @@ def _make_field(field: _Field) -> model.Field:
return model.Field(tag=field.id_, name=field.name)


def _make_enumeration(code_set: _CodeSet, fields: typing.List[_Field]) -> model.Enumeration:
def _make_enumeration(code_set: _CodeSet, fields: list[_Field]) -> model.Enumeration:
primary_fields = [field for field in fields if code_set.id_ == field.id_]
primary_field = primary_fields[0]
secondary_fields = sorted([field for field in fields if code_set.id_ != field.id_],
Expand Down Expand Up @@ -132,14 +132,14 @@ def _make_type(field: _Field, code_set: typing.Optional[_CodeSet]) -> str:
}


def _make_values(code_set: _CodeSet) -> typing.List[model.Value]:
def _make_values(code_set: _CodeSet) -> list[model.Value]:
def value(id_: str, code: _Code) -> model.Value:
name = _NAME_REPLACEMENTS.get((id_, code.value), code.name)
return model.Value(name=name, value=code.value)
return [value(code_set.id_, code) for code in code_set.codes]


def _group_fields_by_type(fields: typing.List[_Field]) -> typing.Dict[str, typing.List[_Field]]:
def _group_fields_by_type(fields: list[_Field]) -> dict[str, list[_Field]]:
sorted_by_type = sorted(fields, key=lambda field: field.type_)
return {type_: list(grouped_by_type) for type_, grouped_by_type in itertools.groupby(sorted_by_type, lambda field: field.type_)}

Expand All @@ -154,7 +154,7 @@ def _has_values(code_set: _CodeSet) -> bool:
return not code_set.id_ in _NO_VALUES and not code_set.type_ == 'Boolean'


def _sorted_codes(codes: typing.List[_Code]) -> typing.List[_Code]:
def _sorted_codes(codes: list[_Code]) -> list[_Code]:
if all(code.sort is not None for code in codes):
return sorted(codes, key=lambda code: code.sort or 0)
return codes
12 changes: 5 additions & 7 deletions applications/generate/philadelphia/quickfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import typing

from . import etree
from . import model


def read_messages(filename: str) -> typing.List[model.Message]:
def read_messages(filename: str) -> list[model.Message]:
def message(elem: etree.Element) -> model.Message:
name = etree.get(elem, 'name')
msgtype = etree.get(elem, 'msgtype')
Expand All @@ -28,13 +26,13 @@ def message(elem: etree.Element) -> model.Message:
return [message(elem) for elem in tree.findall('./messages/message')]


def read_fields(filename: str) -> typing.List[model.Field]:
def read_fields(filename: str) -> list[model.Field]:
tree = etree.parse(filename)
return sorted([_read_field(elem) for elem in tree.findall('./fields/field')],
key=lambda field: int(field.tag))


def read_enumerations(filename: str) -> typing.List[model.Enumeration]:
def read_enumerations(filename: str) -> list[model.Enumeration]:
def enumeration(elem: etree.Element) -> model.Enumeration:
field = _read_field(elem)
values = _read_values(elem)
Expand All @@ -61,7 +59,7 @@ def _read_field(elem: etree.Element) -> model.Field:
return model.Field(tag=number, name=name)


def _read_type(elem: etree.Element, values: typing.List[model.Value]) -> str:
def _read_type(elem: etree.Element, values: list[model.Value]) -> str:
type_ = _TYPES.get(etree.get(elem, 'type'), 'String')
if type_ == 'char' and values and max(len(value.value) for value in values) > 1:
return 'String'
Expand All @@ -72,7 +70,7 @@ def _has_values(elem: etree.Element) -> bool:
return etree.get(elem, 'name') != 'MsgType' and elem.find('value') is not None


def _read_values(root: etree.Element) -> typing.List[model.Value]:
def _read_values(root: etree.Element) -> list[model.Value]:
def value(elem: etree.Element) -> model.Value:
enum = etree.get(elem, 'enum')
description = etree.get(elem, 'description')
Expand Down
22 changes: 11 additions & 11 deletions applications/generate/philadelphia/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from . import model


def read_messages(dirname: str) -> typing.List[model.Message]:
def read_messages(dirname: str) -> list[model.Message]:
def message(elem: etree.Element) -> model.Message:
name = _find(elem, 'Name')
msg_type = _find(elem, 'MsgType')
Expand All @@ -31,12 +31,12 @@ def message(elem: etree.Element) -> model.Message:
return [message(elem) for elem in tree.findall('Message')]


def read_fields(dirname: str) -> typing.List[model.Field]:
def read_fields(dirname: str) -> list[model.Field]:
fields = _read_fields(dirname)
return sorted([_make_field(field) for field in fields], key=lambda field: int(field.tag))


def read_enumerations(dirname: str) -> typing.List[model.Enumeration]:
def read_enumerations(dirname: str) -> list[model.Enumeration]:
fields = _read_fields(dirname)
fields_by_tag = {field.tag: field for field in fields}
enums = _read_enums(dirname)
Expand All @@ -63,14 +63,14 @@ def _make_field(field: _Field) -> model.Field:
return model.Field(tag=field.tag, name=field.name)


def _make_enumeration(field: _Field, enums: typing.List[_Enum]) -> model.Enumeration:
def _make_enumeration(field: _Field, enums: list[_Enum]) -> model.Enumeration:
values = _make_values(enums)
type_ = _make_type(field.type_, values)
return model.Enumeration(primary_field=_make_field(field), secondary_fields=[],
type_=type_, values=values)


def _make_values(enums: typing.List[_Enum]) -> typing.List[model.Value]:
def _make_values(enums: list[_Enum]) -> list[model.Value]:
def value(enum: _Enum) -> model.Value:
return model.Value(name=enum.symbolic_name, value=enum.value)
return [value(enum) for enum in _sorted_enums(enums)]
Expand All @@ -86,7 +86,7 @@ def value(enum: _Enum) -> model.Value:
}


def _make_type(field_type: str, values: typing.List[model.Value]) -> str:
def _make_type(field_type: str, values: list[model.Value]) -> str:
type_ = _TYPES.get(field_type, field_type)
if type_ == 'char' and values and max(len(value.value) for value in values) > 1:
return 'String'
Expand All @@ -97,7 +97,7 @@ def _has_values(field: _Field) -> bool:
return not field.type_ == 'Boolean' and not field.name == 'MsgType'


def _read_fields(dirname: str) -> typing.List[_Field]:
def _read_fields(dirname: str) -> list[_Field]:
def field(elem: etree.Element) -> _Field:
tag = _find(elem, 'Tag')
name = _find(elem, 'Name')
Expand All @@ -108,8 +108,8 @@ def field(elem: etree.Element) -> _Field:
return [field(elem) for elem in tree.findall('Field')]


def _read_enums(dirname: str) -> typing.List[_Enum]:
def enums(elem: etree.Element) -> typing.List[_Enum]:
def _read_enums(dirname: str) -> list[_Enum]:
def enums(elem: etree.Element) -> list[_Enum]:
tag = _find(elem, 'Tag')
value = _read_value(elem, tag)
symbolic_names = _read_symbolic_names(elem, tag, value)
Expand All @@ -122,7 +122,7 @@ def enums(elem: etree.Element) -> typing.List[_Enum]:
key=lambda enum: int(enum.tag))


def _sorted_enums(enums: typing.List[_Enum]) -> typing.List[_Enum]:
def _sorted_enums(enums: list[_Enum]) -> list[_Enum]:
if all(enum.sort is not None for enum in enums):
return sorted(enums, key=lambda enum: enum.sort or 0)
return enums
Expand Down Expand Up @@ -159,7 +159,7 @@ def _read_sort(root: etree.Element) -> typing.Optional[int]:
}


def _read_symbolic_names(elem: etree.Element, tag: str, value: str) -> typing.List[str]:
def _read_symbolic_names(elem: etree.Element, tag: str, value: str) -> list[str]:
symbolic_name = _find(elem, 'SymbolicName')
primary = _SYMBOLIC_NAMES.get((tag, value), symbolic_name)
aliases = _SYMBOLIC_NAME_ALIASES.get((tag, value), [])
Expand Down
6 changes: 3 additions & 3 deletions applications/generate/philadelphia/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

class Source(typing.Protocol):

def read_enumerations(self, filename: str) -> typing.List[model.Enumeration]:
def read_enumerations(self, filename: str) -> list[model.Enumeration]:
...

def read_fields(self, filename: str) -> typing.List[model.Field]:
def read_fields(self, filename: str) -> list[model.Field]:
...

def read_messages(self, filename: str) -> typing.List[model.Message]:
def read_messages(self, filename: str) -> list[model.Message]:
...

0 comments on commit 8afd2ac

Please sign in to comment.