准备写一个将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. CSS 问题集锦

    [1]让DIV中的内容居中 1.文字垂直居中,关键代码:height:100px;line-height:100px(两个值要相等) <div style="margin:0 auto ...

  3. word2vec 实践

    关于word2vec,这方面无论中英文的参考资料相当的多,英文方面既可以看官方推荐的论文,也可以看gensim作者Radim Řehůřek博士写得一些文章.而中文方面,推荐 @licstar的< ...

  4. struts 2.3.14.1 包详解

    1.struts2-convention-plugin-2.3.14.1.jar: @ParentPackage(default-package) @Namespace("/") ...

  5. Mod4-PHP编码规范

    loading... 归纳总结了能找到的一些编码规范,形成自己所需要的编码规范. 参考网址:1.2 一.文件格式 缩进使用四个空格,不使用制表符.左花括号不另起一行. if (1 == $x) { $ ...

  6. Day Five (beta)

    站立式会议 站立式会议内容总结 331 今天:爬虫抓取豆瓣数据; 明天:数据展示在话题详情页,话题页修改 442 今天:了解聊天模块 遇到的问题:无 明天:编写聊天模块 439 今天:学习 遇到困难 ...

  7. iOS开发小技巧--UIButton的另一种布局方法(第一种在layoutSubViews方法中,这一种利用苹果提供的两个返回CGRect的方法)

  8. mysql解决无法远程客户端连接

    1. 改表法.可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 " ...

  9. SpringMVC与Struts2区别与比较总结

    1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr ...

  10. iOS蓝牙开发(一)蓝牙相关基础知识(转)

    转载自:http://www.cocoachina.com/ios/20150915/13454.html 原文作者:刘彦玮 蓝牙常见名称和缩写 MFI ======= make for ipad , ...