import yaml
import logging
import tempfile
import hashlib
from pathlib import Path


logger = logging.getLogger(__name__)
package_name = "{{ package_name }}"


def list_package_tools(raise_error=False):
    """
    List the meta of all tools in the package.
    The key of meta dict is the module name of tools and value is the meta data of the tool.
    """
    # This function is auto generated by pf CLI, please do not modify manually.
    tools = {}
    meta_cache_file = Path(__file__).parent / "yamls" / "tools_meta.yaml"
    if meta_cache_file.exists():
        logger.debug(f"List tools meta from cache file {meta_cache_file.as_posix()}.")
        # Get tool meta from cache file.
        with open(meta_cache_file, "r") as f:
            tools = yaml.safe_load(f)
    else:
        from promptflow.client import PFClient

        pf_client = PFClient()
        tools = pf_client.tools._list_tools_in_package(package_name, raise_error=raise_error)
    return tools
