def SLOT_actionSendToExternalSoftware (self):
		# Send the scene to another software (mine is called Antidote and 
		# accept only a certain type of encoding).
		path=AWPreferences['EXTERNAL_SOFT_PATH']
		if path=="":
			QtGui.QMessageBox.information(self,
			"External Software Sender",
			"Sorry, there is no path to the external software in the "+\
			"configuration file.")
			return False
			
		text=self.textEdit.toXml()
		i=0
		while AWPreferences['TMP_FILE_MARK']+"tmp"+str(i).zfill(3)+'.txt' in \
															os.listdir('.'):
			i+=1
		name=AWPreferences['TMP_FILE_MARK']+"tmp"+str(i).zfill(3)+'.txt'
		FMFileManagement.save(text,name, encoding='utf-8-sig', mode='w')
		s=subprocess.Popen(path+' '+os.path.abspath(name))
		
		res = QtGui.QMessageBox.question(self, "External Software Sender", 
				"Have you finished to correct the file?", 
				QtGui.QMessageBox.Yes | QtGui.QMessageBox.Cancel)
		

		if (res == QtGui.QMessageBox.Yes) :
			text = FMFileManagement.open(name, encoding='utf-8-sig', mode='rb')
			self.textEdit.setText(text,type='xml')
	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 open(filepath):
		file=FMFileManagement.open(filepath,output='readlines')
		
		res=[]
		# We read the config file
		file=FMFileManagement.open(filepath,output='readlines')

		# We fill the res with the values contained into the file
		for ligne in file:
			ligne=ligne.strip()
			ligne = ligne.split(' ')
			res.append(ligne)
		return res
	def SLOT_autosave(self):
		if self.filepath!=None:
			direct,file=os.path.split(self.filepath)
			tmp_filepath = os.path.join(direct,
					AWPreferences['TMP_FILE_MARK']+file)
			res = FMFileManagement.save(
					unicode(self.textEdit.toXml()),tmp_filepath)
	def save(filepath,dict_to_save,descriptions=None):
		"""
		- dict_to_save: dict that we have to save.
		- filepath: where to save the file
		- descriptions: dict that contains the description corresponding keys 
			of dict_to_save
		"""
		if descriptions==None: descriptions={}
		s = CLPreferencesFiles.comment_sign+\
								" Preference for the software AthenaWriter."
		
		for k,v in dict_to_save.items():
			if descriptions.has_key(k):
				s += CLPreferencesFiles.comment_sign+' '+ descriptions[k]+'\n'
				
			if type(v) == list:
				l = [str(a) for a in v]
				s += k+' '+CLPreferencesFiles.entry_separator_sign+' '
				s+= CLPreferencesFiles.separator_sign.join(l) +'\n'
				
			elif type(v) == dict:
				l = [str(kk)+' '+str(vv) for kk,vv in v.items()]
				s += k+' '+CLPreferencesFiles.entry_separator_sign+' '
				s+= CLPreferencesFiles.separator_sign.join(l) +'\n'
				
			else:
				s += k+' '+CLPreferencesFiles.entry_separator_sign+' '
				s += str(v)+'\n'
				
		
		file=FMFileManagement.save(s,filepath=filepath)
	def open(pathway=None,errors='raise'):
		"""
		- pathway : the path to the "config.txt" file
		- errors: in ['raise','print','skip']
			In the case of line line of the file not well written, it either:
			- it raises the error if it is 'raise'
			- it prints the error if it is 'print'
			- it skips the error if it is 'skip'
		"""
		assert errors in ['raise','print','skip']
		pathway=pathway
		result_dictionary={}
		
		# We read the config.txt file
		file=FMFileManagement.open(pathway,output='readlines')

		# We get rid of the comments and empty lines
		file,equivalent_line = CLPreferencesFiles.clean_file(file)
		
		# We fill the result_dictionary with the values contained into the file
		for i,ligne in enumerate(file):
			try :
				# We call the method interpret_ligne to seperate the key from the values
				e,v=CLPreferencesFiles.interpret_ligne(ligne)
				result_dictionary[str(e)]=v
			except CLPreferencesFiles.Error , e:
				e_other = CLPreferencesFiles.Error(e.raison,
							line=equivalent_line[i],
							file=pathway)
				if errors=='raise':
					raise	e_other
				elif  errors=='print':
					print e_other
				elif errors=='skip':
					pass
	def save(words,filepath):
		words_keys = words.keys()
		words_keys.sort()
		words = [(k,words[k]) for k in words_keys]
		words = [' '.join(w) for w in words]
		to_save = '\n'.join(words)
		file=FMFileManagement.save(to_save,filepath=filepath)		
	def open(filepath):
		file=FMFileManagement.open(filepath,output='readlines')
		
		res={}
		# We read the config file
		file=FMFileManagement.open(filepath,output='readlines')

		# We fill the res with the values contained into the file
		for ligne in file:
			ligne=ligne.strip()
			i=ligne.find(' ')
			if i!= -1:
				k=ligne[:i]
				v=ligne[i:].strip()
				res[k]=v
		return res
	def SLOT_actionFileImport(self,filepath=None):
		"""
		If filepath==None, it will display the window to search the file
		"""
		res=self.doSaveDialog()
		if (res != QtGui.QMessageBox.Yes) and (res != QtGui.QMessageBox.No):
			return False
		if filepath==None:
			filepath = FMFileManagement.open_gui_filepath(
					self.get_default_opening_saving_site(),
					self)			
		else :
			filepath=str(filepath)
		if filepath:
			self.clean_tmp_files() # we remove the previous tmp files
			path,e = os.path.splitext(filepath)
			e = e[1:]#the [1:] is to skip the dot in the extension
			self.CMD_FileImport(filepath=filepath,format_name=e)
			
			self.actionFileSave.setEnabled(True)			
			self.setWindowTitle("AthenaWriter : NewFile")
			tmp,filename = os.path.split(filepath)
			self.changeMessageStatusBar("Has imported "+filename)
			return True
		else:
			return False
Exemple #10
0
	def save_file(self,filepath=None,outdir=None):
		"""
		- filepath : the complete path where to save the file (with extension)
			Note: if filepath==None, it will take the self.file name as the path where
			to save the file.
			Note : if the DIImport instance was not created with a 'file' argument, 
			this argument is mandatory.
		- outdir : it will take filemname (if filepath!=None, it will take it own) , and will use 
			the outdir as directory where to save
		"""
		
		assert self.text,"You should run import2xml before"
		assert filepath!=None or self.file!=None,"Please indiquate the filepath"
		if self.file!=None and filepath==None:
			path,ext = os.path.splitext(self.file)
			filepath = path+'.athw'
		if outdir!=None:
			path,fi = os.path.split(self.file)
			filepath = os.path.join(outdir,fi)
		FMFileManagement.save(self.text,filepath)
		return filepath
	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 SLOT_actionFileSave(self):
		"""
		Slot used when saving the current file.
		"""		
		if self.filepath==None:
			filepath = FMFileManagement.save_gui_filepath(
					self.get_default_opening_saving_site(),
					self,filter="AthW files (*.athw);; All files (*.*)")
		else:
			filepath = self.filepath
		if filepath :
			filepath=unicode(filepath)
			
			
			progressBar = QtGui.QProgressBar(parent=self)
			progressBar.setMaximum(0)
			progressBar.setValue(0)
			# center the widget
			point = self.geometry().center()
			point1 = progressBar.geometry().center()
			progressBar.move(point-point1)
			# set the window to the correct flag
			progressBar.setWindowFlags(QtCore.Qt.SplashScreen)
			progressBar.setWindowModality(QtCore.Qt.WindowModal)
			
			progressBar.show()
			
			# this class is a very classic thread just to save the file while
			# displaying the progressBar:
			class TaskThread(QtCore.QThread):
				taskFinished = QtCore.pyqtSignal()
				def run(self1):
					self.CMD_FileSave(filepath=filepath)
					self1.taskFinished.emit()
					
			# What we have to do at the end of the saving
			def finishing():
				progressBar.close()
				self.actionFileSave.setEnabled(False)
				self.setWindowTitle("AthenaWriter : "+self.filepath)
				tmp,filename = os.path.split(self.filepath)
				self.changeMessageStatusBar("Has saved "+filename)
				self.lastFiles.addFile(self.filepath)
			
			self.myLongTask = TaskThread()
			self.connect(self.myLongTask,QtCore.SIGNAL("taskFinished()"),
																	finishing)
					
			# self.myLongTask.taskFinished.connect(finishing)
			self.myLongTask.start()
Exemple #13
0
	def __init__(self,file=None,fromtext=None):
		"""
		A general class for importation (usually used with re-implementations)
		- file : the file to open with the text
		- fromtext: if file==None, we will take the string of these argument
		"""
		assert file!=None or fromtext!=None, 'either file or fromtext argument should not be None'
		if file!=None:
			self.file = file
			self.raw_text = FMFileManagement.open(self.file,with_codecs=False)
		else:
			self.file=None
			self.raw_text = fromtext
		self.text =False
	def SLOT_actionFileSaveAs(self):
		"""
		Slot used when saving as the current file.
		"""
		filepath = FMFileManagement.save_gui_filepath(
				self.get_default_opening_saving_site(),
				self,filter="AthW files (*.athw);; All files (*.*)")
		if filepath :
			self.clean_tmp_files() # we remove the previous tmp files
			self.CMD_FileSave(filepath=unicode(filepath))
			self.actionFileSave.setEnabled(False)
			self.setWindowTitle("AthenaWriter : "+self.filepath)
			tmp,filename = os.path.split(self.filepath)
			self.changeMessageStatusBar("Has saved "+filename)
			self.lastFiles.addFile(self.filepath)
Exemple #15
0
	def odt2html(self,skip=False):
		args = '-headless -convert-to html -outdir'
		dir_file,tmp = os.path.split(self.file)

		path,ext = os.path.splitext(self.file)
		newfile = path+'.html'
		if not skip or not os.path.exists(newfile):
			to_call = DIPreferences['LIBREOFFICE_COMMAND'] + ' ' + \
						args + ' ' +\
						os.path.abspath(dir_file)+ ' '+ \
						os.path.abspath(self.file)
			print 'to_call : ',to_call
			res = os.system(to_call)
			if res!=0:
				raise IOError('Problem during convertion of the file, is the LIBREOFFICE_COMMAND correct ?')
		else:
			print "Skip the creation of the file"

		self.raw_text = FMFileManagement.open(newfile,with_codecs=False)
	def SLOT_actionFileOpen(self,filepath=None):
		"""
		If filepath==None, it will display the window to search the file
		If filepath==None, it will display the window to search the file
		"""
		res=self.doSaveDialog()
		if (res != QtGui.QMessageBox.Yes) and (res != QtGui.QMessageBox.No):
			return False
		if filepath==None:
			filepath = FMFileManagement.open_gui_filepath(
					self.get_default_opening_saving_site(),
					self,filter="AthW files (*.athw);; All files (*.*)")			
		else :
			filepath=str(filepath)
		if filepath:
			self.clean_tmp_files() # we remove the previous tmp files
			try:
				self.CMD_FileOpen(filepath)
			except IOError:
				# if the file do not exist, propose to crreate it
				r = QtGui.QMessageBox.question(self,"Non existing file",
					"The file "+filepath+" do not exist. Do you want to "+\
					"create it ?",
					QtGui.QMessageBox.Yes|QtGui.QMessageBox.No)
					
				if r!= QtGui.QMessageBox.Yes:
					return False
				self.filepath = filepath
				
			self.actionFileSave.setEnabled(False)

			self.setWindowTitle("AthenaWriter : "+self.filepath)
			tmp,filename = os.path.split(filepath)
			self.changeMessageStatusBar("Has opened "+filename)
			self.lastFiles.addFile(self.filepath)
			
			return True
		else:
			return False
	def save(words,filepath):
		words.sort()
		words = list(set(words))
		to_save = '\n'.join(words)
		
		file=FMFileManagement.save(to_save,filepath=filepath)