Commit b57e42df authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Standalone editor instantiate ctype

parent 6a3f0112
......@@ -412,7 +412,7 @@ class asn1Editor(QTreeView):
def getVariable(self, root=None, dest=None):
''' Read the ASN.1 variable from the tree editor
root allows to specify which variable/row to start from
dest is an optional ctypes instance of the given type, that
dest is a ctypes instance of the given type, that
will be filled by the recursive parser.
'''
assert dest is not None
......
......@@ -74,9 +74,23 @@ try:
except ImportError:
DV = None
def import_DV(asnFile=''): # type: str -> Module
''' Generate DV.py from the user-provided asn1 file '''
global DV
if not DV:
try:
module = asn1scc.asn2dataModel(asnFile)
DV = module.DV
except TypeError as err:
logger.error('Could not execute asn2dataModel: ' + str(err))
sys.exit(1)
return module
def asn1sccToasn1ValueEditorTypes(dataview, name, asntype):
''' Convert an ASN.1 type from the Python AST of asn1scc v3
to the AST of the value editor '''
assert DV is not None
result = {'nodeTypename': name}
t = asntype
# find basic type
......@@ -123,7 +137,7 @@ def asn1sccToasn1ValueEditorTypes(dataview, name, asntype):
for child, childtype in t.Children.viewitems():
result['choices'].append(
asn1sccToasn1ValueEditorTypes(dataview, child, childtype.type))
result['choiceIdx'] = {enumerant: "{}".format(chty.EnumID)
result['choiceIdx'] = {enumerant: getattr(DV, chty.EnumID)
for enumerant, chty in t.Children.viewitems()}
elif t.kind.endswith('StringType'):
result['type'] = 'STRING'
......@@ -152,13 +166,8 @@ class SingleValueEditor(QObject):
self.parent = parent
self.tc = {}
global DV
if not DV:
try:
DV = asn1scc.asn2dataModel(asnFile).DV
except TypeError as err:
logger.error('Could not execute asn2dataModel: ' + str(err))
sys.exit(1)
# Make sure DV.py is present and imported
self.asn1ctypes = import_DV(asnFile)
try:
logger.info(asnFile)
......@@ -212,7 +221,7 @@ class SingleValueEditor(QObject):
def _asnTypeWidget(self, name, asnType):
logger.debug("Creating new asnType widget. Name: %(pname)s, asnType: %(asnType)s.",
logger.debug("Creating new asnType widget. Name: %(pname)s, asnType: %(asnType)s.",
{"pname": name, "asnType": asnType})
if name in self.widgets:
......@@ -239,12 +248,8 @@ class SingleValueEditor(QObject):
self.widgets[name]['editor'].hideExtraColumns()
self.widgets[name]['editor'].setAsn1Model(pyType)
def editValue(self, param, defValue=None):
return self.editParam(param, defValue)
def editParam(self, param, defValue=None, asnType=None):
''' Edit parameter (open widget, wait for OK/Cancel and read value) '''
def setupEdit(self, param, defValue=None, asnType=None):
''' Initialize the parameter edition '''
if not self.initializedWidgets:
self.initWidgets()
......@@ -255,13 +260,24 @@ class SingleValueEditor(QObject):
self._asnTypeWidget(param, asnType)
widget = self.widgets[param]
# Create a ctypes instance of the variable and set it to the editor
widget['editor'].asn1Instance = getattr(self.asn1ctypes,
asnType.replace('-', '_'))()
if defValue:
pythonVar = fromValueNotationToPySide(param, defValue)
widget['editor'].updateVariable(pythonVar)
return widget
def editValue(self, param, defValue=None):
return self.editParam(param, defValue)
def editParam(self, param, defValue=None, asnType=None):
''' Edit parameter (open widget, wait for OK/Cancel and read value) '''
widget = setupEdit(param, defValue, asnType)
response = widget['widget'].exec_()
if response:
data = widget['editor'].getVariable()
data = widget['editor'].getVariable(dest=widget['editor'].asn1Instance)
value = toASN1ValueNotation(data[param.replace('_', '-')])
return value
else:
......
......@@ -6,10 +6,17 @@ sys.path.insert(0, '../..')
from asn1_value_editor import standalone_editor as editor
editor.logger.setLevel(editor.logging.DEBUG)
def test_1():
'''
Use py.test2.7 to run these tests, and make sure you have installed pytest-qt
pip install --user pytest-qt
'''
def test_1(qtbot):
''' Test with a single boolean type '''
s = editor.SingleValueEditor('data/dv2.asn',
paramsAndTypes={'param': 'Ahah'})
widget = s.setupEdit('Ahah')
print widget['editor'].getVariable(dest=widget['editor'].asn1Instance)
assert s is not None
......
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