in source/utils.py [0:0]
def extract_keys_from_py_file(filename):
"""
Extracts variable names (keys) assigned in a Python file,
handling various assignment patterns.
Args:
filename: The path to the .py file.
Returns:
A list of variable names (keys).
"""
keys = []
try:
with open(filename, "r") as f:
tokens = tokenize.generate_tokens(f.readline)
prev_token = None
for token_type, token_string, _, _, _ in tokens:
# Check for assignment (=)
if token_type == tokenize.OP and token_string == "=":
# Go back to get the previous token (should be the variable name)
if prev_token and prev_token.type == tokenize.NAME:
# Add a check to skip if the variable name starts with "__" (dunder methods)
if not prev_token.string.startswith("__"):
keys.append(prev_token.string)
prev_token = tokenize.TokenInfo(token_type, token_string, _, _, _) # Store current token as previous
except FileNotFoundError:
print(f"Error: File not found - {filename}")
# Handle file not found error appropriately (e.g., raise an exception, log, etc.)
except Exception as e:
print(f"An unexpected error occurred: {e}")
# Handle other potential exceptions (e.g., log, re-raise, etc.)
return keys