11import ast
2- import jinja2
32
43from collections .abc import Mapping
54from datetime import date , datetime , timedelta
1514)
1615
1716from polyaxon ._flow import ParamSpec
18- from polyaxon ._polyaxonfile .specs .libs .engine import get_engine
17+ from polyaxon ._polyaxonfile .specs .libs .engine import render_template
1918from polyaxon ._polyaxonfile .specs .sections import Sections
20- from polyaxon .exceptions import PolyaxonSchemaError
2119
2220try :
2321 import numpy as np
2826class PolyaxonfileParser :
2927 """Parses the Polyaxonfile."""
3028
31- engine = get_engine ()
32-
3329 @staticmethod
3430 def _get_section_data (section_data ):
3531 if hasattr (section_data , "to_dict" ):
@@ -193,19 +189,6 @@ def parse_io(
193189 @classmethod
194190 def parse_expression ( # pylint:disable=too-many-branches
195191 cls , expression , params : Dict , check_operators : bool = False
196- ):
197- try :
198- return cls ._parse_expression (expression , params , check_operators )
199- except jinja2 .exceptions .TemplateError as e :
200- raise PolyaxonSchemaError (
201- "Encountered a problem parsing the template, "
202- "please make sure your variables are resolvable. "
203- "Error: {}" .format (repr (e ))
204- )
205-
206- @classmethod
207- def _parse_expression ( # pylint:disable=too-many-branches
208- cls , expression , params : Dict , check_operators : bool = False
209192 ):
210193 if isinstance (expression , (int , float , complex , type (None ))):
211194 return expression
@@ -227,7 +210,7 @@ def _parse_expression( # pylint:disable=too-many-branches
227210 if len (expression ) == 1 :
228211 old_key , value = list (expression .items ())[0 ]
229212 # always parse the keys, they must be base object or evaluate to base objects
230- key = cls ._parse_expression (old_key , params )
213+ key = cls .parse_expression (old_key , params )
231214 if check_operators and cls .is_operator (key ):
232215 return cls ._parse_operator ({key : value }, params )
233216 else :
@@ -236,7 +219,7 @@ def _parse_expression( # pylint:disable=too-many-branches
236219 new_expression = {}
237220 for k , v in expression .items ():
238221 new_expression .update (
239- cls ._parse_expression ({k : v }, params , check_operators )
222+ cls .parse_expression ({k : v }, params , check_operators )
240223 )
241224 return new_expression
242225
@@ -253,14 +236,7 @@ def _parse_expression( # pylint:disable=too-many-branches
253236
254237 @classmethod
255238 def _evaluate_expression (cls , expression , params , check_operators ):
256- try :
257- result = cls .engine .from_string (expression ).render (** params )
258- except (ValueError , TypeError ) as e :
259- raise PolyaxonSchemaError (
260- "Encountered a problem parsing the template, "
261- "please make sure your variables are resolvable. "
262- "Error: {}" .format (repr (e ))
263- )
239+ result = render_template (expression , params )
264240 if result == expression :
265241 try :
266242 return ast .literal_eval (result )
0 commit comments