Premier commit : Code et doc (état de publication)
[gedit/flake8] / flake8_integration / windowactivatable.py
1 #-*- coding: utf-8 -*-
2
3 """Main module of the Plugin for gedit that allow flake8 integration
4
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>
9
10 :version: 0.1
11 """
12
13
14 from gettext import gettext as _
15
16 from gi.repository import GObject, Gtk, Gedit, PeasGtk
17
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
22
23 # Menu to insert in gedit's 'Tools' menu
24 ui_str = """<ui>
25   <menubar name="MenuBar">
26     <menu name="ToolsMenu" action="Tools">
27       <placeholder name="ToolsOps_2">
28         <menuitem name="GeditFlake8" action="GeditFlake8"/>
29       </placeholder>
30     </menu>
31   </menubar>
32 </ui>
33 """
34
35
36 class GeditFlake8WindowActivatable(GObject.Object,
37                                    Gedit.WindowActivatable,
38                                    PeasGtk.Configurable):
39     """Main module of the gedit flake8 plugin"""
40
41     __gtype_name__ = "GeditFlake8WindowActivatable"
42
43     window = GObject.property(type=Gedit.Window)
44
45     def __init__(self):
46         """Initialize plugin
47
48         :type  action: function
49         :param action: Called function that we intercept prints
50
51         :rtype:  None
52         """
53         GObject.Object.__init__(self)
54         # Initialize settings
55         self._settings = SettingsManager()
56         self.flake8_tree = None
57
58     def do_activate(self):
59         """Activate the plugin
60
61         :type  self: GeditFlake8WindowActivatable
62         :param self: Current plugin
63
64         :rtype:  None
65         """
66         # Get the Gtk.UIManager
67         manager = self.window.get_ui_manager()
68
69         # Create a new action group
70         self._action_group = Gtk.ActionGroup("GeditFlake8PluginActions")
71         self._action_group.add_actions([("GeditFlake8",
72                                          None,
73                                          _("Check Python source code"),
74                                          None,
75                                          _("Check Python source code"),
76                                          self.on_check_source_code)])
77
78         # Insert the action group
79         manager.insert_action_group(self._action_group, -1)
80
81         # Merge the UI
82         self._ui_id = manager.add_ui_from_string(ui_str)
83
84         # Create a tree
85         self.flake8_tree = GeditFlake8Tree(self.window)
86
87     def do_deactivate(self):
88         """Deactivate the plugin
89
90         :type  self: GeditFlake8WindowActivatable
91         :param self: Current plugin
92
93         :rtype:  None
94         """
95         # Remove the tree from the side panel
96         self.flake8_tree.remove()
97
98         # Delete tree object
99         self.flake8_tree = None
100
101         # Get the Gtk.UIManager
102         manager = self.window.get_ui_manager()
103
104         # Remove the ui
105         manager.remove_ui(self._ui_id)
106
107         # Remove the action group
108         manager.remove_action_group(self._action_group)
109
110         # Delete the action group object
111         self._action_group = None
112
113         # Make sure the manager updates
114         manager.ensure_update()
115
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.
120
121         :type  self: GeditFlake8WindowActivatable
122         :param self: Current plugin
123
124         :rtype:  None
125         """
126         state = False
127         doc = self.window.get_active_document()
128         if doc is not None:
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)
133
134     def do_create_configure_widget(self):
135         """Action when a user want to configure the plugin
136
137         :type  self: GeditFlake8WindowActivatable
138         :param self: Current plugin
139
140         :rtype:  GeditFlake8ConfigWidget
141         :return: a configuration widget
142         """
143         return GeditFlake8ConfigWidget(self._settings)
144
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
148         2\ use flake8
149         3\ put flake8 results into the tree and activate it
150
151         :type  self: GeditFlake8WindowActivatable
152         :param self: Current plugin
153
154         :type  action: Gtk.Action
155         :param action: Called function that we intercept prints
156
157         :rtype:  None
158         """
159
160         # Get the document content
161         doc = self.window.get_active_document()
162         if not doc:
163             return
164
165         # check the language of current document is set
166         language = doc.get_language()
167         if language is None:
168             return
169
170         # check the language of current document is python
171         if language.get_id() not in ["python", "python3"]:
172             return
173
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())
179
180         # Empty store
181         self.flake8_tree.update_model(warnings)
182
183         # Activate tree
184         self.flake8_tree.activate()