您的位置:首页 > 产品设计 > UI/UE

[原创]从Confluence获取html table并将其序列化为C#类文件的工具

2014-07-28 19:25 323 查看
公司项目的游戏数据模型文档写在Confluence上,由于在项目初期模型变动比较频繁,手工去将文档中最新的模型结构同步到代码中比较费时费力,而且还很容易出错,于是写了一个小工具来自动化这个同步更新模型到代码中的工作。

如下是一个野怪的数据模型文档:



最终在Unity的C#代码中它会是这个形式:

using UnityEngine;
using System.Collections;

public class MonsterData
{
public    int         monsterId;                        //人物id
public    string      name;                             //怪物名称
public    int         hp;                               //怪物最大生命值
public    int         dodgeRate;                        //躲闪几率
public    int         hitRate;                          //命中几率
public    int         critHitRate;                      //暴击几率
public    string      description;                      //描述
public    int         exp;                              //经验
public    int         atk;                              //攻击
public    int         def;                              //防御
public    string      imageName;                        //怪物图片的名字
public    int         coins;                            //击杀怪物获得的金币数
public    int         isBoss;                           //是否是Boss
}


我们需要的就是将图1中得结构自动写成包含以上代码内容的C#文件。

下面说说具体的思路

首先,出于从实现难度和跨平台(前端开发用Mac后端开发用Windows)的考虑,我选择用Python来实现这个工具;

然后,分析这个问题可以知道,这个问题可以分解成2个小的问题来解决:

1.从Conflunce上获取对应文档页面的html table;

2.本地解析这个html table后,将其按照C#类的格式写入到文件中。

对于第一个问题

Confluence自带了Remote API,给开发者提供了非常方便的接口来访问或者更改指定页面的数据,点击这里可以查看它的官方文档。不过这个Remote API默认是关闭的,需要手工在设置里面开启,具体开启的步骤可以看这里,每个版本可能设置的布局略有不同,但是设置的东西都是一样的。

在python中可以使用xmlrpclib来调用Confluence的API。具体的实例可以看这两个链接:Updating a Confluence Wiki with a scriptmodify wiki page confluence programmatically.这样,第一个问题就解决了。

这里插一句,我在开始的时候不知道有这个Remote API,所以尝试过不用他来实现获取Confluence的页面内容,能够访问到对应的页面,可是一直拿不到想要的html内容,不知道是什么问题,后来因为知道了Remote API,也没再继续尝试自己去获取了,有时间再研究以下。

对于第二个问题

解析html可以用python的库beautifulsoup来实现。关于beautifulsoup你可以看这里。这个beautifulsoup不仅名字好看,功能也异常强大。

好了,解决问题的思路确定了,下面我们可以动手来实现这个工具了。以下便是我实现的代码,python写的不多,所以代码可能比较丑,见谅:)

#!/usr/bin/python

from bs4 import BeautifulSoup

#for change encoding
import sys

#for login in confluence
import xmlrpclib

import os

def mkdir(path):

path = path.strip()

path = path.rstrip("\\")

isExists = os.path.exists(path)

if not isExists:
print path + ' create successfully!'
os.makedirs(path)
return True
else:
print path + ' exists!'
return False

def makeTableContentList(table):
result = []
allrows = table.findAll('tr')
rowIndex = 0
for row in allrows:
result.append([])
#exclude the strike one
if row.findAll('s'):
continue

allcols = row.findAll('td')
#print "rowIndex = ",rowIndex
#print "allcols = ",allcols

for col in allcols:
#print "col",col
thestrings = [unicode(s) for s in col.findAll(text=True)]
thetext = ''.join(thestrings)

result[-1].append(thetext)
rowIndex += 1
return result

def makeFile(tableContentList):

className = tableContentList[0][0]

outputFile = file("output/" + className + ".cs","w")

#start to write file

#write header
outputFile.write("using UnityEngine;\n")
outputFile.write("using System.Collections;\n\n")
outputFile.write("public class " + className + "\n{\n")

#write members
rowCounter = 0
for row in tableContentList:
if row and rowCounter > 0:  #rowCounter == 0 is className

#--------format---------
beginSpaces = "    public    "
typeString = "{:<12}".format(row[0])
memberName = "{:<30}".format(row[1] + ";")
comments = ""

if len(row[2]) > 1:
comments = "    //" + row[2]

s = beginSpaces + typeString + memberName + comments + "\n"

outputFile.write(s)

rowCounter += 1

#write tail
outputFile.write("}\n")

outputFile.close()

def setDefaultEncodingUTF8():
reload(sys)
sys.setdefaultencoding('utf-8')

def loadConfluencePage(pageID):

# login Confluence
CONFLUENCE_URL = "http://192.168.1.119:8090/rpc/xmlrpc"
CONFLUENCE_USER_NAME = "userName"   # use your Confluence user Name
CONFLUENCE_PASSWORD = "password"    # use your Confluence password

# get this from the page url while editing
# e.g. ../editpage.action?pageId=132350005 <-- here
#PAGE_ID = "4686604"

client = xmlrpclib.Server(CONFLUENCE_URL, verbose = 0)
auth_token = client.confluence2.login(CONFLUENCE_USER_NAME, CONFLUENCE_PASSWORD)
page = client.confluence2.getPage(auth_token, pageID)

htmlContent = page['content']

client.confluence2.logout(auth_token)

return htmlContent

def main():

#change Encoding to UTF8
setDefaultEncodingUTF8()

#make output directory
mkdir(sys.path[0] + "/output")

#there are two pages contain data model
pageIDs = ("4686602","4686604")

for pageID in pageIDs:

print "Make data in page with id: ",pageID

htmlContent = loadConfluencePage(pageID)

soup = BeautifulSoup(htmlContent)
#print soup.prettify()

tables = soup.findAll('table')

for table in tables:
#print table
result = makeTableContentList(table)
makeFile(result)
#print "result = "
#print result

print "Make Over! Have a nice day!"

if __name__ == "__main__":
main()


OK,就到这里,希望大家喜欢:)

转载请注明出处,谢谢:)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: