We are in need of using Python for the purpose of automating the build tasks of JavaScript. I need to use very minimal JavaScript code in my build process, so I have to use the “ng-annotate” in the building process itself. With this, we will be in a position to write all the angular code I need without having to worry about dependency injection. However, we will still take advantage of this feature. However, currently, we have no Python module which we can run our code through this feature.

PyExecJs

The question is, how can cross-language modules be implemented? The Python module PyExecJs can be used for this purpose. With this module, we are able to write JavaScript functions which can be called from Python code. The module also enables us to choose the JavaScript which we need to use.

Begin by executing the command given below:

$ pip install pyexecjs

We want to start our module file by importation of the “get” function from the module execjs, and this will make it possible for us to request for the specific JavaScript engine which we need to use. This is shown in the code given below:

from execjs import get 
import os 
runtime = get('Node')

At this point, we will have a runtime engine which we can use, so we can work with it. What we want to ensure is that after its installation, we can be able to access the node module “ng-annotate.”

To do it, just create a file named “Package.json,” and then add the following code to it:

{     
   "dependencies":{         
      "ng-annotate":"*"     
   } 
}

Lastly, we should add instruction which will be tasked with running “npm install.”
Then in the JavaScript file, we create a function which our module will use, and this should be in a position to access the folder “node_modules” which has been created by npm install.
The JavaScript just has to be wrapped in a call for “runtime.compile,” and we will be in a position to access the inside functions from the Python code. The code given below shows how this can be done:

cont = runtime.compile('''module.paths.push('%s');
ng = require('ng-annotate');     
function annotate(src,cfg){ return ng(src,cfg); } ''' % 
os.path.join(os.path.dirname(__file__),'node_modules'))

At this point, the function “annotate” can be called from within our Python code. This can be done using “context.call(‘annotate’,*args)” and we will get the result that we could get with JavaScript. To make it a standard Python code, the function can be modified to the following:

def ng_annotate(src,cfg=None):     
if cfg is None:         
   cfg = dict(add=True)        
return context.call('annotate',src,cfg)

At this point, we can utilize “from ng_annotate import ng_annotate” so as to use it from our Python code. We will just have to call the function, but we have to first make it to be a very convenient command line tool as shown below:

import sys 
def main():     
    print ng_annotate(open(sys.argv[-1],'r').read()) 
if __name__ == "__main__":     main()

The whole of the Python file “ng_annotate.py” should be as shown below:

import os 
import sys 
from execjs import get 
runtime = get('Node') 
context = runtime.compile('''
   module.paths.push('%s');     
   var ng = require('ng-annotate');     
   function annotate(source,cfg){         
      return ng(source,cfg);     
   } ''' % 
os.path.join(os.path.dirname(__file__),'node_modules')) 
def ng_annotate(src,cfg=None):     
   if cfg is None:         
      cfg = dict(add=True)     
   return context.call('annotate',src,cfg) 
def main():     
   print ng_annotate(open(sys.argv[-1],'r').read())   

if __name__ == "__main__": main()
Anuncios

Write your comment