ProjectDjango/DjangoCheckOptionsDialog.py

changeset 78
5822c2e2b1c7
child 93
cf83715ac2f7
equal deleted inserted replaced
77:f8e92eaaba6e 78:5822c2e2b1c7
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2016 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to enter the options for a check operation.
8 """
9
10 from __future__ import unicode_literals
11 try:
12 str = unicode # __IGNORE_WARNING__
13 except NameError:
14 pass
15
16 import os
17
18 from PyQt5.QtCore import pyqtSlot, Qt, QProcess
19 from PyQt5.QtWidgets import QDialog
20
21 from E5Gui import E5FileDialog
22
23 from .Ui_DjangoCheckOptionsDialog import Ui_DjangoCheckOptionsDialog
24
25 import Preferences
26 import Utilities
27 import UI.PixmapCache
28
29
30 class DjangoCheckOptionsDialog(QDialog, Ui_DjangoCheckOptionsDialog):
31 """
32 Class implementing a dialog to enter the options for a check operation.
33 """
34 def __init__(self, python, path, apps, deployMode, parent=None):
35 """
36 Constructor
37
38 @param python path of the Python executable
39 @type str
40 @param path site path to run the manage.py script with
41 @type str
42 @param apps list of recently used application strings
43 @type list of str
44 @param deployMode flag indicating to activate the deployment mode
45 @type bool
46 @param parent reference to the parent widget
47 @type QWidget
48 """
49 super(DjangoCheckOptionsDialog, self).__init__(parent)
50 self.setupUi(self)
51
52 self.settingsFileButton.setIcon(UI.PixmapCache.getIcon("open.png"))
53
54 self.__python = python
55 self.__path = path
56
57 self.appsComboBox.addItems([""] + apps)
58
59 self.deployCheckBox.setChecked(deployMode)
60 self.on_deployCheckBox_toggled(deployMode)
61
62 @pyqtSlot(bool)
63 def on_deployCheckBox_toggled(self, checked):
64 """
65 Private slot handling a change of the deploy check box.
66
67 @param checked state of the check box
68 @type bool
69 """
70 self.settingsFileGroup.setEnabled(checked)
71 self.__populateTagsList(checked)
72
73 @pyqtSlot()
74 def on_settingsFileButton_clicked(self):
75 """
76 Private slot to select a settings file via a file selection dialog.
77 """
78 path = self.__moduleToPath(self.settingsFileEdit.text())
79 if not path:
80 path = self.__path
81 settingsFile = E5FileDialog.getOpenFileName(
82 self,
83 self.tr("Select settings file"),
84 path,
85 self.tr("Python Files (*.py)"))
86
87 if settingsFile:
88 self.settingsFileEdit.setText(self.__pathToModule(settingsFile))
89
90 def __pathToModule(self, path):
91 """
92 Private method to convert a file path including a .py extension to a
93 module name.
94
95 @param path file path to be converted
96 @type str
97 @return module name
98 @rtype str
99 """
100 if self.__path.endswith(("/", "\\")):
101 # cope with a bug in eric
102 start = self.__path[:-1]
103 else:
104 start = self.__path
105 relPath = Utilities.relativeUniversalPath(path, start)
106 mod = os.path.splitext(relPath)[0].replace("/", ".")
107 return mod
108
109 def __moduleToPath(self, moduleName):
110 """
111 Private method to convert a module name to an file path.
112
113 @param moduleName module name to be converted
114 @type str
115 @return file path
116 @rtype str
117 """
118 if moduleName:
119 mod = "{0}.py".format(moduleName.replace(".", "/"))
120 if not os.path.isabs(mod):
121 mod = os.path.join(self.__path, mod)
122
123 path = Utilities.toNativeSeparators(mod)
124 else:
125 path = ""
126 return path
127
128 def __populateTagsList(self, deployMode):
129 """
130 Private slot to populate the tags list.
131
132 @param deployMode flag indicating the deployment mode
133 @type bool
134 """
135 # step 1: save the selected tags
136 selectedTags = []
137 for itm in self.tagsList.selectedItems():
138 selectedTags.append(itm.text())
139
140 # step 2: clear the list
141 self.tagsList.clear()
142
143 # step 3: get the available tags and populate the list
144 args = []
145 args.append("manage.py")
146 args.append("check")
147 args.append("--list-tags")
148 if deployMode:
149 args.append("--deploy")
150
151 proc = QProcess()
152 if self.__path:
153 proc.setWorkingDirectory(self.__path)
154 proc.start(self.__python, args)
155 if proc.waitForStarted():
156 if proc.waitForFinished():
157 output = str(proc.readAllStandardOutput(),
158 Preferences.getSystem("IOEncoding"), 'replace')
159 for line in output.splitlines():
160 self.tagsList.addItem(line.strip())
161
162 # step 4: re-select tags
163 for tag in selectedTags:
164 items = self.tagsList.findItems(tag, Qt.MatchCaseSensitive)
165 if items:
166 items[0].setSelected(True)
167
168 def getData(self):
169 """
170 Public method to get the options for the check operation.
171
172 @return tuple containing the deployment flag, list of selected tags,
173 applications string and the settings file
174 @rtype tuple of bool, list of str, str and str
175 """
176 selectedTags = []
177 for itm in self.tagsList.selectedItems():
178 selectedTags.append(itm.text())
179
180 return (
181 self.deployCheckBox.isChecked(),
182 selectedTags,
183 self.appsComboBox.currentText(),
184 self.settingsFileEdit.text(),
185 )

eric ide

mercurial