Beispiel #1
0
class AWCore:
    class Error(Exception):
        def __init__(self, raison):
            self.reason = raison

        def __str__(self):
            return self.reason

    def __init__(self):
        self.textEdit = TETextEdit(
            language_name=TLPreferences['DFT_WRITING_LANGUAGE'])
        self.metadata = DPMetaData()
        self.filepath = None
        self.lastFiles = CLLastFiles()
        self.config_spelling = CLSpelling()
        self.config_autocor = CLAutoCorrection()

    def CMD_FileSave(self, filepath=None):
        if filepath == None:
            if self.filepath == None:
                raise self.Error('Please specify the filepath')
            filepath = self.filepath
        else:
            self.filepath = filepath
        # filepath += 'ZIP'

        tmpfile = False
        if os.path.exists(filepath):
            # For safety, we temporary copy the exixting file
            tmpfile = os.path.join(
                os.path.split(filepath)[0], '~' + os.path.split(filepath)[1])
            shutil.copyfile(filepath, tmpfile)

        # we save the main file, the 'w' option is to erase existing files
        res = FMZipFileManagement.save(str(self.textEdit.toXml()),
                                       zipfilepath=filepath,
                                       filename='main.txt',
                                       modezip='w')

        if res:
            # we will save the file .athw_meta as well
            cur = self.textEdit.textCursor()
            self.metadata.__setitem__('lastpos',
                                      int(cur.position()),
                                      protected=False)
            self.metadata['language'] = self.textEdit.language.name
            self.metadata.__setitem__('profile',
                                      self.textEdit.language.profile,
                                      protected=False)

            self.metadata.__setitem__('athw_version',
                                      __version__,
                                      protected=False)

            to_save = self.metadata.toxml()
            res = FMZipFileManagement.save(to_save, filepath, 'meta.xml')

            self.config_spelling.local_file = filepath
            self.config_autocor.local_file = filepath

            self.config_spelling.saveConfig('file')
            self.config_autocor.saveConfig('file')

            def updateLocal(config):
                _, filepath_config, exists = config.get_file_list(
                    where='local')[0]
                if exists:
                    config_tmp = config.__class__(local_file=filepath)
                    if config.dictConfig['local'] != None:
                        config_tmp.update(config.dictConfig['local'], 'local')
                    config = config_tmp
                config.saveConfig('local')
                return config

            self.config_spelling = updateLocal(self.config_spelling)
            self.config_autocor = updateLocal(self.config_autocor)

        if tmpfile:  #  We remove the temporary file
            os.remove(tmpfile)
            if os.path.exists(filepath + '_meta'):
                # if there was a metadata file from previous version
                os.remove(filepath + '_meta')

    def CMD_FileSaveCopy(self, filepath):
        old_filepath = self.filepath
        self.CMD_FileSave(filepath)
        self.filepath = old_filepath

    def CMD_FileOpen(self, filepath):
        if filepath == None:
            if self.filepath == None:
                raise self.Error('Please specify the filepath')
            filepath = self.filepath
        else:
            self.filepath = filepath

        try:
            text = FMZipFileManagement.open(self.filepath, 'main.txt')
            if 'meta.xml' in FMZipFileManagement.listFiles(self.filepath):
                metadata = DPMetaData.init_from_xml_string(
                    FMZipFileManagement.open(self.filepath, 'meta.xml'))
            else:
                metadata = DPMetaData()
            self.config_spelling.changeFile(filepath)
            self.config_autocor.changeFile(filepath)

        except zipfile.BadZipFile:
            text, metadata = self.CMD_FileOpen_version_1(filepath)
        except KeyError as e:
            raise IOError('Not a correct ATHW file' + str(e))

        self.metadata = metadata
        language = self.metadata['language']
        lastpos = self.metadata['lastpos']
        profile = self.metadata['profile']
        if profile == None:
            profile = TLPreferences['DFT_TYPO_PROFILE']

        self.textEdit.setText(text,
                              type='xml',
                              new_language=language,
                              profile=profile)
        if lastpos != None:
            if lastpos == -1:
                lastpos = self.textEdit.document().characterCount()
            cur = self.textEdit.textCursor()
            cur.setPosition(lastpos)
            self.textEdit.setTextCursor(cur)

        return True

    def CMD_FileOpen_version_1(self, filepath):
        """
		In order to open old Athw files that were not a zip file.
		"""
        # Get the text:
        local_dir, tmp = os.path.split(self.filepath)
        text = FMTextFileManagement.open(filepath)

        meta_filepath, tmp = os.path.splitext(self.filepath)
        meta_filepath += '.athw_meta'
        if os.path.exists(meta_filepath):
            metadata = DPMetaData.init_from_xml_string(
                FMTextFileManagement.open(meta_filepath))
        else:
            metadata = DPMetaData()
        return text, metadata

    def CMD_FileImport(self, filepath, format_name, to_skip=None):
        """
		- filepath : the path to the file to import
		- format_name : the name of the format
		- to_skip : the list of the lines to skip
		"""
        if to_skip == None: to_skip = []
        # We check whenever we can import this type of files
        list_extentions = list(DIDict.keys())

        if format_name not in list_extentions:
            raise self.Error(
             'Unknown format <%s> to import: choose in '%format_name+\
                       str(list_extentions))

        fiimport_instance = DIDict[format_name](file=filepath)
        fiimport_instance.import2xml()
        for i in to_skip:
            fiimport_instance.skipLine(i)
        text = fiimport_instance.text
        self.textEdit.setText(text, type='xml')

        self.textEdit.SLOT_actionRecheckTypography()
        self.filepath = None


    def CMD_FileExport(self,format_name,filepath=None,check_typo=False,\
                    **kargs):
        if check_typo:
            self.textEdit.SLOT_actionRecheckTypography()
        list_extentions = [format.name for format in DEList]
        if format_name not in list_extentions:
            raise self.Error('Unknown format to import: choose in '+\
                       str(list_extentions))
        index = list_extentions.index(format_name)
        format = DEList[index](TSManager)

        format.export(self.textEdit, **kargs)
        res = format.export_file(
            #				default_saving_site=self.get_default_opening_saving_site(),
            parent=self,
            filepath=filepath,
        )

        return res
class AWCore:
	class Error (Exception):
		def __init__(self,raison):
			self.raison = raison

		def __str__(self):
			return self.raison.encode('ascii','replace')

	def __init__(self):
		self.textEdit = TETextEdit(language_name=
										TLPreferences['DFT_WRITING_LANGUAGE'])
		self.metadata = DPMetaData()
		self.filepath=None
		self.lastFiles=CLLastFiles(FMLastFilesFile.open())

		
	def CMD_FileSave(self,filepath=None):
		if filepath==None:
			if self.filepath==None:
				raise self.Error('Please specify the filepath')
			filepath = self.filepath
		else:
			self.filepath = filepath
		res = FMFileManagement.save(unicode(self.textEdit.toXml()),filepath)
		
		if res :
			if not self.metadata.isEmpty(): 
				# we will save the file .athw_meta as well
				cur = self.textEdit.textCursor()
				self.metadata.__setitem__('lastpos', int(cur.position()), 
															protected=False)
				self.metadata['language'] = self.textEdit.language.name
				
				to_save = self.metadata.toxml()
				meta_filepath,tmp = os.path.splitext(self.filepath)
				meta_filepath += '.athw_meta'
				
				res = FMFileManagement.save(to_save,meta_filepath)
				
	def	CMD_FileSaveCopy(self,filepath):
		old_filepath = self.filepath
		self.CMD_FileSave(filepath)
		self.filepath = old_filepath 
		
	def CMD_FileOpen(self,filepath):
		if filepath==None:
			if self.filepath==None:
				raise self.Error('Please specify the filepath')
			filepath = self.filepath
		else:
			self.filepath=filepath
		
		
		# Get the text:
		local_dir,tmp = os.path.split(self.filepath)
		text = FMFileManagement.open(filepath)
		
		meta_filepath,tmp = os.path.splitext(self.filepath)
		meta_filepath += '.athw_meta'
		if os.path.exists(meta_filepath):
			self.metadata=DPMetaData.init_from_xml_string(
								FMFileManagement.open(meta_filepath))
		else:
			self.metadata=DPMetaData()
		language = self.metadata['language']
		lastpos = self.metadata['lastpos']
		
		self.textEdit.setText(text,type='xml',new_language=language)
		if lastpos != None:
			cur = self.textEdit.textCursor()
			cur.setPosition(lastpos)
			self.textEdit.setTextCursor(cur)
			
		return True
	
	def CMD_FileImport(self,filepath,format_name,to_skip=None):
		"""
		- filepath : the path to the file to import
		- format_name : the name of the format
		- to_skip : the list of the lines to skip
		"""
		if to_skip==None : to_skip=[]
		# We check whenever we can import this type of files
		list_extentions = [format.extension for format in DIList]
		if format_name not in list_extentions:
			raise self.Error('Unknown format to import: choose in '+\
														str(list_extentions))
		
		
		fiimport_instance = DIList[list_extentions.index(format_name)](
														file=filepath)
		fiimport_instance.import2xml()
		for i in to_skip:
			fiimport_instance.skipLine(i)
		text = fiimport_instance.text
		self.textEdit.setText(text,type='xml')
		self.filepath = None
		

	def CMD_FileExport(self,format_name,filepath=None,check_typo=False,\
																	**kargs):
		if check_typo:
			self.textEdit.SLOT_actionRecheckTypography()
		list_extentions = [format.name for format in DEList]
		if format_name not in list_extentions:
			raise self.Error('Unknown format to import: choose in '+\
														str(list_extentions))
		index = list_extentions.index(format_name)
		format = DEList[index](TSManager)
		
	
		format.export(self.textEdit,**kargs)
		res = format.export_file(
#				default_saving_site=self.get_default_opening_saving_site(),
				parent=self,
				filepath=filepath,
				)
				
		return res