beremiz

Fix wxHMI example after upgrading wxGlade

2018-08-13, Andrey Skvortsov
64bb520009f3
Fix wxHMI example after upgrading wxGlade

Newer wxGlade generates code to initialize GridSizer's with empty
elements.

...
grid_sizer_1.Add(self.window_1, 1, wx.ALIGN_CENTER, 0)
sizer_2.Add((0, 0), 0, 0, 0)
sizer_2.Add((0, 0), 0, 0, 0)
sizer_2.Add((0, 0), 0, 0, 0)
sizer_2.Add((0, 0), 0, 0, 0)
...

That causes following traceback, if new buttons are added
to already full sizer.

PLCobject : Traceback (most recent call last):

File "./Beremiz_service.py", line 389, in default_evaluator
res = (tocall(*args, **kwargs), None)

File "/tmp/tmpQS8ct2/runtime_0.py", line 540, in _runtime_0_start
wx.MessageBox(_("Please stop PLC to close"))

File "/tmp/tmpQS8ct2/runtime_0.py", line 504, in Init
lambda axis:( MakeButtonFunc(self, sizer, axis+"axisMinus"),

File "/tmp/tmpQS8ct2/runtime_0.py", line 502, in
lambda btname: MakeButtonFunc(self, sizer, btname), ActionButtons)

File "/tmp/tmpQS8ct2/runtime_0.py", line 461, in MakeButtonFunc
print sizer, btname

File "/usr/lib/python2.7/dist-packages/wx-3.0-gtk3/wx/_core.py", line 14453, in Add
return _core_.Sizer_Add(*args, **kwargs)

PyAssertionError: C++ assertion "Assert failure" failed at
../src/common/sizer.cpp(1401) in DoInsert(): too many items (11 > 2*5)
in grid sizer (maybe you should omit the number of either rows or
columns?)


Tested with wxGlade version 0.8.3
Closes #41
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of Beremiz.
# See COPYING file for copyrights details.
from __future__ import absolute_import
from plcopen.XSLTModelQuery import XSLTModelQuery, _StringValue, _BoolValue, _translate_args
# -------------------------------------------------------------------------------
# Helpers object for generating pou var list
# -------------------------------------------------------------------------------
class _VariableInfos(object):
__slots__ = ["Name", "Class", "Option", "Location", "InitialValue",
"Edit", "Documentation", "Type", "Tree", "Number"]
def __init__(self, *args):
for attr, value in zip(self.__slots__, args):
setattr(self, attr, value if value is not None else "")
def copy(self):
return _VariableInfos(*[getattr(self, attr) for attr in self.__slots__])
class VariablesInfosFactory(object):
""" Helpers object for generating pou var list """
def __init__(self, variables):
self.Variables = variables
self.TreeStack = []
self.Type = None
self.Dimensions = None
def SetType(self, context, *args):
self.Type = args[0][0]
def GetType(self):
if len(self.Dimensions) > 0:
return ("array", self.Type, self.Dimensions)
return self.Type
def GetTree(self):
return (self.TreeStack.pop(-1), self.Dimensions)
def AddDimension(self, context, *args):
self.Dimensions.append(tuple(
_translate_args([_StringValue] * 2, args)))
def AddTree(self, context, *args):
self.TreeStack.append([])
self.Dimensions = []
def AddVarToTree(self, context, *args):
var = (args[0][0], self.Type, self.GetTree())
self.TreeStack[-1].append(var)
def AddVariable(self, context, *args):
self.Variables.append(_VariableInfos(*(
_translate_args([_StringValue] * 5 + [_BoolValue] + [_StringValue], args) +
[self.GetType(), self.GetTree()])))
class VariableInfoCollector(XSLTModelQuery):
def __init__(self, controller):
XSLTModelQuery.__init__(self,
controller,
"variables_infos.xslt",
[(name, self.FactoryCaller(name))
for name in [
"SetType",
"AddDimension",
"AddTree",
"AddVarToTree",
"AddVariable"]])
def FactoryCaller(self, funcname):
def CallFactory(*args):
return getattr(self.factory, funcname)(*args)
return CallFactory
def Collect(self, root, debug, variables, tree):
self.factory = VariablesInfosFactory(variables)
self._process_xslt(root, debug, tree=str(tree))
res = self.factory
self.factory = None
return res