Commit 683b7614 authored by Thanassis Tsiodras's avatar Thanassis Tsiodras

Added type annotations to utilities. Started typesafe-ing the first A mapper

parent 4671dc45
......@@ -31,7 +31,7 @@ from commonPy.asnParser import g_names, g_leafTypeDict, CleanNameForAST
from commonPy.utility import panic, warn
g_sqlalchemyOutput = None
g_innerTypes = {}
g_innerTypes = {} # type: Dict[str, int]
g_uniqueStringOfASN1files = ""
g_outputDir = "."
g_asnFiles = None
......@@ -75,7 +75,7 @@ def Version():
print("$Id$") # pragma: no cover
g_dependencyGraph = {}
g_dependencyGraph = {} # type: Dict[str, Dict[str, int]]
def FixupAstForSQLAlchemy():
......
......@@ -19,20 +19,24 @@
# generated code.
#
import re
from typing import Union, List, Dict
from commonPy.utility import panicWithCallStack
from commonPy.asnAST import AsnBasicNode, AsnSequence, AsnSet, AsnChoice, AsnSequenceOf, AsnSetOf, AsnEnumerated, AsnMetaMember
from commonPy.asnAST import (
AsnBasicNode, AsnSequence, AsnSet, AsnChoice, AsnSequenceOf,
AsnSetOf, AsnEnumerated, AsnMetaMember, AsnNode
)
class RecursiveMapper:
def maybeElse(self, childNo):
def maybeElse(self, childNo: int) -> str:
if childNo == 1:
return ""
else:
return "else "
def CleanName(self, fieldName):
def CleanName(self, fieldName: str) -> str:
return re.sub(r'[^a-zA-Z0-9_]', '_', fieldName)
def Version(self):
......@@ -68,10 +72,17 @@ class RecursiveMapper:
def MapSetOf(self, unused_srcVar, unused_destVar, unused_node, unused_leafTypeDict, unused_names):
panicWithCallStack("Method undefined in a RecursiveMapper...")
def Map(self, srcVar, destVar, node, leafTypeDict, names):
if isinstance(node, str):
node = names[node]
lines = []
def Map(self,
srcVar,
destVar,
node_or_str: Union[str, AsnNode],
leafTypeDict: Dict[str, str],
names: Dict[str, AsnNode]) -> List[str]:
if isinstance(node_or_str, str):
node = names[node_or_str] # type: AsnNode
else:
node = node_or_str
lines = [] # type: List[str]
if isinstance(node, AsnBasicNode):
realLeafType = leafTypeDict[node._leafType]
if realLeafType == "INTEGER":
......
......@@ -43,49 +43,52 @@ import re
import platform
import traceback
from typing import Dict
from . import configMT
def inform(format, *args):
def inform(fmt: str, *args) -> None:
if configMT.verbose:
print(format % args)
print(fmt % args)
def warn(format, *args):
sys.stderr.write(("WARNING: " + format) % args)
def warn(fmt: str, *args) -> None:
sys.stderr.write(("WARNING: " + fmt) % args)
sys.stderr.write("\n")
def panic(x):
def panic(x: str) -> None:
if not x.endswith("\n"):
x += "\n"
sys.stderr.write("\n"+chr(27)+"[32m" + x + chr(27) + "[0m\n")
sys.stderr.write("\n" + chr(27) + "[32m" + x + chr(27) + "[0m\n")
sys.exit(1)
def panicWithCallStack(msg):
def panicWithCallStack(msg: str) -> None:
if configMT.verbose:
sys.stderr.write("\n"+chr(27)+"[32m" + msg + chr(27) + "[0m\n")
sys.stderr.write("\nCall stack was:\n%s\n" % "".join(traceback.format_stack()))
sys.stderr.write("\n" + chr(27) + "[32m" + msg + chr(27) + "[0m\n")
sys.stderr.write(
"\nCall stack was:\n%s\n" % "".join(traceback.format_stack()))
else:
panic(msg)
def lcfirst(word):
def lcfirst(word: str) -> str:
if len(word):
return word[:1].lower() + word[1:]
else:
return word
def ucfirst(word):
def ucfirst(word: str) -> str:
if len(word):
return word[:1].upper() + word[1:]
else:
return word
def collapseCAPSgroups(word):
def collapseCAPSgroups(word: str) -> str:
while 1:
m = re.match('^(.*?)([A-Z][A-Z]+)(.*?)$', word)
if m:
......@@ -95,8 +98,8 @@ def collapseCAPSgroups(word):
return word
def readContexts(tapNumbers):
data={}
def readContexts(tapNumbers: str) -> Dict[str, str]:
data = {}
for line in open(tapNumbers, "r").readlines():
line = line.rstrip(os.linesep)
lista = line.split(":")
......@@ -108,15 +111,17 @@ class Matcher:
def __init__(self, pattern, flags=0):
self._pattern = re.compile(pattern, flags)
self._lastOne = None
self._match = None
self._search = None
def match(self, line):
self._match=re.match(self._pattern, line)
self._lastOne='Match'
self._match = re.match(self._pattern, line)
self._lastOne = 'Match'
return self._match
def search(self, line):
self._search=re.search(self._pattern, line)
self._lastOne='Search'
self._search = re.search(self._pattern, line)
self._lastOne = 'Search'
return self._search
def group(self, idx):
......@@ -125,7 +130,9 @@ class Matcher:
elif self._lastOne == 'Search':
return self._search.group(idx)
else:
return panic("Matcher group called with index %d before match/search!\n" % idx)
return panic(
"Matcher group called with index "
"%d before match/search!\n" % idx)
def groups(self):
if self._lastOne == 'Match':
......@@ -137,7 +144,8 @@ class Matcher:
def mysystem(cmd):
if platform.system() == "Windows" or platform.system().startswith("CYGWIN"):
p = platform.system()
if p == "Windows" or p.startswith("CYGWIN"):
return os.system('"' + cmd + '"')
else:
return os.system(cmd)
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