Wednesday, November 7, 2007

Python : HTA


<html>
<head>
<TITLE>HTML Application Example</TITLE>
<HTA:APPLICATION ID="HTAEx" APPLICATIONNAME="HTAEx" ICON="e.ico" WINDOWSTATE="normal">
<style type="text/css">

body {
background-color:#fff;
}

.blank{
color:#ffffff;
}

select {
font-size:0.8em;
font-weight: bold;
text-align:center;
padding:15px;
}

#info {
text-align:left;
background-color: #000;
color: #fff;
padding-left: 5px;
font-size:1em;
}

.patience {
background-color:#ccccee;
width=100%;
text-align:center;
color: #000000;
font-weight: bold;
}


.pair{
background-color: #ccccee;
}

.impair{
background-color: #ffccff;
}

.detail {
padding-right: 10px;
padding-left: 6px;
text-align: right;
}

.Ink {

position: absolute;

background-color: #fff;

border-top: 1px solid transparent;

width: 1px;

height: 1px;

}


</style>

<body onLoad="Initialisation()">
<center><h2>Système d'Administration Simplifié</h2></center>
<table align="center" width="80%" style="border:0px solid black; margin-top:40px;">
<tr><td id="message" style="visibility:hidden;flush="true"">TEST</td></tr>
<tr><th style="background-color:#000;color:#fff;">Serveurs</th><th><div id="info">Informations</div></th></tr>
<tr>
<td width="220px" valign="top">
<div id="tabgauche">
<select size="1" name="liste" onChange="selectServeur()">
<option selected="selected" value="None" class="blank">-----------------------------------------</option>
<option value="1" class="opts">&nbsp;Dakar</option>
<option value="2" class="opts">&nbsp;Denver</option>
<option value="3" class="opts">&nbsp;Douala</option>
<option value="4" class="opts">&nbsp;Dublin</option>
<option value="NONE" class="opts"><hr /></option>
<option value="5" class="opts">&nbsp;Fatima</option>
<option value="6" class="opts">&nbsp;Flores</option>
<option value="7" class="opts">&nbsp;Francfort</option>
<option value="8" class="opts">&nbsp;Fresno</option>
<option value="NONE" class="opts"><hr /></option>
<option value="9" class="opts">&nbsp;Python - 127.0.0.1</option>
</select>
</div>
</td>
<td valign="top">
<div id="tabcentreInfo"></div>
</td>
</tr>
<tr><td colspan="2"><hr /></td></tr>
<tr>
<td valign="top" style="background-color:#fff;">
<div id="svc">&nbsp;</div>
</td>
<td valign="top">
<div id="tabcentre">&nbsp;</div>
</td>
</tr>
</table>
<div id="espace"></div>
<!-- Script -->

<script language=Python>

from win32com.client import GetObject,Dispatch
import pythoncom
import win32gui
import os
import thread
import win32api
import locale
import Image,ImageDraw
import cStringIO
from random import randint as rint
import base64

locale.setlocale(locale.LC_NUMERIC, '')

serveurs = {}
services = {}
cmd ={}

Serveur = None
LstService = None
IdServeur = None
Busy = 0

serveurs[1] = ['Dakar']
serveurs[2] = ['Denver']
serveurs[3] = ['Douala']
serveurs[4] = ['Dublin']
serveurs[5] = ['Fatima']
serveurs[6] = ['Flores']
serveurs[7] = ['Francfort']
serveurs[8] = ['Fresno']
serveurs[9] = ["Python"]

svcsysteme = ['systeme','Param&egrave;tre Syst&egrave;me']
svcbanque = ['banque','Etat des Banques']
svcdisque = ['disque','Espace Disque']
svcstream = ['stream','Streaming']

#Dakar
services[1] = [svcsysteme,svcbanque]
#Denver
services[2] = [svcsysteme,svcbanque]
#Douala
services[3] = [svcsysteme,svcbanque]
#Dublin
services[4] = [svcsysteme,svcbanque]

#Fatima
services[5] = [svcsysteme,svcbanque]
#Flores
services[6] = [svcsysteme,svcbanque]
#Francfort
services[7] = [svcsysteme,svcbanque]
#Fresno
services[8] = [svcsysteme,svcbanque]
#Python
services[9] = [svcsysteme,svcdisque,svcstream]

#Gestion Mouse
def Pointer():
document.body.style.cursor = "hand"
return

def DefaultCursor():
document.body.style.cursor = "default"
return

def WaitCursor():
win32gui.PumpWaitingMessages()
document.body.style.cursor = "wait"
return


#Gestion DIV
def getValue(id):
#return document.getElementById(id).value
return document.getElementById(id).getAttribute("value")

def getElt(id):
return document.getElementById(id)

def setValue(divid,data):
elt = document.getElementById(divid)
elt.innerHTML = data

def showMessage(message="info"):
elt = getElt("message")
elt.innerHTML = message
elt.style.visibility = "visible"
return 1
elt = getElt("message")
elt.style.visibility = "hidden"

def majTabInfo():
elt = getElt('tabcentreInfo')
serveur = serveurs[IdServeur][0]
elt.innerHTML = """<span onClick="connectServer('%s')"
onmouseover='Pointer()'
onmouseout='DefaultCursor()'>
<b>Se connecter</b></span>"""%(serveur)


def groupLstByN(l,n,filter=None):
#group elt in l by n
res = []
k = 0
lp = []
for elt in l:
lp.append(elt)
k +=1
if k == n:
k = 0
res.append(lp)
lp = []
return [tuple(item) for item in res]


def number_format(num, places=0):
s = locale.format("%.*f", (places, num), True)
return s.replace('\xa0',' ')


def formatTable(titre,data):
"""
titre = ["t1",t2"]
data = [["a","b"],["1","2"]]
"""
table = "<table border='1'>"
n = len(titre)
th = r"<th>%s</th>"*n
td = "<td class='detail'>%s</td>"*n
coltitre = th%(tuple(titre))
table += coltitre
i = 0
for item in data:
if i%2 == 0:
table += ("<tr class='pair'>"+td%(tuple(item))+"</tr>")
else:
table += ("<tr class='impair'>"+td%(tuple(item))+"</tr>")
i += 1
return table + "</table>"

def randgradient():
img = Image.new("RGB", (300,300), "#FFFFFF")
draw = ImageDraw.Draw(img)

r,g,b = rint(0,255), rint(0,255), rint(0,255)
dr = (rint(0,255) - r)/300.
dg = (rint(0,255) - g)/300.
db = (rint(0,255) - b)/300.
for i in range(300):
r,g,b = r+dr, g+dg, b+db
draw.line((i,0,i,300), fill=(int(r),int(g),int(b)))

f = cStringIO.StringIO()
img.save(f, "PNG")

print "Content-type: image/png\n"
f.seek(0)
return f


# Procédures de service

cmdDisk = r'wmic /Node:%s path win32_logicaldisk WHERE "DriveType=3" GET DeviceID,Freespace,Size '


def infoBanque():
alert("Banque")

def setPauseMsg(started=0):
if started == 1:
WaitCursor()
setValue("tabcentreInfo","<span class='patience'>Veuillez Patienter....</span>")
else:
Pointer()
majTabInfo()
win32gui.PumpWaitingMessages()

def infoSysteme():
setPauseMsg(1)
for i in range(10000):
i = i
titre = ["a","b"]
data = [[1,2],[2,4]]
content = formatTable(titre, data)
setValue("tabcentre",content)
setPauseMsg(0)

def infoDisque():
#cmd: cmdDisk
setPauseMsg(1)
setValue("tabcentre","")
titrecols = ['Nom','Espace Libre','Espace Total','Pourcentage']
c = os.popen(cmdDisk%Serveur).readlines()[1:-1]
items = [item.strip().split() for item in c]
for item in items:
percent = (1 - 1.0*long(item[1])/long(item[2]))*100
item.append("%.2f"%percent+"%")
item[1] = number_format(long(item[1]), 0)
item[2] = number_format(long(item[2]), 0)
# setValue("tabcentre",`items`)
content = formatTable(titrecols,items)
setValue("tabcentre",content)
setPauseMsg(0)

i = 0
def counter():
global i
i += 1
setValue("tabcentre",i)

intcounter = 0
def infoStream():
global intcounter
intcounter = window.setInterval('counter()',500)

#Fin des procédures de service

cmd['systeme'] = infoSysteme
cmd['banque'] = infoBanque
cmd['disque'] = infoDisque
cmd['stream'] = infoStream


def Initialisation():
pass

def majService():
menu = "<ul>"
for item in LstService:
menu += "<li id=%s onmouseover='Pointer()',\
onmouseout='DefaultCursor()',\
onClick='getMenuOption(%s)'>%s</li>"%(item[0],\
LstService.index(item),item[1])
menu += "</ul>"
setValue('svc',menu)

def clearZoneInfo():
elt = getElt('info')
elt.innerHTML = "Informations"

def clearCentre():
elt = getElt('tabcentre')
elt.innerHTML = ""

def connectServer(serveur):
os.system("mstsc -v %s"%serveur)

def getMenuOption(args):
window.clearInterval(intcounter)
global selectedService
service = services[IdServeur][args][0]
libelle = services[IdServeur][args][1]
selectedService = service
elt = getElt('info')
elt.innerHTML = libelle
try:
cmd[service]()
except:
pass

def clearInfo():
clearZoneInfo()
clearCentre()
setValue('tabcentre','')
setValue('svc','')

def selectServeur():
global Serveur, LstService, IdServeur
clearInfo()
try:
IdServeur = int(getValue('liste'))
except:
return
try:
Serveur = serveurs[IdServeur][0]
LstService = services[IdServeur]
majService()
majTabInfo()
except:
return
DefaultCursor()

</script>
</body>
</html>