from pathlib import Path
import pkg_resources


def _create_model_folder(url):
    """ Create the folder for the new model.

    The new folder is created in the `models_data` folder, i.e., at the same
    location of the other models (that can be installed independently from this
    one).
    Structure of the `models_data` folder.
    ```
    models_data
    |- path_model (not mandatory)
    |- snippet_model (independent from this)
    `- snippet_model_author (created with this method)
    ```

    Parameters
    ----------
    url: str
        The url of the repository

    Returns
    -------
    `pathlib.Path`
        The path of the model folder

    Raises
    ------
    FileExistsError
        If the model folder already exists
    """
    # The model name is the name of the author of the repository
    model_name = 'snippet_model_%s' % url.split('/')[-2]
    # Get the models_data folder of credentialdigger
    models_data = Path(pkg_resources.resource_filename('credentialdigger',
                                                       'models_data'))

    # Create model folder. If the model already exists, its folder is already
    # present at this path. In this case, a FileExistsError is raised by the
    # instruction mkdir
    local_model = models_data / model_name
    local_model.mkdir()

    return local_model