[spe.pycs.net] Re: Unicode Encoding?

www.stani.be s_t_a_n_i at yahoo.com
Wed Nov 24 15:53:20 CET 2004


--- Benno Dielmann <mail at benno-dielmann.de> wrote:

> On Tuesday 23 November 2004 21:11, Chris Spencer
> wrote:
> | Benno Dielmann wrote:
> | > I'll paste in my solution to this problem which
> I posted to this mailing
> | > list some time ago. So here it comes:
> |
> | [snip]
> |
> | Thanks, that did the trick. That and changing the
> font to one which
> | supports Asian characters. Although this hack is
> far from streamlined.
> | This trick assumes that all your files will
> conform to this encoding,
> | ignoring Python's protocol to list the encoding at
> the top of the file,
> | e.g. # -*- coding: utf-8 -*- for utf-8. It'd be
> nice to have a drop-down
> | menu that let's you select an encoding, akin to a
> web-browser, or let
> | the SPE auto-detect the encoding for each file.
> 
> Does this really ignore the # -*- coding: ... -
> string at the top of the file? 
> As far as I understand it sets the default encoding
> which applies if there 
> isn't any encoding specified (i.e. no such coding
> string). 
> 
> Why couldn't spe just (auto-)detect the encoding by
> parsing this coding string 
> instead of the user manually selecting the encoding
> in some drop-down box? 
> One whould just have to put the proper coding-string
> on top of each file 
> which is recommended anyway, I think. Python (2.3)
> prints a deprecation 
> warning anyway if there are non-ascii characters in
> the file and no encoding 
> specified. 
> 
> Perhaps spe could support the user constructing the
> coding-string. There we 
> have the drop-down box again ;-). But this box
> whould just construct a proper 
> python coding string and past it in the right place
> in the file. 
> 
> What do you think about it?
Seems interesting. I'm now at the moment implementing
the general spe encoding preference settings, which is
still handy for source code which doesn't provide this
encode comment.

This now the code in the save method of _spe/Child.py:
source=self.source.GetText()
if self.parentPanel.getValue('StripTrailingSpaces'):
    source='\n'.join([l.rstrip() for l in
source.split('\n')])
content = str(source.replace('\r\n','\n'))
if not self.dosLines:
    #convert to Unix lines
    content = str(source.replace('\r\n','\n'))
#Note that the mode here must be "wb" to allow
#line endings to be preserved.
file = open(str(self.fileName),'wb')
file.write(content)
file.close()

Let's suppose e.g. the encoding is utf-16...
Should than the change be?:
file.write(content.encode('utf-16'))

And than how about opening files, now in the openList
method in _spe/Parent.py:
source=open(fileName).read()

Should this than figure out this comment line and
apply afterwards:
source = source.decode('utf-16')

Stani
http://spe.pycs.net
http://www.stani.be

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Spe-user mailing list