U
    ¿du'                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ G dd deZdddZdddZdddZdS )z>
Appraise evaluation framework

See LICENSE for usage details
    )datetime)BaseCommand)CommandError)Dataset)_create_uniform_task_map)_identify_super_users)_load_campaign_manifest)_process_campaign_agendas)_process_campaign_teams)_process_market_and_metadata)_process_users)_validate_language_codes)CAMPAIGN_TASK_TYPES)generate_confirmation_tokenc                   @   s    e Zd ZdZdd Zdd ZdS )Commandz+Initialises campaign based on manifest filec                 C   sb   |j ddtdd |j dtd ddd |j d	td d
dd |j ddddd |j ddddd d S )Nmanifest_jsonzmanifest-jsonz%Path to manifest file in JSON format.)metavartypehelpz--csv-outputz--csvz4Path used to create CSV file containing credentials.)r   defaultr   r   z--xlsx-outputz--xlsxz6Path used to create Excel file containing credentials.z--include-completed
store_trueFz5Include completed tasks in task agenda re-assignment.)actionr   r   z--task-confirmation-tokenszaGenerate valid task confirmation tokens needed for integration with external crowd sourcing apps.)add_argumentstr)selfparser r   Y/var/www/rival/public_html/translation-eval/Campaign/management/commands/init_campaign.pyadd_arguments   s@    zCommand.add_argumentsc           
      O   s   | d}| jd| | d}| jd| |rX| dsXtd|| d}| jd| |r| d	std
|t|}t|| jd}|d  }| dd}	t	|||||	| jd d S )Nr   zJSON manifest path: {0!r}
csv_outputzCSV output path: {0!r}z.csvz,csv_output {0!r} does not point to .csv filexlsx_outputzExcel output path: {0!r}z.xlsxz.xlsx_output {0!r} does not point to .xlsx file)stdoutinclude_completedtask_confirmation_tokensF)
getr!   writeformatlowerendswithr   r   _create_context_init_campaign)
r   argsoptionsr   r   r    manifest_datacontextonly_activatedconfirmation_tokensr   r   r   handleB   s4    



zCommand.handleN)__name__
__module____qualname__r   r   r1   r   r   r   r   r      s   'r   TFNc           	      C   sf   | d }t d| t }|dk	r4|d|d  t||d | d | d d |dk	rb|d | S )	a  Initialises campaign based on manifest data.

    Parameters:
    - context:dict[str]->any dictionary containing manifest data;
    - csv_output:str path to CSV output file, or None;
    - xlsx_output:str path to Excel output file, or None;
    - only_activated:bool only include activated tasks for agenda creation;
    - confirmation_tokens:bool export valid task confirmation tokens.
    ALL_LANGUAGESzAll languages:NzIdentified superuser: {0}r   CAMPAIGN_NAME)domain_namecorpus_namez#Processed Market/Metadata instances)printr   r%   r&   r   )	r.   r   r    r/   r0   skip_agendasr!   r5   Z
superusersr   r   r   r*   i   s    

0r*   c              	   C   s   |rFt |ddd}|| d W 5 Q R X |dk	rF|d| |rt |dd}|| d	 W 5 Q R X |dk	r|d
| dS )a  Export credentials to screen, CSV and Excel files.

    Parameters:
    - export_data:Dataset contains triples or 4-tuples (username,
      password, url, [token]);
    - csv_output:str path to CSV output file, or None;
    - xlsx_output:str path to Excel output file, or None.
    w )modenewlineZcsvNzExported CSV file: {0!r}wb)r=   ZxlsxzExported Excel file: {0!r})openr%   exportr&   )Zexport_datar   r    r!   out_filer   r   r   _export_credentials   s    rC   c                 C   s  dt i}g }t }i }| d D ]T}|\}}}	}
}|| || |||f ||	 }||
|| d |||f< qt| d| krd}|dk	r|d n| dd}|tkrd|d		t
 }t||| d
 | d | d | d | d ||| ddd	}|S )zCreate context from manifest JSON.

    Parameters:
    - manifest_data:dict[str]->any dictionary containing manifest data;
    uniformTASKS_TO_ANNOTATORS
REDUNDANCY	TASK_TYPEDirectNzNo task type found in the manifest file, assuming it is "Direct". If this is incorrect, define "TASK_TYPE" in the manifest file.z6Unrecognized TASK_TYPE '{0}'. Supported tasks are: {1}z, CAMPAIGN_KEYr6   CAMPAIGN_NOCAMPAIGN_URLTASK_OPTIONSr<   )	r5   rI   r6   rJ   rK   rF   rE   rG   rL   )r   setaddappendr   r%   r$   r   r&   joinkeys
ValueError)r-   r!   Z
GENERATORSr5   ZALL_LANGUAGE_CODESrE   Z	pair_datasource_codetarget_coder=   Znum_annotatorsZ	num_tasks	generatorrG   _msgr.   r   r   r   r)      s\    

 
r)   )TFFN)N)N)__doc__r   django.core.management.baser   r   Ztablibr   Campaign.utilsr   r   r   r	   r
   r   r   r   r   Dashboard.utilsr   r   r*   rC   r)   r   r   r   r   <module>   s,   U    
U
