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 os
|
||||||
import time
|
import time
|
||||||
sys.path.append("/usr/scripts/gestion")
|
sys.path.append("/usr/scripts/gestion")
|
||||||
|
sys.path.append("/usr/scripts/wifi")
|
||||||
|
|
||||||
from canal_bornes import bornes_canal
|
from canal_bornes import bornes_canal
|
||||||
from wifi_clients import bornes_clients
|
from wifi_clients import bornes_clients
|
||||||
|
@ -15,8 +16,14 @@ from uptime_bornes import bornes_uptime
|
||||||
from ldap_crans import crans_ldap
|
from ldap_crans import crans_ldap
|
||||||
l = crans_ldap()
|
l = crans_ldap()
|
||||||
|
|
||||||
# On commence le fichier XML
|
import xml.dom.minidom
|
||||||
print '<bornes date="%s">' % time.strftime("%c")
|
|
||||||
|
# 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
|
# On récupère l'ensemble des bornes
|
||||||
bornes = l.search('host=*.wifi.crans.org&canal=*')['borneWifi']
|
bornes = l.search('host=*.wifi.crans.org&canal=*')['borneWifi']
|
||||||
|
@ -40,26 +47,45 @@ for b in bornes:
|
||||||
up = 0
|
up = 0
|
||||||
# Quel nom ?
|
# Quel nom ?
|
||||||
nom = b.nom().split(".")[0]
|
nom = b.nom().split(".")[0]
|
||||||
print '<borne nom="%s" hotspot="%d" up="%d">' % (nom,
|
xml_borne=status.createElement(u"borne")
|
||||||
hotspot,
|
xml_borne.setAttribute(u"nom",unicode(nom))
|
||||||
up)
|
xml_borne.setAttribute(u"hotspot",unicode(hotspot))
|
||||||
print ' <description>%s</description>' % infos[0].encode("utf-8")
|
xml_borne.setAttribute(u"up",unicode(up))
|
||||||
print ' <mac>%s</mac>' % b.mac2()
|
|
||||||
try:
|
|
||||||
print ' <canal>%d</canal>' % int(canaux[nom])
|
xml_desc=status.createElement(u"description")
|
||||||
except KeyError:
|
#xml_desc.appendChild(status.createTextNode(unicode(infos[0])))
|
||||||
pass
|
xml_desc.appendChild(status.createTextNode(u"Description pas compatible unicode/ascii"))
|
||||||
try:
|
xml_borne.appendChild(xml_desc)
|
||||||
print ' <clients>%d</clients>' % int(clients[nom])
|
|
||||||
except KeyError:
|
xml_mac=status.createElement(u"mac")
|
||||||
pass
|
xml_mac.appendChild(status.createTextNode(unicode(b.mac2())))
|
||||||
try:
|
xml_borne.appendChild(xml_mac)
|
||||||
print ' <uptime>%s</uptime>' % uptimes[nom]
|
|
||||||
except KeyError:
|
# Certains résultats (ragnarok) n'ont pas tous les champs.
|
||||||
pass
|
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()
|
position = b.position()
|
||||||
if position:
|
if position:
|
||||||
print ' <position x="%s" y="%s" />' % (position[1], position[0])
|
xml_position=status.createElement(u"position")
|
||||||
print '</borne>'
|
xml_position.setAttribute(u"x",unicode(position[1]))
|
||||||
|
xml_position.setAttribute(u"y",unicode(position[0]))
|
||||||
print '</bornes>'
|
xml_borne.appendChild(xml_position)
|
||||||
|
|
||||||
|
xml_bornes.appendChild(xml_borne)
|
||||||
|
|
||||||
|
print (status.toxml())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue