From 6859fb29bb766af54a78527c93081a2d7e7ef209 Mon Sep 17 00:00:00 2001 From: ibarbech Date: Tue, 6 Nov 2018 11:52:34 +0100 Subject: [PATCH] fixed up problem with parser libraries --- learnbot_components_setup.py | 29 +++-- learnbot_dsl/__init__.py | 2 +- .../blocksConfig/parserConfigBlock.py | 104 ++++-------------- learnbot_dsl/learnbotCode/Button.py | 4 +- learnbot_dsl/learnbotCode/LearnBlock.py | 7 +- learnbot_dsl/learnbotCode/VisualBlock.py | 46 ++++---- learnbot_dsl/learnbotCode/guiTabLibrary.py | 36 +++--- learnbot_dsl_setup.py | 16 +-- 8 files changed, 89 insertions(+), 155 deletions(-) diff --git a/learnbot_components_setup.py b/learnbot_components_setup.py index 3c2b013c..88341abe 100644 --- a/learnbot_components_setup.py +++ b/learnbot_components_setup.py @@ -31,22 +31,19 @@ def readVersion(fname): long_description_content_type='text/markdown', python_requires='!=2.*, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4', install_requires=[ - 'matplotlib==2.2.2', - 'opencv_python_headless==3.4.3.18', - 'scipy==1.0.0', - 'tensorflow==1.10.1', - 'pandas==0.19.1', - 'Keras==2.0.5', - 'h5py==2.7.0', - 'epitran==0.4', - 'numpy==1.13.3', - 'Adafruit-PCA9685==1.0.1', - 'ice==0.0.2', - 'PySide==1.2.4', - 'paho_mqtt==1.4.0', - 'picamera==1.13', - 'wiringpi==2.46.0', - 'wiringpi2==2.32.3'], + 'matplotlib==2.2.2', + 'epitran==0.4', + 'opencv_python_headless==3.4.3.18', + 'numpy==1.13.3', + 'Adafruit_PCA9685==1.0.1', + 'ice==0.0.2', + 'PySide==1.2.4', + 'learnbot_components==0.0.3', + 'paho_mqtt==1.4.0', + 'picamera==1.13', + 'wiringpi==2.46.0', + 'wiringpi2==2.32.3' + ], ) if os.path.exists("build.learnbot_components"): shutil.rmtree("build.learnbot_components") diff --git a/learnbot_dsl/__init__.py b/learnbot_dsl/__init__.py index 402a0a03..8a74133f 100644 --- a/learnbot_dsl/__init__.py +++ b/learnbot_dsl/__init__.py @@ -2,4 +2,4 @@ import os, sys path = os.path.dirname(os.path.realpath(__file__)) -__version__ = '0.2.14' \ No newline at end of file +__version__ = '0.2.16' \ No newline at end of file diff --git a/learnbot_dsl/blocksConfig/parserConfigBlock.py b/learnbot_dsl/blocksConfig/parserConfigBlock.py index 506acb41..a45a3aa5 100644 --- a/learnbot_dsl/blocksConfig/parserConfigBlock.py +++ b/learnbot_dsl/blocksConfig/parserConfigBlock.py @@ -2,97 +2,33 @@ from __future__ import print_function, absolute_import import os, json -from pyparsing import * from learnbot_dsl.blocksConfig.blocks import pathBlocks as pathImgBlocks -pathConfig = os.path.dirname(os.path.realpath(__file__)) - -semicolon = Suppress( Word( ";" ) ) -quote = Suppress( Word( "\"" ) ) -op = Suppress( Word( "{" ) ) -cl = Suppress( Word( "}" ) ) -opp = Suppress( Word( "(" ) ) -clp = Suppress( Word( ")" ) ) - -e = CaselessLiteral('E') -point = Literal('.') -plusorminus = Literal('+') | Literal('-') -number = Word(nums) -integer = Combine( Optional(plusorminus) + number ) -# identifier = Word( alphas+"_-+*/=><()", alphanums+"/-_.+*/=><()" ) - -reserved_words = ( Keyword( "block" ) | Keyword( "type" ) | Keyword( "name" ) | Keyword( "file" ) | Keyword( "img" ) | Keyword( "blocktype" ) | Keyword( "languages" ) ) -iden = Word( alphas+"_-+*/=><()", alphanums+"/-_.+*/=><()" ) -identifier = ~reserved_words + iden - - -# -------------------------TYPE-------------------- -TYPE = Group( Suppress( Word( "type" ) ) + identifier ).setResultsName( "type" ) - -# -------------------------LANGUAGE-------------------- -languages = Literal( "ES" ).setResultsName( 'ES' ) | Literal( "FR" ).setResultsName( 'FR' ) | Literal( "EN" ).setResultsName( 'EN' ) | Literal( "P" ).setResultsName( 'P' ) -translation = Group( languages.setResultsName( "language" ) + Suppress( Literal( ":" ) ) + QuotedString( '"' ).setResultsName( "translation" ) ) -translations = Group( Suppress( Literal( "languages" ) ) + translation + ZeroOrMore( Suppress( "," ) + translation ) ).setResultsName( 'translations' ) -tooltip = Group( Suppress( Literal( "tooltip" ) ) + translation + ZeroOrMore( Suppress( "," ) + translation ) ).setResultsName( 'tooltip' ) - - -# -------------------------IMG-------------------- -img = Group( Suppress( Word( "img" ) ) + identifier + ZeroOrMore( Suppress( "," ) + identifier ) ).setResultsName( "img" ) - -# -------------------------name-------------------- -name = Group( Suppress( Word( "name" ) ) + identifier ).setResultsName( "name" ) - -# -------------------------blocktype-------------------- -# typeblock = Group( Suppress( Word( "blocktype" ) ) + identifier ) - -floatnumber = Combine( integer + - Optional( point + Optional(number) ) - # + - # Optional( e + integer ) - ) - -# -------------------------variables-------------------- -var = identifier.setResultsName( "type" )+identifier.setResultsName( "varName" ) + floatnumber.setResultsName( "defaultValue" ) - - -variables = Suppress( Word( "variables" ) )+op+ Group( var )+ZeroOrMore( Group( var ) )+cl - -block = Group( Suppress( Literal( "block" ) ) + op + TYPE + name + Optional( variables ).setResultsName("variables") + img + Optional( translations ) + Optional( tooltip ) + cl ) - -parser = block + ZeroOrMore( block ) -parser.ignore(pythonStyleComment) - -def parserConfigBlock( file ): - fh = open( file, "r" ) - text = fh.read() - fh.close() - r =[] - try: - r=parser.parseString( text ) - except ParseException as pe: - print( pe ) - print("line: {}".format(pe.line)) - print(" "+" "*pe.col+"^") - return r +pathConfig = os.path.dirname(os.path.realpath(__file__)) -def reload_functions(): +def reload_functions(file=None): blocks = None pathsConfig = [pathConfig, os.path.join(os.getenv('HOME'), ".learnblock", "block") ] - for path in pathsConfig: - if not os.path.exists(path): - continue - for f in os.listdir(path): - if os.path.splitext(f)[-1] == ".conf": - f = os.path.join(path, f) - print(f) - with open(f, "rb") as f: - text = f.read() - if blocks is None: - blocks = json.loads(text) - else: - blocks += json.loads(text) + if file is None: + for path in pathsConfig: + if not os.path.exists(path): + continue + for f in os.listdir(path): + if os.path.splitext(f)[-1] == ".conf": + f = os.path.join(path, f) + print(f) + with open(f, "rb") as f: + text = f.read() + if blocks is None: + blocks = json.loads(text) + else: + blocks += json.loads(text) + else: + with open(file, "r") as f: + text = f.read() + blocks = json.loads(text) for b in blocks: for i in range(len(b["img"])): b["img"][i] = os.path.join(pathImgBlocks, b["img"][i]) diff --git a/learnbot_dsl/learnbotCode/Button.py b/learnbot_dsl/learnbotCode/Button.py index 9cf73cb9..fa68ad43 100644 --- a/learnbot_dsl/learnbotCode/Button.py +++ b/learnbot_dsl/learnbotCode/Button.py @@ -78,7 +78,9 @@ def loadImg(self): def updateToolTip(self): try: - text = self.__dicToolTip[getLanguage()] + text = "" + if len(self.__dicToolTip) is not 0: + text = self.__dicToolTip[getLanguage()] sizeline = 0 if len(self.__dicTrans) is not 0: textout = self.__dicTrans[getLanguage()] + ": " diff --git a/learnbot_dsl/learnbotCode/LearnBlock.py b/learnbot_dsl/learnbotCode/LearnBlock.py index b9f31f85..7b253c62 100644 --- a/learnbot_dsl/learnbotCode/LearnBlock.py +++ b/learnbot_dsl/learnbotCode/LearnBlock.py @@ -742,8 +742,11 @@ def load_blocks(self): for k, table in iter(self.dicTables.items()): table.clear() table.setRowCount(0) - self.listNameBlock.clear() - self.listButtons.clear() + try: + self.listNameBlock.clear() + self.listButtons.clear() + except: + pass for b in blocks: if b["name"] in self.listNameBlock: continue diff --git a/learnbot_dsl/learnbotCode/VisualBlock.py b/learnbot_dsl/learnbotCode/VisualBlock.py index c642049d..53854c1e 100644 --- a/learnbot_dsl/learnbotCode/VisualBlock.py +++ b/learnbot_dsl/learnbotCode/VisualBlock.py @@ -1,7 +1,7 @@ from __future__ import print_function, absolute_import from PySide import QtGui,QtCore from math import * -import pickle, os +import pickle, os, json import learnbot_dsl.guis.EditVar as EditVar from learnbot_dsl.learnbotCode.Block import * from learnbot_dsl.learnbotCode.Language import getLanguage @@ -9,12 +9,11 @@ from learnbot_dsl.learnbotCode.Parser import parserLearntBotCodeOnlyUserFuntion, parserLearntBotCode, HEADER from learnbot_dsl.blocksConfig import pathImgBlocks -# class KeyPressEater(QtCore.QObject): -# def eventFilter(self, obj, event): -# print("hola") -# if isinstance(event, QtCore.QMouseEvent) and event.buttons() & QtCore.Qt.RightButton: -# return True -# return False +class KeyPressEater(QtCore.QObject): + def eventFilter(self, obj, event): + if isinstance(event, QtGui.QMouseEvent) and event.buttons() & QtCore.Qt.RightButton: + return True + return False def toLBotPy(inst, ntab=1): text = inst[0] @@ -153,23 +152,22 @@ def __init__(self, parentBlock, parent=None, scene=None): self.sizeIn = 0 self.shouldUpdateConnections = False self.popMenu = QtGui.QMenu(self) - # self.keyPressEater = KeyPressEater(self) + + self.keyPressEater = KeyPressEater(self.popMenu) + self.popMenu.installEventFilter(self.keyPressEater) + action1 = QtGui.QAction(self.trUtf8('Edit'), self) + action1.triggered.connect(self.on_clicked_menu_edit) + self.popMenu.addAction(action1) if self.parentBlock.name not in ["main", "when"]: if self.parentBlock.type is USERFUNCTION and self.parentBlock.typeBlock is COMPLEXBLOCK: action3 = QtGui.QAction(self.trUtf8('Export Block'), self) action3.triggered.connect(self.on_clicked_menu_export_block) - # action3.installEventFilter(self.keyPressEater) self.popMenu.addAction(action3) else: action0 = QtGui.QAction(self.trUtf8('Duplicate'), self) action0.triggered.connect(self.on_clicked_menu_duplicate) - # action0.installEventFilter(self.keyPressEater) self.popMenu.addAction(action0) - action1 = QtGui.QAction(self.trUtf8('Edit'), self) - action1.triggered.connect(self.on_clicked_menu_edit) - # action1.installEventFilter(self.keyPressEater) - self.popMenu.addAction(action1) self.popMenu.addSeparator() action2 = QtGui.QAction(self.trUtf8('Delete'), self) action2.triggered.connect(self.on_clicked_menu_delete) @@ -180,7 +178,7 @@ def on_clicked_menu_export_block(self): if self.parentBlock.name not in ["main", "when"] and self.parentBlock.type is USERFUNCTION and self.parentBlock.typeBlock is COMPLEXBLOCK: self.scene.stopAllblocks() - path = QtGui.QFileDialog.getExistingDirectory(self, self.trUtf8('Select Library'), '.', QtGui.QFileDialog.ShowDirsOnly) + path = QtGui.QFileDialog.getExistingDirectory(self, self.trUtf8('Select Library'), self.scene.parent.libraryPath, QtGui.QFileDialog.ShowDirsOnly) self.scene.startAllblocks() ret = None try: @@ -207,22 +205,20 @@ def on_clicked_menu_export_block(self): (dic, lBInstance.listNameWhens, lBInstance.listUserFunctions, lBInstance.listNameVars, lBInstance.listNameUserFunctions), fichero, 0) # Save config block - with open(os.path.join(path, self.parentBlock.name + ".conf"),'wr') as f: - text ='''block{ - type library - name ''' + self.parentBlock.name + ''' - img block1 - languages ES: "''' + self.parentBlock.name + '''", EN: "''' + self.parentBlock.name + '''" - }''' - f.write(text) + dictBlock = {} + dictBlock["name"] = self.parentBlock.name + dictBlock["type"] = "library" + dictBlock["img"] = ["block1"] + with open(os.path.join(path, self.parentBlock.name + ".conf"),'w') as f: + json.dump([dictBlock], f) # Save script learnCode inst = self.getInstructions() code = "def " + toLBotPy(inst) + "\nend\n\n" - with open(os.path.join(path, self.parentBlock.name + ".lb"), 'wr') as f: + with open(os.path.join(path, self.parentBlock.name + ".lb"), 'w') as f: f.write(code) # Save script python codePython = parserLearntBotCodeOnlyUserFuntion(code) - with open(os.path.join(path, self.parentBlock.name + ".py"), 'wr') as f: + with open(os.path.join(path, self.parentBlock.name + ".py"), 'w') as f: f.write(codePython) pass diff --git a/learnbot_dsl/learnbotCode/guiTabLibrary.py b/learnbot_dsl/learnbotCode/guiTabLibrary.py index 1d2cbe25..ebbca568 100644 --- a/learnbot_dsl/learnbotCode/guiTabLibrary.py +++ b/learnbot_dsl/learnbotCode/guiTabLibrary.py @@ -2,7 +2,7 @@ import os, sys from PySide import QtGui import learnbot_dsl.guis.TabLibrary as TabLibrary -from learnbot_dsl.blocksConfig.parserConfigBlock import parserConfigBlock +from learnbot_dsl.blocksConfig.parserConfigBlock import reload_functions from learnbot_dsl.blocksConfig.blocks import * from learnbot_dsl.blocksConfig.blocks import pathBlocks as imgPath from learnbot_dsl.learnbotCode.Block import * @@ -42,36 +42,36 @@ def __init__(self, parent, path): if os.path.isdir(os.path.abspath(subPath)): for subsubPath in [os.path.join(subPath, x) for x in os.listdir(subPath)]: if os.path.splitext(subsubPath)[-1] == ".conf": - self.load(parserConfigBlock(subsubPath)) + self.load(reload_functions(subsubPath)) - def load(self, functions): + def load(self, blocks): listRepitFuntions = [] - for f in functions: - if f.name[0] in self.parent.listNameUserFunctions or f.name[0] in self.parent.listNameLibraryFunctions: - listRepitFuntions.append(f.name[0]) + for b in blocks: + if b["name"] in self.parent.listNameUserFunctions or b["name"] in self.parent.listNameLibraryFunctions: + listRepitFuntions.append(b["name"]) continue - self.namesFunctions.append(f.name[0]) - self.parent.listNameLibraryFunctions.append(f.name[0]) + self.namesFunctions.append(b["name"]) + self.parent.listNameLibraryFunctions.append(b["name"]) variables = [] funtionType = LIBRARY HUE = HUE_LIBRARY - for img in f.img: + for img in b["img"]: img = os.path.join(imgPath, img) blockType, connections = loadConfigBlock(img) table = self.ui.tableLibrary table.insertRow(table.rowCount()) - dicTrans = {} - for l in f.translations: - dicTrans[l.language] = l.translation - dicToolTip = {} - for l in f.tooltip: - dicToolTip[l.language] = l.translation - button = Block_Button((self.parent, f.name[0], dicTrans, HUE, self.parent.view, self.parent.scene, + tooltip = {} + languages = {} + if "languages" in b: + languages = b["languages"] + if "tooltip" in b: + tooltip = b["tooltip"] + button = Block_Button((self.parent, b["name"], languages, HUE, self.parent.view, self.parent.scene, img + ".png", connections, variables, blockType, table, table.rowCount() - 1, - funtionType, dicToolTip)) + funtionType, tooltip)) self.parent.listButtons.append(button) - self.listButons.append((button,table.rowCount()- 1)) + self.listButons.append((button,table.rowCount() - 1)) table.setCellWidget(table.rowCount() - 1, 0, button) if len(listRepitFuntions) is not 0: text = "" diff --git a/learnbot_dsl_setup.py b/learnbot_dsl_setup.py index 886c0c16..9ee95879 100644 --- a/learnbot_dsl_setup.py +++ b/learnbot_dsl_setup.py @@ -34,25 +34,25 @@ def readVersion(fname): install_requires=[ "requests", "pyunpack", - "patool", + # "patool", "opencv-python-headless==3.4.3.18", # 'numpy==1.14.5', - 'matplotlib==2.2.2', + # 'matplotlib==2.2.2', # 'pyparsing==2.0.3', 'imutils==0.5.1', 'six==1.10.0', - 'scipy==1.0.0', + # 'scipy==1.0.0', 'tensorflow==1.10.1', 'dlib==19.13.1', - 'pandas==0.19.1', + # 'pandas==0.19.1', 'paramiko==2.4.1', - 'Keras==2.0.5', - 'h5py==2.7.0', - 'epitran==0.4', + # 'Keras==2.0.5', + # 'h5py==2.7.0', + # 'epitran==0.4', # 'zeroc-ice==3.6.0', 'Pillow==5.3.0', # 'PySide', - 'GitPython==2.1.11', + # 'GitPython==2.1.11', 'paho_mqtt==1.4.0', 'future'], )