From b186396559c232b261b16fec525dcfbfb8692119 Mon Sep 17 00:00:00 2001 From: Vaughn Kottler Date: Mon, 16 Oct 2023 23:35:32 -0500 Subject: [PATCH] 3.1.4 - More improvements --- .github/workflows/python-package.yml | 2 +- README.md | 4 +- ifgen/__init__.py | 4 +- ifgen/svd/group/enums.py | 71 ++++++++++++++++++---------- local/variables/package.yaml | 2 +- pyproject.toml | 2 +- 6 files changed, 54 insertions(+), 31 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 9aa2918..3905cc6 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -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' diff --git a/README.md b/README.md index 6113e6a..d28305f 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/ifgen/__init__.py b/ifgen/__init__.py index 0ce1f64..ff6249f 100644 --- a/ifgen/__init__.py +++ b/ifgen/__init__.py @@ -1,7 +1,7 @@ # ===================================== # generator=datazen # version=3.1.4 -# hash=206c822433faa199cbbeaf11941043ba +# hash=e284c895069c6514e8e50d86ca1b174f # ===================================== """ @@ -10,4 +10,4 @@ DESCRIPTION = "An interface generator for distributed computing." PKG_NAME = "ifgen" -VERSION = "3.1.2" +VERSION = "3.1.4" diff --git a/ifgen/svd/group/enums.py b/ifgen/svd/group/enums.py index 0beca92..8c96b04 100644 --- a/ifgen/svd/group/enums.py +++ b/ifgen/svd/group/enums.py @@ -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: @@ -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.""" @@ -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 diff --git a/local/variables/package.yaml b/local/variables/package.yaml index 51084cf..3ca41ba 100644 --- a/local/variables/package.yaml +++ b/local/variables/package.yaml @@ -1,5 +1,5 @@ --- major: 3 minor: 1 -patch: 3 +patch: 4 entry: ig diff --git a/pyproject.toml b/pyproject.toml index b2e7160..8cb5f90 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"