Commits

Henning Schröder committed c0e43cb

fileio synxtax errors fixed

Comments (0)

Files changed (3)

codeaide/fileio.py

 # -*- coding: utf-8 -*-
 import os
 import re
+import mimetypes
 
+from PyQt4.QtGui import QMessageBox, QFileDialog
 
-DEFFAULT_ENCODING = "utf-8"
+
+
+mimetypes.init()
+DEFAULT_ENCODING = "utf-8"
+
+try:
+    i18n
+except NameError:
+    i18n = lambda s:s
+
+
+def confirm_close(doc):
+    if doc.isModified():
+        ret = QMessageBox.warning(
+            self.parent.widget, i18n("Closing file"),
+            i18n("This document has been modified.\n"
+                 "Do you want to save your changes?"),
+            QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel,
+            QMessageBox.Cancel)
+        if ret == QMessageBox.Cancel:
+            return False
+        elif ret == QMessageBox.Save:
+            pass
+
+
+
+
+class FileDialog(object):
+
+
+    def __init__(self, filters, path=None, remember=True):
+        self.filters = filters
+        self.path = path or os.getcwd()
+        self.remember = remember
+
+
+    def open(self, parent=None, label=None):
+        filename = QFileDialog.getOpenFileName(
+            parent, label or i18n("Open file"), self.path, self.filters)
+        if filename:
+            filename = os.path.abspath(unicode(filename))
+            if self.remember:
+                self.path = os.path.dirname(filename)
+        return filename
+
+
+    def save(self, parent=None, label=None):
+        filename = QFileDialog.getSaveFileName(
+            parent, label or i18n("Save file"), self.path, self.filters)
+        if filename:
+            filename = os.path.abspath(unicode(filename))
+            if self.remember:
+                self.path = os.path.dirname(filename)
+        return filename
+
+
+
 
 
 class FileIO(object):
             backup_name="%s~",
             autosave_name="#%s#",
           )
-          
+        self.file_dialog = FileDialog("Python Source (*.py *.pyw)")
 
-    def guess_encoding(self, text):
+
+    def guess_mimetpye(self, filename):
+        return mimetypes.guess_type(filename, strict=False)[0] or "text/plain"
+
+
+    def guess_encoding(self, filename, text):
         lines = text.splitlines()
         enc_match = re.compile("#\s*-\*-\s*coding:\s*(.*?)\s*-\*-").match
         
             return encoding
 
 
+    def open_dialog(self):
+        return self.file_dialog.open()
+
+        
+    def save_dialog(self):
+        return self.file_dialog.save()
+
+
     def load(self, filename, encoding=None):
         # XXX: check if autosave filename is newer
         # XXX: check size below sensible limit 
         f = open(filename)
         text = f.read()
         f.close()
-        encoding = encoding or self.guess_encoding(text)
+        encoding = encoding or self.guess_encoding(filename, text)
         return text.decode(encoding)
 
 
     def save(self, filename, text, **settings):
+        encoding = settings.get("encoding") or self.guess_encoding(filename, text)
         settings = dict(self.settings, **settings)
         tab = " " * setting["tab_width"]
         lines = text.splitlines()
     def autosave(self, filename, text, **settings):
         asettings = dict(self.settings)
         asettings.update(            
-            strip_blank_lines=False
+            strip_blank_lines=False,
             untabify=False)
         asettings.update(settings)
         path, name = os.path.split(filename)
         afilename = os.path.join(path, "#%s#" % name)
         return self.save(afilename, text, backups=False, **settings)
+
+    
+    
+    def readable(self, filename):
+        return True
+    
+    
+    def writeable(self, filename):
+        return True
+    
+    
+    def exists(self, filename):
+        return os.path.exists(filename)
+    
+    
+    def warning(self, msg):
+        print i18n("Warning:"), msg
+
+        
+    def error(self, msg):
+        print i18n("Error:"), msg
+
+
+    def expect_exists(self, filename):
+        self.exists(filename) or self.warning(i18n("The filename %s does not exist."))
 from codeaide.fileio import FileIO
 
 
+FileIO.instance = FileIO()
+
+
+
 class ScrollBar(QScrollBar):
 
 
         QTextDocument.__init__(self)
         Hookable.__init__(self)
         Configurable.__init__(self)
-        if not self.__class__.file_io:
-            self.__class__.file_io = FileIO()
-
         self.filename = filename
         if text is None and filename and os.path.exists(filename):
             text = self.file_io.load(filename)
     def save_buffer(self, filename=None):
         doc = self.document()
         filename = filename or doc.filename
-        doc.file_io.save(filename, unicode(doc.toPlainText()))
+        FileIO.instance.save(filename, unicode(doc.toPlainText()))
 
-    
+
     def switch_buffer(self, num):
         doc = Buffer[num]
         self.setDocument(doc)
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
-
 import codeaide.ide.simple
 from codeaide.ide import api
 
-
+import excepthook
 
 
 if __name__ == "__main__":
     app = api.Application()
+    excepthook.install()
     app.run()