Conditions | 2 |
Total Lines | 31 |
Code Lines | 10 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import ast |
||
18 | def get_functions(page): |
||
19 | """ |
||
20 | Read a python script and extract functions and class methods |
||
21 | and add them to a list |
||
22 | ------ |
||
23 | examples: |
||
24 | |||
25 | @need |
||
26 | from fastest.constants import TestBodies |
||
27 | @end |
||
28 | |||
29 | @let |
||
30 | page = TestBodies.GET_FUNCTIONS_TEST_CASE_1 |
||
31 | page_with_syntax_error = TestBodies.PAGE_WITH_SYNTAX_ERRORS |
||
32 | @end |
||
33 | |||
34 | 1) get_functions(page) -> TestBodies.GET_FUNCTIONS_TEST_CASE_1_EXPECT |
||
35 | 2) get_functions(page_with_syntax_error) -> [] |
||
36 | ------ |
||
37 | :param page: str |
||
38 | :return: list |
||
39 | """ |
||
40 | try: |
||
41 | node = ast.parse(page) |
||
42 | functions = get_functions_from_node(node) |
||
43 | classes = [n for n in node.body if isinstance(n, ast.ClassDef)] |
||
44 | methods = [get_functions_from_node(class_) for class_ in classes] |
||
45 | return functions + methods |
||
46 | except SyntaxError as error: |
||
47 | logger.error(error) |
||
48 | return [] |
||
49 | |||
50 |