准备写一个将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. 10-xargs 简明笔记

    从标准你输入获取内容创建和执行命令 xargs     [options] 选项 -n                                               数字,分组 示例 x ...

  2. 开发错误记录3:问题 Error:failed to find Build Tools revision 23.0.2

    今天导入swiperefreshlayoutdemo 问题 Error:failed to find Build Tools revision 23.0.2 修改build.gradle 里面设置,其 ...

  3. 【BZOJ 1038】【ZJOI 2008】瞭望塔

    http://www.lydsy.com/JudgeOnline/problem.php?id=1038 半平面交裸题,求完半平面后在折线段上的每个点竖直向上和半平面上的每个点竖直向下求距离,统计最小 ...

  4. Kernel Methods (3) Kernel Linear Regression

    Linear Regression 线性回归应该算得上是最简单的一种机器学习算法了吧. 它的问题定义为: 给定训练数据集\(D\), 由\(m\)个二元组\(x_i, y_i\)组成, 其中: \(x ...

  5. hdu2665 && poj2104划分树

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 47066   Accepted: 15743 Ca ...

  6. 浏览器XMLHttpRequest案例

    /* Cross-Browser XMLHttpRequest v1.2 ================================= Emulate Gecko 'XMLHttpRequest ...

  7. 7款应用最广泛的Linux桌面环境盘点

    转载:http://top.jobbole.com/34823/ 多样性应该是 Linux 最好的特性之一,用户可以不断尝试各种喜欢和新鲜玩法与花样,并从中找出最适合自己的应用.无论你是 Linux ...

  8. 小记-虚拟机装ubuntu遇到问题

    今天用虚拟机virtualbox装ubuntu遇到了一个问题,如图 解决方法: 1.首先查看创建虚拟机的时候是基本设置有没有对:(如图) 主要去看所选择的版本对应是否正确,比如32位和64位之类的. ...

  9. eclipse安装插件的方法(简单、ERMaster插件安装)

    ERMaster插件:https://sourceforge.net/projects/ermaster/ 链接:http://pan.baidu.com/s/1o7UWLMa 密码:wkax 可以编 ...

  10. http80端口转发(实现微信公众号接口绑定IP时,同时支持多个公众号)

    http80端口转发 背景 微信公众平台接口绑定服务器时,如果使用IP需要使用80端口,此组件可实现一个IP上绑定多个公众平台接口 使用方法 http://(IP)/WeixinMP/(转发的地址Ba ...