From 9e7cc49fcdc6bf0842e27946a4e16b67896dca55 Mon Sep 17 00:00:00 2001 From: Christoph Schueler Date: Thu, 17 Jan 2019 12:14:29 +0100 Subject: [PATCH] Add signal group stuff --- pydbc/cgen/templates/dbc.tmpl | 3 +++ pydbc/db/common.py | 22 +++++++++++++++++- pydbc/db/load/ldf.py | 7 +++--- pydbc/db/schema.py | 2 ++ pydbc/dbcListener.py | 1 - pydbc/scripts/vndb_importer.py | 16 ++++++++++--- pydbc/tests/test_signal_groups.py | 37 ++++--------------------------- 7 files changed, 47 insertions(+), 41 deletions(-) diff --git a/pydbc/cgen/templates/dbc.tmpl b/pydbc/cgen/templates/dbc.tmpl index 4960134..d4eca13 100644 --- a/pydbc/cgen/templates/dbc.tmpl +++ b/pydbc/cgen/templates/dbc.tmpl @@ -215,6 +215,9 @@ ${int(desc['Value'])} "${desc['Value_Description']}" \ %endfor ; %endfor +%for sigGroup in db.fetchSignalGroups(): +SIG_GROUP_ ${sigGroup['Message_Id']} ${sigGroup['Name']} ${sigGroup['Value']} : ${' '.join(sigGroup['Signals'])}; +%endfor <% signalValueTypes = db.fetchExtendedSignalValueTypes() %>\ %if signalValueTypes: %for item in signalValueTypes: diff --git a/pydbc/db/common.py b/pydbc/db/common.py index 5876a44..9f296ff 100644 --- a/pydbc/db/common.py +++ b/pydbc/db/common.py @@ -100,9 +100,29 @@ def fetchMessageSignalByMessageIDandSignalName(self, messageID, signalName): result = cur.fetchone() return result + def fetchSignalGroups(self): + cur = self.getCursor() + cur.execute("""SELECT t1.RID, t1.Name, t1.Value, + (SELECT t2.Message_Id FROM Message AS t2 WHERE t2.RID = t1.Message) AS Message_Id FROM Signal_Group as t1 + """) + while True: + row = cur.fetchone() + #print("HELLO SG:", row) + if row is None: + return + else: + result = self.db.createDictFromRow(row, cur.description) + cur2 = self.getCursor() + rid = row[0] + res2 = cur2.execute("""SELECT (SELECT t2.Name FROM Signal AS t2 WHERE t2.RID = t1.signal) AS Name + FROM Signal_Group_Signal AS t1 WHERE t1.Signal_Group = {}""".format(rid)) + signals = [s[0] for s in cur2.fetchall()] + result.update(Signals = signals) + yield result + def fetchSignalReceivers(self, messageId, signalId): cur = self.getCursor() - cur.execute("SELECT (SELECT name FROM Node WHERE RID=node) FROM Node_RxSignal WHERE message=? and signal=?", [messageId, signalId]) + cur.execute("SELECT (SELECT name FROM Node WHERE RID = node) FROM Node_RxSignal WHERE message=? and signal=?", [messageId, signalId]) result = [x[0] for x in cur.fetchall()] return result diff --git a/pydbc/db/load/ldf.py b/pydbc/db/load/ldf.py index 4c80006..831af55 100644 --- a/pydbc/db/load/ldf.py +++ b/pydbc/db/load/ldf.py @@ -32,12 +32,13 @@ from pydbc.types import AttributeType, ValueType, CategoryType from .base import BaseLoader +from pprint import pprint + class LdfLoader(BaseLoader): def __init__(self, db, queryClass): - super(DbcLoader, self).__init__(db, queryClass) - self.comments = Comments() + super(LdfLoader, self).__init__(db, queryClass) def _insertValues(self, cur, tree): - print(tree) + pprint(tree) diff --git a/pydbc/db/schema.py b/pydbc/db/schema.py index 9b1ea61..ac32efc 100644 --- a/pydbc/db/schema.py +++ b/pydbc/db/schema.py @@ -52,6 +52,8 @@ "Node_TxMessage", "Node_TxSig", "Message", + "Signal_Group_Signal", + "Signal_Group", "Signal", "Attribute_Value", "AttributeRel_Value", diff --git a/pydbc/dbcListener.py b/pydbc/dbcListener.py index 8614b6b..75632e7 100644 --- a/pydbc/dbcListener.py +++ b/pydbc/dbcListener.py @@ -363,7 +363,6 @@ def exitSignalGroup(self, ctx): signals = [x.value for x in ctx.signals] ctx.value = dict(messageID = messageID, groupName = groupName, gvalue = gvalue, signals = signals) - def exitCategoryDefinitions(self, ctx): ctx.value = [x.value for x in ctx.items] diff --git a/pydbc/scripts/vndb_importer.py b/pydbc/scripts/vndb_importer.py index 7a1922b..2081cbc 100644 --- a/pydbc/scripts/vndb_importer.py +++ b/pydbc/scripts/vndb_importer.py @@ -68,14 +68,24 @@ def execute(fun, name, *args): else: return True + +class Importer: pass + + +class DbcImporter(Importer): + pass + + +class LdfImporter(Importer): + pass + + def importFile(pth): global ucout - fname = pth.parts[-1] fnbase = pth.stem fnext = pth.suffix - print("PTH-ABS:", pth.parent, fname, ) db = CanDatabase(r"{}.vndb".format(fnbase)) @@ -95,7 +105,7 @@ def importFile(pth): print(hl.errorText(" Exiting import function due to exception while parsing: {}\n".format(str(e))), flush = True) return - #print("Finished ANTLR parsing.", flush = True) + print("Finished ANTLR parsing.", flush = True) loader = DbcLoader(db, Queries) diff --git a/pydbc/tests/test_signal_groups.py b/pydbc/tests/test_signal_groups.py index 40c81ef..2a50f6f 100644 --- a/pydbc/tests/test_signal_groups.py +++ b/pydbc/tests/test_signal_groups.py @@ -1,6 +1,5 @@ - -from pydbc import parser +from pydbc.parser import BaseListener, Parser SG0 = """SIG_GROUP_ 288 Switches 1 : WindowSwitch DoorSwitch;""" @@ -13,36 +12,8 @@ SIG_GROUP_ 2316304896 Coulomb_Count 1 : CC_low CC_high; """ -class MyListener(parser.BaseListener): - - def exitSignalGroups(self, ctx): - items = [x.value for x in ctx.items] - ctx.value = items - self.value = ctx.value - - def exitSignalGroup(self, ctx): - messageID = ctx.messageID.value - groupName = ctx.groupName.value - gvalue = ctx.gvalue.value - signals = [x.value for x in ctx.signals] - ctx.value = dict(messageID = messageID, groupName = groupName, gvalue = gvalue, signals = signals) - -class ParserTestCase(parser.BaseListener): - - def __init__(self, grammar, startSymbol, listener, *args): - super(ParserTestCase, self).__init__() - parser.BaseListener.__init__(self) - self.pw = parser.ParserWrapper(grammar, startSymbol, listener) - - def parse(self, data): - return self.pw.parseFromString(data) - - -class SignalGroups(ParserTestCase): - def __init__(self): - print("*** SignalGroups c-tor") - super(SignalGroups, self).__init__("dbc", "signalGroups", MyListener) +class SignalGroups(BaseListener): def exitSignalGroups(self, ctx): items = [x.value for x in ctx.items] @@ -57,8 +28,8 @@ def exitSignalGroup(self, ctx): ctx.value = dict(messageID = messageID, groupName = groupName, gvalue = gvalue, signals = signals) def test_signal_groups(): - tsg = SignalGroups() - res = tsg.parse(SG0) + tsg = Parser("dbc", "signalGroups", SignalGroups) + res = tsg.parseFromString(SG0) print(res) assert len(res) == 1 res = res[0]