-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.py
35 lines (27 loc) · 1.05 KB
/
misc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import ast
import os
# Specify the filename
FILENAME = "eda_module.py"
def list_functions(filename):
"""
This function takes a filename as input and returns a list of all function names in the file.
Parameters:
filename (str): The name of the file to parse.
Returns:
list: A list of function names.
"""
# Validate the filename to prevent path injection
if not os.path.basename(filename) == filename or not os.path.isfile(filename):
raise ValueError("Invalid filename")
# Open the file in read mode
with open(filename, "r") as source_code:
# Parse the source code into an AST
tree = ast.parse(source_code.read())
# Use a list comprehension to get all function names
funcs = [node.name for node in ast.walk(tree) if isinstance(node, ast.FunctionDef)]
return funcs
# Get the list of function names
functions = list_functions(FILENAME)
# Print each function name in the format: "from .eda_module import {function}"
for function in functions:
print(f"from .eda_module import {function}")