Example #1
0
    def request_with_results(self, command_string, received=None, preserve_encoding=False):
        """
        Request with results
        Return tuple (count, results, error_occurred)
        """
        quotedColon = pylms.quote(':', self.charset)
        #debugindent
        if 1==1:
        # try:
            #init
            if command_string:
                #request command string
                resultStr = self.request(command_string, received, True)

            elif received is not None:
                resultStr = received

            if self.player_update_received(resultStr):
                data = [pylms.unquote(i, self.charset) for i in resultStr.split(" ")]
                self.update(data)

            else:
                #get number of results
                resultStr = ' ' + resultStr
                count = 0
                if resultStr.rfind('count%s' % quotedColon) >= 0:
                    count = int(resultStr[resultStr.rfind(
                        'count%s' % quotedColon):].replace(
                        'count%s' % quotedColon, ''))
                # remove number of results from result string and cut
                # result string by "id:"
                idIsSep = True
                if resultStr.find(' id%s' % quotedColon) < 0:
                    idIsSep = False
                if resultStr.find('count') >= 0:
                    resultStr = resultStr[:resultStr.rfind('count')-1]
                results = resultStr.split(' id%s' % quotedColon)

                output = []
                for result in results:
                    result = result.strip()
                    if len(result) > 0:
                        if idIsSep:
                            #fix missing 'id:' at beginning
                            result = 'id%s%s' % (quotedColon, result)
                        subResults = result.split(' ')
                        item = {}
                        for subResult in subResults:
                            #save item
                            try:
                                key, value = subResult.split(quotedColon, 1)
                            except Exception as ex:
                                key, value = subResult, ""
                            if not preserve_encoding:
                                item[urllib.unquote(key)] = pylms.unquote(value, self.charset)
                            else:
                                item[key] = value
                        output.append(item)
                return count, output, False
Example #2
0
 def decode_response(self, response):
     return pylms.decode(pylms.unquote(response, self.charset), self.charset)