3 """Main module of the Plugin for gedit that allow flake8 integration
5 :author: Sébastien CHAZALLET <s.chazallet@gmail.com>
6 :organization: InsPyration EURL
7 :copyright: Copyright © InsPyration EURL <www.inspyration.org>
8 :license: GPL 3 <http://www.gnu.org/licenses/gpl.html>
14 from gettext import gettext as _
16 from gi.repository import GObject, Gtk, Gedit, PeasGtk
18 from integrated import check_code_and_get_formated_result as check_code
19 from settings import SettingsManager
20 from tree import GeditFlake8Tree
21 from configuration import GeditFlake8ConfigWidget
23 # Menu to insert in gedit's 'Tools' menu
25 <menubar name="MenuBar">
26 <menu name="ToolsMenu" action="Tools">
27 <placeholder name="ToolsOps_2">
28 <menuitem name="GeditFlake8" action="GeditFlake8"/>
36 class GeditFlake8WindowActivatable(GObject.Object,
37 Gedit.WindowActivatable,
38 PeasGtk.Configurable):
39 """Main module of the gedit flake8 plugin"""
41 __gtype_name__ = "GeditFlake8WindowActivatable"
43 window = GObject.property(type=Gedit.Window)
48 :type action: function
49 :param action: Called function that we intercept prints
53 GObject.Object.__init__(self)
55 self._settings = SettingsManager()
56 self.flake8_tree = None
58 def do_activate(self):
59 """Activate the plugin
61 :type self: GeditFlake8WindowActivatable
62 :param self: Current plugin
66 # Get the Gtk.UIManager
67 manager = self.window.get_ui_manager()
69 # Create a new action group
70 self._action_group = Gtk.ActionGroup("GeditFlake8PluginActions")
71 self._action_group.add_actions([("GeditFlake8",
73 _("Check Python source code"),
75 _("Check Python source code"),
76 self.on_check_source_code)])
78 # Insert the action group
79 manager.insert_action_group(self._action_group, -1)
82 self._ui_id = manager.add_ui_from_string(ui_str)
85 self.flake8_tree = GeditFlake8Tree(self.window)
87 def do_deactivate(self):
88 """Deactivate the plugin
90 :type self: GeditFlake8WindowActivatable
91 :param self: Current plugin
95 # Remove the tree from the side panel
96 self.flake8_tree.remove()
99 self.flake8_tree = None
101 # Get the Gtk.UIManager
102 manager = self.window.get_ui_manager()
105 manager.remove_ui(self._ui_id)
107 # Remove the action group
108 manager.remove_action_group(self._action_group)
110 # Delete the action group object
111 self._action_group = None
113 # Make sure the manager updates
114 manager.ensure_update()
116 def do_update_state(self):
117 """Allow to activate the plugin only if the document is python code
118 If the document is a new document, it hasto be saved or the language of
119 the document has to be set.
121 :type self: GeditFlake8WindowActivatable
122 :param self: Current plugin
127 doc = self.window.get_active_document()
129 language = doc.get_language()
130 if language is not None:
131 state = language.get_id() in ["python", "python3"]
132 self._action_group.set_sensitive(state)
134 def do_create_configure_widget(self):
135 """Action when a user want to configure the plugin
137 :type self: GeditFlake8WindowActivatable
138 :param self: Current plugin
140 :rtype: GeditFlake8ConfigWidget
141 :return: a configuration widget
143 return GeditFlake8ConfigWidget(self._settings)
145 def on_check_source_code(self, action):
146 """The user ask to check code source :
147 1\ check there is a doc and the doc is python code
149 3\ put flake8 results into the tree and activate it
151 :type self: GeditFlake8WindowActivatable
152 :param self: Current plugin
154 :type action: Gtk.Action
155 :param action: Called function that we intercept prints
160 # Get the document content
161 doc = self.window.get_active_document()
165 # check the language of current document is set
166 language = doc.get_language()
170 # check the language of current document is python
171 if language.get_id() not in ["python", "python3"]:
174 # Use flake8 to get warnings
175 warnings = check_code(
176 doc.get_text(doc.get_start_iter(), doc.get_end_iter(), False),
177 map(str.upper, self._settings.get_ignore_list()),
178 self._settings.get_complexity())
181 self.flake8_tree.update_model(warnings)
184 self.flake8_tree.activate()