JS-Python

From OLPC
Revision as of 15:59, 28 March 2008 by Manu (talk | contribs)
Jump to: navigation, search

Objective

With the increased use of Open Source software development in the field of programming, it is no wonder that we have witnessed a greater influx of better and more efficient platforms for implementing our ideas. Open source development enables programmers to have more exposure and interaction with programming communities and to imbibe the best ideas in order to come up with an eclectic program, which is more efficient in all possible ways.

The aim of this project is to integrate two highly useful platforms, Python and JavaScript in order to form an extremely powerful tool, via which it will be possible to use JavaScript functions on Python platforms. The functionality of this project is apparent in the fact that both Python and JavaScript have their own numerous advantages, but at the same time they also have their limitations.

Python forms a great tool for programming; it does not have the inbuilt features to aid in creation, packaging and distribution of various dynamic web based elements which are available in the web. These features include use of forms for data submission, use of various other web elements to provide users with an enriching experience, et al. These have become essential features of any application now. However, Python does not allow programmers to enjoy these.

On the other hand, JavaScript gives a great platform to provide users with the perfect web experience, including some major advantages like cross-browser support, validating data on the client, and being able to create more sophisticated user interfaces. JavaScript effects are also much faster to download than some other front-end technologies like Flash and Java applets. Nor do users need to download a plug-in before they can view your JavaScript. Other advantages include the fact that no extra tools are required to write JavaScript, any plain text or HTML editor will do. There is a thriving and supportive online community of JavaScript developers and information resources. However, JavaScript is not an independent language. It needs a program to act as a parasite on. JavaScript is included in HTML and PHP files to provide the web interface. But what about its functionality in open source development?

This is the question that we have to ask when we use Python to build various applications, wherein the user interface and the experience of the user forms a very important component. XPCOM or Cross Platform Component Object Model was introduced for this purpose. The goal of XPCOM is to allow different pieces of software to be developed and built independently of one another. In order to allow interoperability between components within an application, XPCOM separates the implementation of a component from the interface. XPCOM also provides several tools and libraries that enable the loading and manipulation of these components, services that help the developer write modular cross-platform code, and versioning support, so that components can be replaced or upgraded without breaking or having to recreate the application.

XPCOM has various components to it. PyXPCOM allows for communication between Python and XPCOM, such that a Python application can access XPCOM objects, and XPCOM can access any Python class that implements an XPCOM interface. With PyXPCOM, a developer can talk to XPCOM from a Python application. Similarly, XPConnect forms the JavaScript-XPCOM Bridge. But these interfaces create additional and complex programs. When we are faced with situations involving updating or memory retrieval, it would involve using a Javascript function on a Python interface. The reason why PyXPCOM is useful is because it allows us to build GUIs using Mozilla as the rendering engine but the application logic using Python and Perl. It is generally much quicker to build applications with Python and Perl than it is with C/C++ . Enabling this interface will allow a new breed of applications to be built that can utilize the Web for network transport and HTML (and XUL) for building the interface, but without relying on clumsy CGI/Servlets trying to hack a real user experience with just HTML. That is the whole point of XPCOM.

In such a situation, we can develop an inter browser functionality wrapping, which would enable users to have a JavaScript experience in Python interfaces directly, without having to depend on external programs. When handling applications such as Spreadsheet, Journal, Sharing, et al, the tasks would involve saving. While the applications themselves can be built on Python based programs such as Sugar, the functions such as Saving can be easily performed using JavaScript. Instead of depending completely of an external interface such as XPCOM, we can make an inbuilt function which mobilizes the inclusion of JavaScript into Python or vice versa.


Creating a Hello World Application

To create a Hello Woorld Application in Javascript, for Mozilla using XPCOM, we rewrite certain components for the extension we want to create. The technique we use moves the parts common to Javascript XPCOM components (factory, registration, etc.) to a single place that can be left alone the ones related to XPCOM bookkeeping (class ID, contract ID, etc.) to another that can be customized, and the code that actually does something to yet another place (xpcom.js) which is where the 99% of the writing is done.If an init() function is provided in xpcom.js, it will be run each time an instance is created (or, in the case of a service, the first time a reference is acquired).

STEP 1:

We prepare the skeleton for an extension. The Firefox extension wizard can be used for the same. This consists of a folder containing

1.Components folder

2.Locale Folder

3.Skin Folder

4.Content Folder

5.Chrome.manifest

6.Config

7.install.rdf


STEP 2: In the components\ directory, we create the Interface for our extension, i.e. MyComponents.idl.


STEP 3: Similarly, in the components\ directory, we create the JAVASCRIPT FILE for our extension, i.e. MyComponents.js.


STEP 4: Now we can generate a UUID for this extension, and insert it in the respective places in both MyComponent.js and MyComponent.idl.


STEP 5: The next step is, compiling the .idl file and converting it into a typelib file, i.e. .xpt file. This is done using the xpidl.exe program available in the "gecko-sdk" interface.


STEP 6: In the content\ directory, we create an xpcom.js file. This file does the function of a Javascript function, calling the Hello World application, using the XPCOM. We also create a test.xul file, which is basically our test file, who's entry is made into the Chrome registry.


STEP 7: Now we can register the extension, either by packaging and installing it or by creating a pointer to its directory. Then we can start Firefox and open chrome://myextension/content/test.xul. This runs the extension.


Project Team

Manusheel Gupta (manu@laptop.org), K.S. Preeti (kspreeti.13@gmail.com), Luke Closs.