svgui/pyjs/pyjs.py
changeset 2459 21164625b393
parent 2450 5024c19ca8f0
equal deleted inserted replaced
2458:2a70d5240300 2459:21164625b393
    11 # distributed under the License is distributed on an "AS IS" BASIS,
    11 # distributed under the License is distributed on an "AS IS" BASIS,
    12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13 # See the License for the specific language governing permissions and
    13 # See the License for the specific language governing permissions and
    14 # limitations under the License.
    14 # limitations under the License.
    15 #
    15 #
    16 # pylint: disable=no-absolute-import
    16 # pylint: disable=no-absolute-import,bad-python3-import
    17 
    17 
    18 from __future__ import print_function
    18 from __future__ import print_function
    19 import sys
    19 import sys
    20 from types import StringType
       
    21 import compiler
    20 import compiler
    22 from compiler import ast
    21 from compiler import ast
    23 import os
    22 import os
    24 import copy
    23 import copy
    25 import cStringIO
    24 from builtins import str as text
       
    25 from past.builtins import basestring
       
    26 from six.moves import cStringIO
    26 
    27 
    27 # the standard location for builtins (e.g. pyjslib) can be
    28 # the standard location for builtins (e.g. pyjslib) can be
    28 # over-ridden by changing this.  it defaults to sys.prefix
    29 # over-ridden by changing this.  it defaults to sys.prefix
    29 # so that on a system-wide install of pyjamas the builtins
    30 # so that on a system-wide install of pyjamas the builtins
    30 # can be found in e.g. {sys.prefix}/share/pyjamas
    31 # can be found in e.g. {sys.prefix}/share/pyjamas
  1357             return str(node.value)
  1358             return str(node.value)
  1358         elif isinstance(node.value, float):
  1359         elif isinstance(node.value, float):
  1359             return str(node.value)
  1360             return str(node.value)
  1360         elif isinstance(node.value, basestring):
  1361         elif isinstance(node.value, basestring):
  1361             v = node.value
  1362             v = node.value
  1362             if isinstance(node.value, unicode):
  1363             if isinstance(node.value, text):
  1363                 v = v.encode('utf-8')
  1364                 v = v.encode('utf-8')
  1364             return "String('%s')" % escapejs(v)
  1365             return "String('%s')" % escapejs(v)
  1365         elif node.value is None:
  1366         elif node.value is None:
  1366             return "null"
  1367             return "null"
  1367         else:
  1368         else:
  1384 
  1385 
  1385     def _mul(self, node, current_klass):
  1386     def _mul(self, node, current_klass):
  1386         return self.expr(node.left, current_klass) + " * " + self.expr(node.right, current_klass)
  1387         return self.expr(node.left, current_klass) + " * " + self.expr(node.right, current_klass)
  1387 
  1388 
  1388     def _mod(self, node, current_klass):
  1389     def _mod(self, node, current_klass):
  1389         if isinstance(node.left, ast.Const) and isinstance(node.left.value, StringType):
  1390         if isinstance(node.left, ast.Const) and isinstance(node.left.value, str):
  1390             self.imported_js.add("sprintf.js")  # Include the sprintf functionality if it is used
  1391             self.imported_js.add("sprintf.js")  # Include the sprintf functionality if it is used
  1391             return "sprintf("+self.expr(node.left, current_klass) + ", " + self.expr(node.right, current_klass)+")"
  1392             return "sprintf("+self.expr(node.left, current_klass) + ", " + self.expr(node.right, current_klass)+")"
  1392         return self.expr(node.left, current_klass) + " % " + self.expr(node.right, current_klass)
  1393         return self.expr(node.left, current_klass) + " % " + self.expr(node.right, current_klass)
  1393 
  1394 
  1394     def _invert(self, node, current_klass):
  1395     def _invert(self, node, current_klass):
  1441     def _lambda(self, node, current_klass):
  1442     def _lambda(self, node, current_klass):
  1442         if node.varargs:
  1443         if node.varargs:
  1443             raise TranslationError("varargs are not supported in Lambdas", node)
  1444             raise TranslationError("varargs are not supported in Lambdas", node)
  1444         if node.kwargs:
  1445         if node.kwargs:
  1445             raise TranslationError("kwargs are not supported in Lambdas", node)
  1446             raise TranslationError("kwargs are not supported in Lambdas", node)
  1446         res = cStringIO.StringIO()
  1447         res = cStringIO()
  1447         arg_names = list(node.argnames)
  1448         arg_names = list(node.argnames)
  1448         function_args = ", ".join(arg_names)
  1449         function_args = ", ".join(arg_names)
  1449         for child in node.getChildNodes():
  1450         for child in node.getChildNodes():
  1450             expr = self.expr(child, None)
  1451             expr = self.expr(child, None)
  1451         print("function (%s){" % function_args, file=res)
  1452         print("function (%s){" % function_args, file=res)
  1529         else:
  1530         else:
  1530             raise TranslationError("unsupported type (in expr)", node)
  1531             raise TranslationError("unsupported type (in expr)", node)
  1531 
  1532 
  1532 
  1533 
  1533 def translate(file_name, module_name, debug=False):
  1534 def translate(file_name, module_name, debug=False):
  1534     f = file(file_name, "r")
  1535     f = open(file_name, "r")
  1535     src = f.read()
  1536     src = f.read()
  1536     f.close()
  1537     f.close()
  1537     output = cStringIO.StringIO()
  1538     output = cStringIO()
  1538     mod = compiler.parseFile(file_name)
  1539     mod = compiler.parseFile(file_name)
  1539     Translator(module_name, module_name, module_name, src, debug, mod, output)
  1540     Translator(module_name, module_name, module_name, src, debug, mod, output)
  1540     return output.getvalue()
  1541     return output.getvalue()
  1541 
  1542 
  1542 
  1543 
  1681         if module_name not in self.library_modules:
  1682         if module_name not in self.library_modules:
  1682             self.library_modules.append(module_name)
  1683             self.library_modules.append(module_name)
  1683 
  1684 
  1684         file_name = self.findFile(module_name + self.extension)
  1685         file_name = self.findFile(module_name + self.extension)
  1685 
  1686 
  1686         output = cStringIO.StringIO()
  1687         output = cStringIO()
  1687 
  1688 
  1688         f = file(file_name, "r")
  1689         f = open(file_name, "r")
  1689         src = f.read()
  1690         src = f.read()
  1690         f.close()
  1691         f.close()
  1691 
  1692 
  1692         mod, override = self.parser.parseModule(module_name, file_name)
  1693         mod, override = self.parser.parseModule(module_name, file_name)
  1693         if override:
  1694         if override:
  1716 
  1717 
  1717         return imported_modules_str + module_str
  1718         return imported_modules_str + module_str
  1718 
  1719 
  1719     def translate(self, module_name, is_app=True, debug=False,
  1720     def translate(self, module_name, is_app=True, debug=False,
  1720                   library_modules=None):
  1721                   library_modules=None):
  1721         app_code = cStringIO.StringIO()
  1722         app_code = cStringIO()
  1722         lib_code = cStringIO.StringIO()
  1723         lib_code = cStringIO()
  1723         imported_js = set()
  1724         imported_js = set()
  1724         self.library_modules = []
  1725         self.library_modules = []
  1725         self.overrides = {}
  1726         self.overrides = {}
  1726         if library_modules is not None:
  1727         if library_modules is not None:
  1727             for library in library_modules:
  1728             for library in library_modules:
  1746             path = self.findFile(js)
  1747             path = self.findFile(js)
  1747             if os.path.isfile(path):
  1748             if os.path.isfile(path):
  1748                 if self.verbose:
  1749                 if self.verbose:
  1749                     print('Including JS', js)
  1750                     print('Including JS', js)
  1750                 print('\n//\n// BEGIN JS '+js+'\n//\n', file=lib_code)
  1751                 print('\n//\n// BEGIN JS '+js+'\n//\n', file=lib_code)
  1751                 print(file(path).read(), file=lib_code)
  1752                 print(open(path).read(), file=lib_code)
  1752                 print('\n//\n// END JS '+js+'\n//\n', file=lib_code)
  1753                 print('\n//\n// END JS '+js+'\n//\n', file=lib_code)
  1753             else:
  1754             else:
  1754                 print('Warning: Unable to find imported javascript:', js, file=sys.stderr)
  1755                 print('Warning: Unable to find imported javascript:', js, file=sys.stderr)
  1755         return lib_code.getvalue(), app_code.getvalue()
  1756         return lib_code.getvalue(), app_code.getvalue()
  1756 
  1757