Commit 5ed832e9 authored by Thanassis Tsiodras's avatar Thanassis Tsiodras

commonpy now passes flake8 and pylint

parent 42bf1a19
......@@ -46,7 +46,7 @@ from commonPy.utility import inform, panic
import commonPy.cleanupNodes
import commonPy.verify as verify
import A_mappers # NOQA
import A_mappers # NOQA pylint:disable=unused-import
def usage(argsToTools):
'''Print usage instructions.'''
......@@ -149,78 +149,80 @@ def main():
# For each ASN.1 grammar file referenced in the system level description
for arg, modelingLanguage in argsToTools.items():
if toolSelected[arg]:
backendFilename = "." + modelingLanguage.lower() + "_A_mapper.py"
inform("Parsing %s...", backendFilename)
try:
backend = import_module(backendFilename[:-3], 'A_mappers') # pragma: no cover
if backendFilename[:-3] not in loadedBackends:
loadedBackends[backendFilename[:-3]] = 1
if commonPy.configMT.verbose:
backend.Version()
except ImportError as err: # pragma: no cover
panic("Failed to load backend (%s): %s" % (backendFilename, str(err))) # pragma: no cover
# Esp. for C, we want to pass the complete list of ASN.1 files to ASN1SCC,
# instead of working per type:
if modelingLanguage.lower() in ["c", "ada", "smp2", "qgenc", "qgenada"]:
if 'OnStartup' in dir(backend):
backend.OnStartup(modelingLanguage, list(uniqueASNfiles.keys()), commonPy.configMT.outputDir, badTypes)
if 'OnShutdown' in dir(backend):
backend.OnShutdown(badTypes)
else:
# Work on each ASN.1 file's types
for asnFile in uniqueASNfiles:
if 'OnStartup' in dir(backend):
backend.OnStartup(modelingLanguage, asnFile, commonPy.configMT.outputDir, badTypes)
leafTypeDict = uniqueASNfiles[asnFile][2]
inform("Executing mappings for types inside %s...", asnFile)
names = uniqueASNfiles[asnFile][0]
for nodeTypename in names:
# Check if this type must be skipped
if nodeTypename in badTypes:
continue
node = names[nodeTypename]
inform("Processing %s (%s)...", nodeTypename, modelingLanguage)
# First, make sure we know what leaf type this node is
assert nodeTypename in leafTypeDict
leafType = leafTypeDict[nodeTypename]
# If it is a base type,
if leafType in ['BOOLEAN', 'INTEGER', 'REAL', 'OCTET STRING']:
# make sure we have mapping instructions for BASE elements
if 'OnBasic' not in dir(backend):
panic("ASN.1 grammar contains literal(%s) but no BASE section found in the mapping grammar (%s)" % (nodeTypename, sys.argv[2])) # pragma: no cover
backend.OnBasic(nodeTypename, node, leafTypeDict)
# if it is a complex type
elif leafType in ['SEQUENCE', 'SET', 'CHOICE', 'SEQUENCEOF', 'SETOF', 'ENUMERATED']:
# make sure we have mapping instructions for the element
mappedName = {
'SEQUENCE': 'OnSequence',
'SET': 'OnSet',
'CHOICE': 'OnChoice',
'SEQUENCEOF': 'OnSequenceOf',
'SETOF': 'OnSetOf',
'ENUMERATED': 'OnEnumerated'
}
if mappedName[leafType] not in dir(backend):
panic("ASN.1 grammar contains %s but no %s section found in the mapping grammar (%s)" % (nodeTypename, mappedName[leafType], backendFilename)) # pragma: no cover
processor = backend.__dict__[mappedName[leafType]]
processor(nodeTypename, node, leafTypeDict)
# what type is it?
else: # pragma: no cover
panic("Unexpected type of element: %s" % leafTypeDict[nodeTypename]) # pragma: no cover
if 'OnShutdown' in dir(backend):
backend.OnShutdown(badTypes)
if not toolSelected[arg]:
continue
backendFilename = "." + modelingLanguage.lower() + "_A_mapper.py"
inform("Parsing %s...", backendFilename)
try:
backend = import_module(backendFilename[:-3], 'A_mappers') # pragma: no cover
if backendFilename[:-3] not in loadedBackends:
loadedBackends[backendFilename[:-3]] = 1
if commonPy.configMT.verbose:
backend.Version()
except ImportError as err: # pragma: no cover
panic("Failed to load backend (%s): %s" % (backendFilename, str(err))) # pragma: no cover
# Esp. for C, we want to pass the complete list of ASN.1 files to ASN1SCC,
# instead of working per type:
if modelingLanguage.lower() in ["c", "ada", "smp2", "qgenc", "qgenada"]:
if 'OnStartup' in dir(backend):
backend.OnStartup(modelingLanguage, list(uniqueASNfiles.keys()), commonPy.configMT.outputDir, badTypes)
if 'OnShutdown' in dir(backend):
backend.OnShutdown(badTypes)
continue
# Work on each ASN.1 file's types
for asnFile in uniqueASNfiles:
if 'OnStartup' in dir(backend):
backend.OnStartup(modelingLanguage, asnFile, commonPy.configMT.outputDir, badTypes)
leafTypeDict = uniqueASNfiles[asnFile][2]
inform("Executing mappings for types inside %s...", asnFile)
names = uniqueASNfiles[asnFile][0]
for nodeTypename in names:
# Check if this type must be skipped
if nodeTypename in badTypes:
continue
node = names[nodeTypename]
inform("Processing %s (%s)...", nodeTypename, modelingLanguage)
# First, make sure we know what leaf type this node is
assert nodeTypename in leafTypeDict
leafType = leafTypeDict[nodeTypename]
# If it is a base type,
if leafType in ['BOOLEAN', 'INTEGER', 'REAL', 'OCTET STRING']:
# make sure we have mapping instructions for BASE elements
if 'OnBasic' not in dir(backend):
panic("ASN.1 grammar contains literal(%s) but no BASE section found in the mapping grammar (%s)" % (nodeTypename, sys.argv[2])) # pragma: no cover
backend.OnBasic(nodeTypename, node, leafTypeDict)
# if it is a complex type
elif leafType in ['SEQUENCE', 'SET', 'CHOICE', 'SEQUENCEOF', 'SETOF', 'ENUMERATED']:
# make sure we have mapping instructions for the element
mappedName = {
'SEQUENCE': 'OnSequence',
'SET': 'OnSet',
'CHOICE': 'OnChoice',
'SEQUENCEOF': 'OnSequenceOf',
'SETOF': 'OnSetOf',
'ENUMERATED': 'OnEnumerated'
}
if mappedName[leafType] not in dir(backend):
panic("ASN.1 grammar contains %s but no %s section found in the mapping grammar (%s)" % (nodeTypename, mappedName[leafType], backendFilename)) # pragma: no cover
processor = backend.__dict__[mappedName[leafType]]
processor(nodeTypename, node, leafTypeDict)
# what type is it?
else: # pragma: no cover
panic("Unexpected type of element: %s" % leafTypeDict[nodeTypename]) # pragma: no cover
if 'OnShutdown' in dir(backend):
backend.OnShutdown(badTypes)
if __name__ == "__main__":
if "-pdb" in sys.argv:
sys.argv.remove("-pdb") # pragma: no cover
import pdb # pragma: no cover
import pdb # pragma: no cover pylint: disable=wrong-import-position,wrong-import-order
pdb.run('main()') # pragma: no cover
else:
main()
......@@ -50,15 +50,15 @@ g_systems = {}
class AadlParameter:
def __init__(self, direction, type):
def __init__(self, direction, typ):
assert direction in ['IN', 'OUT', 'INOUT']
self._direction = direction
self._type = type
self._type = typ
class AadlSubProgramFeature:
def __init__(self, id, parameter):
self._id = id
def __init__(self, iid, parameter):
self._id = iid
self._parameter = parameter
......@@ -69,9 +69,9 @@ class AadlPropertyAssociationNoModes:
class AadlPort:
def __init__(self, direction, type):
def __init__(self, direction, typ):
self._direction = direction
self._type = type
self._type = typ
class AadlEventPort:
......@@ -88,21 +88,21 @@ class AadlEventPort:
class AadlEventDataPort(AadlPort):
def __init__(self, direction, type):
AadlPort.__init__(self, direction, type)
def __init__(self, direction, typ):
AadlPort.__init__(self, direction, typ)
class AadlThreadFeature:
def __init__(self, id, port):
assert(isinstance(port, AadlPort))
self._id = id
def __init__(self, iid, port):
assert isinstance(port, AadlPort)
self._id = iid
self._port = port
class AadlProcessFeature:
def __init__(self, id, port):
assert(isinstance(port, AadlPort))
self._id = id
def __init__(self, iid, port):
assert isinstance(port, AadlPort)
self._id = iid
self._port = port
......@@ -145,8 +145,8 @@ class OutgoingUniPort(UniPort):
class ApLevelContainer:
def __init__(self, id):
self._id = id
def __init__(self, iid):
self._id = iid
self._calls = []
self._params = []
self._connections = []
......@@ -170,8 +170,8 @@ class ApLevelContainer:
class Param:
def __init__(self, aplcID, id, signal, sourceElement):
self._id = id
def __init__(self, aplcID, iid, signal, sourceElement):
self._id = iid
# It is the Process, Thread or Subprogram ID
self._aplcID = aplcID
# Could be string (i.e. AADL DataType name) or Signal (i.e. asnFilename, asnNodename)
......@@ -180,18 +180,18 @@ class Param:
class InParam(Param):
def __init__(self, aplcID, id, signal, sourceElement):
Param.__init__(self, aplcID, id, signal, sourceElement)
def __init__(self, aplcID, iid, signal, sourceElement):
Param.__init__(self, aplcID, iid, signal, sourceElement)
class OutParam(Param):
def __init__(self, aplcID, id, signal, sourceElement):
Param.__init__(self, aplcID, id, signal, sourceElement)
def __init__(self, aplcID, iid, signal, sourceElement):
Param.__init__(self, aplcID, iid, signal, sourceElement)
class InOutParam(Param):
def __init__(self, aplcID, id, signal, sourceElement):
Param.__init__(self, aplcID, id, signal, sourceElement)
def __init__(self, aplcID, iid, signal, sourceElement):
Param.__init__(self, aplcID, iid, signal, sourceElement)
class UniquePortIdentifier:
......
This diff is collapsed.
This diff is collapsed.
......@@ -84,11 +84,10 @@ def DiscoverBadTypes() -> SetOfBadTypenames:
elif isinstance(node, (AsnSequenceOf, AsnSetOf)):
if isinstance(node._containedType, AsnAsciiString):
cache[node] = True
return True
cache[node] = \
isinstance(node._containedType, str) \
and \
CheckNodeForIA5(names[node._containedType])
else:
cache[node] = \
isinstance(node._containedType, str) and \
CheckNodeForIA5(names[node._containedType])
return cache[node]
cache[node] = False
return cache[node]
......
import re
import sys
from typing import Any # NOQA pylint: disable=unused-import
from lxml import etree
from commonPy.asnAST import AsnBool, AsnInt, AsnReal, \
AsnEnumerated, AsnOctetString, AsnSequenceOf, AsnSet, \
AsnSetOf, AsnSequence, AsnChoice, AsnMetaMember
from typing import Any # NOQA
# Level of verbosity
g_verboseLevel = 0
......@@ -113,6 +114,9 @@ class Attributes:
whatever = lxmlEtreeNode.get('href', None)
print a.get('title', None)
'''
base = None # type: str
sourceline = None # type: int
def __init__(self, t):
'''Argument t is an lxml Etree node.'''
self._attrs = {} # type: Dict[str, Any]
......@@ -146,7 +150,8 @@ def MapSMP2Type(attrs, enumOptions, itemTypes, fields):
except: # pragma: no cover
return None # pragma: no cover
dataDict = {"asnFilename": attrs.base, "lineno": attrs.sourceline}
if attrs.type == 'Types:Integer':
def HandleTypesInteger():
low = getMaybe(int, attrs.Minimum)
high = getMaybe(int, attrs.Maximum)
if low == 0 and high == 1:
......@@ -157,21 +162,15 @@ def MapSMP2Type(attrs, enumOptions, itemTypes, fields):
span = [low, high] if low is not None and high is not None else []
dataDict["range"] = span
return AsnInt(**dataDict)
elif attrs.type == 'Types:Float':
def HandleTypesFloat():
low = getMaybe(float, attrs.Minimum)
high = getMaybe(float, attrs.Maximum)
span = [low, high] if low is not None and high is not None else []
dataDict["range"] = span
return AsnReal(**dataDict)
elif attrs.type == 'Types:Enumeration':
dataDict["members"] = enumOptions
return AsnEnumerated(**dataDict)
elif attrs.type == 'Types:String':
high = getMaybe(int, attrs.Length)
span = [high, high] if high is not None else []
dataDict["range"] = span
return AsnOctetString(**dataDict)
elif attrs.type == 'Types:Array':
def HandleTypesArray():
if itemTypes == []:
panic("Missing mandatory ItemType element", location) # pragma: no cover
itemTypeAttrs = Attributes(itemTypes[0])
......@@ -209,7 +208,8 @@ def MapSMP2Type(attrs, enumOptions, itemTypes, fields):
# in the FixupOutOfOrderIdReferences function.
dataDict['containedType'] = containedHref
return AsnSequenceOf(**dataDict)
elif attrs.type == 'Types:Structure': # pylint: disable=too-many-nested-blocks
def HandleTypesStructure():
members = []
for field in fields:
try:
......@@ -259,6 +259,23 @@ def MapSMP2Type(attrs, enumOptions, itemTypes, fields):
else:
dataDict['members'] = members
return AsnSequence(**dataDict)
if attrs.type == 'Types:Integer':
return HandleTypesInteger()
elif attrs.type == 'Types:Float':
return HandleTypesFloat()
elif attrs.type == 'Types:Enumeration':
dataDict["members"] = enumOptions
return AsnEnumerated(**dataDict)
elif attrs.type == 'Types:String':
high = getMaybe(int, attrs.Length)
span = [high, high] if high is not None else []
dataDict["range"] = span
return AsnOctetString(**dataDict)
elif attrs.type == 'Types:Array':
return HandleTypesArray()
elif attrs.type == 'Types:Structure': # pylint: disable=too-many-nested-blocks
return HandleTypesStructure()
panic("Failed to map... (%s)" % attrs.type, location) # pragma: no cover
......@@ -384,7 +401,9 @@ def ConvertCatalogueToASN_AST(inputSmp2Files):
enumOptions = []
if a.type == 'Types:Enumeration':
for node in t.xpath("Literal"):
enumOptions.append([x.replace('_', '-').lower() for x in map(node.get, ['Name', 'Value'])])
enumOptions.append(
[x.replace('_', '-').lower()
for x in [node.get('Name'), node.get('Value')]])
# 2. ItemType data (used in arrays)
itemTypes = t.xpath("ItemType")
......
......@@ -19,7 +19,7 @@
# generated code.
#
import re
from typing import Any, Dict, List # NOQA
from typing import Any, Dict, List # NOQA pylint: disable=unused-import
import commonPy.asnParser
......@@ -41,7 +41,7 @@ def CleanName(name: str) -> str:
def CreatePseudoType(
pseudoType: str, origASTnode: AsnNode,
names: Dict[str, AsnNode], results: List[str]) -> str:
names: Dict[str, AsnNode], results: List[str]) -> str: # pylint: disable=invalid-sequence-index
# if such a pseudo type already exists, add "_t" postfix until you get
# one that doesn't exist.
if pseudoType in names and names[pseudoType] != origASTnode:
......@@ -60,7 +60,8 @@ def ScanChildren(
nodeTypename: str,
node: AsnNode,
names: Dict[str, AsnNode],
results: List[str],
results: List[str], # pylint: disable=invalid-sequence-index
isRoot=False,
createInnerNodesInNames=True):
'''
......@@ -121,7 +122,7 @@ def ScanChildren(
# ... and change the AST, placing the string value (pseudoType)
# inside the _containedType member (i.e. remove the pointer to the AST node)
node._containedType = pseudoType
elif isinstance(node, (AsnSet, AsnSequence, AsnChoice)):
elif isinstance(node, (AsnSet, AsnSequence, AsnChoice)): # pylint: disable=too-many-nested-blocks
# If we are here via a recursive call from a "parent" ScanChildren,
# add the SET/SEQUENCE/CHOICE nodeTypename to the dependency list.
if (not isRoot) and nodeTypename not in results:
......
......@@ -30,46 +30,46 @@ from commonPy.asnAST import (
class RecursiveMapper:
def maybeElse(self, childNo: int) -> str:
def maybeElse(self, childNo: int) -> str: # pylint: disable=no-self-use
if childNo == 1:
return ""
else:
return "else "
def CleanName(self, fieldName: str) -> str:
def CleanName(self, fieldName: str) -> str: # pylint: disable=no-self-use
return re.sub(r'[^a-zA-Z0-9_]', '_', fieldName)
def Version(self):
def Version(self): # pylint: disable=no-self-use
panicWithCallStack("Method undefined in a RecursiveMapper...")
def MapInteger(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
def MapInteger(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names): # pylint: disable=no-self-use
panicWithCallStack("Method undefined in a RecursiveMapper...")
def MapReal(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
def MapReal(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names): # pylint: disable=no-self-use
panicWithCallStack("Method undefined in a RecursiveMapper...")
def MapBoolean(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
def MapBoolean(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names): # pylint: disable=no-self-use
panicWithCallStack("Method undefined in a RecursiveMapper...")
def MapOctetString(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
def MapOctetString(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names): # pylint: disable=no-self-use
panicWithCallStack("Method undefined in a RecursiveMapper...")
def MapEnumerated(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
def MapEnumerated(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names): # pylint: disable=no-self-use
panicWithCallStack("Method undefined in a RecursiveMapper...")
def MapSequence(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
def MapSequence(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names): # pylint: disable=no-self-use
panicWithCallStack("Method undefined in a RecursiveMapper...")
def MapSet(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
def MapSet(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names): # pylint: disable=no-self-use
panicWithCallStack("Method undefined in a RecursiveMapper...")
def MapChoice(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
def MapChoice(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names): # pylint: disable=no-self-use
panicWithCallStack("Method undefined in a RecursiveMapper...")
def MapSequenceOf(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
def MapSequenceOf(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names): # pylint: disable=no-self-use
panicWithCallStack("Method undefined in a RecursiveMapper...")
def MapSetOf(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
def MapSetOf(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names): # pylint: disable=no-self-use
panicWithCallStack("Method undefined in a RecursiveMapper...")
def Map(self,
......@@ -77,7 +77,7 @@ class RecursiveMapper:
destVar: str,
node_or_str: Union[str, AsnNode],
leafTypeDict: Dict[str, str],
names: Dict[str, AsnNode]) -> List[str]:
names: Dict[str, AsnNode]) -> List[str]: # pylint: disable=invalid-sequence-index
if isinstance(node_or_str, str):
node = names[node_or_str] # type: AsnNode
else:
......
......@@ -43,7 +43,7 @@ import re
import platform
import traceback
from typing import Dict, Union, Match # NOQA
from typing import Dict, Union, Match # NOQA pylint: disable=unused-import
from . import configMT
......
......@@ -31,7 +31,6 @@ import commonPy.asnAST
from commonPy.asnAST import AsnNode
import commonPy.configMT
from functools import reduce
def VerifyNodeRange(node):
......@@ -72,7 +71,7 @@ on the exact location of the offending type in the ASN.1 grammar.'''
panic("SequenceOf (in %s) must have SIZE range set!\n" % node.Location())
elif isinstance(node, commonPy.asnAST.AsnEnumerated):
if reduce(lambda x, y: x or y, [x[1] is None for x in node._members]):
if any(x[1] is None for x in node._members):
panic("ENUMERATED must have integer value for each enum! (%s)" % node.Location())
......
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