Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/galaxy/util/template.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author | shellac |
---|---|
date | Sat, 02 May 2020 07:14:21 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:26e78fe6e8c4 |
---|---|
1 """Entry point for the usage of Cheetah templating within Galaxy.""" | |
2 from __future__ import absolute_import | |
3 | |
4 import sys | |
5 import traceback | |
6 from lib2to3.refactor import RefactoringTool | |
7 | |
8 import packaging.version | |
9 from Cheetah.Compiler import Compiler | |
10 from Cheetah.NameMapper import NotFound | |
11 from Cheetah.Template import Template | |
12 from past.translation import myfixes | |
13 | |
14 from . import unicodify | |
15 | |
16 # Skip libpasteurize fixers, which make sure code is py2 and py3 compatible. | |
17 # This is not needed, we only translate code on py3. | |
18 if sys.version_info.major > 2: | |
19 myfixes = [f for f in myfixes if not f.startswith('libpasteurize')] | |
20 refactoring_tool = RefactoringTool(myfixes, {'print_function': True}) | |
21 else: | |
22 myfixes = refactoring_tool = None | |
23 | |
24 | |
25 class FixedModuleCodeCompiler(Compiler): | |
26 | |
27 module_code = None | |
28 | |
29 def getModuleCode(self): | |
30 self._moduleDef = self.module_code | |
31 return self._moduleDef | |
32 | |
33 | |
34 def create_compiler_class(module_code): | |
35 | |
36 class CustomCompilerClass(FixedModuleCodeCompiler): | |
37 pass | |
38 | |
39 setattr(CustomCompilerClass, 'module_code', module_code) | |
40 | |
41 return CustomCompilerClass | |
42 | |
43 | |
44 def fill_template(template_text, | |
45 context=None, | |
46 retry=10, | |
47 compiler_class=Compiler, | |
48 first_exception=None, | |
49 futurized=False, | |
50 python_template_version='3', | |
51 **kwargs): | |
52 """Fill a cheetah template out for specified context. | |
53 | |
54 If template_text is None, an exception will be thrown, if context | |
55 is None (the default) - keyword arguments to this function will be used | |
56 as the context. | |
57 """ | |
58 if template_text is None: | |
59 raise TypeError("Template text specified as None to fill_template.") | |
60 if not context: | |
61 context = kwargs | |
62 if isinstance(python_template_version, str): | |
63 python_template_version = packaging.version.parse(python_template_version) | |
64 klass = Template.compile(source=template_text, compilerClass=compiler_class) | |
65 t = klass(searchList=[context]) | |
66 try: | |
67 return unicodify(t) | |
68 except NotFound as e: | |
69 if first_exception is None: | |
70 first_exception = e | |
71 if refactoring_tool and python_template_version.release[0] < 3 and retry > 0: | |
72 tb = e.__traceback__ | |
73 last_stack = traceback.extract_tb(tb)[-1] | |
74 if last_stack.name == '<listcomp>': | |
75 # On python 3 list, dict and set comprehensions as well as generator expressions | |
76 # have their own local scope, which prevents accessing frame variables in cheetah. | |
77 # We can work around this by replacing `$var` with `var`, but we only do this for | |
78 # list comprehensions, as this has never worked for dict or set comprehensions or | |
79 # generator expressions in Cheetah. | |
80 var_not_found = e.args[0].split("'")[1] | |
81 replace_str = 'VFFSL(SL,"%s",True)' % var_not_found | |
82 lineno = last_stack.lineno - 1 | |
83 module_code = t._CHEETAH_generatedModuleCode.splitlines() | |
84 module_code[lineno] = module_code[lineno].replace(replace_str, var_not_found) | |
85 module_code = "\n".join(module_code) | |
86 compiler_class = create_compiler_class(module_code) | |
87 return fill_template(template_text=template_text, | |
88 context=context, | |
89 retry=retry - 1, | |
90 compiler_class=compiler_class, | |
91 first_exception=first_exception, | |
92 python_template_version=python_template_version, | |
93 ) | |
94 raise first_exception or e | |
95 except Exception as e: | |
96 if first_exception is None: | |
97 first_exception = e | |
98 if sys.version_info.major > 2 and python_template_version.release[0] < 3 and not futurized: | |
99 # Possibly an error caused by attempting to run python 2 | |
100 # template code on python 3. Run the generated module code | |
101 # through futurize and hope for the best. | |
102 module_code = t._CHEETAH_generatedModuleCode | |
103 module_code = futurize_preprocessor(module_code) | |
104 compiler_class = create_compiler_class(module_code) | |
105 return fill_template(template_text=template_text, | |
106 context=context, | |
107 retry=retry, | |
108 compiler_class=compiler_class, | |
109 first_exception=first_exception, | |
110 futurized=True, | |
111 python_template_version=python_template_version, | |
112 ) | |
113 raise first_exception or e | |
114 | |
115 | |
116 def futurize_preprocessor(source): | |
117 source = str(refactoring_tool.refactor_string(source, name='auto_translate_cheetah')) | |
118 # libfuturize.fixes.fix_unicode_keep_u' breaks from Cheetah.compat import unicode | |
119 source = source.replace('from Cheetah.compat import str', 'from Cheetah.compat import unicode') | |
120 return source |