コード例 #1
0
    def setUp(self):
        BaseMainWndTest.setUp(self)
        plugins_dir = [u"../plugins/snippets"]
        self._createWiki()
        self._application = Application
        self._createWiki()
        self._application.wikiroot = self.wikiroot

        self.loader = PluginsLoader(Application)
        self.loader.load(plugins_dir)

        from snippets.gui.variablesdialog import VariablesDialogController
        self._controller = VariablesDialogController(self._application)
コード例 #2
0
    def setUp(self):
        self.initApplication()
        self.wikiroot = self.createWiki()
        self.testPage = WikiPageFactory().create(self.wikiroot, "Страница 1",
                                                 [])
        plugins_dir = ["plugins/snippets"]
        self.application.wikiroot = self.wikiroot

        self.loader = PluginsLoader(self.application)
        self.loader.load(plugins_dir)

        from snippets.gui.variablesdialog import VariablesDialogController
        self._controller = VariablesDialogController(self.application)
コード例 #3
0
ファイル: guicontroller.py プロジェクト: sarutobi/outwiker
    def __init__(self, application):
        self._application = application

        self._menu = None
        self._mainMenu = None

        self.MENU_POS = 6
        self._menuName = None

        self._snippets_id = {}
        self._varDialogController = VariablesDialogController(
            self._application
        )

        self._actions = [
            EditSnippetsAction,
            RunRecentSnippet,
            OpenHelpAction,
        ]
コード例 #4
0
    def setUp(self):
        self.initApplication()
        self.wikiroot = self.createWiki()
        self.testPage = WikiPageFactory().create(self.wikiroot,
                                                 "Страница 1",
                                                 [])
        plugins_dir = ["../plugins/snippets"]
        self.application.wikiroot = self.wikiroot

        self.loader = PluginsLoader(self.application)
        self.loader.load(plugins_dir)

        from snippets.gui.variablesdialog import VariablesDialogController
        self._controller = VariablesDialogController(self.application)
コード例 #5
0
    def __init__(self, application):
        self._application = application

        self._menu = None
        self._mainMenu = None

        self.MENU_POS = 6
        self._menuName = None

        self._snippets_id = {}
        self._varDialogController = VariablesDialogController(
            self._application
        )

        self._actions = [
            UpdateMenuAction,
        ]
コード例 #6
0
    def __init__(self, application):
        self._application = application

        self._menu = None
        self._mainMenu = None

        self.MENU_POS = 6
        self._menuName = None

        self._snippets_id = {}
        self._varDialogController = VariablesDialogController(
            self._application
        )

        self._actions = [
            EditSnippetsAction,
            RunRecentSnippet,
            OpenHelpAction,
        ]
コード例 #7
0
class GuiController(object):
    def __init__(self, application):
        self._application = application

        self._menu = None
        self._mainMenu = None

        self.MENU_POS = 6
        self._menuName = None

        self._snippets_id = {}
        self._varDialogController = VariablesDialogController(
            self._application
        )

        self._actions = [
            EditSnippetsAction,
            RunRecentSnippet,
            OpenHelpAction,
        ]

    def initialize(self):
        global _
        _ = get_()

        if self._application.mainWindow is not None:
            self._mainMenu = self._application.mainWindow.mainMenu
            self._menuName = _(u'Snippets')
            self._createMenu()
            self._varDialogController.onFinishDialogEvent += self._onFinishDialog
            self._application.customEvents.bind(defines.EVENT_UPDATE_MENU,
                                                self._onMenuUpdate)
            self._application.customEvents.bind(defines.EVENT_RUN_SNIPPET,
                                                self._onRunSnippet)

    def _createMenu(self):
        self._menu = wx.Menu(u'')

        controller = self._application.actionController
        map(lambda action: controller.appendMenuItem(action.stringId, self._menu),
            self._actions)

        self._menu.AppendSeparator()
        self._updateMenu()
        self._mainMenu.Insert(self.MENU_POS, self._menu, self._menuName)

    def _onMenuUpdate(self, params):
        self._updateMenu()

    def _removeSnippetsFromMenu(self):
        # Remove all snippets
        for snippet_id, snippet_info in reversed(self._snippets_id.items()):
            menu_item = snippet_info.menuitem
            menu = snippet_info.parentmenu
            self._application.mainWindow.Unbind(wx.EVT_MENU,
                                                handler=self._onClick,
                                                id=snippet_id
                                                )
            menu.RemoveItem(menu_item)
            menu_item.Destroy()
            # wx.Window.UnreserveControlId(snippet_id)

        # Count menu items for snippets. (+-1 because of separator exists)
        menu_snippets_count = (self._menu.GetMenuItemCount() -
                               len(self._actions)) - 1

        for _ in range(menu_snippets_count):
            menu_item = self._menu.FindItemByPosition(len(self._actions) + 1)
            self._menu.RemoveItem(menu_item)
            menu_item.Destroy()

        self._snippets_id = {}

    def _updateMenu(self):
        self._removeSnippetsFromMenu()
        sl = SnippetsLoader(getSnippetsDir())
        snippets_tree = sl.getSnippets()
        self._buildTree(snippets_tree, self._menu)

    def _buildTree(self, snippets_tree, menu):
        # Create submenus
        for subdir in sorted(snippets_tree.dirs, key=lambda x: x.name):
            submenu = wx.Menu(u'')
            self._buildTree(subdir, submenu)
            menu.AppendSubMenu(submenu, subdir.name)

        # Create menu items
        menu.AppendSeparator()
        for snippet in sorted(snippets_tree.snippets):
            name = os.path.basename(snippet)
            menu_item_id = wx.Window.NewControlId()
            menu_item = menu.Append(menu_item_id, name)

            self._snippets_id[menu_item_id] = SnippetInfo(snippet,
                                                          menu_item,
                                                          menu)

            self._application.mainWindow.Bind(
                wx.EVT_MENU,
                handler=self._onClick,
                id=menu_item_id
            )

    def _destroyMenu(self):
        index = self._mainMenu.FindMenu(self._menuName)
        assert index != wx.NOT_FOUND

        actionController = self._application.actionController

        map(lambda action: actionController.removeMenuItem(action.stringId),
            self._actions)
        self._mainMenu.Remove(index)
        self._menu = None

    def destroy(self):
        if self._application.mainWindow is not None:
            self._destroyMenu()
            self._varDialogController.onFinishDialogEvent -= self._onFinishDialog

        self._varDialogController.destroy()

    def _onRunSnippet(self, params):
        # type: (RunSnippetParams) -> None
        editor = self._getEditor()
        selectedText = editor.GetSelectedText() if editor is not None else u''

        snippet_fname = params.snippet_fname
        try:
            template = self._loadTemplate(snippet_fname)
        except EnvironmentError:
            MessageBox(
                _(u"Can't read the snippet\n{}").format(snippet_fname),
                _(u"Error"),
                wx.ICON_ERROR | wx.OK)
            return

        try:
            self._varDialogController.ShowDialog(
                selectedText,
                template,
                snippet_fname
            )
        except SnippetException as e:
            MessageBox(e.message, _(u"Snippet error"), wx.ICON_ERROR | wx.OK)

    def _onClick(self, event):
        assert event.GetId() in self._snippets_id
        snippet_fname = self._snippets_id[event.GetId()].filename

        eventParams = RunSnippetParams(snippet_fname)
        self._application.customEvents(defines.EVENT_RUN_SNIPPET, eventParams)

    def _loadTemplate(self, fname):
        template = readTextFile(fname)
        if template.endswith(u'\n'):
            template = template[:-1]
        return template

    def _getEditor(self):
        if self._application.selectedPage is None:
            return None
        pageView = self._application.mainWindow.pagePanel.pageView
        if 'GetEditor' in dir(pageView):
            return pageView.GetEditor()

    def _onFinishDialog(self, params):
        editor = self._getEditor()
        if editor is not None:
            editor.replaceText(params.text)
コード例 #8
0
class SnippetsVarDialogControllerTest(unittest.TestCase, BaseOutWikerGUIMixin):
    def setUp(self):
        self.initApplication()
        self.wikiroot = self.createWiki()
        self.testPage = WikiPageFactory().create(self.wikiroot, "Страница 1",
                                                 [])
        plugins_dir = ["plugins/snippets"]
        self.application.wikiroot = self.wikiroot

        self.loader = PluginsLoader(self.application)
        self.loader.load(plugins_dir)

        from snippets.gui.variablesdialog import VariablesDialogController
        self._controller = VariablesDialogController(self.application)

    def tearDown(self):
        self._controller.destroy()
        self.loader.clear()
        self.destroyApplication()
        self.destroyWiki(self.wikiroot)

    def test_empty(self):
        seltext = ''
        template = ''
        template_name = 'template'
        self.application.selectedPage = self.testPage

        right_result = ''

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_simple(self):
        seltext = ''
        template = 'Шаблон'
        template_name = 'template'
        self.application.selectedPage = self.testPage

        right_result = 'Шаблон'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_global_var_title(self):
        seltext = ''
        template = '{{__title}}'
        template_name = 'template'
        self.application.selectedPage = self.testPage

        right_result = 'Страница 1'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_global_var_title_alias(self):
        self.testPage.alias = 'Псевдоним'

        seltext = ''
        template = '{{__title}}'
        template_name = 'template'
        self.application.selectedPage = self.testPage

        right_result = 'Псевдоним'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_page_none_01(self):
        seltext = ''
        template = 'Шаблон'
        template_name = 'template'
        self.application.selectedPage = None

        right_result = 'Шаблон'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_var_01(self):
        seltext = ''
        template = 'Переменная = {{varname}}'
        template_name = 'template'
        self.application.selectedPage = None

        right_result = 'Переменная = Абырвалг'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.setStringVariable('varname', 'Абырвалг')

        # To process EVT_VAR_CHANGE event
        wx.GetApp().Yield()

        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_var_02(self):
        seltext = ''
        template = 'Переменная = {{varname1}} = {{varname2}}'
        template_name = 'template'
        self.application.selectedPage = None

        right_result = 'Переменная = Абырвалг1 = Абырвалг2'

        self._controller.ShowDialog(seltext, template, template_name)

        self._controller.dialog.setStringVariable('varname1', 'Абырвалг1')
        wx.GetApp().Yield()

        self._controller.dialog.setStringVariable('varname2', 'Абырвалг2')
        wx.GetApp().Yield()

        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_01(self):
        seltext = ''
        template = ''
        template_name = 'template'
        self.application.selectedPage = None

        right_result = '(:snip file="template":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_02(self):
        seltext = ''
        template = ''
        template_name = 'template'
        self.application.selectedPage = None

        right_result = '(:snip file="template":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_03(self):
        seltext = ''
        template = ''
        template_name = 'snippets\\template'
        self.application.selectedPage = None

        right_result = '(:snip file="snippets/template":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_04(self):
        seltext = ''
        template = '{{varname}}'
        template_name = 'template'
        self.application.selectedPage = None

        right_result = '(:snip file="template" varname="Абырвалг":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True

        self._controller.dialog.setStringVariable('varname', 'Абырвалг')
        wx.GetApp().Yield()

        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_05(self):
        seltext = ''
        template = '{{aaa}} {{ccc}} {{x}} {{a}}'
        template_name = 'template'
        self.application.selectedPage = None

        right_result = '(:snip file="template" a="aaaaa" aaa="aaaaa" ccc="ccccc" x="xxxxx":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True

        self._controller.dialog.setStringVariable('aaa', 'aaaaa')
        self._controller.dialog.setStringVariable('ccc', 'ccccc')
        self._controller.dialog.setStringVariable('x', 'xxxxx')
        self._controller.dialog.setStringVariable('a', 'aaaaa')
        wx.GetApp().Yield()

        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)
コード例 #9
0
class SnippetsVarDialogControllerTest(BaseMainWndTest):
    def setUp(self):
        BaseMainWndTest.setUp(self)
        plugins_dir = [u"../plugins/snippets"]
        self._createWiki()
        self._application = Application
        self._createWiki()
        self._application.wikiroot = self.wikiroot

        self.loader = PluginsLoader(Application)
        self.loader.load(plugins_dir)

        from snippets.gui.variablesdialog import VariablesDialogController
        self._controller = VariablesDialogController(self._application)

    def tearDown(self):
        self._controller.destroy()
        BaseMainWndTest.tearDown(self)
        removeDir(self.path)
        self.loader.clear()

    def _createWiki(self):
        self.path = mkdtemp(prefix=u'Абырвалг абыр')

        self.wikiroot = WikiDocument.create(self.path)

        WikiPageFactory().create(self.wikiroot, u"Страница 1", [])
        self.testPage = self.wikiroot[u"Страница 1"]

    def test_empty(self):
        seltext = u''
        template = u''
        template_name = u'template'
        self._application.selectedPage = self.testPage

        right_result = u''

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_simple(self):
        seltext = u''
        template = u'Шаблон'
        template_name = u'template'
        self._application.selectedPage = self.testPage

        right_result = u'Шаблон'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_global_var_title(self):
        seltext = u''
        template = u'{{__title}}'
        template_name = u'template'
        self._application.selectedPage = self.testPage

        right_result = u'Страница 1'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_global_var_title_alias(self):
        self.testPage.alias = u'Псевдоним'

        seltext = u''
        template = u'{{__title}}'
        template_name = u'template'
        self._application.selectedPage = self.testPage

        right_result = u'Псевдоним'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_page_none_01(self):
        seltext = u''
        template = u'Шаблон'
        template_name = u'template'
        self._application.selectedPage = None

        right_result = u'Шаблон'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_var_01(self):
        seltext = u''
        template = u'Переменная = {{varname}}'
        template_name = u'template'
        self._application.selectedPage = None

        right_result = u'Переменная = Абырвалг'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.setStringVariable(u'varname', u'Абырвалг')

        # To process EVT_VAR_CHANGE event
        wx.GetApp().Yield()

        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_var_02(self):
        seltext = u''
        template = u'Переменная = {{varname1}} = {{varname2}}'
        template_name = u'template'
        self._application.selectedPage = None

        right_result = u'Переменная = Абырвалг1 = Абырвалг2'

        self._controller.ShowDialog(seltext, template, template_name)

        self._controller.dialog.setStringVariable(u'varname1', u'Абырвалг1')
        wx.GetApp().Yield()

        self._controller.dialog.setStringVariable(u'varname2', u'Абырвалг2')
        wx.GetApp().Yield()

        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_01(self):
        seltext = u''
        template = u''
        template_name = u'template'
        self._application.selectedPage = None

        right_result = u'(:snip file="template":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_02(self):
        seltext = u''
        template = u''
        template_name = u'template'
        self._application.selectedPage = None

        right_result = u'(:snip file="template":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_03(self):
        seltext = u''
        template = u''
        template_name = u'snippets\\template'
        self._application.selectedPage = None

        right_result = u'(:snip file="snippets/template":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_04(self):
        seltext = u''
        template = u'{{varname}}'
        template_name = u'template'
        self._application.selectedPage = None

        right_result = u'(:snip file="template" varname="Абырвалг":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True

        self._controller.dialog.setStringVariable(u'varname', u'Абырвалг')
        wx.GetApp().Yield()

        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_05(self):
        seltext = u''
        template = u'{{aaa}} {{ccc}} {{x}} {{a}}'
        template_name = u'template'
        self._application.selectedPage = None

        right_result = u'(:snip file="template" a="aaaaa" aaa="aaaaa" ccc="ccccc" x="xxxxx":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True

        self._controller.dialog.setStringVariable(u'aaa', u'aaaaa')
        self._controller.dialog.setStringVariable(u'ccc', u'ccccc')
        self._controller.dialog.setStringVariable(u'x', u'xxxxx')
        self._controller.dialog.setStringVariable(u'a', u'aaaaa')
        wx.GetApp().Yield()

        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)
コード例 #10
0
ファイル: guicontroller.py プロジェクト: sarutobi/outwiker
class GuiController(object):
    def __init__(self, application):
        self._application = application

        self._menu = None
        self._mainMenu = None

        self.MENU_POS = 6
        self._menuName = None

        self._snippets_id = {}
        self._varDialogController = VariablesDialogController(
            self._application
        )

        self._actions = [
            EditSnippetsAction,
            RunRecentSnippet,
            OpenHelpAction,
        ]

    def initialize(self):
        global _
        _ = get_()

        if self._application.mainWindow is not None:
            self._mainMenu = self._application.mainWindow.mainMenu
            self._menuName = _(u'Snippets')
            self._createMenu()
            self._varDialogController.onFinishDialogEvent += self._onFinishDialog
            self._application.customEvents.bind(defines.EVENT_UPDATE_MENU,
                                                self._onMenuUpdate)
            self._application.customEvents.bind(defines.EVENT_RUN_SNIPPET,
                                                self._onRunSnippet)

    def _createMenu(self):
        self._menu = wx.Menu(u'')

        # Snippet's control menu items (actions)
        controller = self._application.actionController
        map(lambda action: controller.appendMenuItem(action.stringId, self._menu),
            self._actions)

        # Snippets list
        self._menu.AppendSeparator()
        self._updateMenu()

        # Added Snippet's menu to main menu
        self._mainMenu.Insert(self.MENU_POS, self._menu, self._menuName)

    def _onMenuUpdate(self, params):
        self._updateMenu()

    def _removeSnippetsFromMenu(self):
        # Remove all snippets
        for snippet_id, snippet_info in reversed(self._snippets_id.items()):
            menu_item = snippet_info.menuitem
            menu = snippet_info.parentmenu
            self._application.mainWindow.Unbind(wx.EVT_MENU,
                                                handler=self._onClick,
                                                id=snippet_id
                                                )
            menu.RemoveItem(menu_item)
            menu_item.Destroy()
            # wx.Window.UnreserveControlId(snippet_id)

        # Count menu items for snippets. (+-1 because of separator exists)
        menu_snippets_count = (self._menu.GetMenuItemCount() -
                               len(self._actions)) - 1

        for _ in range(menu_snippets_count):
            menu_item = self._menu.FindItemByPosition(len(self._actions) + 1)
            self._menu.RemoveItem(menu_item)
            menu_item.Destroy()

        self._snippets_id = {}

    def _updateMenu(self):
        self._removeSnippetsFromMenu()
        sl = SnippetsLoader(getSnippetsDir())
        snippets_tree = sl.getSnippets()
        self._buildTree(snippets_tree, self._menu)

    def _buildTree(self, snippets_tree, menu):
        # Create submenus
        for subdir in sorted(snippets_tree.dirs, key=lambda x: x.name):
            submenu = wx.Menu(u'')
            self._buildTree(subdir, submenu)
            menu.AppendSubMenu(submenu, subdir.name)

        # Create menu items
        if len(snippets_tree.dirs) != 0 and len(snippets_tree.snippets) != 0:
            menu.AppendSeparator()

        # Append snippets
        for snippet in sorted(snippets_tree.snippets):
            name = os.path.basename(snippet)
            menu_item_id = wx.Window.NewControlId()
            menu_item = menu.Append(menu_item_id, name)

            self._snippets_id[menu_item_id] = SnippetInfo(snippet,
                                                          menu_item,
                                                          menu)

            self._application.mainWindow.Bind(
                wx.EVT_MENU,
                handler=self._onClick,
                id=menu_item_id
            )

    def _destroyMenu(self):
        index = self._mainMenu.FindMenu(self._menuName)
        assert index != wx.NOT_FOUND

        actionController = self._application.actionController

        map(lambda action: actionController.removeMenuItem(action.stringId),
            self._actions)
        self._mainMenu.Remove(index)
        self._menu = None

    def destroy(self):
        if self._application.mainWindow is not None:
            self._destroyMenu()
            self._varDialogController.onFinishDialogEvent -= self._onFinishDialog

        self._varDialogController.destroy()

    def _onRunSnippet(self, params):
        # type: (RunSnippetParams) -> None
        editor = self._getEditor()
        selectedText = editor.GetSelectedText() if editor is not None else u''

        snippet_fname = params.snippet_fname
        try:
            template = self._loadTemplate(snippet_fname)
        except EnvironmentError:
            MessageBox(
                _(u"Can't read the snippet\n{}").format(snippet_fname),
                _(u"Error"),
                wx.ICON_ERROR | wx.OK)
            return

        try:
            self._varDialogController.ShowDialog(
                selectedText,
                template,
                snippet_fname
            )
        except SnippetException as e:
            MessageBox(e.message, _(u"Snippet error"), wx.ICON_ERROR | wx.OK)

    def _onClick(self, event):
        assert event.GetId() in self._snippets_id
        snippet_fname = self._snippets_id[event.GetId()].filename

        eventParams = RunSnippetParams(snippet_fname)
        self._application.customEvents(defines.EVENT_RUN_SNIPPET, eventParams)

    def _loadTemplate(self, fname):
        template = readTextFile(fname)
        if template.endswith(u'\n'):
            template = template[:-1]
        return template

    def _getEditor(self):
        if self._application.selectedPage is None:
            return None
        pageView = self._application.mainWindow.pagePanel.pageView
        if 'GetEditor' in dir(pageView):
            return pageView.GetEditor()

    def _onFinishDialog(self, params):
        editor = self._getEditor()
        if editor is not None:
            editor.replaceText(params.text)
コード例 #11
0
class SnippetsVarDialogControllerTest(unittest.TestCase, BaseOutWikerGUIMixin):
    def setUp(self):
        self.initApplication()
        self.wikiroot = self.createWiki()
        self.testPage = WikiPageFactory().create(self.wikiroot,
                                                 "Страница 1",
                                                 [])
        plugins_dir = ["../plugins/snippets"]
        self.application.wikiroot = self.wikiroot

        self.loader = PluginsLoader(self.application)
        self.loader.load(plugins_dir)

        from snippets.gui.variablesdialog import VariablesDialogController
        self._controller = VariablesDialogController(self.application)

    def tearDown(self):
        self._controller.destroy()
        self.loader.clear()
        self.destroyApplication()
        self.destroyWiki(self.wikiroot)

    def test_empty(self):
        seltext = ''
        template = ''
        template_name = 'template'
        self.application.selectedPage = self.testPage

        right_result = ''

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_simple(self):
        seltext = ''
        template = 'Шаблон'
        template_name = 'template'
        self.application.selectedPage = self.testPage

        right_result = 'Шаблон'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_global_var_title(self):
        seltext = ''
        template = '{{__title}}'
        template_name = 'template'
        self.application.selectedPage = self.testPage

        right_result = 'Страница 1'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_global_var_title_alias(self):
        self.testPage.alias = 'Псевдоним'

        seltext = ''
        template = '{{__title}}'
        template_name = 'template'
        self.application.selectedPage = self.testPage

        right_result = 'Псевдоним'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_page_none_01(self):
        seltext = ''
        template = 'Шаблон'
        template_name = 'template'
        self.application.selectedPage = None

        right_result = 'Шаблон'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_var_01(self):
        seltext = ''
        template = 'Переменная = {{varname}}'
        template_name = 'template'
        self.application.selectedPage = None

        right_result = 'Переменная = Абырвалг'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.setStringVariable('varname', 'Абырвалг')

        # To process EVT_VAR_CHANGE event
        wx.GetApp().Yield()

        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_var_02(self):
        seltext = ''
        template = 'Переменная = {{varname1}} = {{varname2}}'
        template_name = 'template'
        self.application.selectedPage = None

        right_result = 'Переменная = Абырвалг1 = Абырвалг2'

        self._controller.ShowDialog(seltext, template, template_name)

        self._controller.dialog.setStringVariable('varname1', 'Абырвалг1')
        wx.GetApp().Yield()

        self._controller.dialog.setStringVariable('varname2', 'Абырвалг2')
        wx.GetApp().Yield()

        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_01(self):
        seltext = ''
        template = ''
        template_name = 'template'
        self.application.selectedPage = None

        right_result = '(:snip file="template":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_02(self):
        seltext = ''
        template = ''
        template_name = 'template'
        self.application.selectedPage = None

        right_result = '(:snip file="template":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_03(self):
        seltext = ''
        template = ''
        template_name = 'snippets\\template'
        self.application.selectedPage = None

        right_result = '(:snip file="snippets/template":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True
        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_04(self):
        seltext = ''
        template = '{{varname}}'
        template_name = 'template'
        self.application.selectedPage = None

        right_result = '(:snip file="template" varname="Абырвалг":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True

        self._controller.dialog.setStringVariable('varname', 'Абырвалг')
        wx.GetApp().Yield()

        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)

    def test_wiki_command_05(self):
        seltext = ''
        template = '{{aaa}} {{ccc}} {{x}} {{a}}'
        template_name = 'template'
        self.application.selectedPage = None

        right_result = '(:snip file="template" a="aaaaa" aaa="aaaaa" ccc="ccccc" x="xxxxx":)(:snipend:)'

        self._controller.ShowDialog(seltext, template, template_name)
        self._controller.dialog.wikiCommandChecked = True

        self._controller.dialog.setStringVariable('aaa', 'aaaaa')
        self._controller.dialog.setStringVariable('ccc', 'ccccc')
        self._controller.dialog.setStringVariable('x', 'xxxxx')
        self._controller.dialog.setStringVariable('a', 'aaaaa')
        wx.GetApp().Yield()

        self._controller.FinishDialog()
        result = self._controller.GetResult()

        self.assertEqual(result, right_result)