Skip to content

Instantly share code, notes, and snippets.

@weirongxu
Last active December 25, 2015 10:38
Show Gist options
  • Save weirongxu/6962728 to your computer and use it in GitHub Desktop.
Save weirongxu/6962728 to your computer and use it in GitHub Desktop.
在ubuntu下生成precise.xml桌面图片切换(Desktop Slideshow)
#!/usr/bin/python
# coding: utf-8
from xml.dom.minidom import Document
import os, glob, random
DOC = Document() #创建DOM文档对象
def createTime(fnode):
'''生成starttime节点'''
starttime = DOC.createElement('starttime')
addDictNode(starttime, {
'year': '2009',
'month': '08',
'day': '04',
'hour': '00',
'minute': '00',
'second': '00'
})
fnode.appendChild(starttime)
def createImgSwap(fnode, paths):
'''创建图片切换节点'''
plen = len(paths)
for i in range(plen):
if i == plen - 1: inext = 0
else: inext = i + 1
static = DOC.createElement('static')
addDictNode(static, {
'duration': '1795.0',
'file': paths[i]
})
fnode.appendChild(static)
transition = DOC.createElement('transition')
addDictNode(transition, {
'duration': '5.0',
'from': paths[i],
'to': paths[inext]
})
fnode.appendChild(transition)
def addDictNode(fnode, tagDicts):
'''通过字典添加多个文本节点'''
for tagKey in tagDicts.keys():
tNode = DOC.createElement(tagKey)
tText = DOC.createTextNode(tagDicts[tagKey])
tNode.appendChild(tText)
fnode.appendChild(tNode)
return fnode
def createPrecise(*filenames):
'''创建precise的xml文件'''
background = DOC.createElement('background')
createTime(background)
cwd = os.getcwd()
paths = []
for filename in filenames:
paths.extend(glob.glob(filename))
random.shuffle(paths)
for i in range(len(paths)):
paths[i] = cwd + '/' + paths[i]
createImgSwap(background, paths)
DOC.appendChild(background)
'''
生成ubuntu所用的precise.xml文件.
利用当前目录下的图片文件
'''
if __name__ == '__main__':
# 创建并生成precise.xml文件
createPrecise('*.jpg', '*.png', '*.gif')
f = open('precise.xml','w')
f.write(DOC.toprettyxml(indent = ''))
f.close()
print('输入密码,替换系统原有的precise.xml')
os.system('sudo mv -f precise.xml /usr/share/backgrounds/contest/precise.xml')
raw_input('结束')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment