PluginProjectWeb.py

branch
eric7
changeset 38
6a12561fc0b5
parent 35
a3f1dcf94fe4
child 40
a9b17341d181
equal deleted inserted replaced
37:01822a402c97 38:6a12561fc0b5
8 """ 8 """
9 9
10 import contextlib 10 import contextlib
11 import os 11 import os
12 12
13 from PyQt5.QtCore import QObject, QTranslator 13 from PyQt6.QtCore import QObject, QTranslator
14 from PyQt5.QtWidgets import QMenu 14 from PyQt6.QtWidgets import QMenu
15 15
16 from E5Gui.E5Application import e5App 16 from EricWidgets.EricApplication import ericApp
17 17
18 import Preferences 18 import Preferences
19 19
20 try: 20 try:
21 from bs4 import BeautifulSoup # __IGNORE_EXCEPTION__ 21 from bs4 import BeautifulSoup # __IGNORE_EXCEPTION__
27 # Start-Of-Header 27 # Start-Of-Header
28 name = "Generic Web Project Plug-in" 28 name = "Generic Web Project Plug-in"
29 author = "Detlev Offenbach <detlev@die-offenbachs.de>" 29 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
30 autoactivate = True 30 autoactivate = True
31 deactivateable = True 31 deactivateable = True
32 version = "2.2.0" 32 version = "1.0.0"
33 className = "ProjectWebPlugin" 33 className = "ProjectWebPlugin"
34 packageName = "ProjectWeb" 34 packageName = "ProjectWeb"
35 shortDescription = "Support for Web projects and web related tools." 35 shortDescription = "Support for Web projects and web related tools."
36 longDescription = ( 36 longDescription = (
37 """This plug-in provides support for ordinary web projects and some web""" 37 """This plug-in provides support for ordinary web projects and some web"""
38 """ related tools.\n\nIt requires BeautifulSoup4 for some of its""" 38 """ related tools.\n\nIt uses BeautifulSoup4 for some of its"""
39 """ functionality.""" 39 """ functionality."""
40 ) 40 )
41 needsRestart = False 41 needsRestart = False
42 pyqtApi = 2 42 pyqtApi = 2
43 # End-Of-Header 43 # End-Of-Header
51 """ 51 """
52 def __init__(self, ui): 52 def __init__(self, ui):
53 """ 53 """
54 Constructor 54 Constructor
55 55
56 @param ui reference to the user interface object (UI.UserInterface) 56 @param ui reference to the user interface object
57 @type UserInterface
57 """ 58 """
58 super().__init__(ui) 59 super().__init__(ui)
59 self.__ui = ui 60 self.__ui = ui
60 self.__initialize() 61 self.__initialize()
61 62
66 67
67 def __initialize(self): 68 def __initialize(self):
68 """ 69 """
69 Private slot to (re)initialize the plugin. 70 Private slot to (re)initialize the plugin.
70 """ 71 """
71 self.__e5project = e5App().getObject("Project") 72 self.__ericProject = ericApp().getObject("Project")
72 73
73 self.__editors = {} 74 self.__editors = {}
74 self.__mainActions = [] 75 self.__mainActions = []
75 76
76 def activate(self): 77 def activate(self):
77 """ 78 """
78 Public method to activate this plugin. 79 Public method to activate this plugin.
79 80
80 @return tuple of None and activation status (boolean) 81 @return tuple of None and activation status
82 @rtype tuple of (None, bool)
81 """ 83 """
82 global error 84 global error
83 error = "" # clear previous error 85 error = "" # clear previous error
84 86
85 # it is not registered for a specific programming language 87 # it is not registered for a specific programming language
86 self.__e5project.registerProjectType( 88 self.__ericProject.registerProjectType(
87 "Web", self.tr("Web"), 89 "Web", self.tr("Web"),
88 self.fileTypesCallback) 90 self.fileTypesCallback)
89 91
90 from Project.ProjectBrowser import ( 92 from Project.ProjectBrowser import (
91 SourcesBrowserFlag, FormsBrowserFlag, OthersBrowserFlag 93 SourcesBrowserFlag, FormsBrowserFlag, OthersBrowserFlag
103 act = menu.addSeparator() 105 act = menu.addSeparator()
104 self.__mainActions.append(act) 106 self.__mainActions.append(act)
105 act = menu.addMenu(self.__menu) 107 act = menu.addMenu(self.__menu)
106 self.__mainActions.append(act) 108 self.__mainActions.append(act)
107 109
108 e5App().getObject("ViewManager").editorOpenedEd.connect( 110 ericApp().getObject("ViewManager").editorOpenedEd.connect(
109 self.__editorOpened) 111 self.__editorOpened)
110 e5App().getObject("ViewManager").editorClosedEd.connect( 112 ericApp().getObject("ViewManager").editorClosedEd.connect(
111 self.__editorClosed) 113 self.__editorClosed)
112 114
113 for editor in e5App().getObject("ViewManager").getOpenEditors(): 115 for editor in ericApp().getObject("ViewManager").getOpenEditors():
114 self.__editorOpened(editor) 116 self.__editorOpened(editor)
115 117
116 return None, True 118 return None, True
117 119
118 def deactivate(self): 120 def deactivate(self):
119 """ 121 """
120 Public method to deactivate this plugin. 122 Public method to deactivate this plugin.
121 """ 123 """
122 self.__e5project.unregisterProjectType("Web") 124 self.__ericProject.unregisterProjectType("Web")
123 125
124 self.__ui.showMenu.disconnect(self.__populateMenu) 126 self.__ui.showMenu.disconnect(self.__populateMenu)
125 127
126 menu = self.__ui.getMenu("plugin_tools") 128 menu = self.__ui.getMenu("plugin_tools")
127 if menu is not None: 129 if menu is not None:
128 for act in self.__mainActions: 130 for act in self.__mainActions:
129 menu.removeAction(act) 131 menu.removeAction(act)
130 self.__mainActions = [] 132 self.__mainActions = []
131 133
132 e5App().getObject("ViewManager").editorOpenedEd.disconnect( 134 ericApp().getObject("ViewManager").editorOpenedEd.disconnect(
133 self.__editorOpened) 135 self.__editorOpened)
134 e5App().getObject("ViewManager").editorClosedEd.disconnect( 136 ericApp().getObject("ViewManager").editorClosedEd.disconnect(
135 self.__editorClosed) 137 self.__editorClosed)
136 138
137 for editor, acts in self.__editors.items(): 139 for editor, acts in self.__editors.items():
138 editor.showMenu.disconnect(self.__editorShowMenu) 140 editor.showMenu.disconnect(self.__editorShowMenu)
139 menu = editor.getMenu("Tools") 141 menu = editor.getMenu("Tools")
155 translation = "web_{0}".format(loc) 157 translation = "web_{0}".format(loc)
156 translator = QTranslator(None) 158 translator = QTranslator(None)
157 loaded = translator.load(translation, locale_dir) 159 loaded = translator.load(translation, locale_dir)
158 if loaded: 160 if loaded:
159 self.__translator = translator 161 self.__translator = translator
160 e5App().installTranslator(self.__translator) 162 ericApp().installTranslator(self.__translator)
161 else: 163 else:
162 print("Warning: translation file '{0}' could not be" 164 print("Warning: translation file '{0}' could not be"
163 " loaded.".format(translation)) 165 " loaded.".format(translation))
164 print("Using default.") 166 print("Using default.")
165 167
166 def fileTypesCallback(self): 168 def fileTypesCallback(self):
167 """ 169 """
168 Public method to get the filetype associations of the Web project type. 170 Public method to get the filetype associations of the Web project type.
169 171
170 @return dictionary with file type associations 172 @return dictionary with file type associations
171 """ 173 @rtype dict
172 if self.__e5project.getProjectType() == "Web": 174 """
175 if self.__ericProject.getProjectType() == "Web":
173 return { 176 return {
174 "*.html": "FORMS", 177 "*.html": "FORMS",
175 "*.htm": "FORMS", 178 "*.htm": "FORMS",
176 "*.js": "SOURCES", 179 "*.js": "SOURCES",
177 } 180 }
196 199
197 def __menuAboutToShow(self): 200 def __menuAboutToShow(self):
198 """ 201 """
199 Private slot to prepare the menu before it is shown. 202 Private slot to prepare the menu before it is shown.
200 """ 203 """
201 editor = e5App().getObject("ViewManager").activeWindow() 204 editor = ericApp().getObject("ViewManager").activeWindow()
202 selectionAvailable = bool(editor and editor.selectedText() != "") 205 selectionAvailable = bool(editor and editor.selectedText() != "")
203 isHtml = bool(editor and 206 isHtml = bool(editor and
204 editor.getLanguage().lower().startswith("html")) 207 editor.getLanguage().lower().startswith("html"))
205 208
206 self.__html5ToCss3Act.setEnabled( 209 self.__html5ToCss3Act.setEnabled(
212 215
213 def __populateMenu(self, name, menu): 216 def __populateMenu(self, name, menu):
214 """ 217 """
215 Private slot to populate the tools menu with our entries. 218 Private slot to populate the tools menu with our entries.
216 219
217 @param name name of the menu (string) 220 @param name name of the menu
218 @param menu reference to the menu to be populated (QMenu) 221 @type str
222 @param menu reference to the menu to be populated
223 @type QMenu
219 """ 224 """
220 if name not in ["Tools", "PluginTools"]: 225 if name not in ["Tools", "PluginTools"]:
221 return 226 return
222 227
223 if name == "Tools": 228 if name == "Tools":
227 232
228 def __editorOpened(self, editor): 233 def __editorOpened(self, editor):
229 """ 234 """
230 Private slot called, when a new editor was opened. 235 Private slot called, when a new editor was opened.
231 236
232 @param editor reference to the new editor (QScintilla.Editor) 237 @param editor reference to the new editor
238 @type Editor
233 """ 239 """
234 menu = editor.getMenu("Tools") 240 menu = editor.getMenu("Tools")
235 if menu is not None: 241 if menu is not None:
236 self.__editors[editor] = [] 242 self.__editors[editor] = []
237 if not menu.isEmpty(): 243 if not menu.isEmpty():
243 249
244 def __editorClosed(self, editor): 250 def __editorClosed(self, editor):
245 """ 251 """
246 Private slot called, when an editor was closed. 252 Private slot called, when an editor was closed.
247 253
248 @param editor reference to the editor (QScintilla.Editor) 254 @param editor reference to the editor
255 @type Editor
249 """ 256 """
250 with contextlib.suppress(KeyError): 257 with contextlib.suppress(KeyError):
251 del self.__editors[editor] 258 del self.__editors[editor]
252 259
253 def __editorShowMenu(self, menuName, menu, editor): 260 def __editorShowMenu(self, menuName, menu, editor):
254 """ 261 """
255 Private slot called, when the the editor context menu or a submenu is 262 Private slot called, when the the editor context menu or a submenu is
256 about to be shown. 263 about to be shown.
257 264
258 @param menuName name of the menu to be shown (string) 265 @param menuName name of the menu to be shown
259 @param menu reference to the menu (QMenu) 266 @type str
267 @param menu reference to the menu
268 @type QMenu
260 @param editor reference to the editor 269 @param editor reference to the editor
270 @type Editor
261 """ 271 """
262 if ( 272 if (
263 menuName == "Tools" and 273 menuName == "Tools" and
264 self.__menu.menuAction() not in menu.actions() 274 self.__menu.menuAction() not in menu.actions()
265 ): 275 ):
274 def __html5ToCss3(self): 284 def __html5ToCss3(self):
275 """ 285 """
276 Private slot handling the HTML5 to CSS3 conversion. 286 Private slot handling the HTML5 to CSS3 conversion.
277 """ 287 """
278 from ProjectWeb.Html5ToCss3Converter import Html5ToCss3Converter 288 from ProjectWeb.Html5ToCss3Converter import Html5ToCss3Converter
279 vm = e5App().getObject("ViewManager") 289 vm = ericApp().getObject("ViewManager")
280 editor = vm.activeWindow() 290 editor = vm.activeWindow()
281 html = editor.selectedText() 291 html = editor.selectedText()
282 292
283 converter = Html5ToCss3Converter(html) 293 converter = Html5ToCss3Converter(html)
284 css3 = converter.getCss3() 294 css3 = converter.getCss3()
292 def __html5ToJs(self): 302 def __html5ToJs(self):
293 """ 303 """
294 Private slot handling the HTML5 to JavaScript conversion. 304 Private slot handling the HTML5 to JavaScript conversion.
295 """ 305 """
296 from ProjectWeb.Html5ToJsConverter import Html5ToJsConverter 306 from ProjectWeb.Html5ToJsConverter import Html5ToJsConverter
297 vm = e5App().getObject("ViewManager") 307 vm = ericApp().getObject("ViewManager")
298 editor = vm.activeWindow() 308 editor = vm.activeWindow()
299 html = editor.selectedText() 309 html = editor.selectedText()
300 310
301 converter = Html5ToJsConverter(html) 311 converter = Html5ToJsConverter(html)
302 js = converter.getJavaScript() 312 js = converter.getJavaScript()
310 def __html5Prettify(self): 320 def __html5Prettify(self):
311 """ 321 """
312 Private slot handling the Prettify HTML action. 322 Private slot handling the Prettify HTML action.
313 """ 323 """
314 from ProjectWeb.Html5Prettifier import Html5Prettifier 324 from ProjectWeb.Html5Prettifier import Html5Prettifier
315 editor = e5App().getObject("ViewManager").activeWindow() 325 editor = ericApp().getObject("ViewManager").activeWindow()
316 html = editor.text() 326 html = editor.text()
317 327
318 prettifier = Html5Prettifier(html) 328 prettifier = Html5Prettifier(html)
319 newHtml = prettifier.getPrettifiedHtml() 329 newHtml = prettifier.getPrettifiedHtml()
320 330
326 editor.replaceSelectedText(newHtml) 336 editor.replaceSelectedText(newHtml)
327 editor.endUndoAction() 337 editor.endUndoAction()
328 338
329 editor.setCursorPosition(cursorLine, cursorIndex) 339 editor.setCursorPosition(cursorLine, cursorIndex)
330 340
341
342 def installDependencies(pipInstall):
343 """
344 Function to install dependencies of this plug-in.
345
346 @param pipInstall function to be called with a list of package names.
347 @type function
348 """
349 try:
350 import bs4 # __IGNORE_WARNING__
351 except ImportError:
352 pipInstall(["beautifulsoup4"])
353
331 # 354 #
332 # eflag: noqa = M801 355 # eflag: noqa = M801

eric ide

mercurial