Realizzata l'esportazione in PDF (quella in DVI ancora è imprecisa)

Leonardo Robol [2009-06-14 19:35]
Realizzata l'esportazione in PDF (quella in DVI ancora è imprecisa)
Ancora molto sperimentale...
Filename
libs/latex_manager.py
songbook-editor.py
ui/interface.py
ui/interface.ui
diff --git a/libs/latex_manager.py b/libs/latex_manager.py
index fe93725..409cd2f 100644
--- a/libs/latex_manager.py
+++ b/libs/latex_manager.py
@@ -1,4 +1,4 @@
-import re
+import re, subprocess, os, tempfile, shutil

 class latex_manager():
     def __init__(self):
@@ -84,9 +84,7 @@ class latex_manager():
         buf += "\n\n"

         # Index generation
-        buf += "\\MakeTitleIndex\n"
-        buf += "\\MakeTitleContents\n"
-        buf += "\\MakeKeyIndex\n"
+        buf += "\index{" + opt["title"] + "}\n"
         buf += "\\makeindex\n"
         buf += "\n\n"

@@ -109,3 +107,79 @@ class latex_manager():

         # Give buffer back to be printed
         return buf
+
+    def latex_compile(self, latex_file, opt):
+        # We should check that latex exist...
+        lat = subprocess.Popen(["latex", ""], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+        # Gli spediamo il nostro documento latex
+        out, err = lat.communicate(latex_file.encode("utf-8"))
+
+        return out # Binary data
+
+    def create_index_ist(self):
+        buf = unicode();
+        buf += "headings_flag    1\n"
+        buf += "heading_prefix   \"\\n \\\\item \\\\textbf{\"\n"
+        buf += "heading_suffix   \"}\" \n"
+        buf += "symhead_positive \"Simboli\"\n"
+        buf += "symhead_negative \"simboli\"\n"
+        buf += "numhead_positive \"Numeri\"\n"
+        buf += "numhead_negative \"numeri\"\n"
+        buf += "delim_0          \" \\\\dotfill\\\\ \"\n"
+        buf += "delim_1          \" \\\\dotfill\\\\ \"\n"
+        buf += "delim_2          \" \\\\dotfill\\\\ \"\n"
+        return buf
+
+
+    def create_pdf_from_songbook(self, song_list, opt):
+        # Creo una directory temporanea
+        tmpdir = tempfile.mkdtemp()
+
+        # Creo il file index.ist che mi servira' per l'indice
+        f = open(tmpdir + "/index.ist", 'w')
+        f.write(self.create_index_ist())
+        f.close()
+
+        # Creo il file latex
+        f = open(tmpdir + "/canzoniere.tex", 'w')
+        f.write(self.export_songbook(song_list, opt).encode("utf-8"))
+        f.close()
+
+        # Comincio a compilare il codice
+        p = subprocess.Popen(["latex", tmpdir + "/canzoniere.tex"], cwd = tmpdir, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
+        out, err = p.communicate("q\n") # in questo modo va avanti anche in caso di errori
+        p.wait()
+
+        # print tmpdir # Se gia' fa questo non e' male :)
+
+        # Creo l'indice
+        p = subprocess.Popen("makeindex -s index.ist canzoniere.idx", shell=True ,cwd = tmpdir, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
+        p.wait()
+
+        # Ricreo il DVI finale
+        p = subprocess.Popen(["latex", tmpdir + "/canzoniere.tex"], cwd = tmpdir, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
+        out, err = p.communicate("q\n") # in questo modo va avanti anche in caso di errori
+        p.wait()
+
+        # Converto in PS
+        p = subprocess.Popen("dvips -t a5 canzoniere.dvi -q -o", shell=True, cwd = tmpdir, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
+        p.wait()
+
+        # Converto in PDF
+        p = subprocess.Popen(["ps2pdf", "canzoniere.ps"], cwd = tmpdir, stdin = subprocess.PIPE, stdout =subprocess.PIPE, stderr= subprocess.PIPE)
+        p.wait()
+
+        # Leggo il PDF
+        f = open(tmpdir + "/canzoniere.pdf", 'rb')
+        pdf = f.read()
+        f.close()
+
+        # Elimino le cartelle temporanee
+        shutil.rmtree(tmpdir)
+
+        return pdf
+
+
+
+
diff --git a/songbook-editor.py b/songbook-editor.py
index 461e4df..0cdf556 100755
--- a/songbook-editor.py
+++ b/songbook-editor.py
@@ -44,6 +44,8 @@ class interface(QtGui.QMainWindow):
         # Menu Canzone
         self.connect(self.ui.actionSalva_canzone, QtCore.SIGNAL("activated()"), self.save_song_to_file)
         self.connect(self.ui.actionImporta_canzone, QtCore.SIGNAL("activated()"), self.import_song_from_file)
+        self.connect(self.ui.actionEsporta_in_DVI, QtCore.SIGNAL("activated()"), self.export_to_DVI)
+        self.connect(self.ui.actionEsporta_in_PDF, QtCore.SIGNAL("activated()"), self.export_to_PDF)

         # Menu Canzoniere
         self.connect(self.ui.actionEsporta_in_LaTeX, QtCore.SIGNAL("activated()"), self.export_songbook)
@@ -170,7 +172,7 @@ class interface(QtGui.QMainWindow):
             output += sep + rit + par.content()
         return output

-
+    # Convert a file (rcs) to a song object
     def file_to_song(self, file_content):
         buf = file_content.split(self.sep)
         newsong = song(buf[0], [], buf[1], buf[2], buf[3], buf[4])
@@ -184,7 +186,7 @@ class interface(QtGui.QMainWindow):
                 break
         return newsong

-
+    # import a song from a file on the local hard drive, the function is interactive!
     def import_song_from_file(self):
         filetoimport = QtGui.QFileDialog.getOpenFileName(self, "Importa canzone", "", "Canzoni di RobolCanzoniere (*.rcs)")
         handle = open(filetoimport, 'r')
@@ -217,7 +219,7 @@ class interface(QtGui.QMainWindow):
         else:
             self.new_song

-
+    # Converte una canzone in un file LaTeX non autosufficiente, che puo' essere inserito in un altro canzoniere latex.
     def create_latex_song(self):
         song = self.get_active_song()
         filetowrite = lm.create_song(song)
@@ -231,6 +233,8 @@ class interface(QtGui.QMainWindow):
         handle.write(filetowrite.encode("utf-8"))
         handle.close()

+
+    # Esporta il canzoniere in LaTeX, in ogni caso dovra' essere esportato
     def export_songbook(self):
         # Chiedo al latex manager di farlo.. :)
         sbk = lm.export_songbook(self.song_db, widget.opt)
@@ -244,6 +248,35 @@ class interface(QtGui.QMainWindow):
         handle.write(sbk.encode("utf-8"))
         handle.close()

+    # Esporta il canzoniere in DVI
+    def export_to_DVI(self):
+        # Otteniamo il codice LaTeX
+        sbk = lm.export_songbook(self.song_db, widget.opt)
+        # Compiliamo il DVI
+        dvifile = lm.latex_compile(sbk, widget.opt)
+        # Apriamo un file
+        filename = QtGui.QFileDialog.getSaveFileName(self, "Salva file DVI", "", "DVI file (*.dvi)")
+        if(filename == ''):
+            # We do not have to do nothing, the user clicked Cancel
+                return 0
+        handle = open(filename, 'wb')
+        handle.write(dvifile)
+        handle.close()
+
+    # Esporta il canzoniere in PDF
+    def export_to_PDF(self):
+        pdf_file = lm.create_pdf_from_songbook(self.song_db, widget.opt)
+        # Apriamo un file
+        filename = QtGui.QFileDialog.getSaveFileName(self, "Salva file PDF", "", "PDF file (*.pdf)")
+        if(filename == ''):
+            # We do not have to do nothing, the user clicked Cancel
+            return 0
+        handle = open(filename, 'wb')
+        handle.write(pdf_file)
+        handle.close()
+
+
+
     def options(self):
         # self.opt = option_interface()
         option.show()
diff --git a/ui/interface.py b/ui/interface.py
index 35b2e5f..0d4d532 100644
--- a/ui/interface.py
+++ b/ui/interface.py
@@ -2,7 +2,7 @@

 # Form implementation generated from reading ui file 'interface.ui'
 #
-# Created: Sun Apr 26 19:28:13 2009
+# Created: Sun Jun 14 20:05:39 2009
 #      by: PyQt4 UI code generator 4.4.4
 #
 # WARNING! All changes made in this file will be lost!
@@ -125,12 +125,18 @@ class Ui_MainWindow(object):
         self.actionEsporta_in_LaTeX.setObjectName("actionEsporta_in_LaTeX")
         self.actionOpzioni_LaTeX = QtGui.QAction(MainWindow)
         self.actionOpzioni_LaTeX.setObjectName("actionOpzioni_LaTeX")
+        self.actionEsporta_in_DVI = QtGui.QAction(MainWindow)
+        self.actionEsporta_in_DVI.setObjectName("actionEsporta_in_DVI")
+        self.actionEsporta_in_PDF = QtGui.QAction(MainWindow)
+        self.actionEsporta_in_PDF.setObjectName("actionEsporta_in_PDF")
         self.menuFile.addAction(self.actionApri)
         self.menuFile.addAction(self.actionSalva)
         self.menuFile.addSeparator()
         self.menuFile.addAction(self.actionEsci_2)
         self.menuCanzoniere.addAction(self.actionEsporta_in_LaTeX)
         self.menuCanzoniere.addAction(self.actionOpzioni_LaTeX)
+        self.menuCanzoniere.addAction(self.actionEsporta_in_DVI)
+        self.menuCanzoniere.addAction(self.actionEsporta_in_PDF)
         self.menuCanzone.addAction(self.actionSalva_canzone)
         self.menuCanzone.addAction(self.actionImporta_canzone)
         self.menubar.addAction(self.menuFile.menuAction())
@@ -173,4 +179,6 @@ class Ui_MainWindow(object):
         self.actionImporta_canzone.setText(QtGui.QApplication.translate("MainWindow", "Importa canzone", None, QtGui.QApplication.UnicodeUTF8))
         self.actionEsporta_in_LaTeX.setText(QtGui.QApplication.translate("MainWindow", "Esporta in LaTeX", None, QtGui.QApplication.UnicodeUTF8))
         self.actionOpzioni_LaTeX.setText(QtGui.QApplication.translate("MainWindow", "Opzioni LaTeX", None, QtGui.QApplication.UnicodeUTF8))
+        self.actionEsporta_in_DVI.setText(QtGui.QApplication.translate("MainWindow", "Esporta in DVI", None, QtGui.QApplication.UnicodeUTF8))
+        self.actionEsporta_in_PDF.setText(QtGui.QApplication.translate("MainWindow", "Esporta in PDF", None, QtGui.QApplication.UnicodeUTF8))

diff --git a/ui/interface.ui b/ui/interface.ui
index 0c602c2..cb5fd28 100644
--- a/ui/interface.ui
+++ b/ui/interface.ui
@@ -193,6 +193,8 @@ p, li { white-space: pre-wrap; }
     </property>
     <addaction name="actionEsporta_in_LaTeX"/>
     <addaction name="actionOpzioni_LaTeX"/>
+    <addaction name="actionEsporta_in_DVI"/>
+    <addaction name="actionEsporta_in_PDF"/>
    </widget>
    <widget class="QMenu" name="menuCanzone">
     <property name="title">
@@ -246,6 +248,16 @@ p, li { white-space: pre-wrap; }
     <string>Opzioni LaTeX</string>
    </property>
   </action>
+  <action name="actionEsporta_in_DVI">
+   <property name="text">
+    <string>Esporta in DVI</string>
+   </property>
+  </action>
+  <action name="actionEsporta_in_PDF">
+   <property name="text">
+    <string>Esporta in PDF</string>
+   </property>
+  </action>
  </widget>
  <resources/>
  <connections>
ViewGit