import yaml


def replace_pcell_params_from_yaml(pcell_script, params_yaml_path):
    """Replace PCell params from YAML file
    
    Parameters
    ----------
    pcell_script : str
        Python PCell script.
    params_yaml_path : str
        Absolute path of design params YAML.

    Returns
    -------
    pcell_script : str
        PCell script with tags replaced with params in YAML file

    """
    try:
        with open(params_yaml_path, 'r') as f:
            params_dict = yaml.load(f, Loader=yaml.FullLoader)
    except FileNotFoundError:
        print("YAML file does not exist... Cannot obtain design params... Passing...")
        return pcell_script
    except yaml.YAMLError:
        print("Error in YAML file... Cannot obtain design params... Passing...")
        return pcell_script
    
    design_params = params_dict.get('design-params')
    if design_params:
        for param_name, param_val in design_params.items():
            pcell_script = pcell_script.replace('{%s}' % param_name, str(param_val))
    
    simulation_params = params_dict.get('simulation-params')
    if simulation_params:
        for param_name, param_val in simulation_params.items():
            pcell_script = pcell_script.replace('{%s}' % param_name, str(param_val))
            
    compact_model_params = params_dict.get('compact-model')
    if compact_model_params:
        for param_name, param_val in compact_model_params.items():
            pcell_script = pcell_script.replace('{cm-%s}' % param_name, str(param_val))
        
    return pcell_script