예제 #1
0
 def getOutput(self, form):
     
     value = form.getlist("media_slider")[0]
     value = utils.decodeUnicode(value)
         
     return value
예제 #2
0
 def processForm(self, form):
     '''
     Get page number + user info from previous page; serialize user data
     '''
     
     userName = ""
     
     cookieTracker = int(form["cookieTracker"].value)
     cookieTracker += 1
     
     pageNum = int(form["pageNumber"].value)
     lastPageNum = pageNum
     
     # This is the default next page, but in the code below we will see
     # if we need to override that decision
     pageNum += 1
     sequenceTitle = self.testSequence.sequenceTitle
     nextPage = None
     
     # If a user name text control is on the page,
     # extract the user name from it
     if "user_name_init" in form:
         
         userName = utils.decodeUnicode(form["user_name_init"].value)
         userName = userName.strip()
         
         # Return to the login page without setting the userName if there is
         # already data associated with that user name
         outputFN = join(self.outputDir, sequenceTitle, userName + ".csv")
         nameAlreadyExists = (os.path.exists(outputFN))
         
         if nameAlreadyExists or userName == "":
             
             # Is relogging in allowed?
             if self.allowUsersToRelogin is True:
                 # Find the last page of saved user data
                 # If the user is coming back, the 'login' page will
                 # reappear on their output file
                 with io.open(outputFN, "r", encoding='utf-8') as fd:
                     pageLineList = fd.readlines()
                 while len(pageLineList) > 0:
                     pageLine = pageLineList.pop(-1)
                     pageArgList = pageLine.split(";,")[0].split(",")
                     if pageArgList[0] == "login":
                         continue
                     
                     pageNum = int(pageArgList[-1]) + 1
                     break
                 
             # If not, throw an error page
             else:
                 nextPage = factories.loadPage(self, "login_bad_user_name",
                                               [userName, ], {})
                 # We wrongly guessed that we would be progressing
                 pageNum -= 1
     
     # Otherwise, the user name, should be stored in the form
     elif "user_name" in form:
         userName = utils.decodeUnicode(form["user_name"].value)
     
     self._testSequenceOverride(userName)
     
     # Get last page info
     lastPage = self.testSequence.getPage(lastPageNum)
     sequenceTitle = self.testSequence.sequenceTitle
         
     # Serialize all variables
     self.serializeResults(form, lastPage, lastPageNum,
                           userName, sequenceTitle)
     
     # Go to a special end state if the user does not consent to the study
     if lastPage.pageName == 'consent':
         if form['radio'].value == 'dissent':
             nextPage = factories.loadPage(self, "consent_end")
     
     # Go to a special end state if the user cannot play audio files
     if lastPage.pageName == 'media_test':
         if form['radio'].value == 'dissent':
             nextPage = factories.loadPage(self, "media_test_end", [], {})
 
     if nextPage is None:
         nextPage = self.testSequence.getPage(pageNum)
     
     return pageNum, cookieTracker, nextPage, userName
예제 #3
0
    def getOutput(self, form):
        
        def replaceCommas(inputItem):
            if isinstance(inputItem, constants.list):
                outputItem = [inputStr.replace(",", "")
                              for inputStr in inputItem]
            else:
                outputItem = inputItem.replace(",", "")
            return outputItem
        
        tmpList = []
        k = 0
        
        # Filter out items with no inputs (essentially notes/comments)
        dataFullList = [item for item in self.surveyItemList
                        if not all([row[0] == "None"
                                    for row in item.widgetList])]
        
        for item in dataFullList:
            
            for i, currentItem in enumerate(item.widgetList):
                itemType, argList = currentItem
                
                value = form.getvalue(str(k))
                
                if not value:
                    value = ""
                    if itemType in ["Choice", "Item_List", "Choicebox"]:
                        # 1 comma between every element
                        value = "," * (len(argList) - 1)
                else:
                    if itemType not in ["Item_List"]:
                        value = utils.decodeUnicode(value)
                        value = replaceCommas(value)
                        
                    # Remove newlines
                    # (because each newline is a new data entry)
                    if itemType == "Multiline_Textbox":
                        newlineChar = utils.detectLineEnding(value)
                        if newlineChar is not None:
                            value = value.replace(newlineChar, " - ")
                    elif itemType in ["Choice", "Choicebox"]:
                        if itemType == "Choice":
                            index = argList.index(value)
                        elif itemType == "Choicebox":
                            index = int(value)
                            
                        valueList = ["0", ] * len(argList)
                        valueList[index] = "1"
                        value = ",".join(replaceCommas(valueList))
                        
                    elif itemType in ["Item_List"]:
                        if isinstance(value, list):
                            value = [utils.decodeUnicode(subval)
                                     for subval in value]
                        else:
                            value = [utils.decodeUnicode(value), ]
                        
                        indexList = [argList.index(subVal) for subVal in value]
                        valueList = ["1" if i in indexList else "0"
                                     for i in range(len(argList))]
                        value = ",".join(replaceCommas(valueList))
                    
                    elif itemType == "None":
                        continue
                
                tmpList.append(value)
                
                k += 1
        
#         tmpList = outputList
        
        return ",".join(tmpList)
예제 #4
0
    def getOutput(self, form):
        
        def replaceCommas(inputItem):
            if isinstance(inputItem, constants.list):
                outputItem = [inputStr.replace(",", "")
                              for inputStr in inputItem]
            else:
                outputItem = inputItem.replace(",", "")
            return outputItem
        
        tmpList = []
        k = 0
        
        # Filter out items with no inputs (essentially notes/comments)
        dataFullList = [item for item in self.surveyItemList
                        if not all([row[0] == "None"
                                    for row in item.widgetList])]
        
        for item in dataFullList:
            
            for i, currentItem in enumerate(item.widgetList):
                itemType, argList = currentItem
                
                value = form.getvalue(str(k))
                
                if not value:
                    value = ""
                    if itemType in ["Choice", "Item_List", "Choicebox"]:
                        # 1 comma between every element
                        value = "," * (len(argList) - 1)
                else:
                    if itemType not in ["Item_List"]:
                        value = utils.decodeUnicode(value)
                        value = replaceCommas(value)
                        
                    # Remove newlines
                    # (because each newline is a new data entry)
                    if itemType == "Multiline_Textbox":
                        newlineChar = utils.detectLineEnding(value)
                        if newlineChar is not None:
                            value = value.replace(newlineChar, " - ")
                    elif itemType in ["Choice", "Choicebox"]:
                        if itemType == "Choice":
                            index = argList.index(value)
                        elif itemType == "Choicebox":
                            index = int(value)
                            
                        valueList = ["0", ] * len(argList)
                        valueList[index] = "1"
                        value = ",".join(replaceCommas(valueList))
                        
                    elif itemType in ["Item_List"]:
                        if isinstance(value, list):
                            value = [utils.decodeUnicode(subval)
                                     for subval in value]
                        else:
                            value = [utils.decodeUnicode(value), ]
                        
                        indexList = [argList.index(subVal) for subVal in value]
                        valueList = ["1" if i in indexList else "0"
                                     for i in range(len(argList))]
                        value = ",".join(replaceCommas(valueList))
                    
                    elif itemType == "None":
                        continue
                
                tmpList.append(value)
                
                k += 1
        
#         tmpList = outputList
        
        return ",".join(tmpList)
예제 #5
0
 def getOutput(self, form):
     
     value = form.getlist("media_slider")[0]
     value = utils.decodeUnicode(value)
         
     return value
예제 #6
0
 def processForm(self, form):
     '''
     Get page number + user info from previous page; serialize user data
     '''
     
     userName = ""
     
     cookieTracker = int(form["cookieTracker"].value)
     cookieTracker += 1
     
     pageNum = int(form["pageNumber"].value)
     lastPageNum = pageNum
     
     # This is the default next page, but in the code below we will see
     # if we need to override that decision
     pageNum += 1
     sequenceTitle = self.testSequence.sequenceTitle
     nextPage = None
     
     # If a user name text control is on the page,
     # extract the user name from it
     if "user_name_init" in form:
         
         userName = utils.decodeUnicode(form["user_name_init"].value)
         userName = userName.strip()
         
         # Return to the login page without setting the userName if there is
         # already data associated with that user name
         outputFN = join(self.outputDir, sequenceTitle, userName + ".csv")
         nameAlreadyExists = (os.path.exists(outputFN))
         
         if nameAlreadyExists or userName == "":
             
             # Is relogging in allowed?
             if self.allowUsersToRelogin is True:
                 # Find the last page of saved user data
                 # If the user is coming back, the 'login' page will
                 # reappear on their output file
                 with io.open(outputFN, "r", encoding='utf-8') as fd:
                     pageLineList = fd.readlines()
                 while len(pageLineList) > 0:
                     pageLine = pageLineList.pop(-1)
                     pageArgList = pageLine.split(";,")[0].split(",")
                     if pageArgList[0] == "login":
                         continue
                     
                     pageNum = int(pageArgList[-1]) + 1
                     break
                 
             # If not, throw an error page
             else:
                 nextPage = factories.loadPage(self, "login_bad_user_name",
                                               [userName, ], {})
                 # We wrongly guessed that we would be progressing
                 pageNum -= 1
     
     # Otherwise, the user name, should be stored in the form
     elif "user_name" in form:
         userName = utils.decodeUnicode(form["user_name"].value)
     
     self._testSequenceOverride(userName)
     
     # Get last page info
     lastPage = self.testSequence.getPage(lastPageNum)
     sequenceTitle = self.testSequence.sequenceTitle
         
     # Serialize all variables
     self.serializeResults(form, lastPage, lastPageNum,
                           userName, sequenceTitle)
     
     # Go to a special end state if the user does not consent to the study
     if lastPage.pageName == 'consent':
         if form['radio'].value == 'dissent':
             nextPage = factories.loadPage(self, "consent_end")
     
     # Go to a special end state if the user cannot play audio files
     if lastPage.pageName == 'media_test':
         if form['radio'].value == 'dissent':
             nextPage = factories.loadPage(self, "media_test_end", [], {})
 
     if nextPage is None:
         nextPage = self.testSequence.getPage(pageNum)
     
     return pageNum, cookieTracker, nextPage, userName
예제 #7
0
def runExperiment(leafFolder,
                  sequenceFile,
                  languageFile,
                  disableRefresh,
                  audioExtList=None,
                  videoExtList=None,
                  allowUtilityScripts=False,
                  allowUsersToRelogin=False,
                  individualSequences=False):

    form = cgi.FieldStorage(keep_blank_values=True)

    # Utility scripts that override the main functionality
    keyList = [
        "get_test_duration",
        "sequence_check",
        "create_dictionary",
        "update_dictionary",
        "crop_dictionary",
        "post_process_results",
    ]
    keyDict = {}
    for key in keyList:
        if key in form:
            keyDict[key] = utils.decodeUnicode(form[key].value.lower())

    if allowUtilityScripts and len(keyDict) > 0:

        # Get experiment and sequence information
        surveyRoot = join(constants.rootDir, "tests", leafFolder)

        timestampFmt = '{:%Y-%m-%d_%H-%M-%S}'
        projectName = sequence.parseSequence(join(surveyRoot, sequenceFile))[0]
        outputDir = join(surveyRoot, "output", projectName)
        loggerPath = join(surveyRoot, "logs")
        utils.makeDir(loggerPath)

        print('Content-Type: text/html')
        print("\n\n")

        if "create_dictionary" in keyDict.keys():

            with Logger() as output:
                print("Creating dictionary...")
                gen_dict.generateLanguageDictionary("new", leafFolder,
                                                    sequenceFile, languageFile)

        if "update_dictionary" in keyDict.keys():
            with Logger() as output:
                print("Updating dictionary...")
                gen_dict.generateLanguageDictionary("update", leafFolder,
                                                    sequenceFile, languageFile)

        if "crop_dictionary" in keyDict.keys():
            with Logger() as output:
                print("Cropping dictionary...")
                gen_dict.generateLanguageDictionary("crop", leafFolder,
                                                    sequenceFile, languageFile)

        if "get_test_duration" in keyDict.keys():
            with Logger() as output:
                print("Getting experiment duration...")
            now = timestampFmt.format(datetime.datetime.now())
            fn = now + "get_duration.txt"
            with Logger(join(loggerPath, fn)) as output:
                get_test_duration.printTestDuration(outputDir)

        if "post_process_results" in keyDict.keys():
            with Logger() as output:
                print("Post processing results...")
            now = timestampFmt.format(datetime.datetime.now())
            fn = now + "_post_process_results.txt"
            with Logger(join(loggerPath, fn)) as output:
                post_process_results.postProcessResults(
                    leafFolder, sequenceFile, True)

    survey = lmeds_main.WebSurvey(leafFolder,
                                  sequenceFile,
                                  languageFile,
                                  disableRefresh,
                                  audioExtList=audioExtList,
                                  videoExtList=videoExtList,
                                  allowUsersToRelogin=allowUsersToRelogin,
                                  individualSequences=individualSequences)

    # For utility scripts that need the survey:
    if allowUtilityScripts and len(keyDict) > 0:

        if "sequence_check" in keyDict.keys():
            with Logger() as output:
                print("Checking sequence file for errors...")
                sequence_check.checkSequenceFile(survey)

        print("<br /><br />Done")
        exit()

    # The main survey
    survey.run(form)