<commit_before>'''
battery.py: part of psiturkpy package
Functions to generate psiturk batteries

'''
from psiturkpy.experiment import get_experiments, load_experiment
from psiturkpy.utils import copy_directory, get_template, \
     sub_template, get_installdir, save_template
from psiturkpy.vm import custom_battery_download
import os
import re


def generate(battery_dest,battery_repo=None,experiment_repo=None,experiments=None,config=None,make_config=True):
    '''
    generate will create a battery from a template
    and list of experiments

        battery_dest [required] is the output folder for your battery. This folder MUST NOT EXIST.
        battery_repo: location of psiturk-battery repo to use as a template. If not specified, will be
                      downloaded to a temporary directory
        experiment_repo: location of a psiturk-experiments repo to check for valid experiments. If not specified, will be
                      downloaded to a temporary directory
        experiments: a list of experiments, meaning the "tag" variable in the psiturk.json, to include. This variable also
                     conincides with the experiment folder name.
        config: A dictionary with keys that coincide with parameters in the config.txt file for a psiturk experiment. If not provided,
                     a dummy config will be generated.
        make_config: A boolean (default True) to control generation of the config. If there is a config generated before calling this
                     function, this should be set to False.

    '''
    # We can only generate a battery to a folder that does not exist, to be safe
    if not os.path.exists(battery_dest):
        if experiment_repo == None or battery_repo == None:
            tmpdir = custom_battery_download()
            if experiment_repo == None:
                experiment_repo = "%s/experiments" %(tmpdir)     
            if battery_repo == None:
                battery_repo = "%s/battery" %(tmpdir)     

        # Copy battery skeleton to destination
        copy_directory(battery_repo,battery_dest)
        valid_experiments = get_experiments(experiment_repo)

        # If the user wants to select a subset
        if experiments != None:
            subset_experiments = [x for x in valid_experiments if os.path.basename(x) in experiments]
            valid_experiments = subset_experiments      

        # Fill in templates with the experiments, generate config
        template_experiments(battery_dest,battery_repo,valid_experiments)
        if make_config:
            if config == None:
                config = dict()
            generate_config(battery_dest,config)
    else:
        print "Folder exists at %s, cannot generate." %(battery_dest)

        
def template_experiments(battery_dest,battery_repo,valid_experiments):
    '''
    template_experiments:
    For each valid experiment, copies the entire folder into the battery destination
    directory, and generates templates with appropriate paths to run them

        battery_dest: full path to destination folder of battery
        battery_repo: full path to psiturk-battery repo template
        valid_experiments: a list of full paths to experiment folders to include

    '''
    # Generate run template, make substitutions
    template_file = "%s/static/js/load_experiments.js" %(battery_repo)<commit_msg>adding option to specify custom template_file to battery.py<commit_after>'''
battery.py: part of psiturkpy package
Functions to generate psiturk batteries

'''
from psiturkpy.experiment import get_experiments, load_experiment
from psiturkpy.utils import copy_directory, get_template, \
     sub_template, get_installdir, save_template
from psiturkpy.vm import custom_battery_download
import os
import re


def generate(battery_dest,battery_repo=None,experiment_repo=None,experiments=None,config=None,make_config=True):
    '''
    generate will create a battery from a template
    and list of experiments

        battery_dest [required] is the output folder for your battery. This folder MUST NOT EXIST.
        battery_repo: location of psiturk-battery repo to use as a template. If not specified, will be
                      downloaded to a temporary directory
        experiment_repo: location of a psiturk-experiments repo to check for valid experiments. If not specified, will be
                      downloaded to a temporary directory
        experiments: a list of experiments, meaning the "tag" variable in the psiturk.json, to include. This variable also
                     conincides with the experiment folder name.
        config: A dictionary with keys that coincide with parameters in the config.txt file for a psiturk experiment. If not provided,
                     a dummy config will be generated.
        make_config: A boolean (default True) to control generation of the config. If there is a config generated before calling this
                     function, this should be set to False.

    '''
    # We can only generate a battery to a folder that does not exist, to be safe
    if not os.path.exists(battery_dest):
        if experiment_repo == None or battery_repo == None:
            tmpdir = custom_battery_download()
            if experiment_repo == None:
                experiment_repo = "%s/experiments" %(tmpdir)     
            if battery_repo == None:
                battery_repo = "%s/battery" %(tmpdir)     

        # Copy battery skeleton to destination
        copy_directory(battery_repo,battery_dest)
        valid_experiments = get_experiments(experiment_repo)

        # If the user wants to select a subset
        if experiments != None:
            subset_experiments = [x for x in valid_experiments if os.path.basename(x) in experiments]
            valid_experiments = subset_experiments      

        # Fill in templates with the experiments, generate config
        template_experiments(battery_dest,battery_repo,valid_experiments)
        if make_config:
            if config == None:
                config = dict()
            generate_config(battery_dest,config)
    else:
        print "Folder exists at %s, cannot generate." %(battery_dest)

        
def template_experiments(battery_dest,battery_repo,valid_experiments,template_file=None):
    '''
    template_experiments:
    For each valid experiment, copies the entire folder into the battery destination
    directory, and generates templates with appropriate paths to run them

        battery_dest: full path to destination folder of battery
        battery_repo: full path to psiturk-battery repo template
        valid_experiments: a list of full paths to experiment folders to include
        template_file: the load_experiments.js template file. If not specified,
                       the file from the battery repo is used.

    '''
    # Generate run template, make substitutions
    if template_file == None:
        template_file = "%s/static/js/load_experiments.js" %(battery_repo)
{'fraction_non_alphanumeric': 0.053540833809251856, 'fraction_numerical': 0.0, 'mean_word_length': 2.8467874794069195, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4970517', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1760, 'n_words': 691}