in readme_generator.py [0:0]
def parse_sqlx(file_content: str, filename: str) -> dict:
logging.info(f"Parsing file: {filename}")
function_pattern = re.compile(
r"CREATE OR REPLACE (?:AGGREGATE )?FUNCTION\s+\$\{self\(\)\}\((.*?)\)\s+RETURNS\s+([^;]+?)(?=\s+(?:LANGUAGE|OPTIONS|$))",
re.DOTALL
)
description_pattern = re.compile(r"description\s*=\s*['\"]{3}(.*?)['\"]{3}", re.DOTALL)
# Extract function signature and return type
function_match = function_pattern.search(file_content)
if function_match:
function_signature = function_match.group(1).strip()
return_type = function_match.group(2).strip()
logging.debug(f"Function signature: {function_signature}")
logging.debug(f"Return type: {return_type}")
else:
function_signature = ""
return_type = "UNKNOWN"
logging.warning(f"No function signature or return type found in {filename}")
# Extract description
description_match = description_pattern.search(file_content)
description = description_match.group(1).strip() if description_match else "No description available"
description = re.compile(r'\n*For more info.*', re.M | re.S).sub('', description) # remove repetitive links
description = escape_markdown(description)
description = description.replace('\nParam', '\n* Param')
description = description.replace('\nDefault', '\n* Default')
description = description.replace('\nReturn', '\n* Return')
# Extract function arguments and their types
arg_list = []
for arg in re.split(r",\s*(?![^<>]*>)", function_signature): # Split by comma only if not within "<>"
arg_parts = arg.strip().split()
if len(arg_parts) >= 2: # Allow more than two parts for complex arguments
arg_list.append((arg_parts[0], " ".join(arg_parts[1:]))) # (arg_name, arg_type)
elif arg.strip(): # Ignore empty arguments
logging.warning(f"Unexpected argument format in {filename}: {arg}")
# Determine function type
function_type = "AGGREGATE" if "AGGREGATE FUNCTION" in file_content else "SCALAR"
return {
"name": filename[:-5], # Remove file extension .sqlx
"params": f"({', '.join([f'{arg[0]} {arg[1]}' for arg in arg_list])})",
"returns": return_type,
"description": description,
"type": function_type,
}