PageRenderTime 606ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/qpy/compile.py

https://bitbucket.org/pfw/durusworks
Python | 100 lines | 74 code | 9 blank | 17 comment | 14 complexity | 282874d6fc8ecbe241e1a6cc6f785ed7 MD5 | raw file
  1. """
  2. open/DurusWorks/qpy/compile.py
  3. """
  4. from os import stat, listdir
  5. from os.path import join
  6. import py_compile
  7. import re
  8. import symbol
  9. import sys
  10. import token
  11. if sys.version < "3":
  12. import __builtin__ as builtins
  13. else:
  14. import builtins
  15. if sys.platform.startswith('java'):
  16. from qpy.compile_jython import PYC, compile, get_parse_tree
  17. else:
  18. from qpy.compile_cpython import PYC, compile, get_parse_tree
  19. get_parse_tree # for checker
  20. _annotation_re = re.compile(
  21. r"^(?P<indent>[ \t]*)def(?:[ \t]+)"
  22. r"(?P<name>[a-zA-Z_][a-zA-Z_0-9]*)"
  23. r"(?:[ \t]*:[ \t]*)(?P<type>xml|str)(?:[ \t]*)"
  24. r"(?:[ \t]*[\(\\])",
  25. re.MULTILINE|re.VERBOSE)
  26. _template_re = re.compile(
  27. r"^(?P<indent>[ \t]*) def (?:[ \t]+)"
  28. r" (?P<name>[a-zA-Z_][a-zA-Z_0-9]*)"
  29. r" (?:[ \t]*) \[(?P<type>plain|html)\] (?:[ \t]*)"
  30. r" (?:[ \t]*[\(\\])",
  31. re.MULTILINE|re.VERBOSE)
  32. def translate_tokens(buf):
  33. """
  34. def f:xml( ... ) -> def f__xml_template__(...):
  35. def f:str( ... ) -> def f__str_template__(...):
  36. and, for backward compatibility,
  37. def foo [html] (...): -> def foo__xml_template__(...):
  38. def foo [plain] (...): -> def foo__str_template__(...):
  39. """
  40. def replacement(match):
  41. if match.group('type') in ('xml', 'html'):
  42. template_type = 'xml'
  43. elif match.group('type') in ('str', 'plain'):
  44. template_type = 'str'
  45. return '%sdef %s__%s_template__(' % (match.group('indent'),
  46. match.group('name'),
  47. template_type)
  48. translated = _annotation_re.sub(replacement, buf)
  49. translated = _template_re.sub(replacement, translated)
  50. return translated
  51. def timestamp(filename):
  52. try:
  53. s = stat(filename)
  54. except OSError:
  55. return None
  56. return s.st_mtime
  57. def compile_qpy_file(source_name):
  58. """(source_name:str)
  59. Compile the given filename if it is a .qpy file and if it does not already
  60. have an up-to-date .pyc file.
  61. """
  62. if source_name[-4:] == '.qpy':
  63. compile_time = timestamp(source_name[:-4] + PYC)
  64. if compile_time is not None:
  65. source_time = timestamp(source_name)
  66. if compile_time >= source_time:
  67. return # already up-to-date
  68. try:
  69. compile(source_name)
  70. except py_compile.PyCompileError:
  71. exception = sys.exc_info()[1]
  72. exception_msg = getattr(exception, 'msg') or ''
  73. exception_file = getattr(exception, 'file') or ''
  74. if exception_msg and exception_file:
  75. exception.msg = ("\n\nCan't compile %s:\n" % exception_file) + exception_msg
  76. raise
  77. def compile_qpy_files(path):
  78. """(path:str)
  79. Compile the .qpy files in the given directory.
  80. """
  81. for name in listdir(path):
  82. if name[-4:] == '.qpy':
  83. compile_qpy_file(join(path, name))
  84. if __name__ == '__main__':
  85. import qpy.__main__