dabo Commit
Revision 1382
Date: 2005-09-26 10:00:04 -0700 (Mon, 26 Sep 2005)
Author: paul

Changed:
A   trunk/dabo/lib/reportUtils.py

Log:
Added a reportUtils lib, with two functions:

getTempFile(): Return a temporary file name, and delete the file if it 
               exists when Python closes.

 previewPDF(): Display the passed PDF file in the default PDF viewer.



Diff:
Added: trunk/dabo/lib/reportUtils.py
===================================================================
--- trunk/dabo/lib/reportUtils.py       2005-09-26 14:58:57 UTC (rev 1381)
+++ trunk/dabo/lib/reportUtils.py       2005-09-26 17:00:04 UTC (rev 1382)
@@ -0,0 +1,44 @@
+import os
+import sys
+
+
+class TempFileHolder(object):
+       """Utility class to get temporary file names and to make sure they are 
+       deleted when the Python session ends.
+       """
+       def __init__(self):
+               self._tempFiles = []
+
+       def __del__(self):
+               # Try to erase all temp files created during life.
+               for f in self._tempFiles:
+                       try:
+                               os.remove(f)
+                       except:
+                               pass
+
+       def append(self, f):
+               self._tempFiles.append(f)
+
+       def getTempFile(self, ext="pdf"):
+               f = "%s.%s" % (os.tempnam(), ext)
+               self.append(f)
+               return f
+
+tempFileHolder = TempFileHolder()
+getTempFile = tempFileHolder.getTempFile
+
+
+def previewPDF(path):
+       """Preview the passed PDF file in the default PDF viewer."""
+       try:
+               os.startfile(path)
+       except AttributeError:
+               # startfile only available on Windows
+               if sys.platform == "darwin":
+                       os.system("open %s" % path)
+               else:
+                       # on Linux, punt with xpdf:
+                       os.popen2("xpdf %s" % path)
+
+




_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev

Reply via email to