您的位置:首页 > 编程语言 > Ruby

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

2006-03-15 23:28 393 查看
=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')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐