【原创】一段简短的读取libglade的UI文件的Python代码
准备写一个将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代码的更多相关文章
- JS读取client端的文件的代码片段
JS读取client端的文件内容的代码片段 if(window.FileReader){ var html5file = this.pipeDetailFileUpLoader._files[0]; ...
- Java读取Level-1行情dbf文件极致优化(3)
最近架构一个项目,实现行情的接入和分发,需要达到极致的低时延特性,这对于证券系统是非常重要的.接入的行情源是可以配置,既可以是Level-1,也可以是Level-2或其他第三方的源.虽然Level-1 ...
- Java读取Level-1行情dbf文件极致优化(2)
最近架构一个项目,实现行情的接入和分发,需要达到极致的低时延特性,这对于证券系统是非常重要的.接入的行情源是可以配置,既可以是Level-1,也可以是Level-2或其他第三方的源.虽然Level-1 ...
- Java中使用POI读取大的Excel文件或者输入流时发生out of memory异常参考解决方案
注意:此参考解决方案只是针对xlsx格式的excel文件! 背景 前一段时间遇到一种情况,服务器经常宕机,而且没有规律性,查看GC日志发生了out of memory,是堆溢出导致的,分析了一下堆的d ...
- php 读取功能分割大文件实例详解
在php中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents之类的函数.但当所操作的文件是一个比较大的文件时,这些函数可能就显的力不从心, 下面将从一个需求 ...
- Assets 读取assets中的文件
res/raw和assets的相同点: 1.两者目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制. res/raw和assets的不同点:1.res/raw中的文件会被映射到R.j ...
- 读取ClassPath下resource文件的正确姿势
1.前言 为什么要写这篇文章?身为Java程序员你有没有过每次需要读取 ClassPath 下的资源文件的时候,都要去百度一下,然后看到下面的这种答案: Thread.currentThread(). ...
- LcdTools如何实现PX01读取SD中BIN文件并通过端口发出去
在实际应用中我们会碰到需要下载很大容量固件,比如TP固件几百K大小BIN文件,这种情况下用LcdTools写初始化代码的方式实现就不大现实:此时我们可以通过PX01 SD来实现. 首先,把需要操作的B ...
- Android想服务器传图片,透过流的方式。还有读取服务器图片(文件),也通过流的方式。
/** * Created by Administrator on 2016/7/19. */ import android.util.Log; import com.gtercn.asPolice. ...
随机推荐
- linux svn搭建
1 安装: yum install subversion 2 查看svn安装信息: rpm -ql subversion 3 创建svn根目录: svnserve -d -r /svn 4 进入/sv ...
- UTF-8 's format
几篇比较好的博客 古腾龙的博客:编码规则(UTF-8 GBK) GBK 千千秀字 shell set man ascii可以查看ascii码表,man utf-8看以查看utf-8的帮助 Unicod ...
- golang: 把sql结果集以json格式输出
func getJSON(sqlString string) (string, error) { stmt, err := db.Prepare(sqlString) if err != nil { ...
- 跨浏览器图像灰度(grayscale)解决方案
<style type="text/css"> .gray { -webkit-filter: grayscale(100%); /* CSS3 filter方式,we ...
- poj3294 出现次数大于n/2 的公共子串
Life Forms Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 13063 Accepted: 3670 Descr ...
- 全面理解Context
出处:http://blog.csdn.net/lmj623565791/article/details/40481055,本文出自:[张鸿洋的博客] 本文大多数内容翻译自:http://www.do ...
- bzoj3743: [Coci2015]Kamp
首先树dp求出一个点的答案 然后再一遍dfs换根(是叫做换根吗.. 详见代码 #include <iostream> #include <cstdio> #include &l ...
- smtplib.SMTPDataError: (554, 'DT:SPM 126 smtp5错误解决办法
1.自动化测试中,调用邮件模块自动发送邮件时,运行脚本报错: smtplib.SMTPDataError: (554, 'DT:SPM 126 smtp5,jtKowAD3MJz2c1JXLcK2AA ...
- 【poj3241】 Object Clustering
http://poj.org/problem?id=3241 (题目链接) MD被坑了,看到博客里面说莫队要写曼哈顿最小生成树,我就写了一个下午..结果根本没什么关系.不过还是把博客写了吧. 转自:h ...
- 深入了解Mvc路由系统
请求一个MVC页面的处理过程 1.浏览器发送一个Home/Index 的链接请求到iis.iis发现时一个asp.net处理程序.则调用asp.net_isapi 扩展程序发送asp.net框架 2. ...