Monday, June 13, 2011

Scripts of the week

Hi, blogging again after a long time. feeling lots of ups and down during the period..hell leave it for now..
I dedicate last week to writing the scripts, I wrote vim plugin for uploading and downloading the code to/from pastebin and also created an API using wurfl to detect the mobile device config and provide the run time support to it.

Vim Plugin Story :
Finally I wrote my own vim plugin, I had an idea long time ago, but never get the time to implement it, during this weekend I have nothing much to do so, I decide to go with the plugin and give rest to other tasks, I spend two days on writing plugin.
Plugin basically parse the HTML code using the beautiful soup python module. After getting the branch of the code text, I removed the additional HTML tags and copied it to the vim editor.

import urllib2
from BeautifulSoup import BeautifulSoup
import sys
soup = BeautifulSoup(''.join(data))
code=soup('div', {'id' : 'code_frame'})
soup = BeautifulSoup(''.join(str(code[0]).strip()))
code_text = soup.div.div

code_for_vim = BeautifulSoup(str(text).strip(), convertEntities=BeautifulSoup.HTML_ENTITIES)
print code_for_vim

above code print the code pasted on the valid link of the pastebin, this involves parsing of the HTML source and beautiful soup does it beautifully ;). I called above python script from the vim
function command! -nargs=0 PasteBin :call pasteCodeFromPasteBin()
which could be called by user from vim editor by typing :Pastebin.

fun! pasteCodeFromPasteBin()
let g:binLink = input("Enter valid pastebin link : ")
let g:data=system('python ~/.vim/plugin/ '.g:binLink)
set paste
exec "normal O".g:data

and for uploading the code directly from vim editor, I used pastebin api which require code to be pasted, language preference, email ID, title etc details passed as a post argument with an api, for this I used 'curl -d' and perl

let g:data=system('cat '.bufname("%").'| perl -p -e ''s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg''')
let g:query='paste_private=0&paste_code='.theg:data.'&paste_name='.g:name.'&paste_email='.g:pastebin_emailid.'&paste_format='.g:pastebin_language
let g:out=system('curl -d -s"'.g:query.'" ')

WURFL Story:
WURFL is a Device Description Repository (DDR), i.e. a software component which contains the descriptions of thousands of mobile devices with the help of WURFL I create an API which can detect not only the device but can also find the nearest match from the available configs and provide the run time support to it.

// Include the WURFL/Application.php file
require_once "WURFL_Installation_Dir/WURFL/Application.php";

// Provide the absolute or relative path to your wurfl-config.xml
$wurflConfigFile = "wurfl-config.xml";

// Create WURFL Configuration from an XML config file
$wurflConfig = new WURFL_Configuration_XmlConfig($wurflConfigFile);

// Create a WURFL Manager Factory from the WURFL Configuration
$wurflManagerFactory = new WURFL_WURFLManagerFactory($wurflConfig);

// Create a WURFL Manager
$wurflManager = $wurflManagerFactory->create();

WURFL maintains the database in wurfl-config.xml file and which is created on the basis of the user agent of the different devices, by passing the argument of $_SERVER array to the WURFL API we can get the exact model, brand, device OS, resolution and other additional details. online doc available for getting different capability sets of the device.