Commit 6ed0f890 authored by Luke Mewburn's avatar Luke Mewburn
Browse files

compat_asn1: backport to python3.7

The forge pipelines use python3.7, so adapt:
- Don't use the := walrus (added in python3.8)
- Use the older typing syntax (not the changes in python3.9)
parent c504e832
Loading
Loading
Loading
Loading
Loading
+23 −14
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ import logging
import os
import pathlib
import sys
from typing import List, Optional, Set

import pycrate_asn1c.asnobj as pa_asnobj
import pycrate_asn1c.asnproc
@@ -25,10 +26,10 @@ TYPE_CONTAINER_OF = (pa_asnobj.TYPE_SEQ_OF, pa_asnobj.TYPE_SET_OF)

@dataclasses.dataclass
class Context:
    errors: list[str] = dataclasses.field(default_factory=list)
    fa_mod: pa_asnobj.ASN1Dict | None = None
    fb_mod: pa_asnobj.ASN1Dict | None = None
    seen: set[str] = dataclasses.field(default_factory=set)
    errors: List[str] = dataclasses.field(default_factory=list)
    fa_mod: Optional[pa_asnobj.ASN1Dict] = None
    fb_mod: Optional[pa_asnobj.ASN1Dict] = None
    seen: Set[str] = dataclasses.field(default_factory=set)


def process_error(ctx: Context, message: str) -> None:
@@ -45,7 +46,7 @@ def dict_swap(dictionary: dict) -> dict:
    return {value: key for key, value in dictionary.items()}


def parse_asn1(asn_files: list[str]) -> pa_asnobj.ASN1Dict:
def parse_asn1(asn_files: List[str]) -> pa_asnobj.ASN1Dict:
    """Parse `asn_files` as ASN.1 using pycrate, returning the module object."""
    logging.info(f"Parsing {' '.join(asn_files)}")
    asn_text = []
@@ -61,7 +62,8 @@ def parse_asn1(asn_files: list[str]) -> pa_asnobj.ASN1Dict:
def repr_modname(asnobj: pa_asnobj.ASN1Obj) -> str:
    """Return name of `asnobj` as MODULE.NAME or NAME."""
    result = asnobj._name
    if (asnmod := getattr(asnobj, "_mod", None)) is not None:
    asnmod = getattr(asnobj, "_mod", None)
    if asnmod is not None:
        result = f"{asnmod}.{result}"
    return result

@@ -72,9 +74,11 @@ def repr_name(asnobj: pa_asnobj.ASN1Obj) -> str:
    "MODULE.", "PARENT.", and "[TAG]" are optional.
    """
    result = f"{asnobj._name}"
    if (asnparent := getattr(asnobj, "_parent", None)) is not None:
    asnparent = getattr(asnobj, "_parent", None)
    if asnparent is not None:
        result = f"{repr_name(asnparent)}.{result}"
    if (asnmod := getattr(asnobj, "_mod", None)) is not None:
    asnmod = getattr(asnobj, "_mod", None)
    if asnmod is not None:
        result = f"{asnmod}.{result}"
    if asnobj._tag is not None:
        result = f"{result}[{asnobj._tag[0]}]"
@@ -91,16 +95,19 @@ def repr_tagpath(asnobj: pa_asnobj.ASN1Obj) -> str:
    else:
        result = asnobj._name

    if (asnparent := getattr(asnobj, "_parent", None)) is not None:
    asnparent = getattr(asnobj, "_parent", None)
    if asnparent is not None:
        result = f"{repr_tagpath(asnparent)}.{result}"
    if (asnmod := getattr(asnobj, "_mod", None)) is not None:
    asnmod = getattr(asnobj, "_mod", None)
    if asnmod is not None:
        result = f"{asnmod}.{result}"
    return result


def repr_type(asnobj: pa_asnobj.ASN1Obj) -> str:
    """Return name of type of `asnobj`, as MODULE.TYPE or BASETYPE."""
    if (tr := asnobj.get_typeref()) is not None:
    tr = asnobj.get_typeref()
    if tr is not None:
        # type reference
        result = repr_modname(tr)
    elif asnobj._parent is None:
@@ -118,7 +125,8 @@ def repr_type_name(asnobj: pa_asnobj.ASN1Obj) -> str:
    Returns MODULE.TYPE if asnobj has a type reference or is a type,
    otherwise the name of the field.
    """
    if (tr := asnobj.get_typeref()) is not None:
    tr = asnobj.get_typeref()
    if tr is not None:
        # reference to type in module
        result = repr_modname(tr)
    elif asnobj._parent is None:
@@ -136,7 +144,8 @@ def repr_type_tagpath(asnobj: pa_asnobj.ASN1Obj) -> str:
    Returns MODULE.TYPE if asnobj has a type reference or is a type,
    otherwise the tagpath (e.g., an unnamed inline container).
    """
    if (tr := asnobj.get_typeref()) is not None:
    tr = asnobj.get_typeref()
    if tr is not None:
        # reference to type in module
        result = repr_modname(tr)
    elif asnobj._parent is None:
@@ -332,7 +341,7 @@ def compare_asntype(
        logging.debug(f"Unimplemented check of type '{fa_type.TYPE}'")


def compare_files(ctx: Context, a_files: list[str], b_files: list[str]) -> None:
def compare_files(ctx: Context, a_files: List[str], b_files: List[str]) -> None:
    """Compare ASN.1 in files `a_files` with files `b_files` for
    backwards compatibility issues:
    - Module is the same