Ruby编写的取得Pyton所有函数并导出到XML文档的程序

=begin

  遍历python lib的目录 'C:/Python24/Lib'

,对每一个文件和文件夹进行比对,然后建立其函数的xml的tree,并保存到C:/Python.xml中

=end

require 'win32ole'

$dom = WIN32OLE.new('Msxml2.DOMDocument.3.0')

$dom.async = false

$dom.loadXML('')

$r = /^def/s+(/w+?)/s*[(]/

def testfiles(dir,node)

       Dir.chdir(dir)

       dirs = Dir["*"]

       dirs.each do |d|

              filename = dir + "//" +d

              if FileTest.directory?(filename)

                     newdir = $dom.createElement('directory')

                     attr = $dom.createAttribute('DIR')

                     attr.value = d

                     dirNode = node.appendChild(newdir)

                     dirNode.setAttributeNode(attr)

                     testfiles(filename,newdir)

              elsif d =~ //.py|pyw/

                     newfile = $dom.createElement('pyfile')

                     attr = $dom.createAttribute('FILE')

                     attr.value = d

                     filenode = node.appendChild(newfile)

                     filenode.setAttributeNode(attr)                  

                     f = File.new(filename,'r')

                     pytxt = f.read()

                     pytxt.each_line {|t|

                            m = $r.match(t)

                            if m

                                   func = m[1]

                                   if func

                                          newfunc = $dom.createElement('function')

                                          funcNode = filenode.appendChild(newfunc)

                                          funcNode.text = func

                                   end

                            end

                     }                  

              else

              end

       end

end

 

root = $dom.documentElement()

 

path = 'C:/Python24/Lib'

testfiles(path,root)

$dom.save('c:/python.xml')

你可能感兴趣的:(xml,ruby,文档,encoding,path,python,挑战智慧的极限--鬼策挑战赛)