Utilisation du module xml de python. Pour faire propre et pour faire du xml
facile parser par le mme module dans un autre script. Il y a surement des transformations unicode en trop et il reste un problme d'encodage au niveau de la description. Pour le premier problme j'affinerais si ncessaire. Pour le second le problme vient de ce script ou bien d'un champs de description dans la base pas totalement en unicode? darcs-hash:20060422234938-9e428-f556fa2e8040f4ddb63561835fb606e8f78b4882.gz
This commit is contained in:
parent
42d67e76e4
commit
b59ed2c310
1 changed files with 49 additions and 23 deletions
|
@ -7,6 +7,7 @@ import sys
|
|||
import os
|
||||
import time
|
||||
sys.path.append("/usr/scripts/gestion")
|
||||
sys.path.append("/usr/scripts/wifi")
|
||||
|
||||
from canal_bornes import bornes_canal
|
||||
from wifi_clients import bornes_clients
|
||||
|
@ -15,8 +16,14 @@ from uptime_bornes import bornes_uptime
|
|||
from ldap_crans import crans_ldap
|
||||
l = crans_ldap()
|
||||
|
||||
# On commence le fichier XML
|
||||
print '<bornes date="%s">' % time.strftime("%c")
|
||||
import xml.dom.minidom
|
||||
|
||||
# création objet xml
|
||||
status=xml.dom.minidom.Document()
|
||||
|
||||
xml_bornes=status.createElement(u"bornes")
|
||||
status.appendChild(xml_bornes)
|
||||
xml_bornes.setAttribute(u"date",time.strftime("%c"))
|
||||
|
||||
# On récupère l'ensemble des bornes
|
||||
bornes = l.search('host=*.wifi.crans.org&canal=*')['borneWifi']
|
||||
|
@ -40,26 +47,45 @@ for b in bornes:
|
|||
up = 0
|
||||
# Quel nom ?
|
||||
nom = b.nom().split(".")[0]
|
||||
print '<borne nom="%s" hotspot="%d" up="%d">' % (nom,
|
||||
hotspot,
|
||||
up)
|
||||
print ' <description>%s</description>' % infos[0].encode("utf-8")
|
||||
print ' <mac>%s</mac>' % b.mac2()
|
||||
try:
|
||||
print ' <canal>%d</canal>' % int(canaux[nom])
|
||||
except KeyError:
|
||||
pass
|
||||
try:
|
||||
print ' <clients>%d</clients>' % int(clients[nom])
|
||||
except KeyError:
|
||||
pass
|
||||
try:
|
||||
print ' <uptime>%s</uptime>' % uptimes[nom]
|
||||
except KeyError:
|
||||
pass
|
||||
xml_borne=status.createElement(u"borne")
|
||||
xml_borne.setAttribute(u"nom",unicode(nom))
|
||||
xml_borne.setAttribute(u"hotspot",unicode(hotspot))
|
||||
xml_borne.setAttribute(u"up",unicode(up))
|
||||
|
||||
|
||||
xml_desc=status.createElement(u"description")
|
||||
#xml_desc.appendChild(status.createTextNode(unicode(infos[0])))
|
||||
xml_desc.appendChild(status.createTextNode(u"Description pas compatible unicode/ascii"))
|
||||
xml_borne.appendChild(xml_desc)
|
||||
|
||||
xml_mac=status.createElement(u"mac")
|
||||
xml_mac.appendChild(status.createTextNode(unicode(b.mac2())))
|
||||
xml_borne.appendChild(xml_mac)
|
||||
|
||||
# Certains résultats (ragnarok) n'ont pas tous les champs.
|
||||
if (canaux.has_key(nom)):
|
||||
xml_canal=status.createElement(u"canal")
|
||||
xml_canal.appendChild(status.createTextNode(unicode(int(canaux[nom]))))
|
||||
xml_borne.appendChild(xml_canal)
|
||||
|
||||
if (clients.has_key(nom)):
|
||||
xml_clients=status.createElement(u"clients")
|
||||
xml_clients.appendChild(status.createTextNode(unicode(int(clients[nom]))))
|
||||
xml_borne.appendChild(xml_clients)
|
||||
|
||||
if (uptimes.has_key(nom)):
|
||||
xml_uptime=status.createElement(u"uptime")
|
||||
xml_uptime.appendChild(status.createTextNode(unicode(uptimes[nom])))
|
||||
xml_borne.appendChild(xml_uptime)
|
||||
|
||||
|
||||
position = b.position()
|
||||
if position:
|
||||
print ' <position x="%s" y="%s" />' % (position[1], position[0])
|
||||
print '</borne>'
|
||||
|
||||
print '</bornes>'
|
||||
xml_position=status.createElement(u"position")
|
||||
xml_position.setAttribute(u"x",unicode(position[1]))
|
||||
xml_position.setAttribute(u"y",unicode(position[0]))
|
||||
xml_borne.appendChild(xml_position)
|
||||
|
||||
xml_bornes.appendChild(xml_borne)
|
||||
|
||||
print (status.toxml())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue