12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- from PyQt5.QtCore import QThread, pyqtSignal
- from helpers.xmlhandler import XMLHandler
- import os
- class Sign(QThread):
- status_bar_signal = pyqtSignal(str)
- sign_button_signal = pyqtSignal(bool)
- progress_bar_signal = pyqtSignal(bool)
- message_signal = pyqtSignal(tuple)
- def __init__(self, parent, data):
- super(Sign, self).__init__(parent)
- self.data = data
- def run(self):
- if 'file' in self.data and self.sign_file(self.data['file']):
- self.change_status('XML successfully signed.', 'Signing successful')
- elif 'directory' in self.data and self.sign_arrange_directory(self.data['directory']):
- self.change_status('XML successfully signed and files arranged.', 'Signing & arranging successful')
- def sign_file(self, path):
- try:
- xml_file = XMLHandler(path, self.data['config']['xmlreftype'])
- if not xml_file.valid(self.data['config']['xmlschema']):
- self.change_status('XML file does not comply schema.', 'XML file error', False)
- xml_file.sign(self.data['config']['certificate'], self.data['password'])
- return xml_file
- except BaseException as e:
- self.change_status(str(e), 'Signing error', False)
- return None
- def sign_arrange_directory(self, path):
- files = list()
- extensions = list()
- xml_path = None
- for file in os.listdir(path):
- filename, extension = os.path.splitext(file)
- extension = extension.lower()
- extensions.append(extension)
- if extension == '.xml':
- if xml_path is None:
- xml_path = os.path.join(path, file)
- else:
- break
- files.append((file, extension))
- if len(extensions) != len(set(extensions)):
- self.change_status('There are multiple files with same extension in directory.', 'Arranging error', False)
- return False
- elif xml_path is None:
- self.change_status('There is no XML file in directory.', 'Signing error', False)
- return False
- xml_file = self.sign_file(xml_path)
- if not xml_file:
- return False
- general_filename = xml_file.get_general_filename()
- for file, extension in files:
- try:
- os.rename(os.path.join(path, file), os.path.join(path, general_filename + extension))
- except:
- self.change_status('Something went wrong with arranging files.', 'Arranging error', False)
- return False
- return True
- def change_status(self, message, status_msg, success=True):
- self.message_signal.emit((message, success))
- self.status_bar_signal.emit(status_msg)
- self.progress_bar_signal.emit(False)
- self.sign_button_signal.emit(True)
|