准备写一个将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. jni的使用方法

    我们可以通过jni来调用c/c++程序,C/C++一般会被作为动态库的形式来供java程序调用 环境 操作系统:Centos6.7 java: openjdk7 依赖包:java7-devel(yum ...

  2. Android开发之SlidingMenu开源项目的使用和问题

    一.关于如何导入lib 第一步:New Module  点击+: 第二步:选择Import Eclipse ADT Project: 第三步:选择你想引入的lib文件,选择完成后,会开始编译你添加的项 ...

  3. 【Zeyphr】保存json到数据库

    方法一: public int SaveJob(JObject data) { var formWrapper = RequestWrapper.Instance().LoadSettingXmlSt ...

  4. JS 获取上一层目录

    派生到我的代码片 <script type="text/javascript"> //返回当前工作目录 function GetCurrDir(){ var pathN ...

  5. [cross domain] four approachs to cross domain in javascript

    four approachs can cross domain in javascript 1.jsonp 2.document.domain(only in frame and they have ...

  6. [转]浅谈jQuery EasyUI的属性设置

    原文地址:http://www.easyui.info/archives/1664.html 对jQuery EasyUI有一定了解的话,应该知道基本上每一个组件都有一个"options&q ...

  7. 【NOIP 2004】虫食算

    因为一天机房都是断网状态,校内的小V评测这道题总显示Unaccept,所以下午放学后就和xiaoyimi晚上回家自习,来做一做这道题. 搜索+剪枝优化: 一开始我是顺着低位向高位填数,这么暴力在Vij ...

  8. 51nod 1101换零钱(背包)

    N元钱换为零钱,有多少不同的换法?币值包括1 2 5分,1 2 5角,1 2 5 10 20 50 100元.   例如:5分钱换为零钱,有以下4种换法: 1.5个1分 2.1个2分3个1分 3.2个 ...

  9. 100803C

    画个图,很容易发现少兜圈子,就是说这些限制c[i],d[i]可以看做[c[i],d[i]],不让那些区间相交,然后就可以了 #include<iostream> #include<c ...

  10. bzoj4429: [Nwerc2015] Elementary Math小学数学

    先把所有可能的答案算出来,每个算式一个点,每个结果一个点,然后如果一个算式能算出一个结果,那么就连一条边 然后跑匈牙利,没有完美匹配就是impossible 每个算式最多有3个结果,所以边数是O(n) ...