Skip to content

Commit

Permalink
fixed up problem with parser libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
ibarbech committed Nov 6, 2018
1 parent c79d467 commit 6859fb2
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 155 deletions.
29 changes: 13 additions & 16 deletions learnbot_components_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion learnbot_dsl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
import os, sys

path = os.path.dirname(os.path.realpath(__file__))
__version__ = '0.2.14'
__version__ = '0.2.16'
104 changes: 20 additions & 84 deletions learnbot_dsl/blocksConfig/parserConfigBlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
4 changes: 3 additions & 1 deletion learnbot_dsl/learnbotCode/Button.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()] + ": "
Expand Down
7 changes: 5 additions & 2 deletions learnbot_dsl/learnbotCode/LearnBlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 21 additions & 25 deletions learnbot_dsl/learnbotCode/VisualBlock.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
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
from learnbot_dsl.learnbotCode.toQImage import *
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]
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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

Expand Down
36 changes: 18 additions & 18 deletions learnbot_dsl/learnbotCode/guiTabLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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 = ""
Expand Down
16 changes: 8 additions & 8 deletions learnbot_dsl_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
)
Expand Down

0 comments on commit 6859fb2

Please sign in to comment.