Commit 83c67471 authored by Thanassis Tsiodras's avatar Thanassis Tsiodras
Browse files

Addressed mypy findings for asnParser.

parent c635a156
......@@ -59,8 +59,8 @@ import hashlib
import xml.sax # type: ignore
from typing import IO, TypeVar, Type, Optional, Callable, Union, List, Set, Dict, Tuple, Any # NOQA pylint: disable=W0611
from . import configMT
from . import utility
import configMT
import utility
from .asnAST import (
AsnBasicNode, AsnEnumerated, AsnSequence, AsnChoice, AsnSequenceOf,
......@@ -541,11 +541,18 @@ def GetAttr(node: Element, attrName: str) -> Optional[Any]:
return node._attrs[attrName]
def GetChild(node: Element, childName: str) -> Optional[Element]:
def GetAttrCertainly(node: Element, attrName: str) -> Any:
if attrName not in list(node._attrs.keys()):
assert False
else:
return node._attrs[attrName]
def GetChild(node: Element, childName: str) -> Element:
for x in node._children:
if x._name == childName:
return x
return None # pragma: no cover
assert False # pragma: no cover
class Pretty:
......@@ -583,12 +590,12 @@ U = TypeVar('U', int, float)
def GetRange(newModule: Module, lineNo: int, nodeWithMinAndMax: Element, valueType: Type[U]) -> Tuple[U, U]:
try:
mmin = GetAttr(nodeWithMinAndMax, "Min")
mmin = GetAttrCertainly(nodeWithMinAndMax, "Min")
# rangel = ( mmin == "MIN" ) and -2147483648L or valueType(mmin)
if mmin == "MIN":
utility.panic("You missed a range specification, or used MIN/MAX (line %s)" % lineNo) # pragma: no cover
rangel = valueType(mmin)
mmax = GetAttr(nodeWithMinAndMax, "Max")
mmax = GetAttrCertainly(nodeWithMinAndMax, "Max")
# rangeh = ( mmax == "MAX" ) and 2147483647L or valueType(mmax)
if mmax == "MAX":
utility.panic("You missed a range specification, or used MIN/MAX (line %s)" % lineNo) # pragma: no cover
......@@ -673,9 +680,9 @@ def CreateReference(newModule: Module, lineNo: int, xmlReferenceNode: Element) -
return AsnMetaType(
asnFilename=newModule._asnFilename,
lineno=lineNo,
containedType=GetAttr(xmlReferenceNode, "ReferencedTypeName"),
Min=getIntOrFloatOrNone(GetAttr(xmlReferenceNode, "Min")),
Max=getIntOrFloatOrNone(GetAttr(xmlReferenceNode, "Max")))
containedType=GetAttrCertainly(xmlReferenceNode, "ReferencedTypeName"),
Min=getIntOrFloatOrNone(GetAttrCertainly(xmlReferenceNode, "Min")),
Max=getIntOrFloatOrNone(GetAttrCertainly(xmlReferenceNode, "Max")))
V = TypeVar('V', AsnSequenceOf, AsnSetOf)
......@@ -786,7 +793,7 @@ def GenericFactory(newModule: Module, xmlType: Element) -> AsnNode:
"SetType": CreateSet,
"ChoiceType": CreateChoice
} # type: Dict[str, Callable[[Module, int, Element], AsnNode]]
lineNo = GetAttr(xmlType, "Line")
lineNo = GetAttrCertainly(xmlType, "Line")
global g_lineno
g_lineno = lineNo
if len(xmlType._children) == 0: # pylint: disable=len-as-condition
......@@ -809,15 +816,15 @@ def VisitTypeAssignment(newModule: Module, xmlTypeAssignment: Element) -> Tuple[
if isArtificial is None:
utility.panic("You are using an older version of ASN1SCC - please upgrade.")
newNode._isArtificial = isArtificial == "True"
name = GetAttr(xmlTypeAssignment, "Name")
name = GetAttrCertainly(xmlTypeAssignment, "Name")
g_adaUses.setdefault(newModule._id, set()).add(name)
return (name, newNode)
def VisitAsn1Module(xmlAsn1File: Element, xmlModule: Element, modules: List[Module]) -> None: # pylint: disable=invalid-sequence-index
newModule = Module()
newModule._id = GetAttr(xmlModule, "ID")
newModule._asnFilename = GetAttr(xmlAsn1File, "FileName")
newModule._id = GetAttrCertainly(xmlModule, "ID")
newModule._asnFilename = GetAttrCertainly(xmlAsn1File, "FileName")
newModule._exportedTypes = VisitAll(
GetChild(xmlModule, "ExportedTypes"), "ExportedType",
lambda x: GetAttr(x, "Name"))
......@@ -902,8 +909,8 @@ def PrintType(f: IO[Any], xmlType: Element, indent: str, nameCleaner: Callable[[
f.write('BOOLEAN')
elif realType._name == "IntegerType":
f.write('INTEGER')
mmin = GetAttr(realType, "Min")
mmax = GetAttr(realType, "Max")
mmin = GetAttrCertainly(realType, "Min")
mmax = GetAttrCertainly(realType, "Max")
f.write(' (%s .. %s)' % (mmin, mmax))
elif realType._name == "RealType":
f.write('REAL')
......@@ -914,11 +921,11 @@ def PrintType(f: IO[Any], xmlType: Element, indent: str, nameCleaner: Callable[[
utility.panic("BIT STRINGs are not supported, use SEQUENCE OF BOOLEAN") # pragma: no cover
elif realType._name == "OctetStringType" or realType._name == "IA5StringType" or realType._name == "NumericStringType":
f.write('OCTET STRING')
mmin = GetAttr(realType, "Min")
mmax = GetAttr(realType, "Max")
mmin = GetAttrCertainly(realType, "Min")
mmax = GetAttrCertainly(realType, "Max")
f.write(' (SIZE (%s .. %s))' % (mmin, mmax))
elif realType._name == "ReferenceType":
f.write(nameCleaner(GetAttr(realType, "ReferencedTypeName")))
f.write(nameCleaner(GetAttrCertainly(realType, "ReferencedTypeName")))
elif realType._name == "EnumeratedType":
f.write('ENUMERATED {\n')
options = []
......@@ -927,9 +934,9 @@ def PrintType(f: IO[Any], xmlType: Element, indent: str, nameCleaner: Callable[[
options.append(x)
VisitAll(realType, "EnumValue", addNewOption)
if options:
f.write(indent + ' ' + nameCleaner(GetAttr(options[0], "StringValue")) + "(" + GetAttr(options[0], "IntValue") + ")")
f.write(indent + ' ' + nameCleaner(GetAttrCertainly(options[0], "StringValue")) + "(" + GetAttrCertainly(options[0], "IntValue") + ")")
for otherOptions in options[1:]:
f.write(',\n' + indent + ' ' + nameCleaner(GetAttr(otherOptions, "StringValue")) + "(" + GetAttr(otherOptions, "IntValue") + ")")
f.write(',\n' + indent + ' ' + nameCleaner(GetAttrCertainly(otherOptions, "StringValue")) + "(" + GetAttrCertainly(otherOptions, "IntValue") + ")")
f.write('\n' + indent + '}')
elif realType._name == "SequenceType" or realType._name == "SetType":
if realType._name == "SequenceType":
......@@ -937,7 +944,7 @@ def PrintType(f: IO[Any], xmlType: Element, indent: str, nameCleaner: Callable[[
else:
f.write('SET {\n')
if len(realType._children) > 0: # pylint: disable=len-as-condition
f.write(indent + ' ' + nameCleaner(GetAttr(realType._children[0], "VarName")) + "\t")
f.write(indent + ' ' + nameCleaner(GetAttrCertainly(realType._children[0], "VarName")) + "\t")
firstChildOptional = GetAttr(realType._children[0], "Optional") == "True"
if len(realType._children[0]._children) == 0: # pylint: disable=len-as-condition
utility.panic("AST inconsistency: len(realType._children[0]._children) = 0\nContact ESA") # pragma: no cover
......@@ -945,7 +952,7 @@ def PrintType(f: IO[Any], xmlType: Element, indent: str, nameCleaner: Callable[[
if firstChildOptional:
f.write(' OPTIONAL')
for sequenceOrSetChild in realType._children[1:]:
f.write(",\n" + indent + ' ' + nameCleaner(GetAttr(sequenceOrSetChild, "VarName")) + "\t")
f.write(",\n" + indent + ' ' + nameCleaner(GetAttrCertainly(sequenceOrSetChild, "VarName")) + "\t")
childOptional = GetAttr(sequenceOrSetChild, "Optional") == "True"
if len(sequenceOrSetChild._children) == 0: # pylint: disable=len-as-condition
utility.panic("AST inconsistency: len(sequenceOrSetChild._children) = 0\nContact ESA") # pragma: no cover
......@@ -958,12 +965,12 @@ def PrintType(f: IO[Any], xmlType: Element, indent: str, nameCleaner: Callable[[
elif realType._name == "ChoiceType":
f.write('CHOICE {\n')
if len(realType._children) > 0: # pylint: disable=len-as-condition
f.write(indent + ' ' + nameCleaner(GetAttr(realType._children[0], "VarName")) + "\t")
f.write(indent + ' ' + nameCleaner(GetAttrCertainly(realType._children[0], "VarName")) + "\t")
if len(realType._children[0]._children) == 0: # pylint: disable=len-as-condition
utility.panic("AST inconsistency: len(realType._children[0]._children) = 0\nContact ESA") # pragma: no cover
PrintType(f, realType._children[0]._children[0], indent + " ", nameCleaner) # the contained type of the first child
for choiceChild in realType._children[1:]:
f.write(",\n" + indent + ' ' + nameCleaner(GetAttr(choiceChild, "VarName")) + "\t")
f.write(",\n" + indent + ' ' + nameCleaner(GetAttrCertainly(choiceChild, "VarName")) + "\t")
if len(choiceChild._children) == 0: # pylint: disable=len-as-condition
utility.panic("AST inconsistency: len(choiceChild._children) = 0\nContact ESA") # pragma: no cover
PrintType(f, choiceChild._children[0], indent + " ", nameCleaner) # the contained type
......@@ -993,6 +1000,8 @@ def PrintType(f: IO[Any], xmlType: Element, indent: str, nameCleaner: Callable[[
def PrintGrammarFromAST(f: IO[Any], nameCleaner: Callable[[str], str] = SimpleCleaner) -> None:
if g_xmlASTrootNode is None:
return
ourtypeAssignments = []
VisitAll(
g_xmlASTrootNode._children[0], "Asn1File",
......@@ -1000,7 +1009,7 @@ def PrintGrammarFromAST(f: IO[Any], nameCleaner: Callable[[str], str] = SimpleCl
lambda y: ourtypeAssignments.append((x, y))))
for a, t in ourtypeAssignments:
f.write("-- " + GetAttr(a, "FileName") + "\n%s ::= " % nameCleaner(GetAttr(t, "Name")))
f.write("-- " + GetAttrCertainly(a, "FileName") + "\n%s ::= " % nameCleaner(GetAttrCertainly(t, "Name")))
typeChild = GetChild(t, "Type")
if typeChild:
PrintType(f, typeChild, '', nameCleaner)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment