def save(self, event=None): """save header + row x col data to a pickle file """ self.getData(True) # update self.data adjustedNames = False for i, paramName in enumerate(self.data[0]): newName = paramName # ensure its legal as a var name, including namespace check: if self.parent: msg, enable = self.parent._checkName(name=paramName) if msg: # msg not empty means a namespace issue newName = self.parent.exp.namespace.makeValid( paramName, prefix='param') adjustedNames = True elif not valid_var_re.match(paramName): msg, enable = _translate( "Name must be alpha-numeric or _, no spaces"), False newName = _nonalphanumeric_re.sub('_', newName) adjustedNames = True else: msg, enable = "", True # try to ensure its unique: while newName in self.data[0][:i]: adjustedNames = True newName += 'x' # might create a namespace conflict? self.data[0][i] = newName self.header[i].SetValue(newName) # displayed value if adjustedNames: self.tmpMsg.SetLabel(_translate( 'Param name(s) adjusted to be legal. Look ok?')) return False if hasattr(self, 'fileName') and self.fileName: fname = self.fileName else: self.newFile = True fname = self.defaultFileName if self.newFile or not os.path.isfile(fname): fullPath = gui.fileSaveDlg(initFilePath=os.path.split(fname)[0], initFileName=os.path.basename(fname), allowed="Pickle files (*.pkl)|*.pkl") else: fullPath = fname if fullPath: # None if user canceled if not fullPath.endswith('.pkl'): fullPath += '.pkl' f = open(fullPath, 'w') pickle.dump(self.data, f) f.close() self.fileName = fullPath self.newFile = False # ack, sometimes might want relative path if self.parent: self.parent.conditionsFile = fullPath return True
def save(self, event=None): """save header + row x col data to a pickle file """ self.getData(True) # update self.data adjustedNames = False for i, paramName in enumerate(self.data[0]): newName = paramName # ensure its legal as a var name, including namespace check: if self.parent: msg, enable = self.parent._checkName(name=paramName) if msg: # msg not empty means a namespace issue newName = self.parent.exp.namespace.makeValid( paramName, prefix='param') adjustedNames = True elif not valid_var_re.match(paramName): msg, enable = _translate( "Name must be alpha-numeric or _, no spaces"), False newName = _nonalphanumeric_re.sub('_', newName) adjustedNames = True else: msg, enable = "", True # try to ensure its unique: while newName in self.data[0][:i]: adjustedNames = True newName += 'x' # might create a namespace conflict? self.data[0][i] = newName self.header[i].SetValue(newName) # displayed value if adjustedNames: self.tmpMsg.SetLabel( _translate('Param name(s) adjusted to be legal. Look ok?')) return False if hasattr(self, 'fileName') and self.fileName: fname = self.fileName else: self.newFile = True fname = self.defaultFileName if self.newFile or not os.path.isfile(fname): fullPath = gui.fileSaveDlg(initFilePath=os.path.split(fname)[0], initFileName=os.path.basename(fname), allowed="Pickle files (*.pkl)|*.pkl") else: fullPath = fname if fullPath: # None if user canceled if not fullPath.endswith('.pkl'): fullPath += '.pkl' f = open(fullPath, 'w') pickle.dump(self.data, f) f.close() self.fileName = fullPath self.newFile = False # ack, sometimes might want relative path if self.parent: self.parent.conditionsFile = fullPath return True
def checkName(self, event=None, name=None): """check param name (missing, namespace conflict, legal var name) disable save, save-as if bad name """ if self.parent: if event: msg, enable = self.parent._checkName(event=event) else: msg, enable = self.parent._checkName(name=name) else: if (name and not valid_var_re.match(name) or not valid_var_re.match(event.GetString())): msg, enable = _translate( "Name must be alpha-numeric or _, no spaces"), False else: msg, enable = "", True self.tmpMsg.SetLabel(msg) if enable: self.OKbtn.Enable() self.SAVEAS.Enable() else: self.OKbtn.Disable() self.SAVEAS.Disable()
def writeRoutineStartCode(self, buff): if self.params['discard previous'].val: buff.writeIndented('%(name)s.clearEvents()\n' % self.params) active = self.params['active'].val.strip(')], ').lstrip('([, ') if not valid_var_re.match(active): if ',' not in active: active += ',' # to turn an int into tuple active = '(' + active + ')' code = ('%(name)s.active = ' + '%s # tuple or list of int 0..7\n' % active + '%(name)s.setEnabled(%(name)s.active)\n') if self.params['lights'].val: code += '%(name)s.setLights(%(name)s.active)\n' if (self.params['store'].val != 'nothing' or self.params['storeCorrect'].val): code += ("%(name)s.btns = [] # responses stored in .btns and .rt\n" + "%(name)s.rt = []\n") buff.writeIndentedLines(code % self.params)
def addRow(self, row, rowLabel=None): """Add one row of info, either header (col names) or normal data Adds items sequentially; FlexGridSizer moves to next row automatically """ labelBox = wx.BoxSizer(wx.HORIZONTAL) if not rowLabel: if sys.platform == 'darwin': self.SetWindowVariant(variant=wx.WINDOW_VARIANT_SMALL) label = _translate('cond %s:') % str(row + 1 - int(self.hasHeader)).zfill(2) rowLabel = wx.StaticText(self, -1, label=label) rowLabel.SetForegroundColour(darkgrey) if sys.platform == 'darwin': self.SetWindowVariant(variant=wx.WINDOW_VARIANT_NORMAL) labelBox.Add(rowLabel, 1, flag=wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM) self.sizer.Add(labelBox, 1, flag=wx.ALIGN_CENTER) lastRow = [] for col in range(self.cols): # get the item, as unicode for display purposes: if len(str(self.grid[row][col])): # want 0, for example item = str(self.grid[row][col]) else: item = u'' # make a textbox: field = ExpandoTextCtrl(self, -1, item, size=(self.colSizes[col], 20)) field.Bind(EVT_ETC_LAYOUT_NEEDED, self.onNeedsResize) field.SetMaxHeight(100) # ~ 5 lines if self.hasHeader and row == 0: # add a default column name (header) if none provided header = self.grid[0] if item.strip() == '': c = col while self.colName(c) in header: c += 1 field.SetValue(self.colName(c)) field.SetForegroundColour(darkblue) # dark blue # or (self.parent and if not valid_var_re.match(field.GetValue()): # self.parent.exp.namespace.exists(field.GetValue()) ): # was always red when preview .xlsx file -- in # namespace already is fine if self.fixed: field.SetForegroundColour("Red") field.SetToolTip( wx.ToolTip( _translate( 'Should be legal as a variable name (alphanumeric)' ))) field.Bind(wx.EVT_TEXT, self.checkName) elif self.fixed: field.SetForegroundColour(darkgrey) field.SetBackgroundColour(white) # warn about whitespace unless will be auto-removed. invisible, # probably spurious: if (self.fixed or not self.clean) and item != item.strip(): field.SetForegroundColour('Red') # also used in show(): self.warning = _translate('extra white-space') field.SetToolTip(wx.ToolTip(self.warning)) if self.fixed: field.Disable() lastRow.append(field) self.sizer.Add(field, 1) self.inputFields.append(lastRow) if self.hasHeader and row == 0: self.header = lastRow
def addRow(self, row, rowLabel=None): """Add one row of info, either header (col names) or normal data Adds items sequentially; FlexGridSizer moves to next row automatically """ labelBox = wx.BoxSizer(wx.HORIZONTAL) if not rowLabel: if sys.platform == 'darwin': self.SetWindowVariant(variant=wx.WINDOW_VARIANT_SMALL) label = _translate('cond %s:') % str( row + 1 - int(self.hasHeader)).zfill(2) rowLabel = wx.StaticText(self, -1, label=label) rowLabel.SetForegroundColour(darkgrey) if sys.platform == 'darwin': self.SetWindowVariant(variant=wx.WINDOW_VARIANT_NORMAL) labelBox.Add(rowLabel, 1, flag=wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM) self.sizer.Add(labelBox, 1, flag=wx.ALIGN_CENTER) lastRow = [] for col in range(self.cols): # get the item, as unicode for display purposes: if len(str(self.grid[row][col])): # want 0, for example item = str(self.grid[row][col]) else: item = u'' # make a textbox: field = ExpandoTextCtrl( self, -1, item, size=(self.colSizes[col], 20)) field.Bind(EVT_ETC_LAYOUT_NEEDED, self.onNeedsResize) field.SetMaxHeight(100) # ~ 5 lines if self.hasHeader and row == 0: # add a default column name (header) if none provided header = self.grid[0] if item.strip() == '': c = col while self.colName(c) in header: c += 1 field.SetValue(self.colName(c)) field.SetForegroundColour(darkblue) # dark blue # or (self.parent and if not valid_var_re.match(field.GetValue()): # self.parent.exp.namespace.exists(field.GetValue()) ): # was always red when preview .xlsx file -- in # namespace already is fine if self.fixed: field.SetForegroundColour("Red") field.SetToolTip(wx.ToolTip(_translate( 'Should be legal as a variable name (alphanumeric)'))) field.Bind(wx.EVT_TEXT, self.checkName) elif self.fixed: field.SetForegroundColour(darkgrey) field.SetBackgroundColour(white) # warn about whitespace unless will be auto-removed. invisible, # probably spurious: if (self.fixed or not self.clean) and item != item.strip(): field.SetForegroundColour('Red') # also used in show(): self.warning = _translate('extra white-space') field.SetToolTip(wx.ToolTip(self.warning)) if self.fixed: field.Disable() lastRow.append(field) self.sizer.Add(field, 1) self.inputFields.append(lastRow) if self.hasHeader and row == 0: self.header = lastRow