Skip to content

Commit d758522

Browse files
committed
Add preditor.plug.loggerwindow plugin support
Standardize the names of the LoggerWindow's sub-menus for ease of use in plugin development.
1 parent d1f43c5 commit d758522

File tree

5 files changed

+83
-15
lines changed

5 files changed

+83
-15
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ added in [setup.cfg](setup.cfg).
165165
implementing a workbox. See [workbox_mixin.py](preditor/gui/workbox_mixin.py)
166166
for the full interface to implement all features of an editor.
167167

168+
* `preditor.plug.loggerwindow`: Used to customize the LoggerWindow instance when
169+
the LoggerWindow is created. For example, this can be used to create extra Toolbars
170+
or add menu items. When using this plugin, make sure to use the
171+
`preditor.gui.logger_window_plugin.LoggerWindowPlugin` class for your base class.
172+
168173
* `preditor.plug.logging_handlers`: Used to add custom python logging handlers
169174
to the LoggingLevelButton's handlers sub-menus. This allows you to install a
170175
handler instance on a specific logging object.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class LoggerWindowPlugin:
2+
"""Base class for LoggerWindow plugins.
3+
4+
These plugins are loaded using the `preditor.plug.loggerwindow` entry point.
5+
This entry point is loaded when `LoggerWindow` is initialized. For each entry
6+
point defined a single instance of the plugin is created per instance of
7+
a LoggerWindow.
8+
9+
To save preferences override `record_prefs` and `restore_prefs` methods. These
10+
are used to save and load preferences any time the PrEditor save/loads prefs.
11+
"""
12+
13+
def __init__(self, parent):
14+
self.parent = parent
15+
16+
def record_prefs(self, name):
17+
"""Returns any prefs to save with the PrEditor's preferences.
18+
19+
Returns:
20+
dict: A dictionary that will be saved using json or None.
21+
"""
22+
23+
def restore_prefs(self, name, prefs):
24+
"""Restore the preferences saved from a previous launch.
25+
26+
Args:
27+
name(str): The name specified by the `preditor.plug.loggerwindow`
28+
entry point.
29+
prefs(dict or None): The prefs returned by a previous call to
30+
`record_prefs()` from the last preference save. None is passed
31+
if no prefs were recorded.
32+
"""

preditor/gui/loggerwindow.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ def __init__(self, parent, name=None, run_workbox=False, standalone=False):
287287
self.addAction(self.uiClearLogACT)
288288

289289
self.dont_ask_again = []
290+
291+
# Load any plugins that modify the LoggerWindow
292+
self.plugins = {}
293+
for name, plugin in plugins.loggerwindow():
294+
self.plugins[name] = plugin(self)
295+
290296
self.restorePrefs()
291297

292298
# add stylesheet menu options.
@@ -823,6 +829,13 @@ def recordPrefs(self, manual=False):
823829

824830
pref['editor_cls'] = self.editor_cls_name
825831

832+
# Allow any plugins to add their own preferences dictionary
833+
pref["plugins"] = {}
834+
for name, plugin in self.plugins.items():
835+
plugin_pref = plugin.record_prefs(name)
836+
if plugin_pref:
837+
pref["plugins"][name] = plugin_pref
838+
826839
self.save_prefs(pref)
827840

828841
def load_prefs(self):
@@ -973,6 +986,10 @@ def restorePrefs(self):
973986

974987
self.dont_ask_again = pref.get('dont_ask_again', [])
975988

989+
# Allow any plugins to restore their own preferences
990+
for name, plugin in self.plugins.items():
991+
plugin.restore_prefs(name, pref.get("plugins", {}).get(name))
992+
976993
def restoreToolbars(self, pref=None):
977994
if pref is None:
978995
pref = self.load_prefs()

preditor/gui/ui/loggerwindow.ui

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<x>0</x>
9393
<y>0</y>
9494
<width>796</width>
95-
<height>29</height>
95+
<height>21</height>
9696
</rect>
9797
</property>
9898
<widget class="QMenu" name="uiDebugMENU">
@@ -105,7 +105,7 @@
105105
<addaction name="uiLogToFileACT"/>
106106
<addaction name="uiLogToFileClearACT"/>
107107
</widget>
108-
<widget class="QMenu" name="uiScriptingMENU">
108+
<widget class="QMenu" name="uiFileMENU">
109109
<property name="title">
110110
<string comment="File Menu">&amp;File</string>
111111
</property>
@@ -120,19 +120,19 @@
120120
<property name="title">
121121
<string>Help</string>
122122
</property>
123-
<widget class="QMenu" name="menuPreferences">
123+
<widget class="QMenu" name="uiPreferencesMENU">
124124
<property name="title">
125125
<string>Preferences</string>
126126
</property>
127127
<addaction name="uiBrowsePreferencesACT"/>
128128
<addaction name="uiBackupPreferencesACT"/>
129129
</widget>
130-
<addaction name="menuPreferences"/>
130+
<addaction name="uiPreferencesMENU"/>
131131
<addaction name="uiEnvironmentVarsACT"/>
132132
<addaction name="separator"/>
133133
<addaction name="uiAboutPreditorACT"/>
134134
</widget>
135-
<widget class="QMenu" name="menu_Run">
135+
<widget class="QMenu" name="uiRunMENU">
136136
<property name="title">
137137
<string>Run</string>
138138
</property>
@@ -144,7 +144,7 @@
144144
<addaction name="uiClearToLastPromptACT"/>
145145
<addaction name="uiSelectTextACT"/>
146146
</widget>
147-
<widget class="QMenu" name="uiFileMENU">
147+
<widget class="QMenu" name="uiOptionsMENU">
148148
<property name="title">
149149
<string>&amp;Options</string>
150150
</property>
@@ -196,11 +196,11 @@
196196
<addaction name="uiSetPreferredTextEditorPathACT"/>
197197
<addaction name="uiSetWorkboxEditorACT"/>
198198
</widget>
199-
<widget class="QMenu" name="menuEdit">
199+
<widget class="QMenu" name="uiEditMENU">
200200
<property name="title">
201201
<string>Edit</string>
202202
</property>
203-
<widget class="QMenu" name="menuFocus_to_Group">
203+
<widget class="QMenu" name="uiFocus_to_GroupMENU">
204204
<property name="title">
205205
<string>Focus to Group</string>
206206
</property>
@@ -215,7 +215,7 @@
215215
<addaction name="separator"/>
216216
<addaction name="uiGroupLastACT"/>
217217
</widget>
218-
<widget class="QMenu" name="menuFocus_to_Tab">
218+
<widget class="QMenu" name="uiFocus_to_TabMENU">
219219
<property name="title">
220220
<string>Focus to Tab</string>
221221
</property>
@@ -315,17 +315,17 @@
315315
<addaction name="uiNextTabACT"/>
316316
<addaction name="uiPrevTabACT"/>
317317
<addaction name="separator"/>
318-
<addaction name="menuFocus_to_Group"/>
319-
<addaction name="menuFocus_to_Tab"/>
318+
<addaction name="uiFocus_to_GroupMENU"/>
319+
<addaction name="uiFocus_to_TabMENU"/>
320320
<addaction name="separator"/>
321321
<addaction name="uiFindInWorkboxesACT"/>
322322
<addaction name="uiFocusNameACT"/>
323323
</widget>
324-
<addaction name="uiScriptingMENU"/>
325-
<addaction name="menuEdit"/>
326-
<addaction name="uiDebugMENU"/>
327-
<addaction name="menu_Run"/>
328324
<addaction name="uiFileMENU"/>
325+
<addaction name="uiEditMENU"/>
326+
<addaction name="uiDebugMENU"/>
327+
<addaction name="uiRunMENU"/>
328+
<addaction name="uiOptionsMENU"/>
329329
<addaction name="uiHelpMENU"/>
330330
</widget>
331331
<widget class="QToolBar" name="uiConsoleTOOLBAR">

preditor/plugins.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ def initialize(self, name=None):
8888
for ep in self.iterator(group="preditor.plug.initialize"):
8989
yield ep.load()
9090

91+
def loggerwindow(self, name=None):
92+
"""Returns instances of "preditor.plug.loggerwindow" plugins.
93+
94+
These plugins are used by the LoggerWindow to extend its interface. For
95+
example it can be used to add a toolbar or update the menus.
96+
97+
When using this plugin, make sure the returned class is a subclass of
98+
`preditor.gui.logger_window_plugin.LoggerWindowPlugin`.
99+
"""
100+
for ep in self.iterator(group="preditor.plug.loggerwindow"):
101+
if name and ep.name != name:
102+
continue
103+
yield ep.name, ep.load()
104+
91105
def logging_handlers(self, name=None):
92106
for ep in self.iterator(group="preditor.plug.logging_handlers"):
93107
yield ep.name, ep.load()

0 commit comments

Comments
 (0)