B
    âÖÓYü"  ã               @   s¬   d Z ddlmZ ddlZddlmZ ddlZddlmZ ddl	m
Z
 ddlm  mZ yddlZW n  ek
r‚   ddlmZ Y nX dZed Zdd	„ ZG d
d„ deƒZdS )z&Project file loading and manipulation.é    )Úabsolute_importN)ÚOrderedDict)ÚYamlFile)ÚEnvSpec)zanaconda-project.ymlzanaconda-project.yamlz
kapsel.ymlzkapsel.yamlc               C   s   t dg ddfS )NÚdefault© )ÚnameÚchannelsZconda_packages)r   r   r   r   ú<lib/python3.7/site-packages/anaconda_project/project_file.pyÚ_empty_default_env_spec!   s    r   c                   s<   e Zd ZdZeefdd„ƒZef‡ fdd„	Zdd„ Z‡  Z	S )ÚProjectFileaÿ  Represents the ``anaconda-project.yml`` file which describes the project across machines/users.

    State that's specific to a machine/user/checkout/deployment
    should instead be in ``LocalStateFile``.  ``ProjectFile``
    would normally be checked in to source control or otherwise
    act as a shared resource.

    Be careful with creating your own instance of this class,
    because you have to think about when other code might load or
    save in a way that conflicts with your loads and saves.

    c             C   sD   x.t D ]&}tj ||¡}tj |¡rt|ƒS qW ttj |t¡|ƒS )a+  Load the project file from the given directory, even if it doesn't exist.

        If the directory has no project file, the loaded
        ``ProjectFile`` will be empty. It won't actually be
        created on disk unless you call ``save()``.

        If the file has syntax problems, this sets the
        ``corrupted`` and ``corrupted_error_message`` properties,
        and attempts to modify the file will raise an
        exception. If the project file has semantic problems, they
        are not detected by this class but are reported by the
        ``Project`` class.

        Args:
            directory (str): path to the project directory
            default_env_specs_func (function makes list of EnvSpec): if file is created, use these

        Returns:
            a new ``ProjectFile``

        )Úpossible_project_file_namesÚosÚpathÚjoinÚisfiler   ÚDEFAULT_PROJECT_FILENAME)ÚclsZ	directoryÚdefault_env_specs_funcr   r   r   r   r
   Úload_for_directory3   s
    
zProjectFile.load_for_directoryc                s   || _ tt| ƒ |¡ dS )a›  Construct a ``ProjectFile`` with the given filename and requirement registry.

        It's easier to use ``ProjectFile.load_for_directory()`` in most cases.

        If the file has syntax problems, this sets the
        ``corrupted`` and ``corrupted_error_message`` properties,
        and attempts to modify the file will raise an
        exception. If the project file has semantic problems, they
        are not detected by this class but are reported by the
        ``Project`` class.

        Args:
            filename (str): path to the project file
            default_env_specs_func (function makes list of EnvSpec): if file is created, use these

        N)Ú_default_env_specs_funcÚsuperr   Ú__init__)ÚselfÚfilenamer   )Ú	__class__r   r
   r   P   s    zProjectFile.__init__c             C   sú  d}t ƒ }d|d< d|d< d|d< d|d	< d
|d< d|d< d|d< d|d< d|d< d|d< d|d< | jd k	spt‚|  ¡ }|d k	s„t‚dd„ }||ƒ}x¨| ¡ D ]œ\}}|dkrÔtj tj | j¡¡}dt	 
|¡ }	nF|dkrâd}	n8|dkrðd}	n*|dkrt ¡ }
dd  |
¡ d! }	nd"}	|d# ||ƒ | d$ |	 d% }qžW tj|tjd&}x |D ]}| ¡ |d |j< qTW t|ƒd'krött|d ƒƒ}|d | }d(d)„ }d|kr¾||d |d ƒ d|krÚ||d |d ƒ d|krö||d |d ƒ |S )*NzØThis is an Anaconda project file.

Here you can describe your project and how to run it.
Use `anaconda-project run` to run the project.
The file is in YAML format, please see http://www.yaml.org/start.html for more.
z(Set the 'name' key to name your project
r   z0Set the 'icon' key to give your project an icon
Úiconz@Set a one-sentence-or-so 'description' key with project details
Údescriptionz„In the commands section, list your runnable scripts, notebooks, and other code.
Use `anaconda-project add-command` to add commands.
Zcommandsz…In the variables section, list any environment variables your code depends on.
Use `anaconda-project add-variable` to add variables.
Z	variableszIn the services section, list any services that should be
available before your code runs.
Use `anaconda-project add-service` to add services.
ZserviceszIn the downloads section, list any URLs to download to local files
before your code runs.
Use `anaconda-project add-download` to add downloads.
Z	downloadszŽIn the packages section, list any packages that must be installed
before your code runs.
Use `anaconda-project add-packages` to add packages.
ÚpackageszzIn the channels section, list any Conda channel URLs to be searched
for packages.

For example,

channels:
   - mychannel
r	   z¤In the platforms section, list platforms the project should work on
Examples: "linux-64", "osx-64", "win-64"
Use `anaconda-project add-platforms` to add platforms.
Ú	platformszÆYou can define multiple, named environment specs.
Each inherits any global packages or channels,
but can have its own unique ones also.
Use `anaconda-project add-env-spec` to add environment specs.
Z	env_specsc             S   s    dd  |  d¡¡ d  dd¡S )Nz# z
# Ú
z# 
z#
)r   ÚsplitÚreplace)Úcommentr   r   r
   Úcomment_out“   s    z1ProjectFile._default_content.<locals>.comment_outú )r   r   Ú )r	   r   z  []z  [z, ú]z  {}z
#
z:
z


)ÚLoaderé   c             S   s"   |d d …= |  | ¡ | d d …= d S )N)Úextend)ÚsrcÚdestr   r   r
   Úmove_list_elements´   s    

z8ProjectFile._default_content.<locals>.move_list_elements)r   r   ÚAssertionErrorÚitemsr   r   ÚbasenameÚdirnamer   ÚjsonÚdumpsÚ	conda_apiZdefault_platforms_with_currentr   ÚryamlÚloadZRoundTripLoaderZto_jsonr   ÚlenÚnextÚiter)r   ÚheaderZsectionsZdefault_env_specsr$   Zto_parseZsection_namer#   Zdefault_nameZsection_bodyr   Zas_jsonZenv_specZ	spec_nameZ	spec_jsonr-   r   r   r
   Ú_default_contentd   sZ    
$



zProjectFile._default_content)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   r   r   r;   Ú__classcell__r   r   )r   r
   r   %   s
   r   )r?   Z
__future__r   r   Úcollectionsr   r2   Zanaconda_project.yaml_filer   Zanaconda_project.env_specr   Z#anaconda_project.internal.conda_apiZinternalr4   Zruamel_yamlr5   ÚImportErrorZruamel.yamlZyamlr   r   r   r   r   r   r   r
   Ú<module>   s   