Logo Search packages:      
Sourcecode: blender version File versions  Download package

error_po.py

00001 '''
in Blender, error("some string") yields "ERROR: "+"some string"

To custom your script, roll down till "if __name__=='__main__'"

If anybody find a bug, or knows how to use re module to search and strip out  the strings, please let me know. Thanx.

Copyright: iced oyster< http://blender.blogchina.com > 2005
You can use this program freely, but CANNOT claim that you is the author.
'''
import os
import glob
import re
import sys

debug_po=1

func_dict={
'error':[0],
}


00023 def split_blender_str(s):
      '''
      按Blender代码方式分割字符串s
      如果|在s里面,在|处分割成若干子字符串,然后按“|不在s里面”里面的情况处理

      如果|不在s里面,直接返回[s]

      如果s开头有若干 引号同时引号前面有空格(0个或多个),去掉

      还不明白多个%放在一起的处理方式
      '''
      idx_id=s
      temp=[]

      if '|' not in idx_id:         #>>如何处理?去不去掉空格?
            if (idx_id.endswith(':')==0 and len(idx_id.strip())>1) \
                  or (idx_id.endswith(':') and len(idx_id[:-1].strip())>1):
                  #不写长度只有1的字符,它常常是快捷键
                  temp.append(s)
      else:
            idx_id=idx_id.split('|')
            idx_id=filter(lambda e:e not in ('','""'),idx_id)
            #不写长度只有1的字符,它常常是快捷键
            idx_id=filter(lambda e:(e.endswith(':')==0 and len(e.strip())>1) or \
                                                (e.endswith(':') and len(e[:-1].strip())>1),idx_id)
            for i in idx_id:
                  '''
                  有这样的传递字串的方法,为什么这么多的引号?
                  from source\blender\src\buttons_editing.c
                  but=uiDefButS(block, MENU, REDRAWVIEW3D,
                        "Skinnable %x0|" "Unskinnable %x1|" "Head %x2|"
                        "Neck %x3|" "Back %x4|" "Shoulder %x5|" "Arm %x6|"
                        "Hand %x7|" "Finger %x8|" "Thumb %x9|" "Pelvis %x10|"
                        "Leg %x11|" "Foot %x12|" "Toe %x13|" "Tentacle %x14",
                        bx-10,by-19,117,18, &curBone->boneclass, 0.0, 0.0, 0.0, 0.0,
                        "Classification of armature element");
                  '''

                  temp.append(i)

      for kbd in [ 'ctrl ', 'alt ',             #'shift '比较特殊,它可以是一个动词,所以此处不考虑,\
                        'shift+', 'ctrl+', 'alt+', \
                        'numpad',               #排除'emualte numpad'\
                        'leftarrow','rightarrow','uparrow','downarrow',\
                        'left arrow','right arrow','up arrow','down arrow',\
                        'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10', 'f11', 'f12',\
                        'pageup', 'page up', 'pgup', 'pg up',
                        'pagedown','page down', 'pgdn','pg dn',
                        ]:
            temp=filter(lambda e:e.lower().startswith(kbd)==0,temp)

      shift_except=map(lambda e:'f%0i'%e, range(1,13))
      shift_except+=['ctrl ', 'alt ',
                              'numpad',
                              'leftarrow','rightarrow','uparrow','downarrow',\
                              'left arrow','right arrow','up arrow','down arrow',\
                              'pageup', 'page up', 'pgup', 'pg up',
                              'pagedown','page down', 'pgdn','pg dn',
                        ]
      shift_except=map(lambda e:'shift '+e,shift_except)
      for kbd in shift_except:
            temp=filter(lambda e:e.lower().startswith(kbd)==0,temp)
      temp=filter(lambda e:e.lower() not in ['tab','shift tab'],temp)
      temp=filter(lambda e:e.lower().startswith('shift')==0 or\
                        (e.lower().startswith('shift') and len(e)>7) ,temp)

      '''
      末尾,%t,表示是菜单题头;%x0这样的数字,表示是菜单的选项
      都需要去掉,但是不要去掉%前面的空格
      '''
      if temp:
            temp=map(lambda e:e.split('%t')[0],temp)

      #见鬼,blender源程序为什么不按顺序使用菜单,居然用到100多
      #msgid "Add Sequence Strip%t|Images%x1|Movie%x102|Audio%x103|Scene%x101|Plugin%x10|Cross%x2|Gamma Cross%x3|Add%x4|Sub%x5|Mul%x6|Alpha Over%x7|Alpha Under%x8|Alpha Over Drop%x9|Wipe%x13|Glow%x14"
      ##注意这里的|Medium %x2 |,多了一个空格
      ##"GiQuality %t|None %x0|Low %x1|Medium %x2 |High %x3|Higher %x4|Best %x5|Use Blender AO settings %x6"
      if temp:
            temp=map(lambda e:e.split('%x')[0],temp)

      temp=filter(lambda e:e!='',temp)
      return temp

func_dict_key=func_dict.keys()

def genpo(arg, dirname,fname):
      files= [os.path.normpath(os.path.join(dirname, file)) for file in fname]
      all_msgid=[]
      for file in files:
            current_msgid=[]

            if os.path.isfile(file):
                  if os.path.splitext(file)[-1].upper() in ['.C', 'CPP']:     #.H, .HPP?

                        f_in=open(file)
                        line=f_in.readline()

                        while line:
                              if line!=None:
                                    line=line.strip()

                                    line_temp=' '

                                    while line.endswith(';')!=1 and line_temp:
                                          #不是一行,或者是if结构
                                          line_temp=f_in.readline()
                                          if line_temp:
                                                line_temp=line_temp.strip()
                                                if line_temp.endswith('\\'):  #用\换行
                                                      line_temp=line_temp[:-1]
                                                line+=line_temp

                                    #很笨拙、不精确的方法
                                    #首先替换所有""参数
                                    line=line.replace(',""',',NIL_NULL')
                                    line=line.replace(', ""',',NIL_NULL')
                                    line=line.replace(',\t""',',NIL_NULL')
                                    line=line.replace('"",','NIL_NULL,')
                                    line=line.replace('"" ,',',NIL_NULL ,')
                                    line=line.replace('""\t,',',NIL_NULL\t,')
                                    #替换所有" "参数
                                    line=line.replace('," "',',NIL_NULL')
                                    line=line.replace(', " "',',NIL_NULL')
                                    line=line.replace(',\t" "',',NIL_NULL')
                                    line=line.replace('" ",','NIL_NULL,')
                                    line=line.replace('" " ,',',NIL_NULL ,')
                                    line=line.replace('" "\t,',',NIL_NULL\t,')
                                    #替换所有"\t"参数
                                    line=line.replace(',"\t"',',NIL_NULL')
                                    line=line.replace(', "\t"',',NIL_NULL')
                                    line=line.replace(',\t"\t"',',NIL_NULL')
                                    line=line.replace('"\t",','NIL_NULL,')
                                    line=line.replace('"\t" ,',',NIL_NULL ,')
                                    line=line.replace('"\t"\t,',',NIL_NULL\t,')

                                    #Blender源程序中,:
                                    #使用了两个引号连接字符串
                                    line=line.replace('""','')
                                    line=line.replace('" "','')
                                    line=line.replace('"\t"','')

                                    for funcname in func_dict_key:
                                          line_temp=line
                                          # 下面判断程序行是不是我要找的“函数名(”
                                          # 由于有uiDefIconTextButF、uiDefIconTextBut这样开头一样的函数存在
                                          # 所以这里不能用startswith(
                                          #
                                          found=re.search('%s[ \t]*\('%funcname,line_temp)
                                          if found!=None:

                                                line_temp=line_temp[found.start()+len(found.group()):-1]    #-1去掉右边的分号,只留下右括号及里面的参数
                                                line_temp=line_temp.strip()
                                                line_temp=line_temp.replace(r'\"',r"\'")
                                                line_temp=re.split('([^,"]+|"[^"]*")',line_temp)
                                                line_temp=map(lambda e:e.replace(r"\'",r'\"'), line_temp)

                                                line_temp=map(lambda e:e.strip(),line_temp)
                                                line_temp=filter(lambda e:e not in ['',',','"','\\n'],line_temp)


                                                for idx in func_dict[funcname]:
                                                      try:  #因为可能用变量,而不是直接字符串调用函数,所以line_temp可能没有足够长
                                                            idx_id=line_temp[idx]
                                                            if idx_id.startswith('"') and idx_id.endswith('"')  and idx_id!='""':
                                                                  idx_id=idx_id[1:-1]           #去掉左右的引号
                                                                  idx_id_split=split_blender_str(idx_id)

                                                                  #只有最终显示出来的字符串才可以汉化
                                                                  #如果有格式控制符,只是用于中间计算
                                                                  #这里忽略了%%的特殊情况,但是似乎Blender里面没有需要汉化的%%
                                                                  idx_id_split=filter(lambda e:'%' not in e,idx_id_split)

                                                                  if len(idx_id_split)==1:
                                                                        if debug_po:
                                                                              current_msgid+=['msgid "ERROR: '+idx_id_split[0]+'"\n'+'msgstr "错误:'+idx_id_split[0]+'"\n']
                                                                              #all_msgid_dict[idx_id_split[0]]='blahblah'
                                                                        else:
                                                                              current_msgid+=['msgid "ERROR: '+idx_id_split[0]+'"\n'+'msgstr ""\n']

                                                                  elif len(idx_id_split)>1:
                                                                        current_msgid+=['#'*59+'\n'+
                                                                                          '###  must break down the following line at "|"\n'+
                                                                                          '###  msgid "'+idx_id+'"\n'+
                                                                                          '###  msgstr ""\n'
                                                                                          ]
                                                                        for i in idx_id_split:
                                                                              if debug_po:
                                                                                    current_msgid+=['msgid "'+i+'"\n'+'msgstr "错误:'+idx_id_split[0]+'"\n']
                                                                                    #all_msgid_dict[i]='blahblah'
                                                                              else:
                                                                                    current_msgid+=['msgid "'+i+'"\n'+'msgstr ""\n']
                                                                        current_msgid+=['###  breakdown ends\n'+
                                                                                          '#'*59+'\n'
                                                                                          ]
                                                      except:
                                                            pass

                                    line=f_in.readline()

                        if  current_msgid:
                              current_msgid=['#'*59+'\n'+'###  from file %s\n' % file+'#'*59+'\n']+current_msgid

                        if  current_msgid:
                              all_msgid+=current_msgid

      #return all_msgid

      for i in all_msgid:
            print i

if __name__=='__main__':
      #path to your Blender src
      blender_src_path=r'L:\blender src\source'

      #output file
      fout=open('error.po','w')
      sys.stdout=fout

      os.path.walk(blender_src_path,genpo,None)
      #genpo(1,2,3)

      fout.close()
      sys.stdout=sys.__stdout__


Generated by  Doxygen 1.6.0   Back to index