Source code for pydolphinscheduler.tasks.func_wrap
# 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."""Task function wrapper allows using decorator to create a task."""importfunctoolsimportinspectimporttypesfrompathlibimportPathfromstmdency.extractorimportExtractorfrompydolphinscheduler.exceptionsimportPyDSParamExceptionfrompydolphinscheduler.tasks.pythonimportPython
[docs]def_exists_other_decorator(func:types.FunctionType)->None:"""Check if the function has other decorators except @task. :param func: The function which wraps by decorator ``@task``. """lines=inspect.getsourcelines(func)[0]forlineinlines:strip_line=line.strip()ifstrip_line.startswith("@")andnotstrip_line=="@task":raisePyDSParamException("Do no support other decorators for function ``task`` decorator.")
[docs]deftask(func:types.FunctionType):"""Decorate which covert Python functions into pydolphinscheduler task. :param func: The function which wraps by decorator ``@task``. """@functools.wraps(func)defwrapper(*args,**kwargs):_exists_other_decorator(func)loc=func.__code__.co_filenameextractor=Extractor(Path(loc).open("r").read())stm=extractor.get_code(func.__name__)returnPython(name=kwargs.get("name",func.__name__),definition=f"{stm}{func.__name__}()",*args,**kwargs,)returnwrapper