예제 #1
0
def play_url(connectionPool, sourceURL, audioOutput, loopCount, loopDelay):
    if '.mp3' in sourceURL or '.wav' in sourceURL:
        tempFileName = "dlfile_ts{TIMESTAMP}_rnd{RAND}.tmp".format(
            TIMESTAMP=time.strftime("%Y%m%d%H%M%S"), RAND=str(uuid.uuid4()))
        download(connectionPool, sourceURL, tempFileName)
        play(tempFileName, audioOutput, loopCount, loopDelay)
        delete_temp()
    else:
        coutput.print_color('red',
                            'ERROR: Unable to play audio from ' + sourceURL)
예제 #2
0
파일: fileio.py 프로젝트: ditojohn/raspi
def play_url(connectionPool, sourceURL, audioOutput, loopCount, loopDelay):
    if ".mp3" in sourceURL or ".wav" in sourceURL:
        tempFileName = "dlfile_ts{TIMESTAMP}_rnd{RAND}.tmp".format(
            TIMESTAMP=time.strftime("%Y%m%d%H%M%S"), RAND=str(uuid.uuid4())
        )
        download(connectionPool, sourceURL, tempFileName)
        play(tempFileName, audioOutput, loopCount, loopDelay)
        delete_temp()
    else:
        coutput.print_color("red", "ERROR: Unable to play audio from " + sourceURL)
예제 #3
0
def lookup_word(connectionPool, pronAudioOutput, pronLoopCount, pronLoopDelaySec, word, *lookupSource):
    _FUNC_NAME_ = "lookup_word"

    isError = False

    dictSources = []
    if len(lookupSource) == 0:
        dictEntry = fetch_dictionary_entry(connectionPool, word)

        currentDefinitions = dictEntry[1]
        source = dictEntry[2]
        currentClipWord = dictEntry[3]
        currentClipURL = dictEntry[4]
        pronSource = dictEntry[5]

        display_dictionary_entry(connectionPool, pronAudioOutput, pronLoopCount, pronLoopDelaySec, word, currentDefinitions, source, currentClipWord, currentClipURL, pronSource)

    elif lookupSource[0].lower() == 'all' or lookupSource[0].lower() in DICT_SOURCES.keys():
        
        if lookupSource[0].lower() == 'all':
            dictSources = dictSources + PRIORITIZED_DICT_SOURCES

        else:                
            dictSources.append(DICT_SOURCES[lookupSource[0].lower()])

        DEBUG_VAR="dictSources"
        coutput.print_debug(ERR_DEBUG, _FUNC_NAME_, "{0} :: {1}".format(DEBUG_VAR, type(dictSources)))
        coutput.print_debug(ERR_DEBUG, _FUNC_NAME_, eval(DEBUG_VAR))     
        
        for dictSource in dictSources:
            source = dictSource.get_dictionary_source()
            pronSource = dictSource.get_dictionary_source()

            DEBUG_VAR="source"
            coutput.print_debug(ERR_DEBUG, _FUNC_NAME_, "{0} :: {1}".format(DEBUG_VAR, type(source)))
            coutput.print_debug(ERR_DEBUG, _FUNC_NAME_, eval(DEBUG_VAR))

            dictEntryText = dictSource.get_dictionary_entry(connectionPool, word)
            currentDefinitions = dictSource.parse_word_definition(word, dictEntryText)
            [currentClipWord, currentClipURL] = dictSource.parse_word_clip(word, dictEntryText)

            display_dictionary_entry(connectionPool, pronAudioOutput, pronLoopCount, pronLoopDelaySec, word, currentDefinitions, source, currentClipWord, currentClipURL, pronSource)

    else:
        print ""
        displayMessage = "ERROR: Unable to lookup {WORD}. Dictionary source {SOURCE} not supported".format(WORD=word, SOURCE=lookupSource[0])
        coutput.print_color(ERROR_TEXT_COLOR, displayMessage)

    print ""
예제 #4
0
def lookup_word(connectionPool, pronAudioOutput, pronLoopCount, pronLoopDelaySec, word, *lookupSource):
    _FUNC_NAME_ = "lookup_word"

    isError = False

    dictSources = []
    if len(lookupSource) == 0:
        dictEntry = fetch_dictionary_entry(connectionPool, word)

        currentDefinitions = dictEntry[1]
        source = dictEntry[2]
        currentClipWord = dictEntry[3]
        currentClipURL = dictEntry[4]
        pronSource = dictEntry[5]

        display_dictionary_entry(connectionPool, pronAudioOutput, pronLoopCount, pronLoopDelaySec, word, currentDefinitions, source, currentClipWord, currentClipURL, pronSource)

    elif lookupSource[0].lower() == 'all' or lookupSource[0].lower() in DICT_SOURCES.keys():
        
        if lookupSource[0].lower() == 'all':
            dictSources = dictSources + PRIORITIZED_DICT_SOURCES

        else:                
            dictSources.append(DICT_SOURCES[lookupSource[0].lower()])

        coutput.print_watcher(ERR_DEBUG, _FUNC_NAME_, 'dictSources')

        for dictSource in dictSources:
            source = dictSource.get_dictionary_source()
            pronSource = dictSource.get_dictionary_source()

            coutput.print_watcher(ERR_DEBUG, _FUNC_NAME_, 'source')

            dictEntryText = dictSource.get_dictionary_entry(connectionPool, word)
            currentDefinitions = dictSource.parse_word_definition(word, dictEntryText)
            [currentClipWord, currentClipURL] = dictSource.parse_word_clip(word, dictEntryText)

            display_dictionary_entry(connectionPool, pronAudioOutput, pronLoopCount, pronLoopDelaySec, word, currentDefinitions, source, currentClipWord, currentClipURL, pronSource)

    else:
        print ""
        displayMessage = "ERROR: Unable to lookup {WORD}. Dictionary source {SOURCE} not supported".format(WORD=word, SOURCE=lookupSource[0])
        coutput.print_color(ERROR_TEXT_COLOR, displayMessage)

    print ""
예제 #5
0
def display_dictionary_entry(connectionPool, pronAudioOutput, pronLoopCount, pronLoopDelaySec, word, currentDefinitions, source, currentClipWord, currentClipURL, pronSource):
    print ""

    if len(currentDefinitions) == 0:
        displayMessage = "No definitions available for {WORD}".format(WORD=word)
        coutput.print_color(WARNING_TEXT_COLOR, displayMessage)
    else:
        displayMessage = "Definition of {WORD} from {SOURCE}:".format(WORD=word, SOURCE=source)
        coutput.print_color(HEADER_TEXT_COLOR, displayMessage)
        for definition in currentDefinitions:
            print u"{BULLET}{ITEM}".format(BULLET=DICT_LIST_BULLET, ITEM=definition)

    if currentClipWord == "":
        displayMessage = "No pronunciation available for {WORD}".format(WORD=word)
        coutput.print_color(WARNING_TEXT_COLOR, displayMessage)
    else:
        displayMessage = "Pronunciation of {WORD} from {SOURCE}:".format(WORD=currentClipWord, SOURCE=pronSource)
        coutput.print_color(SECTION_TEXT_COLOR, displayMessage)
        print u"{BULLET}{ITEM}".format(BULLET=DICT_LIST_BULLET, ITEM=currentClipURL)
        cfile.play_url(connectionPool, currentClipURL, pronAudioOutput, pronLoopCount, pronLoopDelaySec)
예제 #6
0
def display_dictionary_entry(connectionPool, pronAudioOutput, pronLoopCount, pronLoopDelaySec, word, currentDefinitions, source, currentClipWord, currentClipURL, pronSource):
    print ""

    if len(currentDefinitions) == 0:
        displayMessage = "No definitions available for {WORD}".format(WORD=word)
        coutput.print_color(WARNING_TEXT_COLOR, displayMessage)
    else:
        displayMessage = "Definition of {WORD} from {SOURCE}:".format(WORD=word, SOURCE=source)
        coutput.print_color(HEADER_TEXT_COLOR, displayMessage)
        for definition in currentDefinitions:
            print u"{BULLET}{ITEM}".format(BULLET=DICT_LIST_BULLET, ITEM=definition)

    if currentClipWord == "":
        displayMessage = "No pronunciation available for {WORD}".format(WORD=word)
        coutput.print_color(WARNING_TEXT_COLOR, displayMessage)
    else:
        displayMessage = "Pronunciation of {WORD} from {SOURCE}:".format(WORD=currentClipWord, SOURCE=pronSource)
        coutput.print_color(SECTION_TEXT_COLOR, displayMessage)
        print u"{BULLET}{ITEM}".format(BULLET=DICT_LIST_BULLET, ITEM=currentClipURL)
        cfile.play_url(connectionPool, currentClipURL, pronAudioOutput, pronLoopCount, pronLoopDelaySec)
예제 #7
0
def run_revision(spellBee):
    _FUNC_NAME_ = "run_revision"

    spellBee.print_active_word_list()
    display_help("revise")
    userInput = cinput.get_keypress("\nReady to revise? Press any key when ready ... ")

    testDate = time.strftime('%a %d-%b-%Y %H:%M:%S')
    testTotalCount = spellBee.active_word_count()
    testCorrectCount = 0

    userResponse = SB_EMPTY_STRING
    testValuation = SB_EMPTY_STRING

    spellBee.reset_test_result()
  
    activeWordIndex = 0

    while True:
        coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "activeWordIndexList :: {0}".format(spellBee.activeWordIndexList))
        coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "activeWordIndex :: {0}".format(activeWordIndex))
        if (activeWordIndex < 0) or (activeWordIndex >= len(spellBee.activeWordIndexList)):
            break

        wordIndex = spellBee.activeWordIndexList[activeWordIndex]
        coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "wordIndex :: {0}".format(wordIndex))

        # Lookup word definition
        spellBee.lookup_dictionary_by_index(wordIndex)
        spellBee.display_word_cue(SB_STUDY_WORD_DEFN_TITLE.format(INDEX=wordIndex + 1, WORD=spellBee.activeWord))
        userResponse = cinput.get_keypress("Enter response: ")

        # E[x]it test
        if userResponse.lower() == "x":
            break

        # Re[v]iew active word list
        elif userResponse.lower() == "v":
            print SB_EMPTY_STRING
            spellBee.print_active_word_list()
            continue

        # Display [h]elp and statistics
        elif userResponse.lower() == "h":
            print SB_EMPTY_STRING
            spellBee.display_about()
            display_help("revise")
            continue

        # Process correct response
        elif userResponse.lower() == "y":
            correctResponse = True
            testValuation = SB_RIGHT_SYMBOL + " " + spellBee.activeWord
            testCorrectCount += 1

            # Display valuation
            # Handle display text in ascii
            asciiTestValuation = testValuation.encode('utf-8')
            coutput.print_color('green', " " * 50 + asciiTestValuation)

            # Save valuation
            spellBee.log_test_valuation(testValuation)

            # Move to next word
            activeWordIndex += 1

        # Process incorrect response
        elif userResponse.lower() == "n":
            correctResponse = False
            testValuation = SB_WRONG_SYMBOL + " " + spellBee.activeWord
            spellBee.log_practice_word(spellBee.activeWord)

            # Display valuation
            # Handle display text in ascii
            asciiTestValuation = testValuation.encode('utf-8')
            coutput.print_color('red', " " * 50 + asciiTestValuation)

            # Save valuation
            spellBee.log_test_valuation(testValuation)

            # Move to next word
            activeWordIndex += 1

        # [R]epeat question as default action
        else:
            continue
    
    spellBee.log_test_result(testDate, str(testCorrectCount) + "/" + str(testTotalCount))
    print "\nYour revision is complete. Displaying results..."
    
    spellBee.display_evaluation_result('revise', SB_TEST_SAVE_RESULT, True)
예제 #8
0
def run_test(spellBee):
    _FUNC_NAME_ = "run_test"

    spellBee.display_about()
    display_help("test")
    userInput = cinput.get_keypress("\nReady for the test? Press any key when ready ... ")

    testDate = time.strftime('%a %d-%b-%Y %H:%M:%S')
    testTotalCount = spellBee.active_word_count()
    testCorrectCount = 0

    userResponse = SB_EMPTY_STRING
    testValuation = SB_EMPTY_STRING

    spellBee.reset_test_result()

    
    # Disable saving practice words if :
    # saving is disabled for test results, or
    # the test is based on practice, revision or wild card lists
    savePracticeWordsEnabled = SB_TEST_SAVE_PRACTICE
    if SB_TEST_SAVE_RESULT == False or 'practice' in spellBee.contestList.lower() or 'revision' in spellBee.contestList.lower() or '*' in spellBee.contestList:
        savePracticeWordsEnabled = False
    coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "SB_TEST_SAVE_PRACTICE :: {0}".format(SB_TEST_SAVE_PRACTICE))
    coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "savePracticeWordsEnabled :: {0}".format(savePracticeWordsEnabled))

    activeWordIndex = 0

    while True:
        coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "activeWordIndexList :: {0}".format(spellBee.activeWordIndexList))
        coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "activeWordIndex :: {0}".format(activeWordIndex))
        if (activeWordIndex < 0) or (activeWordIndex >= len(spellBee.activeWordIndexList)):
            break

        wordIndex = spellBee.activeWordIndexList[activeWordIndex]
        coutput.print_debug(SB_ERR_DEBUG, _FUNC_NAME_, "wordIndex :: {0}".format(wordIndex))

        # Lookup word definition
        spellBee.lookup_dictionary_by_index(wordIndex)
        spellBee.display_word_cue(SB_PRACTICE_WORD_DEFN_TITLE.format(INDEX=wordIndex + 1))
        userResponse = cinput.get_input("Enter spelling: ")

        # E[x]it test
        if userResponse.lower() == "x":
            break

        # [R]epeat question
        elif userResponse.lower() == "r":
            continue

        # Display [h]elp and statistics
        elif userResponse.lower() == "h":
            print SB_EMPTY_STRING
            spellBee.display_about()
            display_help("test")
            continue

        else:
            correctResponse = False

            # Process correct response
            if spellBee.valuate_test_response(userResponse, spellBee.activeWord, SB_TEST_MODE):
                correctResponse = True
                testValuation = SB_RIGHT_SYMBOL + " " + userResponse
                testCorrectCount += 1
            # Process incorrect response
            else:
                testValuation = SB_WRONG_SYMBOL + " " + userResponse
                spellBee.log_practice_word(spellBee.activeWord)

            # Indicate correct form of the answer, if different from the response
            if userResponse != spellBee.activeWord:
                testValuation = testValuation + " (" + spellBee.activeWord + ")"

            # Display valuation
            # Handle display text in ascii
            asciiTestValuation = testValuation.encode('utf-8')
            if correctResponse:
                coutput.print_color('green', " " * 50 + asciiTestValuation)
            else:
                coutput.print_color('red', " " * 50 + asciiTestValuation)
            
            # Save valuation
            spellBee.log_test_valuation(testValuation)

            # Move to next word
            activeWordIndex += 1
    
    spellBee.log_test_result(testDate, str(testCorrectCount) + "/" + str(testTotalCount))
    print "\nYour test is complete. Displaying results..."
    
    spellBee.display_evaluation_result('test', SB_TEST_SAVE_RESULT, savePracticeWordsEnabled)
    wordEntry = cdict.fetch_dictionary_entry(connectionPool, word)
    coutput.print_watcher(SB_ERR_DEBUG, _FUNC_NAME_, 'wordEntry')

    SDO_ERR_DEFN_MISSING = False
    SDO_ERR_CLIP_MISSING = False
    
    print unicode("Word: {0}\t{1}", 'utf-8').format(word, logValues[2])

    if SDO_ERR_DEFN_REGEX_PATTERN.match(logValues[2]):
        coutput.print_watcher(SB_ERR_DEBUG, _FUNC_NAME_, 'wordEntry[1]')
        if len(wordEntry[1]) > 0:
            print ">> Downloaded definition override"
            cfile.write(SDO_OVERRIDE_DEFN_FILE.format(WORD=word), coutput.multiline_text(wordEntry[1]))
        else:
            SDO_ERR_DEFN_MISSING = True
            coutput.print_color('yellow', "WARNING: Definition override not available")
 
    if SDO_ERR_AUDIO_REGEX_PATTERN.match(logValues[2]):
        coutput.print_watcher(SB_ERR_DEBUG, _FUNC_NAME_, 'wordEntry[4]')
        if wordEntry[4] != "":
            print ">> Downloaded pronunciation override"
            cfile.download(connectionPool, wordEntry[4], SDO_OVERRIDE_PRON_FILE.format(WORD=word))
        else:
            SDO_ERR_CLIP_MISSING = True
            coutput.print_color('yellow', "WARNING: Pronunciation override not available")

    # Log errors
    errorText = unicode("ERROR:{0}:", 'utf-8').format(word)
    if SDO_ERR_DEFN_MISSING:
        errorText += unicode(">Definition Missing", 'utf-8')
    if SDO_ERR_CLIP_MISSING:
예제 #10
0
# Main Program
################################################################

connectionPool = urllib3.PoolManager(10, headers=SDO_USER_AGENT)

wordList = cfile.read(SDO_CONF_FILE).splitlines()

print u"Downloading overrides ..."
for word in wordList:
    print u"Word: " + word
    wordEntry = cdict.fetch_dictionary_entry(connectionPool, word)
    
    if len(wordEntry[1]) > 0:
        cfile.write(SDO_OVERRIDE_DEFN_FILE.format(WORD=word), coutput.multiline_text(wordEntry[1]))
    else:
        coutput.print_color('yellow', "WARNING: Definition override not available")
    if wordEntry[4] != "":
        cfile.download(connectionPool, wordEntry[4], SDO_OVERRIDE_PRON_FILE.format(WORD=word))
    else:
        coutput.print_color('yellow', "WARNING: Pronunciation override not available")


connectionPool.clear()


########################################################################
# Debugging Commands
########################################################################
'''
cd $PROJ
sudo python spellit_download_overrides.py