From 84efd174e1c5e60bb9b0b7cac84e63932a43a7f8 Mon Sep 17 00:00:00 2001 From: Muthiah Annamalai Date: Sun, 27 Dec 2015 12:24:31 -0500 Subject: [PATCH] 1) Add URL Library hooks to Ezhil - readonly - 2) Partial resolution of issue #163 https://github.com/arcturusannamalai/Ezhil-Lang/issues/163; 3) Added unittest 4) update dir() to be called only on non-windows platforms --- ezhil/Interpreter.py | 7 +++++-- ezhil/ezhil_library.py | 35 +++++++++++++++++++++++++++++++++++ test_cases | 1 + tests/fact.n | 5 ++++- tests/urldemo.n | 23 +++++++++++++++++++++++ 5 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 ezhil/ezhil_library.py create mode 100644 tests/urldemo.n diff --git a/ezhil/Interpreter.py b/ezhil/Interpreter.py index c0412d9..abf2bea 100644 --- a/ezhil/Interpreter.py +++ b/ezhil/Interpreter.py @@ -2,8 +2,6 @@ ## (C) 2007, 2008, 2013, 2014, 2015 Muthiah Annamalai, ## Licensed under GPL Version 3 ## -## -## TODO: extract scanner and AST members into a module for sharing. from __future__ import print_function import argparse @@ -57,6 +55,7 @@ TransformSemanticAnalyzer, TransformConstantFolder from ezhil_serializer import SerializerXML +from ezhil_library import Load_URL_APIs def ezhil_version(): return 0.8 @@ -712,6 +711,10 @@ def install_builtins(self): self.builtin_map["update"]= BuiltinFunction(dict.update,"update",1) + # URL API's - load except in safe mode + if not self.SAFE_MODE: + Load_URL_APIs( self ) + # open-tamil API # get tamil letters self.add_builtin("get_tamil_letters",tamil.utf8.get_letters,nargin=1,ta_alias=u"தமிழ்_எழுத்துக்கள்") diff --git a/ezhil/ezhil_library.py b/ezhil/ezhil_library.py new file mode 100644 index 0000000..69e5fb3 --- /dev/null +++ b/ezhil/ezhil_library.py @@ -0,0 +1,35 @@ +## -*- coding: utf-8 -*- +## (C) 2007, 2008, 2013, 2014, 2015 Muthiah Annamalai, +## Licensed under GPL Version 3 +## +from __future__ import print_function +import sys +from runtime import BuiltinFunction + +PYTHON3 = (sys.version[0] == '3') + +if PYTHON3: + # For Python 3.0 and later + from urllib.request import urlopen +else: + # Fall back to Python 2's urllib2 + from urllib2 import urlopen + +def ezhil_urlopen(*args): + html = urlopen(*args) + return html + +def ezhil_urlread(html): + data = html.read() + if not PYTHON3: + return data.decode("utf-8") + return data + +def ezhil_urlclose(html): + html.close() + +def Load_URL_APIs(interpreter): + interpreter.add_builtin("urlopen",ezhil_urlopen,nargin=1,ta_alias="இணைய_இணைப்பு_திற") + interpreter.add_builtin("urlread",ezhil_urlread,nargin=1,ta_alias="இணைய_இணைப்பு_படி") + interpreter.add_builtin("urlclose",ezhil_urlclose,nargin=1,ta_alias="இணைய_இணைப்பு_மூடு") + \ No newline at end of file diff --git a/test_cases b/test_cases index 192efde..90367b3 100644 --- a/test_cases +++ b/test_cases @@ -123,3 +123,4 @@ partition_estimate.n envchecks.n ifparse.n foo.n +urldemo.n \ No newline at end of file diff --git a/tests/fact.n b/tests/fact.n index f67039b..52f3695 100644 --- a/tests/fact.n +++ b/tests/fact.n @@ -13,7 +13,10 @@ ans = fact ( ப + 4 ) பதிப்பி assert( ans == (1*2*3*4*5*6*7*8) ) profile("begin") -dir("fa") #list all ezhil builtin and user defined functions starting with 'fa' + +@( sys_platform() != "win32") ஆனால் + dir("fa") #list all ezhil builtin and user defined functions starting with 'fa' +முடி ans = fact ( ப - 4/2 ) பதிப்பி ans, assert( ans == 2 ) diff --git a/tests/urldemo.n b/tests/urldemo.n new file mode 100644 index 0000000..672f976 --- /dev/null +++ b/tests/urldemo.n @@ -0,0 +1,23 @@ +# (C) 2015, Ezhil Language Project +# (C) 2015 முத்தையா அண்ணாமலை +# இது ஒரு எழில் தமிழ் நிரலாக்க மொழி உதாரணம் + +# Networking Example: +# இங்கு 'urllib' Python library functions Ezhil language வழியாக +# பயன் படுத்துமாறு நிரல் எ.கா உள்ளது + +இணைய_முகவரி = "http://www.bing.com" +பிங்_தொடர்பு = urlopen( இணைய_முகவரி ) +அடங்கல் = urlread( பிங்_தொடர்பு ) + +@( sys_platform() != "win32") ஆனால் + பதிப்பி "பிங்_தொடர்பு அடங்கல் => கீழே" + பதிப்பி அடங்கல் +இல்லை + பதிப்பி "Test does not print on Windows" +முடி + +பதிப்பி "Size of contents = ",len(அடங்கல்) + +urlclose( பிங்_தொடர்பு ) +exit(0)