准备写一个将Glade/GtkBuilder等格式的UI文件转换成C++代码的python程序

首先完成的是将LIBGlade格式读取至内存中

#!/usr/bin/env python
# -*- coding: utf-8 -*- import os, sys, copy
from xml.etree.ElementTree import ElementTree '''
result format:
[
{
"class" : "GtkWindow"
"id" : "window1"
"property" : [
{ "name" : "can_focus"
"value": "False"
}
{
}
]
"child" : [
{
"object":{
"class" : "GtkWindow"
"id" : "window1"
"property": [
{ "name" : "can_focus"
"value": "False"
}
{
}
]
"child" : [
...
]
}
"packing" :
{
"property": [
{ "name" : "expand"
"value": "True"
}
{
}
]
}
}
{
}
]
}
{
}
] child_node_name = "child"
object_node_name = "widget"
packing_node_name = "packing" ''' def load_packing(parent_node):
packing = {}
propertys = []
tmp_property = {} # property list
for property in parent_node.iterfind("property"):
# name
tmp_property["name"] = property.get("name")
# value
tmp_property["value"] = property.text
# append to list
propertys.append(copy.copy(tmp_property))
# assign value
packing["property"] = propertys return packing def load_object(parent_node):
result = {}
# find widget
widget = parent_node.find("widget")
if widget != None:
result["object"] = load_full_object(widget)
# find packing
packing = parent_node.find("packing")
if packing != None:
result["packing"] = load_packing(packing) return result def load_full_object(self_node):
result = {}
propertys = []
childs = []
tmp_property = {}
tmp_child = {}
# class
result["class"] = self_node.get("class")
# id
result["id"] = self_node.get("id") # property list
for property in self_node.iterfind("property"):
# name
tmp_property["name"] = property.get("name")
# value
tmp_property["value"] = property.text
# other attribute
if property.attrib.has_key("translatable"):
tmp_property["translatable"] = property.get("translatable")
if property.attrib.has_key("context"):
tmp_property["context"] = property.get("context")
if property.attrib.has_key("agent"):
tmp_property["agent"] = property.get("agent")
if property.attrib.has_key("comments"):
tmp_property["comments"] = property.get("comments")
# append to list
propertys.append(copy.copy(tmp_property))
# assign value
result["property"] = propertys
# childs
for child in self_node.iterfind("child"):
# recurse to load object
tmp_child = load_object(child)
# append it to list
childs.append(copy.copy(tmp_child))
# assign value
result["child"] = childs return result def load_xml(xml_file):
result_lst = []
# get root
xml_root = ElementTree(file=xml_file).getroot()
if(xml_root == None):
return -1, result_lst
# check is libglade type?
if (xml_root.tag != "glade-interface"):
return -1, result_lst
for object in xml_root.iterfind("widget"):
#print object
result_lst.append(load_full_object(object)) return 0, result_lst if __name__=="__main__":
argNum=len(sys.argv) # check param num
if argNum <> 2 :
print "parameter number(%d) is not match" % argNum
sys.exit(0) xml_file = sys.argv[1] ret, result = load_xml(xml_file)
print result

【原创】一段简短的读取libglade的UI文件的Python代码的更多相关文章

  1. JS读取client端的文件的代码片段

    JS读取client端的文件内容的代码片段 if(window.FileReader){ var html5file = this.pipeDetailFileUpLoader._files[0]; ...

  2. Java读取Level-1行情dbf文件极致优化(3)

    最近架构一个项目,实现行情的接入和分发,需要达到极致的低时延特性,这对于证券系统是非常重要的.接入的行情源是可以配置,既可以是Level-1,也可以是Level-2或其他第三方的源.虽然Level-1 ...

  3. Java读取Level-1行情dbf文件极致优化(2)

    最近架构一个项目,实现行情的接入和分发,需要达到极致的低时延特性,这对于证券系统是非常重要的.接入的行情源是可以配置,既可以是Level-1,也可以是Level-2或其他第三方的源.虽然Level-1 ...

  4. Java中使用POI读取大的Excel文件或者输入流时发生out of memory异常参考解决方案

    注意:此参考解决方案只是针对xlsx格式的excel文件! 背景 前一段时间遇到一种情况,服务器经常宕机,而且没有规律性,查看GC日志发生了out of memory,是堆溢出导致的,分析了一下堆的d ...

  5. php 读取功能分割大文件实例详解

    在php中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents之类的函数.但当所操作的文件是一个比较大的文件时,这些函数可能就显的力不从心, 下面将从一个需求 ...

  6. Assets 读取assets中的文件

    res/raw和assets的相同点: 1.两者目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制. res/raw和assets的不同点:1.res/raw中的文件会被映射到R.j ...

  7. 读取ClassPath下resource文件的正确姿势

    1.前言 为什么要写这篇文章?身为Java程序员你有没有过每次需要读取 ClassPath 下的资源文件的时候,都要去百度一下,然后看到下面的这种答案: Thread.currentThread(). ...

  8. LcdTools如何实现PX01读取SD中BIN文件并通过端口发出去

    在实际应用中我们会碰到需要下载很大容量固件,比如TP固件几百K大小BIN文件,这种情况下用LcdTools写初始化代码的方式实现就不大现实:此时我们可以通过PX01 SD来实现. 首先,把需要操作的B ...

  9. Android想服务器传图片,透过流的方式。还有读取服务器图片(文件),也通过流的方式。

    /** * Created by Administrator on 2016/7/19. */ import android.util.Log; import com.gtercn.asPolice. ...

随机推荐

  1. java中的static详解

    如果一个类成员被声明为static,它就能够在类的任何对象创建之前被访问,而不必引用任何对象.static 成员的最常见的例子是main( ) .因为在程序开始执行时必须调用main() ,所以它被声 ...

  2. android studio用法笔记

    1.每次创建新工程的时候,就会“check sdk repository”,然而这个进度条一动不动,解决方法:file>setting>plugins>sdk updater禁用就行 ...

  3. 常用数据库高可用和分区解决方案(2) — MongoDB篇

    MongoDB是当前比较流行的文档型数据库,其拥有易使用.易扩展.功能丰富.性能卓越等特性.MongoDB本身就拥有高可用及分区的解决方案,分别为副本集(Replica Set)和分片(shardin ...

  4. JS日历制作获取时间

    1.直接获取 var myDate = new Date(); myDate.getYear(); 获取当前年份(2位) myDate.getFullYear(); 获取完整的年份(4位,1970-? ...

  5. webService访问加密-Soapheader

    WebService head加密,可以对 WebService设置访问用户名和密码,增强 WebService的安全性 使 WebService只能被授权用户使用. 具体实现步骤: 1. 定义一个  ...

  6. 【BZOJ 2648】SJY摆棋子 & 【BZOJ 2716】【Violet 3】天使玩偶

    KDTree模板,双倍经验啦啦啦- #include<cstdio> #include<cstring> #include<algorithm> #define r ...

  7. 强联通 HDU 2767 3836

    n个点m条边 最少需要几条边变成强连通图 设有a个结点的入读为0, b个结点的出度为0, 则 max{a, b}就是答案. 注意特殊情况: 当原图已经强连通时, 答案是0而不是1. 加一条边少一个入度 ...

  8. Jquery每行最后一个LI边距设为0

    下面用的是jquery方法,请加载jquery插件 <script type="text/javascript"> $(function(){ $('#newhouse ...

  9. Oracle查询所有序列

    --查看当前用户的所有序列 select SEQUENCE_OWNER,SEQUENCE_NAME from dba_sequences where sequence_owner='用户名'; --查 ...

  10. js-form进行reset重置

    (需要注意:做这个reset和submit时页面不能有这两个id和名称) <form id="form1" name="formName" action= ...