<commit_before>from pliers.extractors import (DictionaryExtractor,
                               PartOfSpeechExtractor,
                               LengthExtractor,
                               NumUniqueWordsExtractor,
                               PredefinedDictionaryExtractor,
                               TextVectorizerExtractor,
                               WordEmbeddingExtractor,
                               VADERSentimentExtractor,
                               SpaCyExtractor)
from pliers.extractors.base import merge_results
from pliers.stimuli import TextStim, ComplexTextStim
from ..utils import get_test_data_path

import numpy as np
from os.path import join
import pytest
import spacy

TEXT_DIR = join(get_test_data_path(), 'text')

def test_text_extractor():
    stim = ComplexTextStim(join(TEXT_DIR, 'sample_text.txt'),
                           columns='to', default_duration=1)
    td = DictionaryExtractor(join(TEXT_DIR, 'test_lexical_dictionary.txt'),
                             variables=['length', 'frequency'])
    assert td.data.shape == (7, 2)
    result = td.transform(stim)[2].to_df()
    assert result['duration'][0] == 1
    assert result.shape == (1, 6)
    assert np.isclose(result['frequency'][0], 11.729, 1e-5)

def test_text_length_extractor():
    stim = TextStim(text='hello world', onset=4.2, duration=1)
    ext = LengthExtractor()
    result = ext.transform(stim).to_df()
    assert 'text_length' in result.columns
    assert result['text_length'][0] == 11
    assert result['onset'][0] == 4.2
    assert result['duration'][0] == 1

def test_unique_words_extractor():
    stim = TextStim(text='hello hello world')
    ext = NumUniqueWordsExtractor()
    result = ext.transform(stim).to_df()
    assert 'num_unique_words' in result.columns
    assert result['num_unique_words'][0] == 2

def test_dictionary_extractor():
    td = DictionaryExtractor(join(TEXT_DIR, 'test_lexical_dictionary.txt'),
                             variables=['length', 'frequency'])
    assert td.data.shape == (7, 2)

    stim = TextStim(text='annotation')
    result = td.transform(stim).to_df()
    assert np.isnan(result['onset'][0])
    assert 'length' in result.columns
    assert result['length'][0] == 10

    stim2 = TextStim(text='some')
    result = td.transform(stim2).to_df()
    assert np.isnan(result['onset'][0])
    assert 'frequency' in result.columns
    assert np.isnan(result['frequency'][0])

def test_predefined_dictionary_extractor():
    stim = TextStim(text='enormous')
    td = PredefinedDictionaryExtractor(['aoa/Freq_pm'])
    result = td.transform(stim).to_df()
    assert result.shape == (1, 5)
    assert 'aoa_Freq_pm' in result.columns
    assert np.isclose(result['aoa_Freq_pm'][0], 10.313725, 1e-5)

def test_predefined_dictionary_retrieval():
    variables = [
        'affect/D.Mean.H',
        'concreteness/SUBTLEX',
        'subtlexusfrequency/Zipf-value',
        'calgarysemanticdecision/RTclean_mean',
        'massiveauditorylexicaldecision/PhonLev'
    ]
    stim = TextStim(text='perhaps')
    td = PredefinedDictionaryExtractor(variables)
    result = td.transform(stim).to_df().iloc[0]
    assert np.isnan(result['affect_D.Mean.H'])
    assert result['concreteness_SUBTLEX'] == 6939
    assert result['calgarysemanticdecision_RTclean_mean'] == 954.48
    assert np.isclose(result['subtlexusfrequency_Zipf-value'], 5.1331936)
    assert np.isclose(result['massiveauditorylexicaldecision_PhonLev'],
                      6.65101626)

def test_part_of_speech_extractor():
    import nltk
    nltk.download('tagsets')
    stim = ComplexTextStim(join(TEXT_DIR, 'complex_stim_with_header.txt'))
    result = merge_results(PartOfSpeechExtractor().transform(stim),
                           format='wide', extractor_names=False)
    assert result.shape == (4, 54)
    assert result['NN'].sum() == 1
    result = result.sort_values('onset')
    assert result['VBD'].iloc[3] == 1

def test_word_embedding_extractor():
    pytest.importorskip('gensim')
    stims = [TextStim(text='this'), TextStim(text='sentence')]
    ext = WordEmbeddingExtractor(join(TEXT_DIR, 'simple_vectors.bin'),
                                 binary=True)
    result = merge_results(ext.transform(stims), extractor_names='multi',
                           format='wide')
    assert ('WordEmbeddingExtractor', 'embedding_dim99') in result.columns
    assert np.allclose(0.0010911,
                       result[('WordEmbeddingExtractor', 'embedding_dim0')][0])

    unk = TextStim(text='nowaythisinvocab')
    result = ext.transform(unk).to_df()
    assert result['embedding_dim10'][0] == 0.0

    ones = np.ones(100)
    ext = WordEmbeddingExtractor(join(TEXT_DIR, 'simple_vectors.bin'),
                                 binary=True, unk_vector=ones)
    result = ext.transform(unk).to_df()
    assert result['embedding_dim10'][0] == 1.0

    ext = WordEmbeddingExtractor(join(TEXT_DIR, 'simple_vectors.bin'),
                                 binary=True, unk_vector='random')
    result = ext.transform(unk).to_df()
    assert result['embedding_dim10'][0] <= 1.0
    assert result['embedding_dim10'][0] >= -1.0

    ext = WordEmbeddingExtractor(join(TEXT_DIR, 'simple_vectors.bin'),
                                 binary=True, unk_vector='nothing')
    result = ext.transform(unk).to_df()
    assert result['embedding_dim10'][0] == 0.0

def test_vectorizer_extractor():
    pytest.importorskip('sklearn')
    stim = TextStim(join(TEXT_DIR, 'scandal.txt'))
    result = TextVectorizerExtractor().transform(stim).to_df()
    assert 'woman' in result.columns
    assert result['woman'][0] == 3

    from sklearn.feature_extraction.text import TfidfVectorizer
    custom_vectorizer = TfidfVectorizer()
    ext = TextVectorizerExtractor(vectorizer=custom_vectorizer)
    stim2 = TextStim(join(TEXT_DIR, 'simple_text.txt'))
    result = merge_results(ext.transform([stim, stim2]), format='wide',
                           extractor_names='multi')
    assert ('TextVectorizerExtractor', 'woman') in result.columns
    assert np.allclose(0.129568189476,
                       result[('TextVectorizerExtractor', 'woman')][0])

    ext = TextVectorizerExtractor(vectorizer='CountVectorizer',
                                  analyzer='char_wb',
                                  ngram_range=(2, 2))
    result = ext.transform(stim).to_df()
    assert 'wo' in result.columns
    assert result['wo'][0] == 6

def test_vader_sentiment_extractor():
    stim = TextStim(join(TEXT_DIR, 'scandal.txt'))
    ext = VADERSentimentExtractor()
    result = ext.transform(stim).to_df()
    assert result['sentiment_neu'][0] == 0.752

    stim2 = TextStim(text='VADER is smart, handsome, and funny!')
    result2 = ext.transform(stim2).to_df()
    assert result2['sentiment_pos'][0] == 0.752
    assert result2['sentiment_neg'][0] == 0.0
    assert result2['sentiment_neu'][0] == 0.248
    assert result2['sentiment_compound'][0] == 0.8439

def test_spacy_token_extractor():
    pytest.importorskip('spacy')
    stim = TextStim(text='This is a test.')
    ext = SpaCyExtractor(extractor_type='token')
    assert ext.model is not None

    ext2 = SpaCyExtractor(model='en_core_web_sm')
    assert isinstance(ext2.model, spacy.lang.en.English)

    result = ext.transform(stim).to_df()
    assert result['text'][0] == 'This'
    assert result['lemma_'][0].lower() == 'this'
    assert result['pos_'][0] == 'DET'
    assert result['tag_'][0] == 'DT'
    assert result['dep_'][0] == 'nsubj'
    assert result['shape_'][0] == 'Xxxx'
    assert result['is_alpha'][0] == 'True'
    assert result['is_stop'][0] == 'True'
    assert result['is_punct'][0] == 'False'
    assert result['is_ascii'][0] == 'True'
    assert result['is_digit'][0] == 'False'
    assert result['sentiment'][0] == '0.0'

    assert result['text'][1] == 'is'
    assert result['lemma_'][1].lower() == 'be'
    assert result['pos_'][1] == 'VERB'
    assert result['tag_'][1] == 'VBZ'
    assert result['dep_'][1] == 'ROOT'
    assert result['shape_'][1] == 'xx'
    assert result['is_alpha'][1] == 'True'
    assert result['is_stop'][1] == 'True'
    assert result['is_punct'][1] == 'False'
    assert result['is_ascii'][1] == 'True'
    assert result['is_digit'][1] == 'False'
    assert result['sentiment'][1] == '0.0'

    assert result['text'][2] == 'a'
    assert result['lemma_'][2].lower() == 'a'
    assert result['pos_'][2] == 'DET'
    assert result['tag_'][2] == 'DT'
    assert result['dep_'][2] == 'det'
    assert result['shape_'][2] == 'x'
    assert result['is_alpha'][2] == 'True'
    assert result['is_stop'][2] == 'True'
    assert result['is_punct'][2] == 'False'
    assert result['is_ascii'][2] == 'True'
    assert result['is_digit'][2] == 'False'
    assert result['sentiment'][2] == '0.0'

    assert result['text'][3] == 'test'
    assert result['lemma_'][3].lower() == 'test'
    assert result['pos_'][3] == 'NOUN'
    assert result['tag_'][3] == 'NN'
    assert result['dep_'][3] == 'attr'
    assert result['shape_'][3] == 'xxxx'
    assert result['is_alpha'][3] == 'True'
    assert result['is_stop'][3] == 'False'
    assert result['is_punct'][3] == 'False'
    assert result['is_ascii'][3] == 'True'
    assert result['is_digit'][3] == 'False'
    assert result['sentiment'][3] == '0.0'

def test_spacy_doc_extractor():
    pytest.importorskip('spacy')
    stim2 = TextStim(text='This is a test. And we are testing again. This '
                     'should be quite interesting. Tests are totally fun.')
    ext = SpaCyExtractor(extractor_type='doc')
    assert ext.model is not None

    result = ext.transform(stim2).to_df()
    assert result['text'][0]=='This is a test. '
    assert result['is_parsed'][0]
    assert result['is_tagged'][0]
    assert result['is_sentenced'][0]

    assert result['text'][3]=='Tests are totally fun.'
    assert result['is_parsed'][3]
    assert result['is_tagged'][3]
    assert result['is_sentenced'][3]

def test_word_counter_extractor():

    stim_txt = ComplexTextStim(text = 'This is my first test. The extractor counts how many '
                               'times this or that word occurs. '
                               'Counting words is so exciting, and tests!, they are great.')

    stim_from_file = ComplexTextStim(filename = join(TEXT_DIR, 'simple_text_with_repetitions.txt'))
    stim_with_onsets = ComplexTextStim(filename = join(TEXT_DIR, 'complex_stim_with_repetitions.txt'))

    ext = WordCounterExtractor()
    result_stim_txt = merge_results(ext.transform(stim_simple))
    result_stim_from_file = merge_results(ext.transform(stim_from_file))
    result_stim_with_onsets = merge_results(ext.transform(stim_with_onsets))

    assert result_stim_txt.shape[0] == 25 # exp nr rows
    assert all(result_stim_txt['word_count'] >= 1) #test values
    assert result_stim_txt['word_count'][11] == 2 #upper case
    assert result_stim_txt['word_count'][22] == 1 #test non-lemmatized

    assert result_stim_from_file.shape[0] == 45
    assert all(result_stim_from_file['word_count'] >= 1)
    assert result_stim_from_file['word_count'][15] == 2
    assert result_stim_from_file['word_count'][44] == 3

    assert result_stim_with_onsets.shape[0] == 8
    assert result_stim_with_onsets['onset'][2] == 0.8
    assert result_stim_with_onsets['duration'][2] == 0.1
    assert result_stim_with_onsets['word_count'][2] == 2
    assert result_stim_with_onsets['word_count'][5] == 2
    assert result_stim_with_onsets['word_count'][7] == 1

    ext2 = WordCounterExtractor(lemmatize=True)
    result_stim_txt = merge_results(ext2.transform(stim_simple))
    result_stim_from_file = merge_results(ext2.transform(stim_from_file))
    result_stim_with_onsets = merge_results(ext2.transform(stim_with_onsets))

    assert result_stim_txt_df.shape[0] == 25
    assert all(result_stim_txt_df['word_count'] >= 1)
    assert result_stim_txt_df['word_count'][11] == 2
    assert result_stim_txt_df['word_count'][22] == 2

    assert result_stim_from_file.shape[0] == 45
    assert all(result_stim_from_file['word_count'] >= 1)
    assert result_stim_from_file['word_count'][15] == 2
    assert result_stim_from_file['word_count'][44] == 4
    assert result_stim_from_file['word_count'][13] == 2 # test lemmatized verb

    assert result_stim_with_onsets['word_count'][5] == 3
    assert result_stim_with_onsets['word_count'][7] == 2

    ext3 = WordCounterExtractor(log_scale=True)
    result_stim_txt = merge_results(ext3.transform(stim_simple))
    result_stim_from_file = merge_results(ext3.transform(stim_from_file))
    result_stim_with_onsets = merge_results(ext3.transform(stim_with_onsets))

    assert result_stim_txt_df.shape[0] == 25
    assert all(result_stim_txt_df['log_word_count'] >= 0)
    assert result_stim_txt_df['log_word_count'][11] == np.log(2)
    assert result_stim_txt_df['log_word_count'][22] = np.log(1)

    assert result_stim_from_file.shape[0] == 45
    assert all(result_stim_from_file['log_word_count'] >= 0)
    assert result_stim_from_file['log_word_count'][15] == np.log(2)
    assert result_stim_from_file['log_word_count'][44] == np.log(3)
<commit_msg>remove redundant statements<commit_after>from pliers.extractors import (DictionaryExtractor,
                               PartOfSpeechExtractor,
                               LengthExtractor,
                               NumUniqueWordsExtractor,
                               PredefinedDictionaryExtractor,
                               TextVectorizerExtractor,
                               WordEmbeddingExtractor,
                               VADERSentimentExtractor,
                               SpaCyExtractor)
from pliers.extractors.base import merge_results
from pliers.stimuli import TextStim, ComplexTextStim
from ..utils import get_test_data_path

import numpy as np
from os.path import join
import pytest
import spacy

TEXT_DIR = join(get_test_data_path(), 'text')

def test_text_extractor():
    stim = ComplexTextStim(join(TEXT_DIR, 'sample_text.txt'),
                           columns='to', default_duration=1)
    td = DictionaryExtractor(join(TEXT_DIR, 'test_lexical_dictionary.txt'),
                             variables=['length', 'frequency'])
    assert td.data.shape == (7, 2)
    result = td.transform(stim)[2].to_df()
    assert result['duration'][0] == 1
    assert result.shape == (1, 6)
    assert np.isclose(result['frequency'][0], 11.729, 1e-5)

def test_text_length_extractor():
    stim = TextStim(text='hello world', onset=4.2, duration=1)
    ext = LengthExtractor()
    result = ext.transform(stim).to_df()
    assert 'text_length' in result.columns
    assert result['text_length'][0] == 11
    assert result['onset'][0] == 4.2
    assert result['duration'][0] == 1

def test_unique_words_extractor():
    stim = TextStim(text='hello hello world')
    ext = NumUniqueWordsExtractor()
    result = ext.transform(stim).to_df()
    assert 'num_unique_words' in result.columns
    assert result['num_unique_words'][0] == 2

def test_dictionary_extractor():
    td = DictionaryExtractor(join(TEXT_DIR, 'test_lexical_dictionary.txt'),
                             variables=['length', 'frequency'])
    assert td.data.shape == (7, 2)

    stim = TextStim(text='annotation')
    result = td.transform(stim).to_df()
    assert np.isnan(result['onset'][0])
    assert 'length' in result.columns
    assert result['length'][0] == 10

    stim2 = TextStim(text='some')
    result = td.transform(stim2).to_df()
    assert np.isnan(result['onset'][0])
    assert 'frequency' in result.columns
    assert np.isnan(result['frequency'][0])

def test_predefined_dictionary_extractor():
    stim = TextStim(text='enormous')
    td = PredefinedDictionaryExtractor(['aoa/Freq_pm'])
    result = td.transform(stim).to_df()
    assert result.shape == (1, 5)
    assert 'aoa_Freq_pm' in result.columns
    assert np.isclose(result['aoa_Freq_pm'][0], 10.313725, 1e-5)

def test_predefined_dictionary_retrieval():
    variables = [
        'affect/D.Mean.H',
        'concreteness/SUBTLEX',
        'subtlexusfrequency/Zipf-value',
        'calgarysemanticdecision/RTclean_mean',
        'massiveauditorylexicaldecision/PhonLev'
    ]
    stim = TextStim(text='perhaps')
    td = PredefinedDictionaryExtractor(variables)
    result = td.transform(stim).to_df().iloc[0]
    assert np.isnan(result['affect_D.Mean.H'])
    assert result['concreteness_SUBTLEX'] == 6939
    assert result['calgarysemanticdecision_RTclean_mean'] == 954.48
    assert np.isclose(result['subtlexusfrequency_Zipf-value'], 5.1331936)
    assert np.isclose(result['massiveauditorylexicaldecision_PhonLev'],
                      6.65101626)

def test_part_of_speech_extractor():
    import nltk
    nltk.download('tagsets')
    stim = ComplexTextStim(join(TEXT_DIR, 'complex_stim_with_header.txt'))
    result = merge_results(PartOfSpeechExtractor().transform(stim),
                           format='wide', extractor_names=False)
    assert result.shape == (4, 54)
    assert result['NN'].sum() == 1
    result = result.sort_values('onset')
    assert result['VBD'].iloc[3] == 1

def test_word_embedding_extractor():
    pytest.importorskip('gensim')
    stims = [TextStim(text='this'), TextStim(text='sentence')]
    ext = WordEmbeddingExtractor(join(TEXT_DIR, 'simple_vectors.bin'),
                                 binary=True)
    result = merge_results(ext.transform(stims), extractor_names='multi',
                           format='wide')
    assert ('WordEmbeddingExtractor', 'embedding_dim99') in result.columns
    assert np.allclose(0.0010911,
                       result[('WordEmbeddingExtractor', 'embedding_dim0')][0])

    unk = TextStim(text='nowaythisinvocab')
    result = ext.transform(unk).to_df()
    assert result['embedding_dim10'][0] == 0.0

    ones = np.ones(100)
    ext = WordEmbeddingExtractor(join(TEXT_DIR, 'simple_vectors.bin'),
                                 binary=True, unk_vector=ones)
    result = ext.transform(unk).to_df()
    assert result['embedding_dim10'][0] == 1.0

    ext = WordEmbeddingExtractor(join(TEXT_DIR, 'simple_vectors.bin'),
                                 binary=True, unk_vector='random')
    result = ext.transform(unk).to_df()
    assert result['embedding_dim10'][0] <= 1.0
    assert result['embedding_dim10'][0] >= -1.0

    ext = WordEmbeddingExtractor(join(TEXT_DIR, 'simple_vectors.bin'),
                                 binary=True, unk_vector='nothing')
    result = ext.transform(unk).to_df()
    assert result['embedding_dim10'][0] == 0.0

def test_vectorizer_extractor():
    pytest.importorskip('sklearn')
    stim = TextStim(join(TEXT_DIR, 'scandal.txt'))
    result = TextVectorizerExtractor().transform(stim).to_df()
    assert 'woman' in result.columns
    assert result['woman'][0] == 3

    from sklearn.feature_extraction.text import TfidfVectorizer
    custom_vectorizer = TfidfVectorizer()
    ext = TextVectorizerExtractor(vectorizer=custom_vectorizer)
    stim2 = TextStim(join(TEXT_DIR, 'simple_text.txt'))
    result = merge_results(ext.transform([stim, stim2]), format='wide',
                           extractor_names='multi')
    assert ('TextVectorizerExtractor', 'woman') in result.columns
    assert np.allclose(0.129568189476,
                       result[('TextVectorizerExtractor', 'woman')][0])

    ext = TextVectorizerExtractor(vectorizer='CountVectorizer',
                                  analyzer='char_wb',
                                  ngram_range=(2, 2))
    result = ext.transform(stim).to_df()
    assert 'wo' in result.columns
    assert result['wo'][0] == 6

def test_vader_sentiment_extractor():
    stim = TextStim(join(TEXT_DIR, 'scandal.txt'))
    ext = VADERSentimentExtractor()
    result = ext.transform(stim).to_df()
    assert result['sentiment_neu'][0] == 0.752

    stim2 = TextStim(text='VADER is smart, handsome, and funny!')
    result2 = ext.transform(stim2).to_df()
    assert result2['sentiment_pos'][0] == 0.752
    assert result2['sentiment_neg'][0] == 0.0
    assert result2['sentiment_neu'][0] == 0.248
    assert result2['sentiment_compound'][0] == 0.8439

def test_spacy_token_extractor():
    pytest.importorskip('spacy')
    stim = TextStim(text='This is a test.')
    ext = SpaCyExtractor(extractor_type='token')
    assert ext.model is not None

    ext2 = SpaCyExtractor(model='en_core_web_sm')
    assert isinstance(ext2.model, spacy.lang.en.English)

    result = ext.transform(stim).to_df()
    assert result['text'][0] == 'This'
    assert result['lemma_'][0].lower() == 'this'
    assert result['pos_'][0] == 'DET'
    assert result['tag_'][0] == 'DT'
    assert result['dep_'][0] == 'nsubj'
    assert result['shape_'][0] == 'Xxxx'
    assert result['is_alpha'][0] == 'True'
    assert result['is_stop'][0] == 'True'
    assert result['is_punct'][0] == 'False'
    assert result['is_ascii'][0] == 'True'
    assert result['is_digit'][0] == 'False'
    assert result['sentiment'][0] == '0.0'

    assert result['text'][1] == 'is'
    assert result['lemma_'][1].lower() == 'be'
    assert result['pos_'][1] == 'VERB'
    assert result['tag_'][1] == 'VBZ'
    assert result['dep_'][1] == 'ROOT'
    assert result['shape_'][1] == 'xx'
    assert result['is_alpha'][1] == 'True'
    assert result['is_stop'][1] == 'True'
    assert result['is_punct'][1] == 'False'
    assert result['is_ascii'][1] == 'True'
    assert result['is_digit'][1] == 'False'
    assert result['sentiment'][1] == '0.0'

    assert result['text'][2] == 'a'
    assert result['lemma_'][2].lower() == 'a'
    assert result['pos_'][2] == 'DET'
    assert result['tag_'][2] == 'DT'
    assert result['dep_'][2] == 'det'
    assert result['shape_'][2] == 'x'
    assert result['is_alpha'][2] == 'True'
    assert result['is_stop'][2] == 'True'
    assert result['is_punct'][2] == 'False'
    assert result['is_ascii'][2] == 'True'
    assert result['is_digit'][2] == 'False'
    assert result['sentiment'][2] == '0.0'

    assert result['text'][3] == 'test'
    assert result['lemma_'][3].lower() == 'test'
    assert result['pos_'][3] == 'NOUN'
    assert result['tag_'][3] == 'NN'
    assert result['dep_'][3] == 'attr'
    assert result['shape_'][3] == 'xxxx'
    assert result['is_alpha'][3] == 'True'
    assert result['is_stop'][3] == 'False'
    assert result['is_punct'][3] == 'False'
    assert result['is_ascii'][3] == 'True'
    assert result['is_digit'][3] == 'False'
    assert result['sentiment'][3] == '0.0'

def test_spacy_doc_extractor():
    pytest.importorskip('spacy')
    stim2 = TextStim(text='This is a test. And we are testing again. This '
                     'should be quite interesting. Tests are totally fun.')
    ext = SpaCyExtractor(extractor_type='doc')
    assert ext.model is not None

    result = ext.transform(stim2).to_df()
    assert result['text'][0]=='This is a test. '
    assert result['is_parsed'][0]
    assert result['is_tagged'][0]
    assert result['is_sentenced'][0]

    assert result['text'][3]=='Tests are totally fun.'
    assert result['is_parsed'][3]
    assert result['is_tagged'][3]
    assert result['is_sentenced'][3]

def test_word_counter_extractor():

    stim_txt = ComplexTextStim(text = 'This is my first test. The extractor counts how many '
                               'times this or that word occurs. '
                               'Counting words is so exciting, and tests!, they are great.')

    stim_from_file = ComplexTextStim(filename = join(TEXT_DIR, 'simple_text_with_repetitions.txt'))
    stim_with_onsets = ComplexTextStim(filename = join(TEXT_DIR, 'complex_stim_with_repetitions.txt'))

    ext = WordCounterExtractor()
    result_stim_txt = merge_results(ext.transform(stim_simple))
    result_stim_from_file = merge_results(ext.transform(stim_from_file))
    result_stim_with_onsets = merge_results(ext.transform(stim_with_onsets))

    assert result_stim_txt.shape[0] == 25 # exp nr rows
    assert all(result_stim_txt['word_count'] >= 1) #test values
    assert result_stim_txt['word_count'][11] == 2 #upper case
    assert result_stim_txt['word_count'][22] == 1 #test non-lemmatized

    assert result_stim_from_file.shape[0] == 45
    assert all(result_stim_from_file['word_count'] >= 1)
    assert result_stim_from_file['word_count'][15] == 2
    assert result_stim_from_file['word_count'][44] == 3

    assert result_stim_with_onsets.shape[0] == 8
    assert result_stim_with_onsets['onset'][2] == 0.8
    assert result_stim_with_onsets['duration'][2] == 0.1
    assert result_stim_with_onsets['word_count'][2] == 2
    assert result_stim_with_onsets['word_count'][5] == 2
    assert result_stim_with_onsets['word_count'][7] == 1

    ext2 = WordCounterExtractor(lemmatize=True)
    result_stim_txt = merge_results(ext2.transform(stim_simple))
    result_stim_from_file = merge_results(ext2.transform(stim_from_file))
    result_stim_with_onsets = merge_results(ext2.transform(stim_with_onsets))

    assert result_stim_txt.shape[0] == 25
    assert result_stim_txt['word_count'][11] == 2
    assert result_stim_txt['word_count'][22] == 2
    assert result_stim_from_file['word_count'][15] == 2
    assert result_stim_from_file['word_count'][44] == 4
    assert result_stim_from_file['word_count'][13] == 2
    assert result_stim_with_onsets['word_count'][5] == 3
    assert result_stim_with_onsets['word_count'][7] == 2

    ext3 = WordCounterExtractor(log_scale=True)
    result_stim_txt = merge_results(ext3.transform(stim_simple))

    assert all(result_stim_txt['log_word_count'] >= 0)
    assert result_stim_txt['log_word_count'][11] == np.log(2)
    assert result_stim_txt['log_word_count'][22] = np.log(1)<|endoftext|>


<commit_before>#######################################################################
# Common SCons code

import os
import os.path
import sys
import platform as _platform

#######################################################################
# Defaults

_platform_map = {
	'linux2': 'linux',
	'win32': 'winddk',
}

default_platform = sys.platform
default_platform = _platform_map.get(default_platform, default_platform)

_machine_map = {
	'x86': 'x86',
	'i386': 'x86',
	'i486': 'x86',
	'i586': 'x86',
	'i686': 'x86',
	'ppc' : 'ppc',
	'x86_64': 'x86_64',
}
if 'PROCESSOR_ARCHITECTURE' in os.environ:
	default_machine = os.environ['PROCESSOR_ARCHITECTURE']
else:
	default_machine = _platform.machine()
default_machine = _machine_map.get(default_machine, 'generic')

if default_platform in ('linux', 'freebsd', 'darwin'):
	default_dri = 'yes'
elif default_platform in ('winddk', 'windows', 'wince'):
	default_dri = 'no'
else:
	default_dri = 'no'

#######################################################################
# Common options

def AddOptions(opts):
	try:
		from SCons.Variables.BoolVariable import BoolVariable as BoolOption
	except ImportError:
		from SCons.Options.BoolOption import BoolOption
	try:
		from SCons.Variables.EnumVariable import EnumVariable as EnumOption
	except ImportError:
		from SCons.Options.EnumOption import EnumOption
	opts.Add(BoolOption('debug', 'debug build', 'no'))
	opts.Add(BoolOption('profile', 'profile build', 'no'))
	opts.Add(BoolOption('quiet', 'quiet command lines', 'yes'))
	opts.Add(EnumOption('machine', 'use machine-specific assembly code', default_machine,
											 allowed_values=('generic', 'ppc', 'x86', 'x86_64')))
	opts.Add(EnumOption('platform', 'target platform', default_platform,
											 allowed_values=('linux', 'cell', 'windows', 'winddk', 'wince')))
	opts.Add(EnumOption('toolchain', 'compiler toolchain', 'default',
											 allowed_values=('default', 'crossmingw', 'winsdk', 'winddk')))
	opts.Add(BoolOption('llvm', 'use LLVM', 'no'))
	opts.Add(BoolOption('dri', 'build DRI drivers', default_dri))

<commit_msg>scons: Set default_dri to no for Mac OS.<commit_after>#######################################################################
# Common SCons code

import os
import os.path
import sys
import platform as _platform

#######################################################################
# Defaults

_platform_map = {
	'linux2': 'linux',
	'win32': 'winddk',
}

default_platform = sys.platform
default_platform = _platform_map.get(default_platform, default_platform)

_machine_map = {
	'x86': 'x86',
	'i386': 'x86',
	'i486': 'x86',
	'i586': 'x86',
	'i686': 'x86',
	'ppc' : 'ppc',
	'x86_64': 'x86_64',
}
if 'PROCESSOR_ARCHITECTURE' in os.environ:
	default_machine = os.environ['PROCESSOR_ARCHITECTURE']
else:
	default_machine = _platform.machine()
default_machine = _machine_map.get(default_machine, 'generic')

if default_platform in ('linux', 'freebsd'):
	default_dri = 'yes'
elif default_platform in ('winddk', 'windows', 'wince', 'darwin'):
	default_dri = 'no'
else:
	default_dri = 'no'

#######################################################################
# Common options

def AddOptions(opts):
	try:
		from SCons.Variables.BoolVariable import BoolVariable as BoolOption
	except ImportError:
		from SCons.Options.BoolOption import BoolOption
	try:
		from SCons.Variables.EnumVariable import EnumVariable as EnumOption
	except ImportError:
		from SCons.Options.EnumOption import EnumOption
	opts.Add(BoolOption('debug', 'debug build', 'no'))
	opts.Add(BoolOption('profile', 'profile build', 'no'))
	opts.Add(BoolOption('quiet', 'quiet command lines', 'yes'))
	opts.Add(EnumOption('machine', 'use machine-specific assembly code', default_machine,
											 allowed_values=('generic', 'ppc', 'x86', 'x86_64')))
	opts.Add(EnumOption('platform', 'target platform', default_platform,
											 allowed_values=('linux', 'cell', 'windows', 'winddk', 'wince')))
	opts.Add(EnumOption('toolchain', 'compiler toolchain', 'default',
											 allowed_values=('default', 'crossmingw', 'winsdk', 'winddk')))
	opts.Add(BoolOption('llvm', 'use LLVM', 'no'))
	opts.Add(BoolOption('dri', 'build DRI drivers', default_dri))

<|endoftext|>


<commit_before>#######################################################################
# Common SCons code

import os
import os.path
import re
import subprocess
import sys
import platform as _platform

import SCons.Script.SConscript

#######################################################################
# Defaults

host_platform = _platform.system().lower()
if host_platform.startswith('cygwin'):
    host_platform = 'cygwin'

# Search sys.argv[] for a "platform=foo" argument since we don't have
# an 'env' variable at this point.
if 'platform' in SCons.Script.ARGUMENTS:
    target_platform = SCons.Script.ARGUMENTS['platform']
else:
    target_platform = host_platform

_machine_map = {
    'x86': 'x86',
    'i386': 'x86',
    'i486': 'x86',
    'i586': 'x86',
    'i686': 'x86',
    'BePC': 'x86',
    'Intel': 'x86',
    'ppc': 'ppc',
    'BeBox': 'ppc',
    'BeMac': 'ppc',
    'AMD64': 'x86_64',
    'x86_64': 'x86_64',
    'sparc': 'sparc',
    'sun4u': 'sparc',
}

# find host_machine value
if 'PROCESSOR_ARCHITECTURE' in os.environ:
    host_machine = os.environ['PROCESSOR_ARCHITECTURE']
else:
    host_machine = _platform.machine()
host_machine = _machine_map.get(host_machine, 'generic')

default_machine = host_machine
default_toolchain = 'default'

if target_platform == 'windows' and host_platform != 'windows':
    default_machine = 'x86'
    default_toolchain = 'crossmingw'

# find default_llvm value
if 'LLVM' in os.environ:
    default_llvm = 'yes'
else:
    default_llvm = 'no'
    try:
        if target_platform != 'windows' and \
           subprocess.call(['llvm-config', '--version'],
                           stdout=subprocess.PIPE) == 0:
            default_llvm = 'yes'
    except:
        pass

#######################################################################
# Common options

def AddOptions(opts):
    try:
        from SCons.Variables.BoolVariable import BoolVariable as BoolOption
    except ImportError:
        from SCons.Options.BoolOption import BoolOption
    try:
        from SCons.Variables.EnumVariable import EnumVariable as EnumOption
    except ImportError:
        from SCons.Options.EnumOption import EnumOption
    opts.Add(EnumOption('build', 'build type', 'debug',
                        allowed_values=('debug', 'checked', 'profile',
                                        'release', 'opt')))
    opts.Add(BoolOption('verbose', 'verbose output', 'no'))
    opts.Add(EnumOption('machine', 'use machine-specific assembly code',
                        default_machine,
                        allowed_values=('generic', 'ppc', 'x86', 'x86_64')))
    opts.Add(EnumOption('platform', 'target platform', host_platform,
                        allowed_values=('cygwin', 'darwin', 'freebsd', 'haiku',
                                        'linux', 'sunos', 'windows')))
    opts.Add(BoolOption('embedded', 'embedded build', 'no'))
    opts.Add(BoolOption('analyze',
                        'enable static code analysis where available', 'no'))
    opts.Add(BoolOption('asan', 'enable Address Sanitizer', 'no'))
    opts.Add('toolchain', 'compiler toolchain', default_toolchain)
    opts.Add(BoolOption('gles', 'EXPERIMENTAL: enable OpenGL ES support',
                        'no'))
    opts.Add(BoolOption('llvm', 'use LLVM', default_llvm))
    opts.Add(BoolOption('openmp', 'EXPERIMENTAL: compile with openmp (swrast)',
                        'no'))
    opts.Add(BoolOption('debug', 'DEPRECATED: debug build', 'yes'))
    opts.Add(BoolOption('profile', 'DEPRECATED: profile build', 'no'))
    opts.Add(BoolOption('quiet', 'DEPRECATED: profile build', 'yes'))
    opts.Add(BoolOption('texture_float',
                        'enable floating-point textures and renderbuffers',
                        'no'))
    if host_platform == 'windows':
        opts.Add('MSVC_VERSION', 'Microsoft Visual C/C++ version')
<commit_msg>scons: Add swr compile option<commit_after>#######################################################################
# Common SCons code

import os
import os.path
import re
import subprocess
import sys
import platform as _platform

import SCons.Script.SConscript

#######################################################################
# Defaults

host_platform = _platform.system().lower()
if host_platform.startswith('cygwin'):
    host_platform = 'cygwin'

# Search sys.argv[] for a "platform=foo" argument since we don't have
# an 'env' variable at this point.
if 'platform' in SCons.Script.ARGUMENTS:
    target_platform = SCons.Script.ARGUMENTS['platform']
else:
    target_platform = host_platform

_machine_map = {
    'x86': 'x86',
    'i386': 'x86',
    'i486': 'x86',
    'i586': 'x86',
    'i686': 'x86',
    'BePC': 'x86',
    'Intel': 'x86',
    'ppc': 'ppc',
    'BeBox': 'ppc',
    'BeMac': 'ppc',
    'AMD64': 'x86_64',
    'x86_64': 'x86_64',
    'sparc': 'sparc',
    'sun4u': 'sparc',
}

# find host_machine value
if 'PROCESSOR_ARCHITECTURE' in os.environ:
    host_machine = os.environ['PROCESSOR_ARCHITECTURE']
else:
    host_machine = _platform.machine()
host_machine = _machine_map.get(host_machine, 'generic')

default_machine = host_machine
default_toolchain = 'default'

if target_platform == 'windows' and host_platform != 'windows':
    default_machine = 'x86'
    default_toolchain = 'crossmingw'

# find default_llvm value
if 'LLVM' in os.environ:
    default_llvm = 'yes'
else:
    default_llvm = 'no'
    try:
        if target_platform != 'windows' and \
           subprocess.call(['llvm-config', '--version'],
                           stdout=subprocess.PIPE) == 0:
            default_llvm = 'yes'
    except:
        pass

#######################################################################
# Common options

def AddOptions(opts):
    try:
        from SCons.Variables.BoolVariable import BoolVariable as BoolOption
    except ImportError:
        from SCons.Options.BoolOption import BoolOption
    try:
        from SCons.Variables.EnumVariable import EnumVariable as EnumOption
    except ImportError:
        from SCons.Options.EnumOption import EnumOption
    opts.Add(EnumOption('build', 'build type', 'debug',
                        allowed_values=('debug', 'checked', 'profile',
                                        'release', 'opt')))
    opts.Add(BoolOption('verbose', 'verbose output', 'no'))
    opts.Add(EnumOption('machine', 'use machine-specific assembly code',
                        default_machine,
                        allowed_values=('generic', 'ppc', 'x86', 'x86_64')))
    opts.Add(EnumOption('platform', 'target platform', host_platform,
                        allowed_values=('cygwin', 'darwin', 'freebsd', 'haiku',
                                        'linux', 'sunos', 'windows')))
    opts.Add(BoolOption('embedded', 'embedded build', 'no'))
    opts.Add(BoolOption('analyze',
                        'enable static code analysis where available', 'no'))
    opts.Add(BoolOption('asan', 'enable Address Sanitizer', 'no'))
    opts.Add('toolchain', 'compiler toolchain', default_toolchain)
    opts.Add(BoolOption('gles', 'EXPERIMENTAL: enable OpenGL ES support',
                        'no'))
    opts.Add(BoolOption('llvm', 'use LLVM', default_llvm))
    opts.Add(BoolOption('openmp', 'EXPERIMENTAL: compile with openmp (swrast)',
                        'no'))
    opts.Add(BoolOption('debug', 'DEPRECATED: debug build', 'yes'))
    opts.Add(BoolOption('profile', 'DEPRECATED: profile build', 'no'))
    opts.Add(BoolOption('quiet', 'DEPRECATED: profile build', 'yes'))
    opts.Add(BoolOption('texture_float',
                        'enable floating-point textures and renderbuffers',
                        'no'))
    opts.Add(BoolOption('swr', 'Build OpenSWR', 'no'))
    if host_platform == 'windows':
        opts.Add('MSVC_VERSION', 'Microsoft Visual C/C++ version')
<|endoftext|>


<commit_before>""" Extra handlers """
from .handlers import BaseHandler

class TwilioHandler(BaseHandler):

    """
    Check handler that sends SMS via twilio

    Requires that the twilio client be installed ("pip install twilio")

    Parameters
    ----------
    to : str or list
        The number or list of numbers to send the SMS to.
    body : str
        The message body. Will be truncated at 160 chars.
    sid : str, optional
        The account SID (may be specified in config.ini as palantir.twilio.sid)
    token : str, optional
        The auth token (may be specified in config.ini as palantir.twilio.token)
    from_num : str, optional
        The phone number to send the SMS from (may be specified in config.ini
        as palantir.twilio.from_num)

    """
    name = 'twilio'

    def __init__(self, to=(), body="", sid=None, token=None, from_num=None):
        if isinstance(to, basestring):
            self.to = [num.strip() for num in to.split(',')]
        else:
            self.to = to
        if len(body) > 160:
            self.body = self.body[:159] + u'\u2026'
        else:
            self.body = body
        self.sid = sid
        self.token = token
        self.from_num = from_num

    def __call__(self, request, check, normalized_retcode, results, **kwargs):
        token = request.registry.settings.get('palantir.twilio.token',
                                              self.token)
        sid = request.registry.settings.get('palantir.twilio.sid', self.sid)
        from_num = request.registry.settings.get('palantir.twilio.from_num',
                                                 self.from_num)

        from twilio.rest import TwilioRestClient
        client = TwilioRestClient(sid, token)

        for to_num in self.to:
            client.sms.messages.create(
                body=self.body,
                to=to_num,
                from_=from_num)
<commit_msg>Inline pylint warning disable<commit_after>""" Extra handlers """
from .handlers import BaseHandler

class TwilioHandler(BaseHandler):

    """
    Check handler that sends SMS via twilio

    Requires that the twilio client be installed ("pip install twilio")

    Parameters
    ----------
    to : str or list
        The number or list of numbers to send the SMS to.
    body : str
        The message body. Will be truncated at 160 chars.
    sid : str, optional
        The account SID (may be specified in config.ini as palantir.twilio.sid)
    token : str, optional
        The auth token (may be specified in config.ini as palantir.twilio.token)
    from_num : str, optional
        The phone number to send the SMS from (may be specified in config.ini
        as palantir.twilio.from_num)

    """
    name = 'twilio'

    def __init__(self, to=(), body="", sid=None, token=None, from_num=None):
        if isinstance(to, basestring):
            self.to = [num.strip() for num in to.split(',')]
        else:
            self.to = to
        if len(body) > 160:
            self.body = self.body[:159] + u'\u2026'
        else:
            self.body = body
        self.sid = sid
        self.token = token
        self.from_num = from_num

    def __call__(self, request, check, normalized_retcode, results, **kwargs):
        token = request.registry.settings.get('palantir.twilio.token',
                                              self.token)
        sid = request.registry.settings.get('palantir.twilio.sid', self.sid)
        from_num = request.registry.settings.get('palantir.twilio.from_num',
                                                 self.from_num)

        from twilio.rest import TwilioRestClient # pylint: disable=F0401
        client = TwilioRestClient(sid, token)

        for to_num in self.to:
            client.sms.messages.create(
                body=self.body,
                to=to_num,
                from_=from_num)
<|endoftext|>


<commit_before>"""
test_plot:
==========

A module intended for use with Nose.

"""
from __future__ import absolute_import

import requests
import six
import json

from unittest import TestCase
from nose.plugins.attrib import attr
from nose.tools import raises

from plotly.tests.utils import PlotlyTestCase
import plotly.tools as tls
from plotly.plotly import plotly as py
from plotly.exceptions import PlotlyError, PlotlyEmptyDataError
from plotly.files import CONFIG_FILE

# username for tests: 'PlotlyImageTest'
# api_key for account: '786r5mecv0'

@attr('slow')
def test_plot_valid():
    fig = {
        'data': [
            {
                'x': [1, 2, 3],
                'y': [2, 1, 2]
            }
        ]
    }
    py.plot(fig, auto_open=False, filename='plot_valid')

@raises(PlotlyError)
def test_plot_invalid():
    fig = {
        'data': [
            {
                'x': [1, 2, 3],
                'y': [2, 1, 2],
                'z': [3, 4, 1]
            }
        ]
    }
    py.plot(fig, auto_open=False, filename='plot_invalid')

@raises(TypeError)
def test_plot_invalid_args_1():
    py.plot(x=[1, 2, 3], y=[2, 1, 2], auto_open=False, filename='plot_invalid')

@raises(PlotlyError)
def test_plot_invalid_args_2():
    py.plot([1, 2, 3], [2, 1, 2], auto_open=False, filename='plot_invalid')

class TestPlot(TestCase):
    def setUp(self):
        py.sign_in('PlotlyImageTest', '786r5mecv0',
                   plotly_domain='https://plot.ly')
        self.simple_figure = {'data': [{'x': [1, 2, 3], 'y': [2, 1, 2]}]}
        super(TestPlot, self).setUp()

    def test_plot_empty_data(self):
        py.sign_in('PlotlyImageTest', '786r5mecv0')
        self.assertRaises(PlotlyEmptyDataError, py.plot, [],
                          filename='plot_invalid')

    def test_plot_sharing_invalid_argument(self):

        # Raise an error if sharing argument is incorrect
        # correct arguments {'public, 'private', 'secret'}

        kwargs = {'filename': 'invalid-sharing-argument',
                  'sharing': 'privste'}

        with self.assertRaisesRegexp(
                PlotlyError,
                "The 'sharing' argument only accepts"):
            py.plot(self.simple_figure, **kwargs)

    def test_plot_world_readable_sharing_conflict_1(self):

        # Raise an error if world_readable=False but sharing='public'

        kwargs = {'filename': 'invalid-privacy-setting',
                  'world_readable': False,
                  'sharing': 'public'}

        with self.assertRaisesRegexp(
                PlotlyError,
                'setting your plot privacy to both public and private.'):
            py.plot(self.simple_figure, **kwargs)

    def test_plot_world_readable_sharing_conflict_2(self):

        # Raise an error if world_readable=True but sharing='secret'

        kwargs = {'filename': 'invalid-privacy-setting',
                  'world_readable': True,
                  'sharing': 'secret'}

        with self.assertRaisesRegexp(
                PlotlyError,
                'setting your plot privacy to both public and private.'):
            py.plot(self.simple_figure, **kwargs)

    def test_plot_option_logic_only_world_readable_given(self):

        # If sharing is not given and world_readable=False,
        # sharing should be set to private

        kwargs = {'filename': 'test',
                  'auto_open': True,
                  'fileopt': 'overwrite',
                  'validate': True,
                  'world_readable': False}

        plot_option_logic = py._plot_option_logic(kwargs)

        expected_plot_option_logic = {'filename': 'test',
                                      'auto_open': True,
                                      'fileopt': 'overwrite',
                                      'validate': True,
                                      'world_readable': False,
                                      'sharing': 'private'}
        self.assertEqual(plot_option_logic, expected_plot_option_logic)

    def test_plot_option_logic_only_sharing_given(self):

        # If world_readable is not given and sharing ='private',
        # world_readable should be set to False

        kwargs = {'filename': 'test',
                  'auto_open': True,
                  'fileopt': 'overwrite',
                  'validate': True,
                  'sharing': 'private'}

        plot_option_logic = py._plot_option_logic(kwargs)

        expected_plot_option_logic = {'filename': 'test',
                                      'auto_open': True,
                                      'fileopt': 'overwrite',
                                      'validate': True,
                                      'world_readable': False,
                                      'sharing': 'private'}
        self.assertEqual(plot_option_logic, expected_plot_option_logic)

    @attr('slow')
    def test_plot_url_given_sharing_key(self):

        # Give share_key is requested, the retun url should contain
        # the share_key

        validate = True
        fig = tls.return_figure_from_figure_or_data(self.simple_figure,
                                                    validate)
        kwargs = {'filename': 'is_share_key_included',
                  'fileopt': 'overwrite',
                  'world_readable': False,
                  'sharing': 'secret'}
        response = py._send_to_plotly(fig, **kwargs)
        plot_url = response['url']

        self.assertTrue('share_key=' in plot_url)

    @attr('slow')
    def test_plot_url_response_given_sharing_key(self):

        # Given share_key is requested, get request of the url should
        # be 200

        kwargs = {'filename': 'is_share_key_included',
                  'fileopt': 'overwrite',
                  'auto_open': False,
                  'world_readable': False,
                  'sharing': 'secret'}

        plot_url = py.plot(self.simple_figure, **kwargs)
        response = requests.get(plot_url)
        self.assertEqual(response.status_code, 200)

    @attr('slow')
    def test_private_plot_response_with_and_without_share_key(self):

        # The json file of the private plot should be 404 and once
        # share_key is added it should be 200

        kwargs = {'filename': 'is_share_key_included',
                  'fileopt': 'overwrite',
                  'world_readable': False,
                  'sharing': 'private'}

        private_plot_url = py._send_to_plotly(self.simple_figure,
                                              **kwargs)['url']
        private_plot_response = requests.get(private_plot_url + ".json")

        # The json file of the private plot should be 404
        self.assertEqual(private_plot_response.status_code, 404)

        secret_plot_url = py.add_share_key_to_url(private_plot_url)
        urlsplit = six.moves.urllib.parse.urlparse(secret_plot_url)
        secret_plot_json_file = six.moves.urllib.parse.urljoin(
            urlsplit.geturl(), "?.json" + urlsplit.query)
        secret_plot_response = requests.get(secret_plot_json_file)

        # The json file of the secret plot should be 200
        self.assertTrue(secret_plot_response.status_code, 200)

class TestPlotOptionLogic(PlotlyTestCase):
    conflicting_option_set = (
        {'world_readable': True, 'sharing': 'secret', },
        {'world_readable': True, 'sharing': 'private'},
        {'world_readable': False, 'sharing': 'public'}
    )

    def test_default_options(self):
        options = py._plot_option_logic({})
        config_options = tls.get_config_file()
        for key in options:
            if key != 'fileopt' and key in config_options:
                self.assertEqual(options[key], config_options[key])

    def test_conflicting_plot_options_in_plot_option_logic(self):
        for plot_options in self.conflicting_option_set:
            self.assertRaises(PlotlyError, py._plot_option_logic,
                              plot_options)

    def test_set_config_updates_plot_options(self):
        original_config = tls.get_config_file()
        new_options = {
            'world_readable': not original_config['world_readable'],
            'auto_open': not original_config['auto_open'],
            'sharing': ('public' if original_config['world_readable'] is False
                        else 'secret')
        }
        tls.set_config_file(**new_options)
        options = py._plot_option_logic({})
        for key in new_options:
            self.assertEqual(new_options[key], options[key])

def generate_conflicting_plot_options_in_signin():
    """sign_in overrides the default plot options.
    conflicting options aren't raised until plot or iplot is called,
    through _plot_option_logic
    """
    def gen_test(plot_options):
        def test(self):
            py.sign_in('username', 'key', **plot_options)
            self.assertRaises(PlotlyError, py._plot_option_logic, {})
        return test

    for i, plot_options in enumerate(TestPlotOptionLogic.conflicting_option_set):
        setattr(TestPlotOptionLogic,
                'test_conflicting_plot_options_in_signin_{}'.format(i),
                gen_test(plot_options))
generate_conflicting_plot_options_in_signin()

def generate_conflicting_plot_options_in_tools_dot_set_config():
    """tls.set_config overrides the default plot options.
    conflicting options are actually raised when the options are saved,
    because we push out default arguments for folks, and we don't want to
    require users to specify both world_readable and secret *and* we don't
    want to raise an error if they specified only one of these options
    and didn't know that a default option was being saved for them.
    """
    def gen_test(plot_options):
        def test(self):
            self.assertRaises(PlotlyError, tls.set_config_file,
                              **plot_options)
        return test

    for i, plot_options in enumerate(TestPlotOptionLogic.conflicting_option_set):
        setattr(TestPlotOptionLogic,
                'test_conflicting_plot_options_in_'
                'tools_dot_set_config{}'.format(i),
                gen_test(plot_options))
generate_conflicting_plot_options_in_tools_dot_set_config()

def generate_conflicting_plot_options_with_json_writes_of_config():
    """ if the user wrote their own options in the config file,
    then we'll raise the error when the call plot or iplot through
    _plot_option_logic
    """
    def gen_test(plot_options):
        def test(self):
            config = json.load(open(CONFIG_FILE))
            with open(CONFIG_FILE, 'w') as f:
                config.update(plot_options)
                f.write(json.dumps(config))
            self.assertRaises(PlotlyError, py._plot_option_logic, {})
        return test

    for i, plot_options in enumerate(TestPlotOptionLogic.conflicting_option_set):
        setattr(TestPlotOptionLogic,
                'test_conflicting_plot_options_with_'
                'json_writes_of_config{}'.format(i),
                gen_test(plot_options))
generate_conflicting_plot_options_with_json_writes_of_config()

def generate_private_sharing_and_public_world_readable_precedence():
    """ Test that call signature arguments applied through _plot_option_logic
    overwrite options supplied through py.sign_in which overwrite options
    set through tls.set_config
    """
    plot_option_sets = (
        {
            'parent': {'world_readable': True, 'auto_open': False},
            'child': {'sharing': 'secret', 'auto_open': True},
            'expected_output': {'world_readable': False,
                                'sharing': 'secret',
                                'auto_open': True}
        },
        {
            'parent': {'world_readable': True, 'auto_open': True},
            'child': {'sharing': 'private', 'auto_open': False},
            'expected_output': {'world_readable': False,
                                'sharing': 'private',
                                'auto_open': False}
        },
        {
            'parent': {'world_readable': False, 'auto_open': False},
            'child': {'sharing': 'public', 'auto_open': True},
            'expected_output': {'world_readable': True,
                                'sharing': 'public',
                                'auto_open': True}
        }
    )

    def gen_test_signin(plot_options):
        def test(self):
            py.sign_in('username', 'key', **plot_options['parent'])
            options = py._plot_option_logic(plot_options['child'])
            for option, value in plot_options['expected_output'].iteritems():
                self.assertEqual(options[option], value)
        return test

    def gen_test_config(plot_options):
        def test(self):
            tls.set_config(**plot_options['parent'])
            options = py._plot_option_logic(plot_options['child'])
            for option, value in plot_options['expected_output'].iteritems():
                self.assertEqual(options[option], value)

    for i, plot_options in enumerate(plot_option_sets):
        setattr(TestPlotOptionLogic,
                'test_private_sharing_and_public_'
                'world_readable_precedence_signin{}'.format(i),
                gen_test_signin(plot_options))

        setattr(TestPlotOptionLogic,
                'test_private_sharing_and_public_'
                'world_readable_precedence_config{}'.format(i),
                gen_test_config(plot_options))

generate_private_sharing_and_public_world_readable_precedence()
<commit_msg>signin with plotly test case<commit_after>"""
test_plot:
==========

A module intended for use with Nose.

"""
from __future__ import absolute_import

import requests
import six
import json

from unittest import TestCase
from nose.plugins.attrib import attr
from nose.tools import raises

from plotly.tests.utils import PlotlyTestCase
import plotly.tools as tls
from plotly.plotly import plotly as py
from plotly.exceptions import PlotlyError, PlotlyEmptyDataError
from plotly.files import CONFIG_FILE

# username for tests: 'PlotlyImageTest'
# api_key for account: '786r5mecv0'

class TestPlot(TestCase):
    def setUp(self):
        py.sign_in('PlotlyImageTest', '786r5mecv0',
                   plotly_domain='https://plot.ly')
        self.simple_figure = {'data': [{'x': [1, 2, 3], 'y': [2, 1, 2]}]}
        super(TestPlot, self).setUp()

    @attr('slow')
    def test_plot_valid(self):
        fig = {
            'data': [
                {
                    'x': [1, 2, 3],
                    'y': [2, 1, 2]
                }
            ]
        }
        py.plot(fig, auto_open=False, filename='plot_valid')

    @raises(PlotlyError)
    def test_plot_invalid(self):
        fig = {
            'data': [
                {
                    'x': [1, 2, 3],
                    'y': [2, 1, 2],
                    'z': [3, 4, 1]
                }
            ]
        }
        py.plot(fig, auto_open=False, filename='plot_invalid')

    @raises(TypeError)
    def test_plot_invalid_args_1(self):
        py.plot(x=[1, 2, 3], y=[2, 1, 2], auto_open=False, filename='plot_invalid')

    @raises(PlotlyError)
    def test_plot_invalid_args_2(self):
        py.plot([1, 2, 3], [2, 1, 2], auto_open=False, filename='plot_invalid')

    def test_plot_empty_data(self):
        self.assertRaises(PlotlyEmptyDataError, py.plot, [],
                          filename='plot_invalid')

    def test_plot_sharing_invalid_argument(self):

        # Raise an error if sharing argument is incorrect
        # correct arguments {'public, 'private', 'secret'}

        kwargs = {'filename': 'invalid-sharing-argument',
                  'sharing': 'privste'}

        with self.assertRaisesRegexp(
                PlotlyError,
                "The 'sharing' argument only accepts"):
            py.plot(self.simple_figure, **kwargs)

    def test_plot_world_readable_sharing_conflict_1(self):

        # Raise an error if world_readable=False but sharing='public'

        kwargs = {'filename': 'invalid-privacy-setting',
                  'world_readable': False,
                  'sharing': 'public'}

        with self.assertRaisesRegexp(
                PlotlyError,
                'setting your plot privacy to both public and private.'):
            py.plot(self.simple_figure, **kwargs)

    def test_plot_world_readable_sharing_conflict_2(self):

        # Raise an error if world_readable=True but sharing='secret'

        kwargs = {'filename': 'invalid-privacy-setting',
                  'world_readable': True,
                  'sharing': 'secret'}

        with self.assertRaisesRegexp(
                PlotlyError,
                'setting your plot privacy to both public and private.'):
            py.plot(self.simple_figure, **kwargs)

    def test_plot_option_logic_only_world_readable_given(self):

        # If sharing is not given and world_readable=False,
        # sharing should be set to private

        kwargs = {'filename': 'test',
                  'auto_open': True,
                  'fileopt': 'overwrite',
                  'validate': True,
                  'world_readable': False}

        plot_option_logic = py._plot_option_logic(kwargs)

        expected_plot_option_logic = {'filename': 'test',
                                      'auto_open': True,
                                      'fileopt': 'overwrite',
                                      'validate': True,
                                      'world_readable': False,
                                      'sharing': 'private'}
        self.assertEqual(plot_option_logic, expected_plot_option_logic)

    def test_plot_option_logic_only_sharing_given(self):

        # If world_readable is not given and sharing ='private',
        # world_readable should be set to False

        kwargs = {'filename': 'test',
                  'auto_open': True,
                  'fileopt': 'overwrite',
                  'validate': True,
                  'sharing': 'private'}

        plot_option_logic = py._plot_option_logic(kwargs)

        expected_plot_option_logic = {'filename': 'test',
                                      'auto_open': True,
                                      'fileopt': 'overwrite',
                                      'validate': True,
                                      'world_readable': False,
                                      'sharing': 'private'}
        self.assertEqual(plot_option_logic, expected_plot_option_logic)

    @attr('slow')
    def test_plot_url_given_sharing_key(self):

        # Give share_key is requested, the retun url should contain
        # the share_key

        validate = True
        fig = tls.return_figure_from_figure_or_data(self.simple_figure,
                                                    validate)
        kwargs = {'filename': 'is_share_key_included',
                  'fileopt': 'overwrite',
                  'world_readable': False,
                  'sharing': 'secret'}
        response = py._send_to_plotly(fig, **kwargs)
        plot_url = response['url']

        self.assertTrue('share_key=' in plot_url)

    @attr('slow')
    def test_plot_url_response_given_sharing_key(self):

        # Given share_key is requested, get request of the url should
        # be 200

        kwargs = {'filename': 'is_share_key_included',
                  'fileopt': 'overwrite',
                  'auto_open': False,
                  'world_readable': False,
                  'sharing': 'secret'}

        plot_url = py.plot(self.simple_figure, **kwargs)
        response = requests.get(plot_url)
        self.assertEqual(response.status_code, 200)

    @attr('slow')
    def test_private_plot_response_with_and_without_share_key(self):

        # The json file of the private plot should be 404 and once
        # share_key is added it should be 200

        kwargs = {'filename': 'is_share_key_included',
                  'fileopt': 'overwrite',
                  'world_readable': False,
                  'sharing': 'private'}

        private_plot_url = py._send_to_plotly(self.simple_figure,
                                              **kwargs)['url']
        private_plot_response = requests.get(private_plot_url + ".json")

        # The json file of the private plot should be 404
        self.assertEqual(private_plot_response.status_code, 404)

        secret_plot_url = py.add_share_key_to_url(private_plot_url)
        urlsplit = six.moves.urllib.parse.urlparse(secret_plot_url)
        secret_plot_json_file = six.moves.urllib.parse.urljoin(
            urlsplit.geturl(), "?.json" + urlsplit.query)
        secret_plot_response = requests.get(secret_plot_json_file)

        # The json file of the secret plot should be 200
        self.assertTrue(secret_plot_response.status_code, 200)

class TestPlotOptionLogic(PlotlyTestCase):
    conflicting_option_set = (
        {'world_readable': True, 'sharing': 'secret', },
        {'world_readable': True, 'sharing': 'private'},
        {'world_readable': False, 'sharing': 'public'}
    )

    def test_default_options(self):
        options = py._plot_option_logic({})
        config_options = tls.get_config_file()
        for key in options:
            if key != 'fileopt' and key in config_options:
                self.assertEqual(options[key], config_options[key])

    def test_conflicting_plot_options_in_plot_option_logic(self):
        for plot_options in self.conflicting_option_set:
            self.assertRaises(PlotlyError, py._plot_option_logic,
                              plot_options)

    def test_set_config_updates_plot_options(self):
        original_config = tls.get_config_file()
        new_options = {
            'world_readable': not original_config['world_readable'],
            'auto_open': not original_config['auto_open'],
            'sharing': ('public' if original_config['world_readable'] is False
                        else 'secret')
        }
        tls.set_config_file(**new_options)
        options = py._plot_option_logic({})
        for key in new_options:
            self.assertEqual(new_options[key], options[key])

def generate_conflicting_plot_options_in_signin():
    """sign_in overrides the default plot options.
    conflicting options aren't raised until plot or iplot is called,
    through _plot_option_logic
    """
    def gen_test(plot_options):
        def test(self):
            py.sign_in('username', 'key', **plot_options)
            self.assertRaises(PlotlyError, py._plot_option_logic, {})
        return test

    for i, plot_options in enumerate(TestPlotOptionLogic.conflicting_option_set):
        setattr(TestPlotOptionLogic,
                'test_conflicting_plot_options_in_signin_{}'.format(i),
                gen_test(plot_options))
generate_conflicting_plot_options_in_signin()

def generate_conflicting_plot_options_in_tools_dot_set_config():
    """tls.set_config overrides the default plot options.
    conflicting options are actually raised when the options are saved,
    because we push out default arguments for folks, and we don't want to
    require users to specify both world_readable and secret *and* we don't
    want to raise an error if they specified only one of these options
    and didn't know that a default option was being saved for them.
    """
    def gen_test(plot_options):
        def test(self):
            self.assertRaises(PlotlyError, tls.set_config_file,
                              **plot_options)
        return test

    for i, plot_options in enumerate(TestPlotOptionLogic.conflicting_option_set):
        setattr(TestPlotOptionLogic,
                'test_conflicting_plot_options_in_'
                'tools_dot_set_config{}'.format(i),
                gen_test(plot_options))
generate_conflicting_plot_options_in_tools_dot_set_config()

def generate_conflicting_plot_options_with_json_writes_of_config():
    """ if the user wrote their own options in the config file,
    then we'll raise the error when the call plot or iplot through
    _plot_option_logic
    """
    def gen_test(plot_options):
        def test(self):
            config = json.load(open(CONFIG_FILE))
            with open(CONFIG_FILE, 'w') as f:
                config.update(plot_options)
                f.write(json.dumps(config))
            self.assertRaises(PlotlyError, py._plot_option_logic, {})
        return test

    for i, plot_options in enumerate(TestPlotOptionLogic.conflicting_option_set):
        setattr(TestPlotOptionLogic,
                'test_conflicting_plot_options_with_'
                'json_writes_of_config{}'.format(i),
                gen_test(plot_options))
generate_conflicting_plot_options_with_json_writes_of_config()

def generate_private_sharing_and_public_world_readable_precedence():
    """ Test that call signature arguments applied through _plot_option_logic
    overwrite options supplied through py.sign_in which overwrite options
    set through tls.set_config
    """
    plot_option_sets = (
        {
            'parent': {'world_readable': True, 'auto_open': False},
            'child': {'sharing': 'secret', 'auto_open': True},
            'expected_output': {'world_readable': False,
                                'sharing': 'secret',
                                'auto_open': True}
        },
        {
            'parent': {'world_readable': True, 'auto_open': True},
            'child': {'sharing': 'private', 'auto_open': False},
            'expected_output': {'world_readable': False,
                                'sharing': 'private',
                                'auto_open': False}
        },
        {
            'parent': {'world_readable': False, 'auto_open': False},
            'child': {'sharing': 'public', 'auto_open': True},
            'expected_output': {'world_readable': True,
                                'sharing': 'public',
                                'auto_open': True}
        }
    )

    def gen_test_signin(plot_options):
        def test(self):
            py.sign_in('username', 'key', **plot_options['parent'])
            options = py._plot_option_logic(plot_options['child'])
            for option, value in plot_options['expected_output'].iteritems():
                self.assertEqual(options[option], value)
        return test

    def gen_test_config(plot_options):
        def test(self):
            tls.set_config(**plot_options['parent'])
            options = py._plot_option_logic(plot_options['child'])
            for option, value in plot_options['expected_output'].iteritems():
                self.assertEqual(options[option], value)

    for i, plot_options in enumerate(plot_option_sets):
        setattr(TestPlotOptionLogic,
                'test_private_sharing_and_public_'
                'world_readable_precedence_signin{}'.format(i),
                gen_test_signin(plot_options))

        setattr(TestPlotOptionLogic,
                'test_private_sharing_and_public_'
                'world_readable_precedence_config{}'.format(i),
                gen_test_config(plot_options))

generate_private_sharing_and_public_world_readable_precedence()
<|endoftext|>


<commit_before>#!/usr/bin/python2.3
# -*- mode: python -*-
#
# Copyright (c) 2004 Specifix, Inc.
# All rights reserved
#

import sys
if sys.version_info < (2, 3):
    print "error: python 2.3 or greater is requried"
    sys.exit(1)

import options
import branch
import commit
import cook
import cscmd
from local import database
import display
import helper
import importrpm
import log
import os
import queryrep
import repository
import rollbacks
import conarycfg
import srcctl
import updatecmd
import util
import xmlrpclib

sys.excepthook = util.excepthook

try:
    cfg = conarycfg.ConaryConfiguration()
except conarycfg.ParseError, e:
    log.error(str(e))
    sys.exit(1)

def usage(rc = 1):
    print "usage: conary branch <newbranch> <branchfrom> [<trove>]"
    print "       conary changeset <pkg> [<oldver>] <newver> <outfile>"
    print "       conary cook [--prep] [--debug-exceptions] [--macros file] <file.recipe>+"
    print "       conary commit       <changeset>"
    print "       conary erase        <pkgname> [<version>]"
    print "       conary localcs      <pkg> <outfile>"
    print "       conary localcommit  <changeset>"
    print "       conary pkglist      <pkgname> [<version>]"
    print "       conary remove       <path>"
    print "       conary replist      <pkgname> [<version>]"
    print "       conary rblist"
    print "       conary rollback     <rollback>"
    print "       conary source       [usage]"
    print "       conary update       <pkgname> <version>"
    print "              update       <changeset>"
    print "       conary usage"
    print ""
    print "commit flags:  --target-branch <branch>"
    print ""
    print 'common flags:  --build-label <label>'
    print '               --config "<item> <value>"'
    print '               --install-label <label>'
    print "               --reppath <repository-path>"
    print "               --root <root>"
    print ""
    print "cook flags:    --macros"
    print "               --prep"
    print "		  --debug-exceptions"
    print "               --target-branch <branch>"
    print ""
    print "pkglist flags: --full-versions"
    print "               --ids"
    print "               --ls"
    print "               --sha1s"
    print ""
    print "replist flags: --all"
    print "               --full-versions"
    print "               --ids"
    print "               --info"
    print "               --leaves"
    print "               --ls"
    print "               --sha1s"
    print "               --tags"
    print ""
    print "update flags: --keep-existing"
    print "              --replace-files"
    return rc

def openRepository(repMap):
    try:
        return helper.openRepository(repMap)
    except repository.repository.OpenError, e:
	log.error('Unable to open repository %s: %s', path, str(e))
	sys.exit(1)

def openDatabase(root, path):
    try:
        db = database.Database(root, path)
    except repository.repository.OpenError, e:
        log.error('Unable to open database %s%s%s: %s', root, os.sep, path, str(e))
        sys.exit(1)
    return db

def realMain():
    argDef = {}
    cfgMap = {}

    cfgMap["build-label"] = "buildLabel"
    cfgMap["install-label"] = "installLabel"
    cfgMap["root"] = "root"

    argDef["all"] = 0
    argDef["config"] = 2
    argDef["debug"] = 0
    argDef["debug-exceptions"] = 0
    argDef["full-versions"] = 0
    argDef["ids"] = 0
    argDef["info"] = 0
    argDef["keep-existing"] = 0
    argDef["leaves"] = 0
    argDef["ls"] = 0
    argDef["macros"] = 1
    argDef["message"] = 1
    argDef["prep"] = 0
    argDef["profile"] = 0
    argDef["replace-files"] = 0
    argDef["sha1s"] = 0
    argDef["tag-script"] = 1
    argDef["tags"] = 0
    argDef["target-branch"] = 1

    argDef.update(srcctl.argDef)

    try:
        argSet, otherArgs = options.processArgs(argDef, cfgMap, cfg, usage)
    except options.OptionError, e:
        sys.exit(e.val)

    profile = False
    if argSet.has_key('profile'):
	import hotshot
	prof = hotshot.Profile('conary.prof')
	prof.start()
	profile = True
	del argSet['profile']

    if (len(otherArgs) < 2):
	return usage()
    elif (otherArgs[1] == "branch"):
	if argSet: return usage
	if len(otherArgs) < 4 or len(otherArgs) > 5: return usage()
	repos = openRepository(cfg.repositoryMap)

	args = [repos, ] + otherArgs[2:]
	branch.branch(*args)
    elif (otherArgs[1] == "changeset"):
	# current usage is "package file oldversion newversion"
	if len(otherArgs) != 5 and len(otherArgs) != 6:
	    return usage()

	name = otherArgs[2]
	if len(otherArgs) == 6:
	    (old, new) = (otherArgs[3], otherArgs[4])
	    outFile = otherArgs[5]
	else:
	    (old, new) = (None, otherArgs[3])
	    outFile = otherArgs[4]

	repos = openRepository(cfg.repositoryMap)

	cscmd.ChangeSetCommand(repos, cfg, name, outFile, old, new)
    elif (otherArgs[1] == "commit"):
	targetBranch = None
	if argSet.has_key('target-branch'):
	    targetBranch  = argSet['target-branch']
	    del argSet['target-branch']
	if len(otherArgs) < 3: return usage()
	repos = openRepository(cfg.repositoryMap)
	for changeSet in otherArgs[2:]:
	    commit.doCommit(repos, changeSet, targetBranch)
    elif (otherArgs[1] == "config"):
	if argSet: return usage
	if (len(otherArgs) > 2):
	    return usage()
	else:
	    cfg.display()
    elif (otherArgs[1] == "cook"):
	log.setVerbosity(1)
	macros = {}
	prep = 0
	buildBranch = None
	if argSet.has_key('prep'):
	    del argSet['prep']
	    prep = 1
	if argSet.has_key('debug-exceptions'):
	    del argSet['debug-exceptions']
	    cfg.debugRecipeExceptions = True
	if argSet.has_key('macros'):
	    argSet['macros']
	    f = open(argSet['macros'])
	    # XXX sick hack
	    macroSrc = "macros =" + f.read()
	    exec macroSrc
	    del f
	    del argSet['macros']

	if argSet.has_key('target-branch'):
	    buildBranch = argSet['target-branch']
	    del argSet['target-branch']

	if argSet: return usage()

	cook.cookCommand(cfg, otherArgs[2:], prep, macros)
    elif (otherArgs[1] == "erase"):
	kwargs = {}

	if argSet.has_key('tag-script'):
	    kwargs['tagScript'] = argSet['tag-script']
	    del argSet['tag-script']

	if argSet: return usage()

	if len(otherArgs) >= 3 and len(otherArgs) <=4:
	    db = openDatabase(cfg.root, cfg.dbPath)

	    args = [db, cfg] + otherArgs[2:]
	    updatecmd.doErase(*args, **kwargs)
	else:
	    return usage()
    elif (otherArgs[1] == "import"):
	if len(otherArgs) != 3 and len(otherArgs) != 3:
	    return usage()

	repos = openRepository(cfg.repositoryMap)
	importrpm.doImport(repos, cfg, otherArgs[2])
    elif (otherArgs[1] == "localcs"):
	if len(otherArgs) != 4 and len(otherArgs) != 4:
	    return usage()

	name = otherArgs[2]
	outFile = otherArgs[3]

	db = database.Database(cfg.root, cfg.dbPath, "r")
	cscmd.LocalChangeSetCommand(db, cfg, name, outFile)
    elif (otherArgs[1] == "localcommit"):
	if len(otherArgs) < 3: return usage()
	db = database.Database(cfg.root, cfg.dbPath, "c")
	for changeSet in otherArgs[2:]:
	    commit.doLocalCommit(db, changeSet)
    elif (otherArgs[1] == "pkglist"):
	ls = argSet.has_key('ls')
	if ls: del argSet['ls']

	ids = argSet.has_key('ids')
	if ids: del argSet['ids']

	sha1s = argSet.has_key('sha1s')
	if sha1s: del argSet['sha1s']

	fullVersions = argSet.has_key('full-versions')
	if fullVersions: del argSet['full-versions']

	db = openDatabase(cfg.root, cfg.dbPath)

	if argSet: return usage()

	if len(otherArgs) >= 2 and len(otherArgs) <= 4:
	    args = [db, cfg, ls, ids, sha1s, fullVersions] + otherArgs[2:]
	    try:
		display.displayTroves(*args)
	    except IOError, msg:
		sys.stderr.write(msg.strerror + '\n')
		sys.exit(1)
	else:
	    return usage()
    elif (otherArgs[1] == "replist"):
	all = argSet.has_key('all')
	if all: del argSet['all']

	ls = argSet.has_key('ls')
	if ls: del argSet['ls']

	fullVersions = argSet.has_key('full-versions')
	if fullVersions: del argSet['full-versions']

	ids = argSet.has_key('ids')
	if ids: del argSet['ids']

	info = argSet.has_key('info')
	if info: del argSet['info']

	tags = argSet.has_key('tags')
	if tags: del argSet['tags']

	sha1s = argSet.has_key('sha1s')
	if sha1s: del argSet['sha1s']

	leaves = argSet.has_key('leaves')
	if leaves: del argSet['leaves']

	repos = openRepository(cfg.repositoryMap)

	if argSet: return usage()

	if len(otherArgs) >= 2 and len(otherArgs) <= 4:
	    args = [repos, cfg, all, ls, ids, sha1s, leaves, fullVersions,
		    info, tags] + otherArgs[2:]
	    try:
		queryrep.displayTroves(*args)
	    except IOError, msg:
		sys.stderr.write(msg.strerror + '\n')
		sys.exit(1)
	else:
	    return usage()
    elif (otherArgs[1] == "rblist"):
	if argSet: return usage
	db = openDatabase(cfg.root, cfg.dbPath)
	rollbacks.listRollbacks(db, cfg)
    elif (otherArgs[1] == "remove"):
	if len(otherArgs) != 3: return usage()
	if argSet: return usage
	db = openDatabase(cfg.root, cfg.dbPath)
	fullPath = util.joinPaths(cfg.root, otherArgs[2])
	if os.path.exists(fullPath):
	    os.unlink(fullPath)
	else:
	    log.warning("%s has already been removed", fullPath)
	db.removeFile(otherArgs[2])
    elif (otherArgs[1] == "rollback"):
	if argSet: return usage
	db = openDatabase(cfg.root, cfg.dbPath)
	args = [db, cfg] + otherArgs[2:]
	rollbacks.apply(*args)
    elif (otherArgs[1] == "source" or otherArgs[1] == "src"):
	return srcctl.sourceCommand(cfg, otherArgs[2:], argSet)
    elif (otherArgs[1] == "update"):
	kwargs = {}

	replaceFiles = argSet.has_key('replace-files')
	if replaceFiles:
	    kwargs['replaceFiles'] = True
	    del argSet['replace-files']

	keepExisting = argSet.has_key('keep-existing')
	if keepExisting:
	    kwargs['keepExisting'] = True
	    del argSet['keep-existing']

	if argSet.has_key('tag-script'):
	    kwargs['tagScript'] = argSet['tag-script']
	    del argSet['tag-script']

	if argSet: return usage
	if len(otherArgs) >=3 and len(otherArgs) <= 4:
	    repos = openRepository(cfg.repositoryMap)
	    db = openDatabase(cfg.root, cfg.dbPath)

	    args = [repos, db, cfg] + otherArgs[2:]
	    updatecmd.doUpdate(*args, **kwargs)
	else:
	    return usage()
    elif (otherArgs[1] == "return usage"):
	return usage(rc = 0)
    else:
	return usage()

    if profile:
	prof.stop()

    if log.errorOccurred():
	sys.exit(1)

def main():
    try:
	realMain()
    except xmlrpclib.ProtocolError, e:
	if e.errcode == 403:
	    print >> sys.stderr, \
		"remote server denied permission for the requested operation"
	else:
	    raise

if __name__ == "__main__":
    sys.exit(main())
<commit_msg>use tabs, not spaces to indent<commit_after>#!/usr/bin/python2.3
# -*- mode: python -*-
#
# Copyright (c) 2004 Specifix, Inc.
# All rights reserved
#

import sys
if sys.version_info < (2, 3):
    print "error: python 2.3 or greater is requried"
    sys.exit(1)

import options
import branch
import commit
import cook
import cscmd
from local import database
import display
import helper
import importrpm
import log
import os
import queryrep
import repository
import rollbacks
import conarycfg
import srcctl
import updatecmd
import util
import xmlrpclib

sys.excepthook = util.excepthook

try:
    cfg = conarycfg.ConaryConfiguration()
except conarycfg.ParseError, e:
    log.error(str(e))
    sys.exit(1)

def usage(rc = 1):
    print "usage: conary branch <newbranch> <branchfrom> [<trove>]"
    print "       conary changeset <pkg> [<oldver>] <newver> <outfile>"
    print "       conary cook [--prep] [--debug-exceptions] [--macros file] <file.recipe>+"
    print "       conary commit       <changeset>"
    print "       conary erase        <pkgname> [<version>]"
    print "       conary localcs      <pkg> <outfile>"
    print "       conary localcommit  <changeset>"
    print "       conary pkglist      <pkgname> [<version>]"
    print "       conary remove       <path>"
    print "       conary replist      <pkgname> [<version>]"
    print "       conary rblist"
    print "       conary rollback     <rollback>"
    print "       conary source       [usage]"
    print "       conary update       <pkgname> <version>"
    print "              update       <changeset>"
    print "       conary usage"
    print ""
    print "commit flags:  --target-branch <branch>"
    print ""
    print 'common flags:  --build-label <label>'
    print '               --config "<item> <value>"'
    print '               --install-label <label>'
    print "               --reppath <repository-path>"
    print "               --root <root>"
    print ""
    print "cook flags:    --macros"
    print "               --prep"
    print "               --debug-exceptions"
    print "               --target-branch <branch>"
    print ""
    print "pkglist flags: --full-versions"
    print "               --ids"
    print "               --ls"
    print "               --sha1s"
    print ""
    print "replist flags: --all"
    print "               --full-versions"
    print "               --ids"
    print "               --info"
    print "               --leaves"
    print "               --ls"
    print "               --sha1s"
    print "               --tags"
    print ""
    print "update flags: --keep-existing"
    print "              --replace-files"
    return rc

def openRepository(repMap):
    try:
        return helper.openRepository(repMap)
    except repository.repository.OpenError, e:
	log.error('Unable to open repository %s: %s', path, str(e))
	sys.exit(1)

def openDatabase(root, path):
    try:
        db = database.Database(root, path)
    except repository.repository.OpenError, e:
        log.error('Unable to open database %s%s%s: %s', root, os.sep, path, str(e))
        sys.exit(1)
    return db

def realMain():
    argDef = {}
    cfgMap = {}

    cfgMap["build-label"] = "buildLabel"
    cfgMap["install-label"] = "installLabel"
    cfgMap["root"] = "root"

    argDef["all"] = 0
    argDef["config"] = 2
    argDef["debug"] = 0
    argDef["debug-exceptions"] = 0
    argDef["full-versions"] = 0
    argDef["ids"] = 0
    argDef["info"] = 0
    argDef["keep-existing"] = 0
    argDef["leaves"] = 0
    argDef["ls"] = 0
    argDef["macros"] = 1
    argDef["message"] = 1
    argDef["prep"] = 0
    argDef["profile"] = 0
    argDef["replace-files"] = 0
    argDef["sha1s"] = 0
    argDef["tag-script"] = 1
    argDef["tags"] = 0
    argDef["target-branch"] = 1

    argDef.update(srcctl.argDef)

    try:
        argSet, otherArgs = options.processArgs(argDef, cfgMap, cfg, usage)
    except options.OptionError, e:
        sys.exit(e.val)

    profile = False
    if argSet.has_key('profile'):
	import hotshot
	prof = hotshot.Profile('conary.prof')
	prof.start()
	profile = True
	del argSet['profile']

    if (len(otherArgs) < 2):
	return usage()
    elif (otherArgs[1] == "branch"):
	if argSet: return usage
	if len(otherArgs) < 4 or len(otherArgs) > 5: return usage()
	repos = openRepository(cfg.repositoryMap)

	args = [repos, ] + otherArgs[2:]
	branch.branch(*args)
    elif (otherArgs[1] == "changeset"):
	# current usage is "package file oldversion newversion"
	if len(otherArgs) != 5 and len(otherArgs) != 6:
	    return usage()

	name = otherArgs[2]
	if len(otherArgs) == 6:
	    (old, new) = (otherArgs[3], otherArgs[4])
	    outFile = otherArgs[5]
	else:
	    (old, new) = (None, otherArgs[3])
	    outFile = otherArgs[4]

	repos = openRepository(cfg.repositoryMap)

	cscmd.ChangeSetCommand(repos, cfg, name, outFile, old, new)
    elif (otherArgs[1] == "commit"):
	targetBranch = None
	if argSet.has_key('target-branch'):
	    targetBranch  = argSet['target-branch']
	    del argSet['target-branch']
	if len(otherArgs) < 3: return usage()
	repos = openRepository(cfg.repositoryMap)
	for changeSet in otherArgs[2:]:
	    commit.doCommit(repos, changeSet, targetBranch)
    elif (otherArgs[1] == "config"):
	if argSet: return usage
	if (len(otherArgs) > 2):
	    return usage()
	else:
	    cfg.display()
    elif (otherArgs[1] == "cook"):
	log.setVerbosity(1)
	macros = {}
	prep = 0
	buildBranch = None
	if argSet.has_key('prep'):
	    del argSet['prep']
	    prep = 1
	if argSet.has_key('debug-exceptions'):
	    del argSet['debug-exceptions']
	    cfg.debugRecipeExceptions = True
	if argSet.has_key('macros'):
	    argSet['macros']
	    f = open(argSet['macros'])
	    # XXX sick hack
	    macroSrc = "macros =" + f.read()
	    exec macroSrc
	    del f
	    del argSet['macros']

	if argSet.has_key('target-branch'):
	    buildBranch = argSet['target-branch']
	    del argSet['target-branch']

	if argSet: return usage()

	cook.cookCommand(cfg, otherArgs[2:], prep, macros)
    elif (otherArgs[1] == "erase"):
	kwargs = {}

	if argSet.has_key('tag-script'):
	    kwargs['tagScript'] = argSet['tag-script']
	    del argSet['tag-script']

	if argSet: return usage()

	if len(otherArgs) >= 3 and len(otherArgs) <=4:
	    db = openDatabase(cfg.root, cfg.dbPath)

	    args = [db, cfg] + otherArgs[2:]
	    updatecmd.doErase(*args, **kwargs)
	else:
	    return usage()
    elif (otherArgs[1] == "import"):
	if len(otherArgs) != 3 and len(otherArgs) != 3:
	    return usage()

	repos = openRepository(cfg.repositoryMap)
	importrpm.doImport(repos, cfg, otherArgs[2])
    elif (otherArgs[1] == "localcs"):
	if len(otherArgs) != 4 and len(otherArgs) != 4:
	    return usage()

	name = otherArgs[2]
	outFile = otherArgs[3]

	db = database.Database(cfg.root, cfg.dbPath, "r")
	cscmd.LocalChangeSetCommand(db, cfg, name, outFile)
    elif (otherArgs[1] == "localcommit"):
	if len(otherArgs) < 3: return usage()
	db = database.Database(cfg.root, cfg.dbPath, "c")
	for changeSet in otherArgs[2:]:
	    commit.doLocalCommit(db, changeSet)
    elif (otherArgs[1] == "pkglist"):
	ls = argSet.has_key('ls')
	if ls: del argSet['ls']

	ids = argSet.has_key('ids')
	if ids: del argSet['ids']

	sha1s = argSet.has_key('sha1s')
	if sha1s: del argSet['sha1s']

	fullVersions = argSet.has_key('full-versions')
	if fullVersions: del argSet['full-versions']

	db = openDatabase(cfg.root, cfg.dbPath)

	if argSet: return usage()

	if len(otherArgs) >= 2 and len(otherArgs) <= 4:
	    args = [db, cfg, ls, ids, sha1s, fullVersions] + otherArgs[2:]
	    try:
		display.displayTroves(*args)
	    except IOError, msg:
		sys.stderr.write(msg.strerror + '\n')
		sys.exit(1)
	else:
	    return usage()
    elif (otherArgs[1] == "replist"):
	all = argSet.has_key('all')
	if all: del argSet['all']

	ls = argSet.has_key('ls')
	if ls: del argSet['ls']

	fullVersions = argSet.has_key('full-versions')
	if fullVersions: del argSet['full-versions']

	ids = argSet.has_key('ids')
	if ids: del argSet['ids']

	info = argSet.has_key('info')
	if info: del argSet['info']

	tags = argSet.has_key('tags')
	if tags: del argSet['tags']

	sha1s = argSet.has_key('sha1s')
	if sha1s: del argSet['sha1s']

	leaves = argSet.has_key('leaves')
	if leaves: del argSet['leaves']

	repos = openRepository(cfg.repositoryMap)

	if argSet: return usage()

	if len(otherArgs) >= 2 and len(otherArgs) <= 4:
	    args = [repos, cfg, all, ls, ids, sha1s, leaves, fullVersions,
		    info, tags] + otherArgs[2:]
	    try:
		queryrep.displayTroves(*args)
	    except IOError, msg:
		sys.stderr.write(msg.strerror + '\n')
		sys.exit(1)
	else:
	    return usage()
    elif (otherArgs[1] == "rblist"):
	if argSet: return usage
	db = openDatabase(cfg.root, cfg.dbPath)
	rollbacks.listRollbacks(db, cfg)
    elif (otherArgs[1] == "remove"):
	if len(otherArgs) != 3: return usage()
	if argSet: return usage
	db = openDatabase(cfg.root, cfg.dbPath)
	fullPath = util.joinPaths(cfg.root, otherArgs[2])
	if os.path.exists(fullPath):
	    os.unlink(fullPath)
	else:
	    log.warning("%s has already been removed", fullPath)
	db.removeFile(otherArgs[2])
    elif (otherArgs[1] == "rollback"):
	if argSet: return usage
	db = openDatabase(cfg.root, cfg.dbPath)
	args = [db, cfg] + otherArgs[2:]
	rollbacks.apply(*args)
    elif (otherArgs[1] == "source" or otherArgs[1] == "src"):
	return srcctl.sourceCommand(cfg, otherArgs[2:], argSet)
    elif (otherArgs[1] == "update"):
	kwargs = {}

	replaceFiles = argSet.has_key('replace-files')
	if replaceFiles:
	    kwargs['replaceFiles'] = True
	    del argSet['replace-files']

	keepExisting = argSet.has_key('keep-existing')
	if keepExisting:
	    kwargs['keepExisting'] = True
	    del argSet['keep-existing']

	if argSet.has_key('tag-script'):
	    kwargs['tagScript'] = argSet['tag-script']
	    del argSet['tag-script']

	if argSet: return usage
	if len(otherArgs) >=3 and len(otherArgs) <= 4:
	    repos = openRepository(cfg.repositoryMap)
	    db = openDatabase(cfg.root, cfg.dbPath)

	    args = [repos, db, cfg] + otherArgs[2:]
	    updatecmd.doUpdate(*args, **kwargs)
	else:
	    return usage()
    elif (otherArgs[1] == "return usage"):
	return usage(rc = 0)
    else:
	return usage()

    if profile:
	prof.stop()

    if log.errorOccurred():
	sys.exit(1)

def main():
    try:
	realMain()
    except xmlrpclib.ProtocolError, e:
	if e.errcode == 403:
	    print >> sys.stderr, \
		"remote server denied permission for the requested operation"
	else:
	    raise

if __name__ == "__main__":
    sys.exit(main())
<|endoftext|>