--- a/Plugins/VcsPlugins/vcsMercurial/HgUserConfigDialog.py Sun Nov 06 13:39:36 2016 +0100 +++ b/Plugins/VcsPlugins/vcsMercurial/HgUserConfigDialog.py Sun Nov 06 17:02:45 2016 +0100 @@ -33,16 +33,20 @@ """ Class implementing a dialog to enter some user data. """ - def __init__(self, version=(0, 0), parent=None): + def __init__(self, version=(0, 0, 0), parent=None): """ Constructor - @param version Mercurial version info (tuple of two integers) - @param parent reference to the parent widget (QWidget) + @param version Mercurial version info + @type tuple of three integers + @param parent reference to the parent widget + @type QWidget """ super(HgUserConfigDialog, self).__init__(parent) self.setupUi(self) + self.__version = version + self.lfUserCachePicker.setMode(E5PathPickerModes.DirectoryMode) if Globals.isLinuxPlatform(): self.lfUserCachePicker.setDefaultDirectory(os.path.expanduser( @@ -178,27 +182,89 @@ ################################################################### ## hostfingerprints section ################################################################### - if self.fingerprintsList.topLevelItemCount() > 0: - self.__config["hostfingerprints"] = {} - for row in range(self.fingerprintsList.topLevelItemCount()): - itm = self.fingerprintsList.topLevelItem(row) - self.__config["hostfingerprints"][itm.text(0)] = itm.text(1) + if self.__version < (3, 9, 0): + # + # delete hostsecurity section + # + if "hostsecurity" in self.__config: + del self.__config["hostsecurity"] + + # + # hostfingerprints section + # + if self.fingerprintsList.topLevelItemCount() > 0: + self.__config["hostfingerprints"] = {} + for row in range(self.fingerprintsList.topLevelItemCount()): + itm = self.fingerprintsList.topLevelItem(row) + self.__config["hostfingerprints"][itm.text(0)] = \ + itm.text(1) + else: + if "hostfingerprints" in self.__config: + del self.__config["hostfingerprints"] else: + # + # delete hostfingerprints section + # if "hostfingerprints" in self.__config: del self.__config["hostfingerprints"] + + # + # hostsecurity section + # + if self.fingerprintsList.topLevelItemCount() > 0: + if "hostsecurity" not in self.__config: + self.__config["hostsecurity"] = {} + self.__clearFingerprints() + fingerprints = self.__assembleFingerprints() + for host in fingerprints: + key = "{0}:fingerprints".format(host) + self.__config["hostsecurity"][key] = \ + "\\\n" + ", \\\n".join(fingerprints[host]) + else: + if "hostsecurity" in self.__config: + self.__clearFingerprints() + if len(self.__config.options("hostsecurity")) == 0: + del self.__config["hostsecurity"] ################################################################### cfgFile = getConfigPath() with open(cfgFile, "w") as configFile: self.__config.write(configFile) + def __clearFingerprints(self): + """ + Private method to clear the fingerprints from the hostsecurity section. + """ + if "hostsecurity" in self.__config: + for key in self.__config.options("hostsecurity"): + if key.endswith(":fingerprints"): + self.__config.remove_option("hostsecurity", key) + + def __assembleFingerprints(self): + """ + Private method to assemble a list of host fingerprints. + + @return dictionary with list of fingerprints per host + @rtype dict with str as key and list of str as value + """ + hostFingerprints = {} + for row in range(self.fingerprintsList.topLevelItemCount()): + itm = self.fingerprintsList.topLevelItem(row) + host = itm.text(0) + fingerprint = itm.text(1) + if host in hostFingerprints: + hostFingerprints[host].append(fingerprint) + else: + hostFingerprints[host] = [fingerprint] + return hostFingerprints + def readUserConfig(self): """ Public method to read the user configuration file. """ cfgFile = getConfigPath() - self.__config = E5ConfigParser() + self.__config = E5ConfigParser(delimiters=("=",)) if self.__config.read(cfgFile): # step 1: extract user name and email try: @@ -259,13 +325,34 @@ self.proxyBypassEdit.setText( self.__config["http_proxy"]["no"]) - # step 5: extract host fingerprints + # step 5a: extract host fingerprints if "hostfingerprints" in self.__config: for host in self.__config.options("hostfingerprints"): - QTreeWidgetItem(self.fingerprintsList, [ - host, - self.__config["hostfingerprints"][host] - ]) + if self.__version < (3, 9, 0): + QTreeWidgetItem(self.fingerprintsList, [ + host, + self.__config["hostfingerprints"][host] + ]) + else: + # convert to hostsecurity fingerprint + QTreeWidgetItem(self.fingerprintsList, [ + host, + "sha1:" + self.__config["hostfingerprints"][host] + ]) + self.__finalizeFingerprintsColumns() + + # step 5b: extract hostsecurity fingerprints + if "hostsecurity" in self.__config: + for key in self.__config.options("hostsecurity"): + if key.endswith(":fingerprints"): + host = key.replace(":fingerprints", "") + fingerprints = \ + self.__config["hostsecurity"][key].split(",") + for fingerprint in fingerprints: + QTreeWidgetItem(self.fingerprintsList, [ + host, + fingerprint.replace("\\", "").strip() + ]) self.__finalizeFingerprintsColumns() @pyqtSlot() @@ -325,7 +412,7 @@ """ Private slot to add a fingerprints entry. """ - dlg = HgUserConfigHostFingerprintDialog(self) + dlg = HgUserConfigHostFingerprintDialog(self, version=self.__version) if dlg.exec_() == QDialog.Accepted: host, fingerprint = dlg.getData() itm = QTreeWidgetItem(self.fingerprintsList, [host, fingerprint]) @@ -360,7 +447,8 @@ if itm is not None: host = itm.text(0) fingerprint = itm.text(1) - dlg = HgUserConfigHostFingerprintDialog(self, host, fingerprint) + dlg = HgUserConfigHostFingerprintDialog(self, host, fingerprint, + version=self.__version) if dlg.exec_() == QDialog.Accepted: host, fingerprint = dlg.getData() itm.setText(0, host)