Source code for airflow.providers.cncf.kubernetes.python_kubernetes_script
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License."""Utilities for using the kubernetes decorator."""from__future__importannotationsimportosfromcollectionsimportdequeimportjinja2def_balance_parens(after_decorator):num_paren=1after_decorator=deque(after_decorator)after_decorator.popleft()whilenum_paren:current=after_decorator.popleft()ifcurrent=="(":num_paren+=1elifcurrent==")":num_paren-=1return"".join(after_decorator)
[docs]defremove_task_decorator(python_source:str,task_decorator_name:str)->str:""" Remove @task.kubernetes or similar as well as @setup and @teardown. :param python_source: python source code :param task_decorator_name: the task decorator name """def_remove_task_decorator(py_source,decorator_name):ifdecorator_namenotinpy_source:returnpython_sourcesplit=python_source.split(decorator_name)before_decorator,after_decorator=split[0],split[1]ifafter_decorator[0]=="(":after_decorator=_balance_parens(after_decorator)ifafter_decorator[0]=="\n":after_decorator=after_decorator[1:]returnbefore_decorator+after_decoratordecorators=["@setup","@teardown",task_decorator_name]fordecoratorindecorators:python_source=_remove_task_decorator(python_source,decorator)returnpython_source
[docs]defwrite_python_script(jinja_context:dict,filename:str,render_template_as_native_obj:bool=False,):""" Render the python script to a file to execute in the virtual environment. :param jinja_context: The jinja context variables to unpack and replace with its placeholders in the template file. :param filename: The name of the file to dump the rendered script to. :param render_template_as_native_obj: If ``True``, rendered Jinja template would be converted to a native Python object """template_loader=jinja2.FileSystemLoader(searchpath=os.path.dirname(__file__))template_env:jinja2.Environmentifrender_template_as_native_obj:template_env=jinja2.nativetypes.NativeEnvironment(loader=template_loader,undefined=jinja2.StrictUndefined)else:template_env=jinja2.Environment(loader=template_loader,undefined=jinja2.StrictUndefined)template=template_env.get_template("python_kubernetes_script.jinja2")template.stream(**jinja_context).dump(filename)