1634 return path.replace(".", "/") + ext |
1634 return path.replace(".", "/") + ext |
1635 |
1635 |
1636 |
1636 |
1637 class AppTranslator(object): |
1637 class AppTranslator(object): |
1638 |
1638 |
1639 def __init__(self, library_dirs=[], parser=None, dynamic=False, |
1639 def __init__(self, library_dirs=None, parser=None, dynamic=False, |
1640 optimize=False, verbose=True): |
1640 optimize=False, verbose=True): |
1641 self.extension = ".py" |
1641 self.extension = ".py" |
1642 self.optimize = optimize |
1642 self.optimize = optimize |
1643 self.library_modules = [] |
1643 self.library_modules = [] |
1644 self.overrides = {} |
1644 self.overrides = {} |
|
1645 library_dirs = [] if library_dirs is None else library_dirs |
1645 self.library_dirs = path + library_dirs |
1646 self.library_dirs = path + library_dirs |
1646 self.dynamic = dynamic |
1647 self.dynamic = dynamic |
1647 self.verbose = verbose |
1648 self.verbose = verbose |
1648 |
1649 |
1649 if not parser: |
1650 if not parser: |
1673 return full_file_name |
1674 return full_file_name |
1674 |
1675 |
1675 raise Exception("file not found: " + file_name) |
1676 raise Exception("file not found: " + file_name) |
1676 |
1677 |
1677 def _translate(self, module_name, is_app=True, debug=False, |
1678 def _translate(self, module_name, is_app=True, debug=False, |
1678 imported_js=set()): |
1679 imported_js=None): |
1679 if module_name not in self.library_modules: |
1680 if module_name not in self.library_modules: |
1680 self.library_modules.append(module_name) |
1681 self.library_modules.append(module_name) |
1681 |
1682 |
1682 file_name = self.findFile(module_name + self.extension) |
1683 file_name = self.findFile(module_name + self.extension) |
1683 |
1684 |
1699 t = Translator(mn, module_name, module_name, |
1700 t = Translator(mn, module_name, module_name, |
1700 src, debug, mod, output, self.dynamic, self.optimize, |
1701 src, debug, mod, output, self.dynamic, self.optimize, |
1701 self.findFile) |
1702 self.findFile) |
1702 |
1703 |
1703 module_str = output.getvalue() |
1704 module_str = output.getvalue() |
|
1705 if imported_js is None: |
|
1706 imported_js = set() |
1704 imported_js.update(set(t.imported_js)) |
1707 imported_js.update(set(t.imported_js)) |
1705 imported_modules_str = "" |
1708 imported_modules_str = "" |
1706 for module in t.imported_modules: |
1709 for module in t.imported_modules: |
1707 if module not in self.library_modules: |
1710 if module not in self.library_modules: |
1708 self.library_modules.append(module) |
1711 self.library_modules.append(module) |
1711 # module, False, debug=debug, imported_js=imported_js) |
1714 # module, False, debug=debug, imported_js=imported_js) |
1712 |
1715 |
1713 return imported_modules_str + module_str |
1716 return imported_modules_str + module_str |
1714 |
1717 |
1715 def translate(self, module_name, is_app=True, debug=False, |
1718 def translate(self, module_name, is_app=True, debug=False, |
1716 library_modules=[]): |
1719 library_modules=None): |
1717 app_code = cStringIO.StringIO() |
1720 app_code = cStringIO.StringIO() |
1718 lib_code = cStringIO.StringIO() |
1721 lib_code = cStringIO.StringIO() |
1719 imported_js = set() |
1722 imported_js = set() |
1720 self.library_modules = [] |
1723 self.library_modules = [] |
1721 self.overrides = {} |
1724 self.overrides = {} |
1722 for library in library_modules: |
1725 if library_modules is not None: |
1723 if library.endswith(".js"): |
1726 for library in library_modules: |
1724 imported_js.add(library) |
1727 if library.endswith(".js"): |
1725 continue |
1728 imported_js.add(library) |
1726 self.library_modules.append(library) |
1729 continue |
1727 if self.verbose: |
1730 self.library_modules.append(library) |
1728 print('Including LIB', library) |
1731 if self.verbose: |
1729 print('\n//\n// BEGIN LIB '+library+'\n//\n', file=lib_code) |
1732 print('Including LIB', library) |
1730 print(self._translate(library, False, debug=debug, imported_js=imported_js), |
1733 print('\n//\n// BEGIN LIB '+library+'\n//\n', file=lib_code) |
1731 file=lib_code) |
1734 print(self._translate(library, False, debug=debug, imported_js=imported_js), |
1732 |
1735 file=lib_code) |
1733 print("/* initialize static library */", file=lib_code) |
1736 |
1734 print("%s%s();\n" % (UU, library), file=lib_code) |
1737 print("/* initialize static library */", file=lib_code) |
1735 |
1738 print("%s%s();\n" % (UU, library), file=lib_code) |
1736 print('\n//\n// END LIB '+library+'\n//\n', file=lib_code) |
1739 |
|
1740 print('\n//\n// END LIB '+library+'\n//\n', file=lib_code) |
1737 if module_name: |
1741 if module_name: |
1738 print(self._translate(module_name, is_app, debug=debug, imported_js=imported_js), |
1742 print(self._translate(module_name, is_app, debug=debug, imported_js=imported_js), |
1739 file=app_code) |
1743 file=app_code) |
1740 for js in imported_js: |
1744 for js in imported_js: |
1741 path = self.findFile(js) |
1745 path = self.findFile(js) |