You can use Python with XUL with the help of a http server
which execute plain Python Scripts sent by the client.
Like the following :
CPython Server
<?xml version="1।0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="example-window" title="Example 7.2.1"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script language="javascript">
var wmi=" \n\
import wmi \n\
c = wmi.WMI('Python') \n\
for os in c.Win32_OperatingSystem(): \n\
caption = os.Caption \n\
response = caption \n\
filename = 'foo'"
function WebMethod(url,request,callback)
{
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
} catch (e) {
alert("Permission UniversalBrowserRead denied.");
}
var http = new XMLHttpRequest();
var mode = request?"POST":"GET";
http.open(mode,url,true);
if(mode=="POST"){http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');}
http.onreadystatechange=function(){if(http.readyState==4){callback(http.responseText);}};
http.send(request);
}
function CallBack(r)
{
alert(r);
}
</script>
<box id="aBox" width="200">
<button label="GetOsName" oncommand="WebMethod('http://127.0.0.1:9981',wmi,CallBack);"/>
</box>
</window>
Sunday, December 30, 2007
Thursday, December 27, 2007
WMI - Mochikit
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
table.prettytable thead,th,tfoot
{
background-color : #676789;
text-align : center;
font-weight: bold;
color: #fff;
}
table.prettytable
{
border: 1px solid black;
}
</style>
<script type="text/javascript" src="../../lib/MochiKit/MochiKit.js"></script>
<script type="text/javascript">
result = []
row_display = function (row)
{
return TR(null, map(partial(TD, null), row));
}
function getDiskInfo()
{
var loc = new ActiveXObject("WbemScripting.SWbemLocator");
var wmi = loc.ConnectServer("", "root\\cimv2");
var ins = wmi.ExecQuery("select * from Win32_LogicalDisk");
for (var col = new Enumerator(ins);!col.atEnd();col.moveNext())
{
var disq = col.item()
result.push([disq.DeviceId,disq.FreeSpace,disq.Size])
}
var newTable = TABLE({'class': 'prettytable'},
THEAD(null,row_display(["Name", "FreeSpace", "Size"])),
TFOOT(null,row_display(["foot1", "foot2", "foot3"])),
TBODY(null,map(row_display, result)));
appendChildNodes("theBody",newTable);
}
</script>
</head>
<body id="theBody" onload="getDiskInfo()">
</body>
</html>
Powered by ScribeFire.
Wednesday, December 26, 2007
wmi - IronPython
import System
import clr
clr.AddReference("System.Management")
import System.Management
def requestWMI(squery,sproperties):
output = ""
objMgt = System.Management.ManagementObjectSearcher(squery)
for result in objMgt.Get():
output += result.GetPropertyValue(sproperties)
return output
print requestWMI("Select * from Win32_BIOS", "Caption")
raw_input()
Powered by ScribeFire.
Tuesday, December 18, 2007
JScript .NET + WMI
Finding documentation for using WMI with JScript is not easy.
Compile this code with 'jsc' (shipped with .Net) in an exe file
import System;
import System.Management;
var diskSearch = new ManagementObjectSearcher("Select * from Win32_LogicalDisk")
var obj = diskSearch.Get()
var prop = new Object()
for (prop in obj){
print(prop['Caption'])
}
Compile this code with 'jsc' (shipped with .Net) in an exe file
import System;
import System.Management;
var diskSearch = new ManagementObjectSearcher("Select * from Win32_LogicalDisk")
var obj = diskSearch.Get()
var prop = new Object()
for (prop in obj){
print(prop['Caption'])
}
Powered by ScribeFire.
Monday, November 26, 2007
VBS from Python
from win32com.client import Dispatch
#ref : http://mow001.blogspot.com/2006/03/vbscript-hosting-in-msh-inputbox-in.html
vbs = Dispatch("MSScriptControl.ScriptControl")
vbs.Language = "vbscript";
wmi= r"""
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT Caption,Size FROM Win32_LogicalDisk where drivetype='3'",,48)
"""
ldisque = []
def cleanString(s):
car = [';','"','\n','\t',' ']
for c in car:
try:
s = s.replace(c,'')
except:
continue
head = s.index('{')
tail = s.index('}')
s = s.replace(':',',')
s = s.replace('=',':')
return s
vbs.addcode(wmi)
col = vbs.eval('colItems')
for item in col:
disque = item.GetObjectText_()
print cleanString(disque)
vbs = None
Calling VBS from Python
from win32com.client import Dispatch
#ref : http://mow001.blogspot.com/2006/03/vbscript-hosting-in-msh-inputbox-in.html
vbs = Dispatch("MSScriptControl.ScriptControl")
vbs.Language = "vbscript";
code = """
function Input()
Input = inputbox("message","titre")
end function
function add(x,y)
add = x + y
end function
"""
vbs.addcode(code)
txt = vbs.eval('Input()')
print txt
somme = vbs.eval('add(2,3)')
vbs.ExecuteStatement('msgbox "The result is %d",,"Call VBS Add from Python"'%somme)
#ref : http://mow001.blogspot.com/2006/03/vbscript-hosting-in-msh-inputbox-in.html
vbs = Dispatch("MSScriptControl.ScriptControl")
vbs.Language = "vbscript";
code = """
function Input()
Input = inputbox("message","titre")
end function
function add(x,y)
add = x + y
end function
"""
vbs.addcode(code)
txt = vbs.eval('Input()')
print txt
somme = vbs.eval('add(2,3)')
vbs.ExecuteStatement('msgbox "The result is %d",,"Call VBS Add from Python"'%somme)
Powered by ScribeFire.
Tuesday, November 20, 2007
wxPython - TreeControl - SplitPanel
# -*- coding: iso-8859-1 -*-
# Référence : wxPython in Action from Robin Dunn - Noel Rappin
import os
import wx
ID_ABOUT = wx.NewId()
ID_OPEN = wx.NewId()
ID_EXIT = wx.NewId()
tree = [
['Dakar',
['Administration',
['Supervision',
['Disques','Banques','File d''attentes']],
'Réseaux'],
], #/Dakar
['Denver',
['Administration','Supervision','Réseaux'],
] #/Denver
]
class TreeServeur(wx.TreeCtrl):
def __init__(self, parent,id):
super(TreeServeur, self).__init__(parent,id)
self.Bind(wx.EVT_TREE_ITEM_EXPANDING, self.OnExpandItem)
self.Bind(wx.EVT_TREE_ITEM_COLLAPSING, self.OnCollapseItem)
self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated)
root = self.AddRoot("Serveurs")
self.AddTreeNodes(root,tree)
def AddTreeNodes(self, parentItem, items):
for item in items:
if type(item) == str:
self.AppendItem(parentItem, item)
else:
newItem = self.AppendItem(parentItem, item[0])
self.AddTreeNodes(newItem, item[1])
def OnExpandItem(self, evt):
pass
def OnCollapseItem(self,evt):
pass
def OnActivated(self,evt):
print "OnActivated: ", self.GetItemText(evt.GetItem())
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(1024, 800))
self.initpos = 50
#Status Bar
self.CreateStatusBar()
#Menu Fichier
filemenu = wx.Menu()
filemenu.Append(ID_OPEN,'&Ouvrir')
filemenu.Append(ID_EXIT,'&Quitter')
#Menu Aide
aidemenu = wx.Menu()
aidemenu.Append(ID_ABOUT,'&Aide')
#Barre de Menu
menubar = wx.MenuBar()
menubar.Append(filemenu,'&Fichier')
menubar.Append(aidemenu,'&Aide')
self.SetMenuBar(menubar)
#Gestionnaire d'evenement menu
wx.EVT_MENU(self, ID_ABOUT, self.OnAbout)
wx.EVT_MENU(self, ID_EXIT, self.OnExit)
wx.EVT_MENU(self, ID_OPEN, self.OnOpen)
#Splitter
splitter1 = wx.SplitterWindow(self, -1, style=wx.SP_3D)
splitter1.SetMinimumPaneSize(200)
splitter1.SetSashPosition(40)
splitter1.UpdateSize()
splitter2 = wx.SplitterWindow(splitter1, -1, style=wx.SP_3D)
splitter2.SetMinimumPaneSize(50)
splitter2.SetSashPosition(40)
self.sidebar = TreeServeur(splitter1, id=-1)
self.up =wx.TextCtrl(splitter2, wx.NewId(), "Un texte", size=(100, -1),pos=(80, 50))
self.down = wx.ListCtrl(splitter2, -1, style=wx.LC_LIST)
splitter1.SplitVertically(self.sidebar, splitter2,200)
splitter2.SplitHorizontally(self.up, self.down,0)
self.Centre()
def OnActivated(self,evt):
print "OnActivated: ", self.GetItemText(evt.GetItem())
def ShowMessage(self,msg):
d = wx.MessageDialog(self, msg, style=wx.OK)
d.ShowModal()
d.Destroy()
def OnAbout(self, event):
self.ShowMessage("About")
def OnExit(self,e):
self.Close(True)
def OnOpen(self,e):
#Ouverture d'un fichier"
self.dirname = ""
wildcard = "*.py"
dlg = wx.FileDialog(
self, message="Choose a file",
defaultDir=os.getcwd(),
defaultFile="",
wildcard=wildcard,
style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
f = open(path)
self.control.SetValue(f.read())
dlg.Destroy()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, "dragdrop.py")
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(redirect=False)
app.MainLoop()
Subscribe to:
Posts (Atom)