2004年09月28日

There is an Shell window in NewEdit, and you can type Python code in it, just like in IDLE. This window is derived from wx.py.shell.Shell class. Some friends and me have found that typing Chinese characters will cause error. So as someone submited this error, I’d to said to him it was not my fault, though I wanted to resolve it. And yesterday  I tried to resolve it, and I made it successfully I think so. What I do all in ShellWindow.py, and the code is:

import wx.py
from wx.py.interpreter import Interpreter
from wx.py import dispatcher
import wx.stc
import types
import locale

class ShellWindow(wx.py.shell.Shell):
    def __init__(self, parent, mainframe):
        wx.py.shell.Shell.__init__(self, parent, -1, InterpClass=NEInterpreter)
        self.parent = parent
        self.mainframe = mainframe

        #set style
        font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
        self.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT, “face:%s,size:10″ % font.GetFaceName())
       
    def canClose(self):
        return False

    def write(self, text):
        “”"Display text in the shell.
   
        Replace line endings with OS-specific endings.”"”
        if not isinstance(text, types.UnicodeType):
            text = unicode(text, locale.getdefaultlocale()[1])
        text = self.fixLineEndings(text)
        self.AddText(text)
        self.EnsureCaretVisible()
   

class NEInterpreter(Interpreter):
    def push(self, command):
        “”"Send command to the interpreter to be executed.
       
        Because this may be called recursively, we append a new list
        onto the commandBuffer list and then append commands into
        that.  If the passed in command is part of a multi-line
        command we keep appending the pieces to the last list in
        commandBuffer until we have a complete command. If not, we
        delete that last list.”"”
       
        if isinstance(command, types.UnicodeType):
            command = command.encode(locale.getdefaultlocale()[1])
        if not self.more:
            try: del self.commandBuffer[-1]
            except IndexError: pass
        if not self.more: self.commandBuffer.append([])
        self.commandBuffer[-1].append(command)
        source = ‘\n’.join(self.commandBuffer[-1])
        more = self.more = self.runsource(source)
        dispatcher.send(signal=’Interpreter.push’, sender=self,
                        command=command, more=more, source=source)
        return more

What I do are only two things, one is creating a new class NEInterpreter dirived from wx.py.interpreter.Interpreter, and overrides the push method. In the new push method, I add unicode process. If the command is encoded with unicode, I’ll convert it to default locale encoding.

The other thing is: modifying old ShellWindow class and passing InterpClass=NEInterpreter to construct function __init__. Then orverride the write method and add unicode support.

2004年09月27日

To get the newest source code from CVS is easy. You can follow the instructions at http://newedit.tigris.org/servlets/ProjectSource。And I list the steps here:

1. You should have a cvs tool. I suggest a command line tool, just like: cvs.exe. In window, you can download it from http://www.cvshome.org or if you have installed cygwin it’ll be shipped with cygwin.

2. First time:

You should login first:

cvs -d :pserver:anoncvs@cvs.tigris.org:/cvs login

Then checkout the module:

cvs -d :pserver:anoncvs@cvs.tigris.org:/cvs checkout newedit

This will create a directory NewEdit in your current directory. And the newest source code is just in there. This step may be executed only once, unless you change the directory.

3. Second time and later:

Change directory to NewEdit which your create early throught cvs checkout command. And only execute:

cvs update

command. That’s it.

 

得到最新的 NewEdit 源码很容易。你可以按照http://newedit.tigris.org/servlets/ProjectSource所说的去做。下面是我列出的步骤:

1. 你应该有一个cvs工具。我建议使用命令行工具,如:cvs.exe。在Windows下,你可以从http://www.cvshome.org上下载或如果你安装了cygwin,则其中已经包含它了。

2. 第一次:

首先要登录:

cvs -d :pserver:anoncvs@cvs.tigris.org:/cvs login

然后checkout出模块:

cvs -d :pserver:anoncvs@cvs.tigris.org:/cvs checkout newedit

这样将会在你的当前目录下创建一个newedit的目录。最新的源码就在里面。这一步应该只执行一次,除非你改变了目录。

3. 第二次和后面

进入以前使用checkout创建出来的newedit目录,然后只要执行:

cvs update

就可以了。

I’v been working at new blog function of NewEdit for a couple of days. And today I completed it approximately. I’ll try to improve PyBlosxom, so that I can test it after National Day.

The mainly changes are:

  • Simplify the layout of blog editor window. So there are no more buttons disturbing you.
  • Add pyblosxom plugin. So there are two blog plugins now, one is used to handle common things, and the other is used to handle the new functions what I coded by myself — xmlrpc_newedit.py. Therefore, if you want to use them both, you should set them all checked in Plugin Manage Menu.

I’v update the CVS, so if you are interestring in it, you can check them out throught CVS. 

2004年09月24日

Yesterday Perrin asked me if she can use NewEdit to make a replacement in regular expression, just like:

source: “(\d{1,3})”
destination: (\1=)

i.e. search every digit string which has 1 to  3 number, then append a equal sign. I said no to her, NewEdit is not so much strong to do that.

But when I came home, I thought this function is very good, so I change the replacement code, and made it reality. So the new version of NewEdit will have this feature.  

在我发这个post的时候,我首先得到:

操作失败。 错误信息为: 索引(从零开始)必须大于或等于零,且小于参数列表的大小。

我想可能是正则表达式对donews的blog系统有影响,因此就在正则表达式中加入了空格,尽管不再是正则表达了,但是想可以发出去,但又报这样的错:

操作失败。 错误信息为: 输入字符串的格式不正确。

最后的解决办法是:我先把正则表达式那两行删除,然后提交。成功,再编辑,再拷贝,又成功了。然后我写了这些东西。烦!!!

2004年09月23日

I used to like to go to comp.lang.python via web browser for a lot of months, because it’s an easy way, and if I don’t like to , I can just don’t go. But I can’t connect there for several months, and I don’t know why, even throught the google’s web site. And today, I tried to connect to it, and I found the message about the web forums of comp.lang.* have stopped archived. That’s the reason. And I had to go to Python.org for help. Finally I found I can still access it via subscribing the comp.lang.python maillist. It’s the better that have something than have nothing.

If you want subscribe the maillist, you can check out http://www.python.org/mailman/listinfo/python-list. Maybe you’v known about it. :)

At the past, I used the DetectUTF_8.py to detect the encoding of a file — DetectUTF_8.py is writted by myself, and if it returns True, I’ll convert the text of the file from utf-8 encoding to unicode. It works always well till Perrin(thanks toPerrin) report me a failure on openning a file because of the auto detecting utf-8 encoding. At first I strongly trusted my code, and doubted that it was happened due to her computer. And I asked her sent me the file, tried it in my computer, I can’t open the file either. Oh, it was my fault not the computer. So I think the arithmetic is not exactly accurate, and I changed the arithmetic today. I use two try-except clause,the first is used to convert the text from utf-8 encoding to unicode if auto detecting utf-8 encoding option is True, and if it’s failed, I’ll use the second clause to convert the text from default locale encoding to unicode. It works well till now. And the change can even improve the speed of openning a file, because of reducing the process to the text of the file. The code snippet is blow:

    if not encoding:
        if win.mainframe.pref.auto_detect_utf8:
            encoding = ‘utf-8′
        else:
            encoding = win.defaultlocale
    try:
        s = unicode(text, encoding)
        win.locale = encoding
    except:
        if win.mainframe.pref.auto_detect_utf8 and encoding == ‘utf-8′:
            encoding = win.defaultlocale
            try:
                s = unicode(text, encoding)
                win.locale = encoding
            except:
                error.traceback()
                raise MyUnicodeException(win, tr(“Cann’t convert file encoding [%s] to unicode!\nThe file cann’t be openned!”) % encoding,
                    tr(“Unicode Error”))
        else:
            error.traceback()
            raise MyUnicodeException(win, tr(“Cann’t convert file encoding [%s] to unicode!\nThe file cann’t be openned!”) % encoding,
                tr(“Unicode Error”))

2004年09月22日

This plugin is written by me. More details is in source code, and you can review it at LINK. And you can download it at there.

PyBlosxom has xmlrpc_metaweblog.py plugin, it can edit and new post, but it  doesn’t implement metaWeblog.newMediaObject method. And according to the document of metaWeblog.newMediaObject , it should use MIME to implement this interface. So I decide to implement my own xmlrpc method, and I call it xmlrpc_newedit.py. It’s a plugin of PyBlosxom. One thing I should resolve is how to transfer binary data via xmlrpclib.

I found it is very easy. The only thing you need to do is creating a xmlrpclib.Binary ojbect, then pass it to a xmlrpc method. That’s it. Is it easy?

For example, this is a test to my pyblosxom:

>>> import xmlrpclib
>>> server = xmlrpclib.ServerProxy(‘http://11.160.3.16/limodou/pyblosxom.cgi/RPC’)
>>> data = xmlrpclib.Binary(file(‘d:/a.jpg’, ‘rb’).read())
>>> server.newedit_putFile(‘a.jpg’, data)
True

The example above is a demonstration of how to use my new xmlrpc_newedit.py plugin to upload a image. Because image data is binary, so I create a Binary object, and just pass the image data to it. Then invoke the newedit_putFile method with the Binary object, that’s it.

I found two articles about xml-rpc programming. There are the links:

XML-RPC in Python

XML-RPC: It Works Both Ways

2004年09月21日

I want to write an new plugin about PyBlosxom base on xmlrpclib. And I’v completed the editCategory method. This method is used to editting the description of  an existing category or creating a new category if it’s not existed. It takes two parameters, one is the path of the category, and the other is the description of the category. The editCategory method will make the path if the path is not existed, and save the descrpition in a file. I tried the method in English first, it runs ok. But when I use Chinese characters, it complained unicode error. At first I thought the encoding parameter in xmlrpclib.ServerProxy is needed, but after I added it, the result is still error. So I thought again and again, finally I see it was happend at writing categories to the file. Because the string passed by xmlrpclib is unicode, so as I save it directly, the os will raise exception. And I converted the parameter path and description to utf-8 at the beginning of the method. As I run again, everything is ok. So I want to remind you don’t forget the Unicode is different.