dabo Commit
Revision 7138
Date: 2012-04-24 01:46:22 -0700 (Tue, 24 Apr 2012)
Author: Ed
Trac: http://trac.dabodev.com/changeset/7138

Changed:
U   trunk/dabo/ui/uiwx/dEditor.py
U   trunk/dabo/ui/uiwx/dPemMixin.py

Log:
Added encoding declaration detection when reading/writing script files to 
dEditor. Cleaned up some string handling for Captions in dPemMixin.

Diff:
Modified: trunk/dabo/ui/uiwx/dEditor.py
===================================================================
--- trunk/dabo/ui/uiwx/dEditor.py       2012-04-06 15:36:03 UTC (rev 7137)
+++ trunk/dabo/ui/uiwx/dEditor.py       2012-04-24 08:46:22 UTC (rev 7138)
@@ -93,6 +93,8 @@
                "arrows": stc.STC_MARK_ARROWS,
                "rectangle": stc.STC_MARK_SMALLRECT}
 
+# Encoding declaration pattern
+encoding_pat = re.compile(r"coding[=:]\s*([-\w.]+)")
 
 ## testing load performance:
 delay = False
@@ -1466,8 +1468,8 @@
                return fname
 
 
-       def saveFile(self, fname=None, force=False):
-               if not force and (not self.isChanged() and self._fileName):
+       def saveFile(self, fname=None, force=False, isTmp=False):
+               if not force and not isTmp and (not self.isChanged() and 
self._fileName):
                        # Nothing changed
                        return
                if fname is None:
@@ -1482,32 +1484,39 @@
                                # user canceled in the prompt: don't continue
                                return False
                else:
-                       try:
-                               fModTime = os.stat(fname).st_mtime
-                       except OSError:
-                               fModTime = None
-                       if fModTime > self._fileModTime:
-                               if not dabo.ui.areYouSure(_("""The file has 
been modified on the disk since you opened it.
-Do you want to overwrite it?"""), _("File Conflict"), defaultNo=True, 
cancelButton=False):
-                                       return
+                       if not isTmp:
+                               try:
+                                       fModTime = os.stat(fname).st_mtime
+                               except OSError:
+                                       fModTime = None
+                               if fModTime > self._fileModTime:
+                                       prompt = _("""The file has been 
modified on the disk since you opened it.
+Do you want to overwrite it?""")
+                                       if not dabo.ui.areYouSure(prompt, 
_("File Conflict"),
+                                                       defaultNo=True, 
cancelButton=False):
+                                               return
+               txt = self.GetText()
+               enc = self._getEncodingDeclaration(txt)
+               if enc:
+                       txt = txt.encode(enc)
                try:
-                       open(fname, 
"wb").write(self.GetText().encode(self.Encoding))
+                       open(fname, "wb").write(txt)
                except OSError:
                        dabo.ui.stop(_("Could not save file '%s'. Please check 
your write permissions.") % fname)
                        return False
-               # set self._fileName, in case it was changed with a Save As
-               self._fileName = fname
-               self._fileModTime = os.stat(fname).st_mtime
-               self._clearDocument(clearText=False, clearUndoBuffer=False)
-               # Save the appearance settings
-               app = self.Application
-               app.setUserSetting("editor.fontsize", self._fontSize)
-               app.setUserSetting("editor.fontface", self._fontFace)
+               if not isTmp:
+                       # set self._fileName, in case it was changed with a 
Save As
+                       self._fileName = fname
+                       self._fileModTime = os.stat(fname).st_mtime
+                       self._clearDocument(clearText=False, 
clearUndoBuffer=False)
+                       # Save the appearance settings
+                       app = self.Application
+                       app.setUserSetting("editor.fontsize", self._fontSize)
+                       app.setUserSetting("editor.fontface", self._fontFace)
 
-               #if the file extension changed, automatically set the language 
if extension is known.
-               fext = os.path.splitext(fname)[1]
-               self.Language = fileFormatsDic.get(fext, self.Language)
-
+                       #if the file extension changed, automatically set the 
language if extension is known.
+                       fext = os.path.splitext(fname)[1]
+                       self.Language = fileFormatsDic.get(fext, self.Language)
                return True
 
 
@@ -1587,6 +1596,14 @@
                        return False
 
 
+       def _getEncodingDeclaration(self, txt):
+               """Extract the encoding declaration, if any, and return it, or 
return None."""
+               try:
+                       return encoding_pat.search(txt).groups()[0]
+               except (AttributeError, IndexError) as e:
+                       return None
+
+
        def openFile(self, fileSpec=None, checkChanges=True):
                """Open a new file and edit it."""
                cc = True
@@ -1599,8 +1616,15 @@
                                        return False
                        try:
                                f = open(fileSpec, "rb")
-                               text = f.read().decode(self.Encoding)
+                               raw_text = f.read()             
#.decode(self.Encoding)
                                f.close()
+                               # Check for encoding
+                               encoding = 
self._getEncodingDeclaration(raw_text)
+                               if encoding:
+                                       text = raw_text.decode(encoding)
+                               else:
+                                       text = raw_text
+                                       
                        except IOError:
                                if os.path.exists(fileSpec):
                                        dabo.ui.stop(_("Could not open %s.  
Please check that you have read permissions.") % fileSpec)

Modified: trunk/dabo/ui/uiwx/dPemMixin.py
===================================================================
--- trunk/dabo/ui/uiwx/dPemMixin.py     2012-04-06 15:36:03 UTC (rev 7137)
+++ trunk/dabo/ui/uiwx/dPemMixin.py     2012-04-24 08:46:22 UTC (rev 7138)
@@ -2051,21 +2051,23 @@
                # Force the value to string
                val = "%s" % val
                def __captionSet(val):
-                       # Windows textboxes change their value when SetLabel() 
is called; this
-                       # avoids that problem.
+                       """Windows textboxes change their value when SetLabel() 
is called; this
+                       avoids that problem.
+                       """
                        if not isinstance(self, (dabo.ui.dTextBox, 
dabo.ui.dEditBox)):
                                self._caption = val
+                               uval = ustr(val)
                                ## 2/23/2005: there is a bug in wxGTK that 
resets the font when the
                                ##            caption changes. So this is a 
workaround:
                                font = self.Font
-                               self.SetLabel(val)
+                               self.SetLabel(uval)
                                self.Font = font
                                self.refresh()
 
                                # Frames have a Title separate from Label, but 
I can't think
                                # of a reason why that would be necessary... 
can you?
                                try:
-                                       self.SetTitle(val)
+                                       self.SetTitle(uval)
                                except AttributeError:
                                        # wxPython 2.7.x started not having 
this attribute for labels
                                        # at least.



_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message: 
http://leafe.com/archives/byMID/[email protected]

Reply via email to