in experimental/piranha_playground/rule_inference/rule_application.py [0:0]
def refactor_snippet(source_code: str, language: str, rules: str) -> str:
"""
Refactors a code snippet based on the provided rules.
:param source_code: str: The source code to be refactored.
:param language: str: The language of the source code.
:param rules: str: The refactoring rules in a .toml format.
:return: str: The refactored code or error message.
:raises CodebaseRefactorerException: If the refactoring fails.
"""
try:
toml_dict = toml.loads(rules)
arguments = toml_dict.get("arguments", [{}])[0]
arguments = flatten_dict_list(arguments)
refactored_code, success = run_piranha_with_timeout(
source_code,
language,
RawRuleGraph.from_toml(toml_dict),
timeout=5,
**arguments,
)
return refactored_code
except multiprocessing.context.TimeoutError as e:
raise CodebaseRefactorerException(
"Piranha is likely in an infinite loop. Please check your rules."
) from e
except Exception as e:
raise CodebaseRefactorerException(str(e)) from e