<commit_before>import synapseclient
from synapseclient import File, Project, Folder, Table, Schema, Link, Wiki, Activity, exceptions
import time
from synapseclient.exceptions import *
import tempfile
import re
############################################################
##                 Copy Functions                         ##
############################################################

def copy(syn, entity, destinationId=None, copyWikiPage=True, **kwargs):
    """
    - This function will assist users in copying entities (Tables, Links, Files, Folders, Projects),
      and will recursively copy everything in directories.
    - A Mapping of the old entities to the new entities will be created and all the wikis of each entity
      will also be copied over and links to synapse Ids will be updated.

    :param syn:             A synapse object: syn = synapseclient.login()- Must be logged into synapse

    :param entity:          A synapse entity ID

    :param destinationId:   Synapse ID of a folder/project that the copied entity is being copied to

    :param copyWikiPage:    Determines whether the wiki of the entity is copied over
                            Default is True

    Examples::                        
    import synapseutils
    import synapseclient
    syn = synapseclient.login()
    synapseutils.copy(syn, ...)

    Examples and extra parameters unique to each copy function
    -- COPYING FILES

    :param version:         Can specify version of a file. 
                            Default to None

    :param updateExisting:  When the destination has an entity that has the same name, 
                            users can choose to update that entity.  
                            It must be the same entity type
                            Default to False
    
    :param setProvenance:   Has three values to set the provenance of the copied entity:
                                traceback: Sets to the source entity
                                existing: Sets to source entity's original provenance (if it exists)
                                None: No provenance is set

    Examples::
    synapseutils.copy(syn, "syn12345", "syn45678", updateExisting=False, setProvenance = "traceback",version=None)

    -- COPYING FOLDERS/PROJECTS

    :param excludeTypes:    Accepts a list of entity types (file, table, link) which determines which entity types to not copy.
                            Defaults to an empty list.

    Examples::
    #This will copy everything in the project into the destinationId except files and tables.
    synapseutils.copy(syn, "syn123450","syn345678",excludeTypes=["file","table"])

    :returns: a mapping between the original and copied entity: {'syn1234':'syn33455'}
    """
    updateLinks = kwargs.get('updateLinks', True)
    updateSynIds = kwargs.get('updateSynIds', True)
    entitySubPageId = kwargs.get('entitySubPageId',None)
    destinationSubPageId = kwargs.get('destinationSubPageId',None)

    mapping = _copyRecursive(syn, entity, destinationId, **kwargs)
    if copyWikiPage:
        for oldEnt in mapping:
            newWikig = copyWiki(syn, oldEnt, mapping[oldEnt], entitySubPageId = entitySubPageId,
                                destinationSubPageId = destinationSubPageId, updateLinks = updateLinks, 
                                updateSynIds = updateSynIds, entityMap = mapping)
    return(mapping)

def _copyRecursive(syn, entity, destinationId, mapping=None, **kwargs):
    """
    Recursively copies synapse entites, but does not copy the wikis

    :param entity:             A synapse entity ID

    :param destinationId:      Synapse ID of a folder/project that the copied entity is being copied to
    
    :returns: a mapping between the original and copied entity: {'syn1234':'syn33455'}
    """

    version = kwargs.get('version', None)
    setProvenance = kwargs.get('setProvenance', "traceback")
    excludeTypes = kwargs.get('excludeTypes',[])
    updateExisting = kwargs.get('updateExisting',False)
    copiedId = None
    if mapping is None:
        mapping=dict()
    #Check that passed in excludeTypes is file, table, and link
    if not isinstance(excludeTypes,list):
        raise ValueError("Excluded types must be a list") 
    elif not all([i in ["file","link","table"] for i in excludeTypes]):
        raise ValueError("Excluded types can only be a list of these values: file, table, and link") 

    ent = syn.get(entity,downloadFile=False)
    if ent.id == destionationId:
        raise ValueError("destinationId cannot be the same as entity id")

    if (isinstance(ent, Project) or isinstance(ent, Folder)) and version is not None:
        raise ValueError("Cannot specify version when copying a project of folder")

    if not isinstance(ent, (Project, Folder, File, Link, Schema)):
        raise ValueError("Not able to copy this type of file")

    if isinstance(ent, Project):
        if not isinstance(syn.get(destinationId),Project):
            raise ValueError("You must give a destinationId of a new project to copy projects")
        copiedId = destinationId
        entities = syn.chunkedQuery('select id, name from entity where parentId=="%s"' % ent.id)<commit_msg>Fix typo<commit_after>import synapseclient
from synapseclient import File, Project, Folder, Table, Schema, Link, Wiki, Activity, exceptions
import time
from synapseclient.exceptions import *
import tempfile
import re
############################################################
##                 Copy Functions                         ##
############################################################

def copy(syn, entity, destinationId=None, copyWikiPage=True, **kwargs):
    """
    - This function will assist users in copying entities (Tables, Links, Files, Folders, Projects),
      and will recursively copy everything in directories.
    - A Mapping of the old entities to the new entities will be created and all the wikis of each entity
      will also be copied over and links to synapse Ids will be updated.

    :param syn:             A synapse object: syn = synapseclient.login()- Must be logged into synapse

    :param entity:          A synapse entity ID

    :param destinationId:   Synapse ID of a folder/project that the copied entity is being copied to

    :param copyWikiPage:    Determines whether the wiki of the entity is copied over
                            Default is True

    Examples::                        
    import synapseutils
    import synapseclient
    syn = synapseclient.login()
    synapseutils.copy(syn, ...)

    Examples and extra parameters unique to each copy function
    -- COPYING FILES

    :param version:         Can specify version of a file. 
                            Default to None

    :param updateExisting:  When the destination has an entity that has the same name, 
                            users can choose to update that entity.  
                            It must be the same entity type
                            Default to False
    
    :param setProvenance:   Has three values to set the provenance of the copied entity:
                                traceback: Sets to the source entity
                                existing: Sets to source entity's original provenance (if it exists)
                                None: No provenance is set

    Examples::
    synapseutils.copy(syn, "syn12345", "syn45678", updateExisting=False, setProvenance = "traceback",version=None)

    -- COPYING FOLDERS/PROJECTS

    :param excludeTypes:    Accepts a list of entity types (file, table, link) which determines which entity types to not copy.
                            Defaults to an empty list.

    Examples::
    #This will copy everything in the project into the destinationId except files and tables.
    synapseutils.copy(syn, "syn123450","syn345678",excludeTypes=["file","table"])

    :returns: a mapping between the original and copied entity: {'syn1234':'syn33455'}
    """
    updateLinks = kwargs.get('updateLinks', True)
    updateSynIds = kwargs.get('updateSynIds', True)
    entitySubPageId = kwargs.get('entitySubPageId',None)
    destinationSubPageId = kwargs.get('destinationSubPageId',None)

    mapping = _copyRecursive(syn, entity, destinationId, **kwargs)
    if copyWikiPage:
        for oldEnt in mapping:
            newWikig = copyWiki(syn, oldEnt, mapping[oldEnt], entitySubPageId = entitySubPageId,
                                destinationSubPageId = destinationSubPageId, updateLinks = updateLinks, 
                                updateSynIds = updateSynIds, entityMap = mapping)
    return(mapping)

def _copyRecursive(syn, entity, destinationId, mapping=None, **kwargs):
    """
    Recursively copies synapse entites, but does not copy the wikis

    :param entity:             A synapse entity ID

    :param destinationId:      Synapse ID of a folder/project that the copied entity is being copied to
    
    :returns: a mapping between the original and copied entity: {'syn1234':'syn33455'}
    """

    version = kwargs.get('version', None)
    setProvenance = kwargs.get('setProvenance', "traceback")
    excludeTypes = kwargs.get('excludeTypes',[])
    updateExisting = kwargs.get('updateExisting',False)
    copiedId = None
    if mapping is None:
        mapping=dict()
    #Check that passed in excludeTypes is file, table, and link
    if not isinstance(excludeTypes,list):
        raise ValueError("Excluded types must be a list") 
    elif not all([i in ["file","link","table"] for i in excludeTypes]):
        raise ValueError("Excluded types can only be a list of these values: file, table, and link") 

    ent = syn.get(entity,downloadFile=False)
    if ent.id == destinationId:
        raise ValueError("destinationId cannot be the same as entity id")

    if (isinstance(ent, Project) or isinstance(ent, Folder)) and version is not None:
        raise ValueError("Cannot specify version when copying a project of folder")

    if not isinstance(ent, (Project, Folder, File, Link, Schema)):
        raise ValueError("Not able to copy this type of file")

    if isinstance(ent, Project):
        if not isinstance(syn.get(destinationId),Project):
            raise ValueError("You must give a destinationId of a new project to copy projects")
        copiedId = destinationId
        entities = syn.chunkedQuery('select id, name from entity where parentId=="%s"' % ent.id)
{'fraction_non_alphanumeric': 0.0964340490797546, 'fraction_numerical': 0.007668711656441718, 'mean_word_length': 2.7101706970128023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3908543', 'n_tokens_mistral': 2707, 'n_tokens_neox': 2569, 'n_words': 1026}