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('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><root></root>')

$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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值