本文整理汇总了Python中etree.ElementTree.fromstring方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.fromstring方法的具体用法?Python ElementTree.fromstring怎么用?Python ElementTree.fromstring使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类etree.ElementTree
的用法示例。
在下文中一共展示了ElementTree.fromstring方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: EditChannel
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import fromstring [as 别名]
def EditChannel(request, device_id=None, channel_name=None):
if(device_id in device_list):
xml_request = request.body
root = ET.fromstring(xml_request)
sensor = root.find('sensor')
ch_value = sensor.get('value')
ch_unit = sensor.get('unit')
instance_name = device_list[device_id].get('name')
data = self.driver_channel_get(instance_name, channel_name)
value = data.value
unit = data.unit
if(ch_value == None):
ch_value = value
if(ch_unit == None):
ch_unit = unit
self.driver_channel_set(instance_name, channel_name, ch_value, ch_unit)
xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n'
xml_response += '<response status="202">\n'
xml_response += '<device id="%s" message="Channel Updated!"/>\n' % device_id
xml_response += '</response>'
return Response(xml_response, status=202, content_type='text/xml')
else:
raise NotFound('device not found!')
示例2: add_notes_overlapping
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import fromstring [as 别名]
def add_notes_overlapping(self, svg_contents):
svg = etree.fromstring(svg_contents)
mask_fill_color, did, tags, header, footer = get_params_for_add_notes()
# Add notes to the current deck of the collection:
notes_from_svg.add_notes_overlapping(svg, mask_fill_color,
tags, self.image_path,
header, footer, did)
示例3: add_notes_overlapping
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import fromstring [as 别名]
def add_notes_overlapping(self, svg_contents):
svg = etree.fromstring(svg_contents.encode('utf-8'))
mask_fill_color, did, tags, header, footer = get_params_for_add_notes()
# Add notes to the current deck of the collection:
add_notes_overlapping(svg, mask_fill_color,
tags, self.mw.image_occlusion2_image_path,
header, footer, did)
示例4: menu_main
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import fromstring [as 别名]
def menu_main(id,category=''):
text = read_xml(id,1)
root = ElementTree.fromstring(text)
elemroot = root.iter('Gen')
j = 0
for elem in elemroot:
name = elem.attrib['name']
if re.search(ListOmit, name.encode('utf-8')): continue
id = elem.attrib['id']
se = elem.attrib['enableSearch']
if se=="1":
catType = elem.attrib['search'][:-1].encode('utf-8') #change unicode to str
else:
catType = ""
orderID = int(elem.attrib['orderid'][:5]) #xbmc cannot sort >10000
j += 1
li=xbmcgui.ListItem('[COLOR FF00FF00][ ' + name + ' ][/COLOR]')
li.setInfo( type="Video", infoLabels={"Title":name, "Episode":orderID})
u=sys.argv[0]+"?mode=gen&name="+urllib.quote_plus(name.encode('utf-8'))+"&id="+id+"&category="+urllib.quote_plus(catType)
xbmcplugin.addDirectoryItem(int(sys.argv[1]),u,li,True)
#添加一个“PPS搜索”
li = xbmcgui.ListItem('[COLOR FFFF00FF]PPS.TV 网络电视[/COLOR][COLOR FFFFFF00] (主页) [/COLOR][COLOR FF00FFFF]共计:'+str(j)+'[/COLOR]【[COLOR FF00FF00]点此输入搜索内容[/COLOR]】')
li.setInfo(type="Video", infoLabels={"Title":name, "Episode":1})
u=sys.argv[0]+"?mode=search&name="+urllib.quote_plus('PPS搜索')
xbmcplugin.addDirectoryItem(int(sys.argv[1]),u,li,True)
xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_EPISODE)
示例5: AddChannel
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import fromstring [as 别名]
def AddChannel(request, device_id=None, channel_name=None):
if(device_id in device_list):
xml_request = request.body
root = ET.fromstring(xml_request)
sensor = root.find('sensor')
ch_unit = sensor.get('unit')
instance_name = device_list[device_id].get('name')
if(ch_unit == None):
type = device_list[device_id].get('type')
try:
ch_unit = Driver_map[type].get('channel_settings').split(',')[1]
except: ch_unit = 'no unit'
self.driver_channel_add(instance_name, channel_name, ch_unit)
xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n'
xml_response += '<response status="201">\n'
xml_response += '<device id="%s" message="Channel Created!"/>\n' % device_id
xml_response += '</response>'
return Response(xml_response, status=201, content_type='text/xml')
else:
raise NotFound('device not found!')
示例6: EditDevice
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import fromstring [as 别名]
def EditDevice(request):
xml_request = request.body
root = ET.fromstring(xml_request)
device = root.find('device')
device_id = device.get('id')
if(device_id == None or not device_id.isdigit()):
raise Conflict('Device Id is empty or not integer!')
device_id_new = device.get('id_new')
if(device_id_new != None and not device_id_new.isdigit()):
raise Conflict('New Device Id is not integer!')
device_type = device.get('type')
if(device_type != None and device_type != ''):
device_type = device_type.lower()
if(device_type not in Device_types):
raise Conflict('Device Type not found!')
device_name = device.get('name')
if(device_name != None and device_name != device_list[device_id].get('name')):
flag = 0
for i in device_list:
if(device_list[i].get('name') == device_name):
flag = 1
break
if(flag):
raise Conflict('Device Name already exist!')
device_mac = device.get('mac')
device_sample_rate = device.get('sample_rate')
if(device_sample_rate != None):
try:
device_sample_rate = float(device_sample_rate)
except:
raise Conflict('Device Sample Rate not integer or float!')
if(device_id in device_list):
if(device_type == None or device_type == ''):
device_type = device_list[device_id].get('type').lower()
if(device_name == None):
device_name = device_list[device_id].get('name')
if(device_mac == None):
device_mac = device_list[device_id].get('mac')
if(device_sample_rate == None):
device_sample_rate = device_list[device_id].get('sample_rate')
instance_name = device_name
self.driver_remove(instance_name)
del device_list[device_id]
if(device_id_new != None):
device_id_new = int(device_id_new)
while(str(device_id_new) in device_list): #if device_id_new exist -> device_id++ and try again
device_id_new += 1
device_id = str(device_id_new)
device_list[device_id] = {'type': device_type, 'name': device_name, 'mac': device_mac, 'sample_rate': device_sample_rate}
instance_name = device_name
driver_path = Driver_map[device_type].get('path')
settings = {}
if Driver_map[device_type].get('settings').find('xbee_device_manager') != -1:
if(self.xbee_device_manager_name == None):
self.xbee_device_manager_name = 'xbee_device_manager'
self.driver_add('xbee_device_manager', 'devices.xbee.xbee_device_manager.xbee_device_manager:XBeeDeviceManager', settings={'worker_threads':2})
time.sleep(1)
settings['xbee_device_manager'] = self.xbee_device_manager_name
settings['extended_address'] = device_mac
if Driver_map[device_type].get('settings').find('sample_rate_ms') != -1:
if(device_sample_rate == None):
device_sample_rate = 30
settings['sample_rate_ms'] = int(float(device_sample_rate) * 1000)
elif Driver_map[device_type].get('settings').find('sample_rate_sec') != -1:
if(device_sample_rate == None):
device_sample_rate = 1.0
settings['sample_rate_sec'] = float(device_sample_rate)
if Driver_map[device_type].get('settings').find('channel_settings') != -1:
settings['channel_settings'] = Driver_map[device_type].get('channel_settings')
self.driver_add(instance_name, driver_path, settings)
try:
self.device_list_update()
except:
traceback.print_exc()
xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n'
xml_response += '<response status="202">\n'
xml_response += '<device id="%s" message="Device Updated!"/>\n' % device_id
xml_response += '</response>'
return Response(xml_response, status=202, content_type='text/xml')
#.........这里部分代码省略.........
示例7: Graph
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import fromstring [as 别名]
t1 = datetime.now()
self.log.info("SPARQL Query took %s" % (t1-t0,))
self.log.debug("SPARQL Result:\n%s" % (response,))
except urllib2.HTTPError, e:
self.log.error("HTTP Response %s: %s" % (e.code, e.msg))
if e.message:
self.log.error("%s" % (e.message,))
raise
except:
self.log.error("Error in post to %s:\n%s" % (url, traceback.format_exc()))
raise
###
### parse the result using ElementTree
###
tree = ElementTree.fromstring(response)
###
### Fork here to handle the differing output from DESCRIBE and CONSTRUCT vs SELECT
###
if tree.tag.endswith('RDF'):
graph = Graph()
graph.parse(StringIO.StringIO(response))
for x in graph:
yield(x)
else:
### SELECT RESULTS
sparql_ns = "http://www.w3.org/2005/sparql-results#"
head = "{%s}head" % (sparql_ns,)
binding = "{%s}binding" % (sparql_ns,)
示例8: Search
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import fromstring [as 别名]
def Search(mname):
keyboard = Apps('','输入所查影片中文信息-拼音或简拼(拼音首字母)')
xbmc.sleep(1500)
keyboard.doModal()
keyword=keyboard.getText()
url="http://listso.ppstream.com/search.php?acp=936&w="+urllib.quote_plus(keyword)
text = getHttpData(url)
text = unicode(text, 'gb18030','replace').encode('utf8')
text = text.replace('gb18030', 'utf-8')
text = text.replace('GB18030', 'utf-8')
image_en = __addon__.getSetting('image_en')
root = ElementTree.fromstring(text)
#文件
i = 0
elemroot = root.iter("Ch")
for node in elemroot:
CHON=0
CHBKID=""
CHBKVM=0.0
year='1990'
elemtop = node.find('ID')
CHID=elemtop.attrib['ID']
try: CHON=int(node.attrib['ON'])
except: pass
try: p_url = elemtop.attrib['image']
except: p_url=''
try: CHBKID=elemtop.attrib['BKID']
except: pass
try: CHBKVM=float(elemtop.attrib['VM'])
except: pass
try:
cs=elemtop.attrib['search']
yy=re.compile('.+?:.+?:.+?:(.+?);').findall(cs)
if yy: year=yy[0]
except: pass
elemtop = node.find("Name")
CHName = elemtop.text
if CHBKVM and (CHBKVM > 0.01):
CHName += ' [COLOR FFFF00FF]['+str(CHBKVM)+'][/COLOR]'
elemtop = node.find("URL")
CHURL = elemtop.text
if image_en == 'true':
p_thumb=''
if p_url: p_thumb = "http://image1.webscache.com/baike/haibao/navi/" + p_url
li=xbmcgui.ListItem(CHName, iconImage='', thumbnailImage=p_thumb)
else:
li=xbmcgui.ListItem(CHName)
try: # some contains no CHURL
li.setInfo( type="Video", infoLabels={"Title":CHName, "count": CHON, "Rating":CHBKVM, "CODE":CHBKID,"Year":year})
u=sys.argv[0]+"?mode=ch&name="+CHName+"&id="+CHID+"&url="+urllib.quote_plus(CHURL.encode('gb18030'))
xbmcplugin.addDirectoryItem(int(sys.argv[1]),u,li,False)
i+=1 # increment only if everything is OK
except: pass
#次目录
j = 0
elemroot = root.iter("Gen")
for elemx in elemroot:
title = elemx.attrib['name']
if re.search(ListOmit, title.encode('utf-8')): continue
elemsub = elemx.iter("Sub")
for elem in elemsub:
j+=1
name = elem.attrib['name']
id = elem.attrib['id']
try:
cnt = elem.attrib['op']
except:
cnt ="sc='22';on='1';vm=''"
try: p_url = elem.attrib['image']
except: p_url=''
try: tag = elem.attrib['tags']
except: tag = ''
try: on = int(get_params2(cnt)['on'])
except: on = 1
try: vm = float(get_params2(cnt)['vm'])
except: vm = ''
catType = ''
p_list=title+': '+'[COLOR FF00FF00]'+name+'[/COLOR][COLOR FF00FFFF] ['+str(on)+'][/COLOR]'
if vm: p_list += '[COLOR FFFF00FF]['+str(vm)+'][/COLOR]'
if image_en == 'true':
p_thumb=''
if p_url: p_thumb = "http://image1.webscache.com/baike/haibao/navi/" + p_url
li=xbmcgui.ListItem(p_list, iconImage='', thumbnailImage=p_thumb)
else:
li=xbmcgui.ListItem(p_list)
li.setInfo( type="Video", infoLabels={"Title":name, "count":on, "Rating":vm})
#.........这里部分代码省略.........
示例9: menu_ch
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import fromstring [as 别名]
def menu_ch(name,id,category,rating,thumb):
text = read_xml(id,0)
root = ElementTree.fromstring(text)
elemroot = root.iter("Ch")
# Fetch & build video titles list for user selection, highlight user selected filter
#eList='[COLOR FF00FF00]类型:全部[/COLOR]'
eList='[COLOR FF00FF00]'+name+'[/COLOR]'
if category:
catFilter = updateFilter(category, False)
#catFilter = category
#category=""
eList = ''
sfltr=[]
xlist = catFilter.split(';')
for i in range(0, len(xlist)):
eList += COLOR_LIST[i%5]+xlist[i]+'[/COLOR]|'
item = xlist[i].split(':')[1]
if item != '全部':
#sfltr.append(unicode(item,'utf-8'))
sfltr.append(item)
eList = eList[:-1]
i = 0
image_en = __addon__.getSetting('image_en')
# required if series contains zero item.
p_order=1000
for node in elemroot:
elemtop = node.find('ID')
if category:
catType = elemtop.attrib['search'].encode('utf-8') # convert unicode to str
find = 1
for ifltr in sfltr:
if re.search(ifltr, catType) == None:
find = 0
break
if find == 0: continue # search not match, skip to next item
CHON=0
CHBKID=""
if rating: CHBKVM=float(rating)
else: CHBKVM=0
year='1990'
p_order=1000
#elemtop = node.find('ID')
CHID=elemtop.attrib['ID']
try: CHON=int(node.attrib['ON'])
except: pass
try: p_url = elemtop.attrib['image']
except: p_url=''
try: p_order = int(elemtop.attrib['ordnum'])
except: pass
try: CHBKID=elemtop.attrib['BKID']
except: pass
try: CHBKVM=float(elemtop.attrib['VM'])
except: pass
try: Tag=elemtop.attrib['tag'].encode('utf-8')
except: Tag=''
try:
cs=elemtop.attrib['search']
yy=re.compile('.+?:.+?:.+?:(.+?);').findall(cs)
# (类型):(动作,惊怵,犯罪,剧情;产地):(中国香港;年份):(2005); | 年份:80年代
if yy: year=yy[0]
except: pass
elemtop = node.find("Name")
CHName = elemtop.text
if re.search('高清', Tag): CHName = '[COLOR FF00FFFF]['+CHName+'][/COLOR]'
elif re.search('国语', Tag) or re.search('粤语', Tag):
CHName = '[COLOR FFFFFF00]['+CHName+'][/COLOR]'
#else: CHName = '[COLOR FF00FF00]['+CHName+'][/COLOR]'
if CHBKVM: CHName += ' [COLOR FFFF00FF]['+str(CHBKVM)+'][/COLOR]'
elemtop = node.find("URL")
CHURL = elemtop.text
if image_en == 'true':
p_thumb=''
if p_url: p_thumb = "http://image1.webscache.com/baike/haibao/navi/" + p_url
li=xbmcgui.ListItem(CHName, iconImage='', thumbnailImage=p_thumb)
else:
li=xbmcgui.ListItem(CHName)
try: # some contains no CHURL
li.setInfo( type="Video", infoLabels={"Title":CHName, "count": CHON, "Rating":CHBKVM, "CODE":CHBKID,"Year":year, "Episode":p_order})
u=sys.argv[0]+"?mode=ch&name="+CHName+"&id="+CHID+"&url="+urllib.quote_plus(CHURL.encode('gb18030'))
xbmcplugin.addDirectoryItem(int(sys.argv[1]),u,li,False)
i+=1 # increment only if everything is OK
except: pass
li = xbmcgui.ListItem('[COLOR FFFF00FF]当前选择: [/COLOR][COLOR FFFFFF00]('+eList+') [/COLOR][COLOR FF00FFFF]共计:'+str(i)+'[/COLOR]【[COLOR FF00FF00]'+'按此选择'+'[/COLOR]】')
li.setInfo( type="Video", infoLabels={"Title":" ", "count":1000000, "Rating":10.0})
u=sys.argv[0]+"?mode=sub&name="+name+"&id="+id+"&category="+category+"&rating="+rating+"&thumb="+thumb
xbmcplugin.addDirectoryItem(int(sys.argv[1]), u, li, True)
if p_order == 1000:
#.........这里部分代码省略.........
示例10: menu_sub
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import fromstring [as 别名]
def menu_sub(name,id,category):
text = read_xml(id,1)
root = ElementTree.fromstring(text)
# Fetch & build video titles list for user selection, highlight user selected filter
eList='[COLOR FF00FF00]类型:全部[/COLOR]'
if category:
catFilter = updateFilter(category, True)
eList = ''
sfltr=[]
xlist = catFilter.split(';')
for i in range(0, len(xlist)):
eList += COLOR_LIST[i%5]+xlist[i]+'[/COLOR]|'
item = xlist[i].split(':')[1]
if item != '全部':
#sfltr.append(unicode(item,'utf-8'))
sfltr.append(item)
eList = eList[:-1]
j = 0
image_en = __addon__.getSetting('image_en')
elemroot = root.iter("Sub")
for elem in elemroot:
if category:
catType = elem.attrib['search'].encode('utf-8') # convert unicode to str
find = 1
for ifltr in sfltr:
if re.search(ifltr, catType) == None:
find = 0
break
if find == 0: continue # search not match, skip to next item
name = elem.attrib['name']
p_id = elem.attrib['id']
try:
cnt = elem.attrib['op']
except:
cnt ="sc='22';on='1';vm=''"
try:
p_url = elem.attrib['dj']
continue
except: dj=''
try: p_url = elem.attrib['image']
except: p_url=''
try: tag = elem.attrib['tags']
except: tag = ''
try: on = int(get_params2(cnt)['on'])
except: on = 1
try: vm = float(get_params2(cnt)['vm'])
except: vm = ''
catShow=''
if category:
types = catType.decode('utf-8').split(';')
for i in range(0, len(types)-2):
catShow += types[i].split(':')[1]+';'
catShow = catShow[:-1]
catType = ''
# category search not required at sub menu
try:
if elem.attrib['enableSearch2'] == '1':
catType = elem.attrib['search'][:-1].encode('utf-8') #change unicode to str
except:
pass
p_list='[COLOR FF00FF00]'+name+'[/COLOR][COLOR FF00FFFF] ['+str(on)+'][/COLOR]'
if vm: p_list += '[COLOR FFFF00FF]['+str(vm)+'][/COLOR]'
if not catType and tag: p_list += '[COLOR FFFFFF00]['+tag+'][/COLOR]'
p_list += ' '+catShow
if image_en == 'true':
p_thumb=''
if p_url: p_thumb = "http://image1.webscache.com/baike/haibao/navi/" + p_url
li=xbmcgui.ListItem(p_list, iconImage='', thumbnailImage=p_thumb)
else:
li=xbmcgui.ListItem(p_list)
li.setInfo( type="Video", infoLabels={"Title":name, "count":on, "Rating":vm})
u=sys.argv[0]+"?mode=sub&name="+name+"&id="+p_id+"&rating="+str(vm)+"&category="+urllib.quote_plus(catType)+"&thumb="+p_thumb
xbmcplugin.addDirectoryItem(int(sys.argv[1]),u,li,True)
j+=1 # increment only if everything is OK
li = xbmcgui.ListItem(' [COLOR FFFF00FF]当前选择: [/COLOR][COLOR FFFFFF00]('+eList+') [/COLOR][COLOR FF00FFFF]共计:'+str(j)+'[/COLOR]【[COLOR FF00FF00]'+'按此选择'+'[/COLOR]】')
li.setInfo( type="Video", infoLabels={"Title":name, "count":1000000000, "Rating":10.0})
u=sys.argv[0]+"?mode=gen&id="+id+"&category="+category
xbmcplugin.addDirectoryItem(int(sys.argv[1]), u, li, True)
#sort = [名称|评价|播放次数]
sortOrder = __addon__.getSetting('sort')
if sortOrder=='0':
xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_LABEL)
elif sortOrder=='1':
xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_VIDEO_RATING)
xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_PROGRAM_COUNT)
else:
xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_PROGRAM_COUNT)
xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_VIDEO_RATING)