Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.1.4 - More improvements #56

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:

- run: |
mk python-release owner=vkottler \
repo=ifgen version=3.1.2
repo=ifgen version=3.1.4
if: |
matrix.python-version == '3.11'
&& matrix.system == 'ubuntu-latest'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
=====================================
generator=datazen
version=3.1.4
hash=975b48a74c4be1ac5e11b915ab1b99fd
hash=47a602927c774ebb52efdfa7c6f72910
=====================================
-->

# ifgen ([3.1.2](https://pypi.org/project/ifgen/))
# ifgen ([3.1.4](https://pypi.org/project/ifgen/))

[![python](https://img.shields.io/pypi/pyversions/ifgen.svg)](https://pypi.org/project/ifgen/)
![Build Status](https://github.com/vkottler/ifgen/workflows/Python%20Package/badge.svg)
Expand Down
4 changes: 2 additions & 2 deletions ifgen/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.1.4
# hash=206c822433faa199cbbeaf11941043ba
# hash=e284c895069c6514e8e50d86ca1b174f
# =====================================

"""
Expand All @@ -10,4 +10,4 @@

DESCRIPTION = "An interface generator for distributed computing."
PKG_NAME = "ifgen"
VERSION = "3.1.2"
VERSION = "3.1.4"
71 changes: 47 additions & 24 deletions ifgen/svd/group/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,18 @@ def as_alnum(word: str) -> str:
return result


SKIP = {"-"}


def handle_enum_name(name: str, description: str = None) -> str:
"""Attempt to generate more useful enumeration names."""

if name.startswith("value") and description:
new_name = description.replace("-", "_")

alnum_parts = [as_alnum(x.strip().lower()) for x in new_name.split()]
alnum_parts = [
as_alnum(x.strip().lower().replace("-", "_"))
for x in description.split()
if x not in SKIP
]

# Prune some words if the description is very long.
if len(alnum_parts) > 1:
Expand All @@ -101,6 +106,34 @@ def handle_enum_name(name: str, description: str = None) -> str:
return name


def remove_common_prefixes(data: dict[str, Any]) -> dict[str, Any]:
"""Attempt to remove common prefixes in enumeration names."""

result = data

length = len(commonprefix(list(result)))
if length > 1:
result = {
key[length:] if length < len(key) else key: value
for key, value in result.items()
}

return result


def handle_duplicate(existing: dict[str, Any], key: str, value: Any) -> None:
"""Handle key de-duplication for enumerations."""

assert key
if key[0].isnumeric():
key = "_" + key

while key in existing:
key += "_x"

existing[key] = value


def translate_enums(enum: EnumeratedValues) -> EnumValues:
"""Generate an enumeration definition."""

Expand Down Expand Up @@ -128,27 +161,17 @@ def translate_enums(enum: EnumeratedValues) -> EnumValues:
else:
enum_data["value"] = int(value_str)

final_name = handle_enum_name(name, value.raw_data.get("description"))
assert final_name

# Truncate.
final_name = (
final_name if len(final_name) < 51 else final_name[:45] + "_cont"
handle_duplicate(
result,
handle_enum_name(name, value.raw_data.get("description")),
enum_data,
)
assert len(final_name) < 51

while final_name in result:
final_name += "_"

assert final_name not in result, (name, final_name)
result[final_name] = enum_data

# Remove common prefix (if present) from enums.
length = len(commonprefix(list(result)))
if length > 1:
result = {
key[length:] if length < len(key) else key: value
for key, value in result.items()
}
# Truncate names.
new_result: dict[str, Any] = {}
for key, value in remove_common_prefixes(result).items():
handle_duplicate(
new_result, key if len(key) < 51 else key[:45] + "_cont", value
)

return result
return new_result
2 changes: 1 addition & 1 deletion local/variables/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
major: 3
minor: 1
patch: 3
patch: 4
entry: ig
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__"

[project]
name = "ifgen"
version = "3.1.2"
version = "3.1.4"
description = "An interface generator for distributed computing."
readme = "README.md"
requires-python = ">=3.11"
Expand Down