Friday, January 18, 2008

Boo on .Net

What language choose for .Net : C# VB.NET IronPython ?
Although my language of choice is Python, I must admit
that Boo is very attractive.
It does not have the verbosity of VB.NET or C# and it's syntax
is greatly inspired by Python.
Take a look at this presentation :
http://www.infoq.com/articles/boo-intro

Sunday, December 30, 2007

XUL and Python

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>

Thursday, December 27, 2007

WMI - Mochikit


&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;style type="text/css"&gt;
table.prettytable thead,th,tfoot
{
background-color : #676789;
text-align : center;
font-weight: bold;
color: #fff;
}
table.prettytable
{
border: 1px solid black;
}

&lt;/style&gt;
&lt;script type="text/javascript" src="../../lib/MochiKit/MochiKit.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;

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);

}
&lt;/script&gt;
&lt;/head&gt;
&lt;body id="theBody" onload="getDiskInfo()"&gt;

&lt;/body&gt;
&lt;/html&gt;



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'])
}


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)



Powered by ScribeFire.