def testClear2(self): event = Event() event += self.event1 event += self.event2 event.clear() self.assertEqual(len(event), 0) event(111) self.assertEqual(self.value1, 0) self.assertEqual(self.value2, 0)
def testClear1(self): event = Event() self.assertEqual(len(event), 0) event.clear() self.assertEqual(len(event), 0)
class VariablesDialogController(object): ''' Controller to manage VariablesDialog. ''' def __init__(self, application): self._application = application self.onFinishDialogEvent = Event() self._parser = None self._selectedText = u'' self._config = SnippetsConfig(self._application.config) self._recentSnippetPath = None self._dialog = VariablesDialog(self._application.mainWindow) self._dialog.ok_button.Bind(wx.EVT_BUTTON, handler=self._onOk) self._dialog.Bind(EVT_VAR_CHANGE, handler=self._onVarChange) self._dialog.SetClientSize((self._config.variablesDialogWidth, self._config.variablesDialogHeight)) @property def dialog(self): return self._dialog def ShowDialog(self, selectedText, template, template_path): self._recentSnippetPath = template_path dirname = os.path.dirname(template_path) self._selectedText = selectedText self._parser = SnippetParser(template, dirname, self._application) variables_list = self._parser.getVariables() # Get non builtin variables variables = sorted([var for var in variables_list if not var.startswith('__')]) self.dialog.hideVarPanel() self.dialog.setSnippetText(template) [*map(lambda var: self.dialog.addStringVariable(var), variables)] shortTemplateName = self._getShortTemplateName(template_path) self.dialog.templateFileName = shortTemplateName self.dialog.wikiCommandChecked = False # Show dialog if user must enter variable's values self._updateResult() if (self._application.selectedPage is not None and self._application.selectedPage.getTypeString() == u'wiki'): self.dialog.setWikiCommandSetVisible(True) else: self.dialog.setWikiCommandSetVisible(False) self.dialog.setFocusToFirstVariable() self.dialog.Show() def _getShortTemplateName(self, template_fname): ''' Convert full template path to short path ''' snippets_dir = getSnippetsDir() shortTemplateName = template_fname if shortTemplateName.startswith(snippets_dir): shortTemplateName = shortTemplateName[len(snippets_dir) + 1:] shortTemplateName = shortTemplateName.replace(u'\\', u'/') return shortTemplateName def destroy(self): self.onFinishDialogEvent.clear() self.dialog.ok_button.Unbind(wx.EVT_BUTTON, handler=self._onOk) self.dialog.Unbind(EVT_VAR_CHANGE, handler=self._onVarChange) self.dialog.Close() self.dialog.Destroy() def FinishDialog(self): try: w, h = self._dialog.GetClientSize() self._config.variablesDialogWidth = w self._config.variablesDialogHeight = h self._config.recentSnippet = self._recentSnippetPath except EnvironmentError: pass text = self.GetResult() self.onFinishDialogEvent(FinishDialogParams(text)) self.dialog.Close() def CloseDialog(self): self.dialog.Close() def GetResult(self): if not self.dialog.wikiCommandChecked: return self.dialog.getResult() else: return self._makeWikiCommand(self.dialog.getVarDict(), self.dialog.templateFileName) def _makeWikiCommand(self, vardict, template_name): vars_str = u'' for varname in sorted(vardict.keys()): value = vardict[varname] if u'"' not in value: vars_str += u' {name}="{value}"'.format(name=varname, value=vardict[varname]) else: vars_str += u" {name}='{value}'".format(name=varname, value=vardict[varname]) result = u'(:snip file="{template}"{vars}:)(:snipend:)'.format( template=template_name, vars=vars_str) return result def _onOk(self, event): self.FinishDialog() def _onVarChange(self, event): self._updateResult() def _updateResult(self): variables = self.dialog.getVarDict() text = self._parser.process(self._selectedText, self._application.selectedPage, **variables) self.dialog.setResult(text)
class VariablesDialogController(object): ''' Controller to manage VariablesDialog. ''' def __init__(self, application): self._application = application self.onFinishDialog = Event() self._dialog = None self._parser = None self._selectedText = u'' def ShowDialog(self, selectedText, template): if self._application.selectedPage is None: return if self._dialog is None: self._dialog = VariablesDialog(self._application.mainWindow, self._application) self._dialog.ok_button.Bind(wx.EVT_BUTTON, handler=self._onOk) self._dialog.Bind(EVT_VAR_CHANGE, handler=self._onVarChange) self._selectedText = selectedText self._parser = SnippetParser(template, self._application) # Get non builtin variables variables = sorted([var for var in self._parser.getVariables() if not var.startswith('__')]) self._dialog.setSnippetText(template) map(lambda var: self._dialog.addStringVariable(var), variables) # Show dialog if user must enter variable's values if variables: self._updateResult() self._dialog.Show() else: self._finishDialog() def destroy(self): self.onFinishDialog.clear() if self._dialog is not None: self._dialog.ok_button.Unbind(wx.EVT_BUTTON, handler=self._onOk) self._dialog.Unbind(EVT_VAR_CHANGE, handle=self._onVarChange) self._dialog.Close() self._dialog = None def _finishDialog(self): if self._application.selectedPage is not None: text = self._dialog.getResult() self.onFinishDialog(FinishDialogParams(text)) self._dialog.Close() self._dialog = None def _onOk(self, event): self._finishDialog() def _onVarChange(self, event): self._updateResult() def _updateResult(self): variables = self._dialog.getVarDict() text = self._parser.process(self._selectedText, self._application.selectedPage, **variables) self._dialog.setResult(text)