Move existing Parms into Folder, paste Folder from another node

I am building kind of "archetype" node types, and adding my custom Parm Folders before the initial default Folders:


# group initial parms
PTG_initial = node_target.parmTemplateGroup()
PTG_new     = hou.ParmTemplateGroup()
entries     = PTG_initial.entries()

FOLDER_init = hou.FolderParmTemplate("folder", "Initial Parms")
FOLDER_init.setFolderType(hou.folderType.Tabs)

for entry in entries :
    FOLDER_init.addParmTemplate(entry)

PTG_new.append(FOLDER_init)
node_target.setParmTemplateGroup(PTG_new)


# copy parms
which_folder         = "JOB"
insert_before_folder = "Initial Parms"
node_src             = node_archetype
node_dst             = node_target
copypaste_ptg_folder( which_folder, insert_before_folder, node_src, node_dst )

Here is the copypaste_ptg_folder() definition:

def copypaste_ptg_folder( which_folder, insert_before_folder, node_src, node_dst ) :

    ptg_dst = node_dst.parmTemplateGroup()
    ptg_src = node_src.parmTemplateGroup()

    # remove target folder if already exists
    try:
        ptg_dst.remove(  ptg_dst.findFolder( which_folder )  )
        node_dst.setParmTemplateGroup(ptg_dst)
    except:
        pass
    
    # find and insert the folder, set PTG
    cargo    = ptg_src.findFolder( which_folder )
    location = ptg_dst.findFolder( insert_before_folder )
    ptg_dst.insertBefore( location , cargo )
    node_dst.setParmTemplateGroup(ptg_dst)

Here is the use of saveItemsToFile()

def copypaste_all_contents( container_src, container_dst ) :

    # copy contents

    temp_path = hou.getenv("wf_path") + "/temp/__archetype.temp"
    items     = container_src.allItems()
    container_src.saveItemsToFile(items, temp_path)

    # paste contents
    container_dst.loadItemsFromFile(temp_path)