PluginExtensionProtobuf.py

changeset 1
7157a39d4a0f
parent 0
f6cff7759b24
child 3
31678035389a
equal deleted inserted replaced
0:f6cff7759b24 1:7157a39d4a0f
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a plugin to add support for Protobuf development.
8 """
9
10 import os
11
12 from PyQt6.QtCore import QCoreApplication, QObject, QTranslator
13
14 from eric7 import Globals, Preferences
15 from eric7.EricWidgets import EricMessageBox
16 from eric7.EricWidgets.EricApplication import ericApp
17 from ExtensionProtobuf.ProjectProtocolsBrowser import ProjectProtocolsBrowser
18
19 # Start-Of-Header
20 name = "Protobuf and gRPC Extension Plugin"
21 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
22 autoactivate = True
23 deactivateable = True
24 version = "10.0.0"
25 className = "ProtobufExtensionPlugin"
26 packageName = "ExtensionProtobuf"
27 shortDescription = "Support for the development of Protobuf and gRPC projects"
28 longDescription = (
29 "This plugin adds support for the development of Protobuf and gRPC related"
30 " projects."
31 )
32 needsRestart = False
33 pyqtApi = 2
34 # End-Of-Header
35
36 error = ""
37
38 protobufExtensionPluginObject = None
39
40
41 def exeDisplayDataList():
42 """
43 Module function to support the display of some executable info.
44
45 @return list of dictionaries containing the data to query the presence of
46 the executable
47 @rtype list of dict
48 """
49 global protobufExtensionPluginObject
50
51 # 1. Protobuf compiler
52 if protobufExtensionPluginObject is None:
53 protocData = {
54 "programEntry": False,
55 "header": QCoreApplication.translate(
56 "ProtobufExtensionPlugin", "Protobuf Compiler"
57 ),
58 "text": QCoreApplication.translate(
59 "ProtobufExtensionPlugin",
60 "Protobuf and gRPC Support plugin is not activated",
61 ),
62 "version": QCoreApplication.translate(
63 "ProtobufExtensionPlugin", "(inactive)"
64 ),
65 }
66 else:
67 exe = protobufExtensionPluginObject.getPreferences("protoc")
68 if not exe:
69 exe = "protoc.exe" if Globals.isWindowsPlatform() else "protoc"
70
71 protocData = {
72 "programEntry": True,
73 "header": QCoreApplication.translate(
74 "ProtobufExtensionPlugin", "Protobuf Compiler"
75 ),
76 "exe": exe,
77 "versionCommand": "--version",
78 "versionStartsWith": "libprotoc",
79 "versionRe": "",
80 "versionPosition": -1,
81 "version": "",
82 "versionCleanup": None,
83 "exeModule": None,
84 }
85
86 # 2. grpc compiler
87 if protobufExtensionPluginObject is None:
88 grpcData = {
89 "programEntry": False,
90 "header": QCoreApplication.translate(
91 "ProtobufExtensionPlugin", "gRPC Compiler"
92 ),
93 "text": QCoreApplication.translate(
94 "ProtobufExtensionPlugin",
95 "Protobuf and gRPC Support plugin is not activated",
96 ),
97 "version": QCoreApplication.translate(
98 "ProtobufExtensionPlugin", "(inactive)"
99 ),
100 }
101 else:
102 env = protobufExtensionPluginObject.getPreferences("grpcPythonEnv")
103 interpreter = (
104 ericApp().getObject("VirtualEnvManager").getVirtualenvInterpreter(env)
105 )
106 if not interpreter:
107 interpreter = Globals.getPythonExecutable()
108
109 grpcData = {
110 "programEntry": True,
111 "header": QCoreApplication.translate(
112 "ProtobufExtensionPlugin", "gRPC Compiler"
113 ),
114 "exe": interpreter,
115 "versionCommand": "--version",
116 "versionStartsWith": "libprotoc",
117 "versionRe": "",
118 "versionPosition": -1,
119 "version": "",
120 "versionCleanup": None,
121 "exeModule": ["-m", "grpc_tools.protoc"],
122 }
123
124 return [protocData, grpcData]
125
126
127 def getConfigData():
128 """
129 Module function returning data as required by the configuration dialog.
130
131 @return dictionary containing the relevant data
132 @rtype dict
133 """
134 return {
135 "protobufPage": [
136 QCoreApplication.translate("ProtobufExtensionPlugin", "Protobuf and gRPC"),
137 os.path.join("ExtensionProtobuf", "icons", "protobuf"),
138 createProtobufPage,
139 None,
140 None,
141 ],
142 }
143
144
145 def createProtobufPage(configDlg):
146 """
147 Module function to create the Protobuf configuration page.
148
149 @param configDlg reference to the configuration dialog
150 @type ConfigurationWidget
151 @return reference to the configuration page
152 @rtype CorbaPage
153 """
154 from ExtensionProtobuf.ConfigurationPage.ProtobufPage import ProtobufPage
155
156 # __IGNORE_WARNING_I102__
157
158 global protobufExtensionPluginObject
159
160 page = ProtobufPage(protobufExtensionPluginObject)
161 return page
162
163
164 def prepareUninstall():
165 """
166 Module function to prepare for an un-installation.
167 """
168 Preferences.getSettings().remove(ProtobufExtensionPlugin.PreferencesKey)
169
170
171 class ProtobufExtensionPlugin(QObject):
172 """
173 Class implementing a plugin to add support for Protobuf development.
174 """
175
176 PreferencesKey = "Corba"
177
178 def __init__(self, ui):
179 """
180 Constructor
181
182 @param ui reference to the user interface object
183 @type UI.UserInterface
184 """
185 super().__init__(ui)
186 self.__ui = ui
187 self.__initialize()
188
189 self.__defaults = {
190 "protoc": "",
191 "grpcPythonEnv": "",
192 }
193
194 def __initialize(self):
195 """
196 Private slot to (re)initialize the plugin.
197 """
198 global protobufExtensionPluginObject
199 protobufExtensionPluginObject = None
200
201 self.__browser = None
202
203 def activate(self):
204 """
205 Public method to activate this plug-in.
206
207 @return tuple of None and activation status
208 @rtype bool
209 """
210 global error, protobufExtensionPluginObject
211 error = "" # clear previous error
212
213 if self.__ui.versionIsNewer("22.12"):
214 protobufExtensionPluginObject = self
215
216 self.__browser = ProjectProtocolsBrowser(self)
217
218 return None, True
219 else:
220 EricMessageBox.warning(
221 self.__ui,
222 self.tr("Protobuf and gRPC Extension"),
223 self.tr(
224 "The Protobuf and gRPC extension cannot be activated because it"
225 " requires eric7 23.1 or newer."
226 ),
227 )
228 error = self.tr(
229 "The Protobuf and gRPC extension cannot be activated because it"
230 " requires eric7 23.1 or newer."
231 )
232
233 return None, False
234
235 def deactivate(self):
236 """
237 Public method to deactivate this plug-in.
238 """
239 self.__browser.deactivate()
240
241 self.__initialize()
242
243 def __loadTranslator(self):
244 """
245 Private method to load the translation file.
246 """
247 if self.__ui is not None:
248 loc = self.__ui.getLocale()
249 if loc and loc != "C":
250 locale_dir = os.path.join(
251 os.path.dirname(__file__), "ExtensionProtobuf", "i18n"
252 )
253 translation = "protobuf_{0}".format(loc)
254 translator = QTranslator(None)
255 loaded = translator.load(translation, locale_dir)
256 if loaded:
257 self.__translator = translator
258 ericApp().installTranslator(self.__translator)
259 else:
260 print(
261 "Warning: translation file '{0}' could not be"
262 " loaded.".format(translation)
263 )
264 print("Using default.")
265
266 def getPreferences(self, key):
267 """
268 Public method to retrieve the various settings values.
269
270 @param key the key of the value to get
271 @type str
272 @return the requested setting value
273 @rtype any
274 """
275 return Preferences.Prefs.settings.value(
276 self.PreferencesKey + "/" + key, self.__defaults[key]
277 )
278
279 def setPreferences(self, key, value):
280 """
281 Public method to store the various settings values.
282
283 @param key the key of the setting to be set
284 @type str
285 @param value the value to be set
286 @type any
287 """
288 Preferences.Prefs.settings.setValue(self.PreferencesKey + "/" + key, value)
289
290
291 def installDependencies(pipInstall):
292 """
293 Function to install dependencies of this plug-in.
294
295 @param pipInstall function to be called with a list of package names.
296 @type function
297 """
298 try:
299 import grpc_tools # __IGNORE_WARNING__
300 except ImportError:
301 pipInstall(["protobuf", "grpcio", "grpcio-tools"])
302
303
304 #
305 # eflag: noqa = M801

eric ide

mercurial