def test_should_raise_if_jinja_syntax_error(): """ Test jinja templating raise an MLVToolException if jinja syntax error """ docstring = '""" This is a docstring using conf: {{ a.my_data """' with pytest.raises(MlVToolException) as e: resolve_docstring(docstring, docstring_conf={}) assert isinstance(e.value.__cause__, TemplateSyntaxError)
def test_should_raise_if_template_variable_is_missing(missing_pattern): """ Test jinja templating raise an MLVToolException if a template variable is missing """ docstring = f'""" This is a docstring using conf: {missing_pattern} """' with pytest.raises(MlVToolException) as e: resolve_docstring(docstring, docstring_conf={}) assert isinstance(e.value.__cause__, UndefinedError)
def test_should_resolve_docstring(): """ Test jinja templating works with docstring using user configuration """ docstring = '""" This is a docstring using conf: {{ conf.my_data }} """' user_conf = {'my_data': 'Test Value'} assert resolve_docstring( docstring, user_conf) == '""" This is a docstring using conf: Test Value """'
def extract_docstring_from_file(input_path: str, docstring_conf: dict = None) -> DocstringInfo: """ Extract method docstring information (docstring, method_name, input_path) The provided python script must have one and only one method The extracted docstring is parsed and returned in docstring info """ logging.info(f'Extract docstring from "{input_path}".') try: with open(input_path, 'r') as fd: root = ast.parse(fd.read()) except FileNotFoundError as e: raise MlVToolException( f'Python input script {input_path} not found.') from e except SyntaxError as e: raise MlVToolException( f'Invalid python script format: {input_path}') from e for node in ast.walk(root): if isinstance(node, ast.FunctionDef): method_name = node.name docstring_str = ast.get_docstring(node) if docstring_conf: docstring_str = resolve_docstring(docstring_str, docstring_conf) docstring = dc_parse(docstring_str) break else: logging.error(f'Not method found in {input_path}') raise MlVToolException(f'Not method found in {input_path}') logging.debug( f'Docstring extracted from method {method_name}: {docstring_str}') docstring_info = DocstringInfo(method_name=method_name, docstring=docstring, repr=docstring_str, file_path=input_path) return docstring_info