src/eric7/Plugins/PluginWizardEricPlugin.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
40 40
41 class WizardEricPluginWizard(QObject): 41 class WizardEricPluginWizard(QObject):
42 """ 42 """
43 Class implementing the eric plug-in wizard plug-in. 43 Class implementing the eric plug-in wizard plug-in.
44 """ 44 """
45
45 def __init__(self, ui): 46 def __init__(self, ui):
46 """ 47 """
47 Constructor 48 Constructor
48 49
49 @param ui reference to the user interface object (UI.UserInterface) 50 @param ui reference to the user interface object (UI.UserInterface)
50 """ 51 """
51 super().__init__(ui) 52 super().__init__(ui)
52 self.__ui = ui 53 self.__ui = ui
53 self.__action = None 54 self.__action = None
54 55
55 def __initialize(self): 56 def __initialize(self):
56 """ 57 """
57 Private slot to (re)initialize the plug-in. 58 Private slot to (re)initialize the plug-in.
58 """ 59 """
59 self.__act = None 60 self.__act = None
60 61
61 def activate(self): 62 def activate(self):
62 """ 63 """
63 Public method to activate this plug-in. 64 Public method to activate this plug-in.
64 65
65 @return tuple of None and activation status (boolean) 66 @return tuple of None and activation status (boolean)
66 """ 67 """
67 self.__initAction() 68 self.__initAction()
68 self.__initMenu() 69 self.__initMenu()
69 70
70 return None, True 71 return None, True
71 72
72 def deactivate(self): 73 def deactivate(self):
73 """ 74 """
74 Public method to deactivate this plug-in. 75 Public method to deactivate this plug-in.
75 """ 76 """
76 menu = self.__ui.getMenu("wizards") 77 menu = self.__ui.getMenu("wizards")
77 if menu: 78 if menu:
78 menu.removeAction(self.__action) 79 menu.removeAction(self.__action)
79 self.__ui.removeEricActions([self.__action], 'wizards') 80 self.__ui.removeEricActions([self.__action], "wizards")
80 81
81 def __initAction(self): 82 def __initAction(self):
82 """ 83 """
83 Private method to initialize the action. 84 Private method to initialize the action.
84 """ 85 """
85 self.__action = EricAction( 86 self.__action = EricAction(
86 self.tr('eric Plug-in Wizard'), 87 self.tr("eric Plug-in Wizard"),
87 self.tr('eric Plug-in Wizard...'), 88 self.tr("eric Plug-in Wizard..."),
88 0, 0, self, 89 0,
89 'wizards_eric_plugin') 90 0,
90 self.__action.setStatusTip(self.tr('eric Plug-in Wizard')) 91 self,
91 self.__action.setWhatsThis(self.tr( 92 "wizards_eric_plugin",
92 """<b>eric Plug-in Wizard</b>""" 93 )
93 """<p>This wizard opens a dialog for entering all the parameters""" 94 self.__action.setStatusTip(self.tr("eric Plug-in Wizard"))
94 """ needed to create the basic contents of an eric plug-in file.""" 95 self.__action.setWhatsThis(
95 """ The generated code is inserted at the current cursor""" 96 self.tr(
96 """ position.</p>""" 97 """<b>eric Plug-in Wizard</b>"""
97 )) 98 """<p>This wizard opens a dialog for entering all the parameters"""
99 """ needed to create the basic contents of an eric plug-in file."""
100 """ The generated code is inserted at the current cursor"""
101 """ position.</p>"""
102 )
103 )
98 self.__action.triggered.connect(self.__handle) 104 self.__action.triggered.connect(self.__handle)
99 105
100 self.__ui.addEricActions([self.__action], 'wizards') 106 self.__ui.addEricActions([self.__action], "wizards")
101 107
102 def __initMenu(self): 108 def __initMenu(self):
103 """ 109 """
104 Private method to add the actions to the right menu. 110 Private method to add the actions to the right menu.
105 """ 111 """
106 menu = self.__ui.getMenu("wizards") 112 menu = self.__ui.getMenu("wizards")
107 if menu: 113 if menu:
108 menu.addAction(self.__action) 114 menu.addAction(self.__action)
109 115
110 def __callForm(self, editor): 116 def __callForm(self, editor):
111 """ 117 """
112 Private method to display a dialog and get the code. 118 Private method to display a dialog and get the code.
113 119
114 @param editor reference to the current editor 120 @param editor reference to the current editor
115 @return generated code (string), the plug-in package name (string) 121 @return generated code (string), the plug-in package name (string)
116 and a flag indicating success (boolean) 122 and a flag indicating success (boolean)
117 """ 123 """
118 from WizardPlugins.EricPluginWizard.PluginWizardDialog import ( 124 from WizardPlugins.EricPluginWizard.PluginWizardDialog import PluginWizardDialog
119 PluginWizardDialog 125
120 )
121 dlg = PluginWizardDialog(None) 126 dlg = PluginWizardDialog(None)
122 if dlg.exec() == QDialog.DialogCode.Accepted: 127 if dlg.exec() == QDialog.DialogCode.Accepted:
123 return (dlg.getCode(), dlg.packageName(), True) 128 return (dlg.getCode(), dlg.packageName(), True)
124 else: 129 else:
125 return (None, "", False) 130 return (None, "", False)
126 131
127 def __handle(self): 132 def __handle(self):
128 """ 133 """
129 Private method to handle the wizards action. 134 Private method to handle the wizards action.
130 """ 135 """
131 editor = ericApp().getObject("ViewManager").activeWindow() 136 editor = ericApp().getObject("ViewManager").activeWindow()
132 137
133 if editor is None: 138 if editor is None:
134 EricMessageBox.critical( 139 EricMessageBox.critical(
135 self.__ui, 140 self.__ui,
136 self.tr('No current editor'), 141 self.tr("No current editor"),
137 self.tr('Please open or create a file first.')) 142 self.tr("Please open or create a file first."),
143 )
138 else: 144 else:
139 code, packageName, ok = self.__callForm(editor) 145 code, packageName, ok = self.__callForm(editor)
140 if ok: 146 if ok:
141 line, index = editor.getCursorPosition() 147 line, index = editor.getCursorPosition()
142 # It should be done on this way to allow undo 148 # It should be done on this way to allow undo
143 editor.beginUndoAction() 149 editor.beginUndoAction()
144 editor.insertAt(code, line, index) 150 editor.insertAt(code, line, index)
145 editor.endUndoAction() 151 editor.endUndoAction()
146 if not editor.getFileName(): 152 if not editor.getFileName():
147 editor.setLanguage("dummy.py") 153 editor.setLanguage("dummy.py")
148 154
149 if packageName: 155 if packageName:
150 project = ericApp().getObject("Project") 156 project = ericApp().getObject("Project")
151 packagePath = os.path.join(project.getProjectPath(), 157 packagePath = os.path.join(project.getProjectPath(), packageName)
152 packageName)
153 if not os.path.exists(packagePath): 158 if not os.path.exists(packagePath):
154 try: 159 try:
155 os.mkdir(packagePath) 160 os.mkdir(packagePath)
156 except OSError as err: 161 except OSError as err:
157 EricMessageBox.critical( 162 EricMessageBox.critical(
158 self, 163 self,
159 self.tr("Create Package"), 164 self.tr("Create Package"),
160 self.tr( 165 self.tr(
161 """<p>The package directory <b>{0}</b>""" 166 """<p>The package directory <b>{0}</b>"""
162 """ could not be created. Aborting...""" 167 """ could not be created. Aborting..."""
163 """</p><p>Reason: {1}</p>""") 168 """</p><p>Reason: {1}</p>"""
164 .format(packagePath, str(err))) 169 ).format(packagePath, str(err)),
170 )
165 return 171 return
166 packageFile = os.path.join(packagePath, "__init__.py") 172 packageFile = os.path.join(packagePath, "__init__.py")
167 if not os.path.exists(packageFile): 173 if not os.path.exists(packageFile):
168 try: 174 try:
169 with open(packageFile, "w", encoding="utf-8"): 175 with open(packageFile, "w", encoding="utf-8"):
173 self, 179 self,
174 self.tr("Create Package"), 180 self.tr("Create Package"),
175 self.tr( 181 self.tr(
176 """<p>The package file <b>{0}</b> could""" 182 """<p>The package file <b>{0}</b> could"""
177 """ not be created. Aborting...</p>""" 183 """ not be created. Aborting...</p>"""
178 """<p>Reason: {1}</p>""") 184 """<p>Reason: {1}</p>"""
179 .format(packageFile, str(err))) 185 ).format(packageFile, str(err)),
186 )
180 return 187 return
181 project.appendFile(packageFile) 188 project.appendFile(packageFile)
182 project.saveProject() 189 project.saveProject()
183 ericApp().getObject("ViewManager").openSourceFile( 190 ericApp().getObject("ViewManager").openSourceFile(packageFile)
184 packageFile) 191
185 192
186 # 193 #
187 # eflag: noqa = M801 194 # eflag: noqa = M801

eric ide

mercurial