Example #1
0
 def encode(self, input, final=False):
     if self.first:
         self.first = 0
         return codecs.BOM_UTF8 + \
                codecs.utf_8_encode(input, self.errors)[0]
     else:
         return codecs.utf_8_encode(input, self.errors)[0]
Example #2
0
    def write(self, message):
        """Write a message to the output stream."""

        message = self.encoder.encode(message)
        self.out.write(utf_8_encode(message)[0])
        self.out.write(utf_8_encode('\n// END\n')[0])
        self.out.flush()
Example #3
0
 def Instructions(self,myfsm):
     self.score_to_win=500
     base.setBackgroundColor(0,0,0)
     for i in self.items:
         i.destroy()
     self.logo = OnscreenImage(image = 'models/BG_Screen01.jpg', pos = (0,0,0), scale=1.5)
     font = loader.loadFont('fonts/PLUMP.TTF')
     self.accept("s", myfsm.request, ["Game", self.level])
     self.accept("escape", sys.exit)
     self.logo2= OnscreenImage(image = 'models/Normal.png', pos = (-1.25,0,0.3), scale=0.115)
     self.logo3= OnscreenImage(image = 'models/mayor.png', pos = (-1.25,0.0,0), scale=0.115)
     self.logo4= OnscreenImage(image = 'models/Dorothy.png', pos = (-1.25,0,-0.3), scale=0.115)
     self.tx = OnscreenText(text=(codecs.utf_8_encode("To complete this level you must get "+"\n"+str(self.score_to_win)+" points")[0]), fg=(1,0.7,0.4,1), scale = 0.09, pos=(0,0.7),font=font, shadow= (0,0,0,1))
     self.tx1= OnscreenText(text=(codecs.utf_8_encode("One normal house gives you 50 points"+"\n"+"\n"+"\n"+"\n"+
                                                    "The mayor house gives you 100 points"+"\n"+"\n"+"\n"+"\n"+
                                                    "The Dorothy house gives you 200 points"+"\n"+"\n"+"\n"+"\n")[0]), fg=(1,0.7,0.4,1), scale = 0.07, pos=(0,0.3),font=font, shadow= (0,0,0,1))
     self.tx2 = OnscreenText(text=(codecs.utf_8_encode("PLEASE WOODY, SAVE THE VILLAGE!!!")[0]), fg=(1,0.7,0.4,1), scale = 0.1, pos=(0,-0.65),font=font, shadow= (0,0,0,1))
     self.tx3 = OnscreenText(text=(codecs.utf_8_encode("S: start game - P: previous - ESC: Exit")[0]), fg=(0.76,0.21,0,02), scale = 0.065, pos=(0,-0.87),font=font, shadow= (0,0,0,1))
     self.items.append(self.tx)
     self.items.append(self.tx2)
     self.items.append(self.tx3)
     self.items.append(self.tx1)
     self.items.append(self.logo)
     self.items.append(self.logo2)
     self.items.append(self.logo3)
     self.items.append(self.logo4)
Example #4
0
    def _buffer_encode_codepoint(self, input, final):
        # Find the next byte position that indicates a variant of UTF-8.
        # CESU-8 sequences always start with 0xed,
        # Java nulls always start with 0xc0.

        index1 = 0
        try:
            while ord(input[index1]) <= 0xffff:
                index1 += 1
        except IndexError:
            index1 = -1
        index2 = input.find('\x00')

        # Set `index` to whichever index comes first.
        if index1 != -1 and index2 != -1:
            index = min(index1, index2)
        elif index1 != -1:
            index = index1
        elif index2 != -1:
            index = index2
        else:
            # No special characters found; encode the remaining
            # input as standard UTF-8
            return codecs.utf_8_encode(input, self.errors)

        if index1 == 0:
            # Encode a six-byte sequence starting with 0xed.
            print([
                    0xED,
                    0xA0 | (((ord(input[0]) >> 16) - 1) & 0x0f),
                    0x80 | ((ord(input[0]) >> 10) & 0x3f),
                    0xED,
                    0xB0 | ((ord(input[0]) >> 6) & 0x0f),
                    0x80 | (ord(input[0]) & 0x3f),
                ])
            return bytes(bytearray([
                    0xED,
                    0xA0 | (((ord(input[0]) >> 16) - 1) & 0x0f),
                    0x80 | ((ord(input[0]) >> 10) & 0x3f),
                    0xED,
                    0xB0 | ((ord(input[0]) >> 6) & 0x0f),
                    0x80 | (ord(input[0]) & 0x3f),
                ])), 1
        elif index2 == 0:
            # Encode a two-byte sequence, 0xc0 0x80.
            return b'\xc0\x80', 1
        else:
            # Encode the bytes up until the next weird thing as UTF-8.
            return codecs.utf_8_encode(input[:index], self.errors)
def save_il_to_word_cloud_file(word_cloud_file,int_scores_il,vocab_size,float_score=False,tagged=False,utf8=False,small_font_size=.3,large_font_size=7,call_R=False):
    """
    Use the item list C{int_scores_il} to print out 

    save this string in the file R_Batch_file
    If call_R = True, call R wordcloud package on wordcloud file
"""
    ctr = 0
    word_set = set()
    with open(word_cloud_file,'w') as ofh:
        print >> ofh, '"Word"   "Score"'
        for (i,w) in enumerate(int_scores_il):
            if i == vocab_size:
                break
            s=int_scores_il[w]
            if utf8:
               w = codecs.utf_8_encode(w)[0]
            if w in word_set:
                continue
            else:
                ctr += 1
                word_set.add(w)
                if float_score:
                    print >> ofh, '"%d" "%s" %.8f' % (ctr,w,s)
                else:
                    print >> ofh, '"%d" "%s" %d' % (ctr,w,s)
    if call_R:
        call_R_on_wordcloud_file (word_cloud_file,small_font_size,large_font_size)
    def remember(self, environ, identity):
        if self.include_ip:
            remote_addr = environ['REMOTE_ADDR']
        else:
            remote_addr = '0.0.0.0'

        cookies = get_cookies(environ)
        old_cookie = cookies.get(self.cookie_name)
        existing = cookies.get(self.cookie_name)
        old_cookie_value = getattr(existing, 'value', None)

        timestamp, userid, tokens, userdata = None, '', (), ''

        if old_cookie_value:
            validation = validateTicket(self.secret, old_cookie_value,
                                        remote_addr, timeout=self.timeout,
                                        mod_auth_tkt=not self.enhanced_hashing)
            if validation:
                ignore, userid, tokens, user_data, timestamp = validation
            else:
                return
        tokens = tuple(tokens)

        who_userid = identity['repoze.who.userid']
        who_tokens = tuple(identity.get('tokens', ()))
        who_userdata = identity.get('userdata', '')

        who_userid = utf_8_encode(who_userid)[0]

        old_data = (userid, tokens, userdata)
        new_data = (who_userid, who_tokens, who_userdata)
Example #7
0
    def parseProduction(parser, lhs, str, tok=None, this=0):
	"The parser itself."

	if tok == "": return tok, this # EOF    
	lookupTable = parser.branchTable[lhs]
	rhs = lookupTable.get(tok, None)  # Predict branch from token
	if rhs == None: raise SyntaxError(
		"Found %s when expecting some form of %s,\n\tsuch as %s\n\t%s"
		    %(utf_8_encode(tok)[0], lhs, lookupTable.keys(), parser.around(str, this)))
	print "%i  %s means expand %s as %s" %(parser.lineNumber,tok, lhs, rhs.value())
	for term in rhs:
	    if isinstance(term, Literal):
		lit = term.value()
		next = this + len(lit)
		if str[this:next] == lit: pass
		elif "@"+str[this:next-1] == lit: next = next-1
		else: raise SyntaxError(
		    "Found %s where %s expected\n\t %s" %
			(str[this:next], lit, parser.around(str, this)))
	    else:
		rexp = tokenRegexps.get(term, None)
		if rexp == None: # Not token
		    tok, this = parser.parseProduction(term, str, tok, this)
		    continue
		m = rexp.match(str, this)
		if m == None:
		    raise SyntaxError("Token: should match %s\n\t" % 
				(rexp.pattern, parser.around(str, this)))
#		print u"Token matched to <%s> as pattern <%s>" % (str[this:m.end()], rexp.pattern)
		next = m.end()
	    tok, this = parser.token(str, next)  # Next token
	return tok, this
Example #8
0
def validate(s):
    correct_utf16 = codecs.utf_16_encode(s)[0][2:]
    utf8 = codecs.utf_8_encode(s)
    p = subprocess.Popen("./utf16", stdin = subprocess.PIPE, stdout = subprocess.PIPE)
    maybe_utf16 = p.communicate(utf8[0])[0]
    if correct_utf16 != maybe_utf16:
        print u"tried to do %r, got back %r, expected %r" % (utf8[0], maybe_utf16, correct_utf16)
        raise "failed"
Example #9
0
def eval_b(x):
    """Evaluate if to encode the value is necessary.
    """
    if sys.version_info.major == 2:
        return x
    else:
        import codecs
        return codecs.utf_8_encode(x)[0]
Example #10
0
def u2s(uni):
    """u2s - unicode to string, the safe way"""

    if isinstance(uni, str):
        return uni
    else:
        try:
            return codecs.utf_8_encode(uni)[0]
        except:
            raise Exception("Ya talkin' gibberish? '{0}'".format(uni))
Example #11
0
def get_nice_arg(arg, sig):
    '''Convert an argument into a Java type when appropriate'''
    env = get_env()
    is_java = (isinstance(arg, javabridge.JB_Object) or
               isinstance(arg, javabridge.JB_Class))
    if sig[0] == 'L' and not is_java:
        #
        # Check for the standard packing of java objects into class instances
        #
        if hasattr(arg, "o"):
            return arg.o
    if (sig in ('Ljava/lang/String;','Ljava/lang/Object;') and not
         isinstance(arg, javabridge.JB_Object)):
        if isinstance(arg, unicode):
            arg, _ = codecs.utf_8_encode(arg)
        else:
            arg = str(arg)
        return env.new_string_utf(arg)
    if sig == 'Ljava/lang/Integer;' and type(arg) in [int, long, bool]:
        return make_instance('java/lang/Integer', '(I)V', int(arg))
    if sig == 'Ljava/lang/Long' and type(arg) in [int, long, bool]:
        return make_instance('java/lang/Long', '(J)V', long(arg))
    if sig == 'Ljava/lang/Boolean;' and type(arg) in [int, long, bool]:
        return make_instance('java/lang/Boolean', '(Z)V', bool(arg))
    if isinstance(arg, np.ndarray):
        if sig == '[Z':
            return env.make_boolean_array(np.ascontiguousarray(arg.flatten(), np.bool8))
        elif sig == '[B':
            return env.make_byte_array(np.ascontiguousarray(arg.flatten(), np.uint8))
        elif sig == '[S':
            return env.make_short_array(np.ascontiguousarray(arg.flatten(), np.int16))
        elif sig == '[I':
            return env.make_int_array(np.ascontiguousarray(arg.flatten(), np.int32))
        elif sig == '[J':
            return env.make_long_array(np.ascontiguousarray(arg.flatten(), np.int64))
        elif sig == '[F':
            return env.make_float_array(np.ascontiguousarray(arg.flatten(), np.float32))
        elif sig == '[D':
            return env.make_double_array(np.ascontiguousarray(arg.flatten(), np.float64))
    elif sig.startswith('L') and sig.endswith(';') and not is_java:
        #
        # Desperately try to make an instance of it with an integer constructor
        #
        if isinstance(arg, (int, long, bool)):
            return make_instance(sig[1:-1], '(I)V', int(arg))
        elif isinstance(arg, (str, unicode)):
            return make_instance(sig[1:-1], '(Ljava/lang/String;)V', arg)
    if sig.startswith('[L') and (not is_java) and hasattr(arg, '__iter__'):
        objs = [get_nice_arg(subarg, sig[1:]) for subarg in arg]
        k = env.find_class(sig[2:-1])
        a = env.make_object_array(len(objs), k)
        for i, obj in enumerate(objs):
            env.set_object_array_element(a, i, obj)
        return a
    return arg
 def __init__(self, filename):
     self.csl_test = CslTest({}, None, (filename, ),
                             os.path.basename(filename))
     self.csl_test.parse()
     csl_io = io.BytesIO(utf_8_encode(self.data['csl'])[0])
     self.style = CitationStylesStyle(csl_io)
     self._fix_input(self.data['input'])
     self.references = [item['id'] for item in self.data['input']]
     self.references_dict = CiteProcJSON(self.data['input'])
     self.bibliography = CitationStylesBibliography(self.style,
                                                    self.references_dict)
     self.expected = self.data['result'].splitlines()
Example #13
0
def check_password_hash(password_raw, password_hash, password_salt):
    """
    Checks the given raw password against the hash and salt.

    Assumes that the hash and salt are given in ASCII hex representation
    and checks whether the password re-generates the hash when hashed
    using the salt.
    """

    return password_hash == hexlify(pbkdf2_hmac(
        'sha256', utf_8_encode(password_raw)[0], unhexlify(password_salt),
        _rounds))
Example #14
0
    def __init__(self, filename):
        with open(filename, encoding='UTF-8') as f:
            self.json_data = json.load(f)

        csl_io = io.BytesIO(utf_8_encode(self.json_data['csl'])[0])
        self.style = CitationStylesStyle(csl_io)
        self._fix_input(self.json_data['input'])
        self.references = [item['id'] for item in self.json_data['input']]
        self.references_dict = CiteProcJSON(self.json_data['input'])
        self.bibliography = CitationStylesBibliography(self.style,
                                                       self.references_dict)
        self.expected = self.json_data['result'].splitlines()
Example #15
0
    def Credits(self,myfsm):
        base.setBackgroundColor(0,0,0)
        for i in self.items:
            i.destroy()
        font = loader.loadFont('fonts/PLUMP.TTF')
        self.accept("i", myfsm.request, ["Intro"])
        self.accept("escape", sys.exit)
        self.tx = OnscreenText(text=(codecs.utf_8_encode("Credits")[0]), fg=(1,1,1,1), scale = 0.1, pos=(0,0.85),font=font, shadow= (0,0,0,1))
        self.tx1= OnscreenText(text=(codecs.utf_8_encode("Gameplay Designer and Developer"+"\n"+"\n"+
                                                       "Andrea Tommaso Bonanno"+"\n"+"\n"+"\n"+
                                                       "Physics Implementation"+"\n"+"\n"+
                                                       "Alfredo Motta"+"\n"+"\n"+"\n"+
                                                       "Sound effects and developer"+"\n"+"\n"+
                                                       "Valerio Panzica La Manna"+"\n"+"\n"+"\n"+
                                                       "Art"+"\n"+"\n"+
                                                       "Ephraim Zev Zimmerman")[0]), fg=(1,1,1,1), scale = 0.07, pos=(0,0.64),font=font, shadow= (0,0,0,1))

        self.tx2 = OnscreenText(text=(codecs.utf_8_encode("I: Intro - ESC: Exit")[0]), fg=(0.76,0.21,0,02), scale = 0.065, pos=(0,-0.9),font=font, shadow= (0,0,0,1))
        self.items.append(self.tx)
        self.items.append(self.tx1)
        self.items.append(self.tx2)
Example #16
0
def create_password_hash(password_raw):
    """
    Create hash and salt for the given raw password.

    The password is encoded as UTF-8 and a salt read from "os.random".
    Returns ASCII hex representation of the hash and salt.
    """

    password_salt = urandom(32)
    password_hash = pbkdf2_hmac(
        'sha256', utf_8_encode(password_raw)[0], password_salt, _rounds)

    return (hexlify(password_hash), hexlify(password_salt))
Example #17
0
    def send(self, message):

        message = utf_8_encode(unicode(message, 'latin-1'))[0] 
        msglen = len(message)
        frameheader = 0x81
        maskbit = 0
        if msglen <= 125: masklen = chr(maskbit | msglen)
        elif msglen < (1 << 16): masklen = chr(maskbit | 126) + struct.pack('!H', msglen)
        elif msglen < (1 << 63): masklen = chr(maskbit | 127) + struct.pack('!Q', msglen)

        framebytes = chr(frameheader) + masklen + message
        with self._writelock:
            self.socket.sendall(framebytes)
Example #18
0
def create(uid,cn,sn):
  l = ldapsetup()

  if exists(uid):
    return

  uidNumber = random.randint(2000000,5000000)

  while l.search_ext_s(ldapbase, filterstr='(uidNumber=%s)' % uidNumber, scope=ldap.SCOPE_SUBTREE):
      uidNumber = random.randint(2000000,5000000)

  l.add_s('uid=%s,ou=Clients,ou=Users,dc=bils,dc=se' % uid,
             [('homeDirectory', ['/home/%s' % str(uid)]),
              ('sn', [codecs.utf_8_encode(sn)[0]]),
              ('cn', [codecs.utf_8_encode(cn)[0]]),
              ('uid',[str(uid)]),
              ('mail',[str(uid)]),
              ('loginShell', ['/bin/bash']),
              ('objectClass', ['posixAccount', 'shadowAccount', 'inetOrgPerson', 'person', 'top', 'organizationalPerson']),
              ('uidNumber', [str(uidNumber)]),
              ('gidNumber', [str(uidNumber)]),
              ])
Example #19
0
 def _buffer_encode_step(self, char, errors, final):
     codepoint = ord(char)
     if codepoint < 65535:
         return codecs.utf_8_encode(char, errors)
     else:
         seq = bytearray(6)
         seq[0] = 0xED
         seq[1] = 0xA0 | (((codepoint & 0x1F0000) >> 16) - 1)
         seq[2] = 0x80 | (codepoint & 0xFC00) >> 10
         seq[3] = 0xED
         seq[4] = 0xB0 | ((codepoint >> 6) & 0x3F)
         seq[5] = 0x80 | (codepoint & 0x3F)
         return bytes(seq), 1
Example #20
0
    def render(self, **kwargs):
        output = getattr(self.source(), self.file_extension)

        RESPONSE = self.request.RESPONSE
        RESPONSE.setHeader(
            "Content-disposition",
            'filename="{}"'.format(codecs.utf_8_encode(self.filename)[0])
        )
        RESPONSE.setHeader(
            "Content-Type", "{};charset=utf-8".format(self.content_type)
        )
        RESPONSE.setHeader("Content-Length", len(output))

        return output
Example #21
0
    def get_response(self, filename, filehandle):
        filename = codecs.utf_8_encode('filename="%s.pdf"' % filename)[0]
        self.request.RESPONSE.setHeader('Content-disposition', filename)
        self.request.RESPONSE.setHeader('Content-Type', 'application/pdf')

        response = filehandle.getvalue()
        filehandle.seek(0, os.SEEK_END)

        filesize = filehandle.tell()
        filehandle.close()

        self.request.RESPONSE.setHeader('Content-Length', filesize)

        return response
Example #22
0
    def handle_export(self):
        try:
            export_fields = self.parameters.get('export_fields')
            export_fields = [
                (id, title) for id, title
                in self.portal_type_fields
                if id in export_fields
            ]

            review_state = self.parameters.get('review_state')
            review_state = None if review_state == '__all__' else review_state

            include_inactive = self.parameters.get('include_inactive')

            dataset = export_people(
                self.request,
                self.context,
                self.portal_type.id,
                export_fields,
                review_state,
                include_inactive
            )

            variant = self.get_variant_by_name(self.parameters.get('variant'))
            if variant:
                dataset = variant.adjust_dataset(dataset, self.parameters)

                if dataset is None:
                    raise ContentExportError(
                        _(u"The selected variant could not be applied")
                    )

            format = self.parameters.get('export_format').lower()
            filename = '%s.%s' % (self.context.title, format)
            filename = codecs.utf_8_encode('filename="%s"' % filename)[0]

            output = getattr(dataset, format)

            RESPONSE = self.request.RESPONSE
            RESPONSE.setHeader("Content-disposition", filename)
            RESPONSE.setHeader(
                "Content-Type", "application/%s;charset=utf-8" % format
            )
            RESPONSE.setHeader("Content-Length", len(output))

            self.output = output

        except ContentExportError as e:
            self.raise_action_error(e.translate(self.request))
Example #23
0
    def render(self, **kwargs):
        filename = '%s.%s' % (self.context.title, self.file_extension)
        filename = codecs.utf_8_encode('filename="%s"' % filename)[0]

        dataset = self.source()
        output = getattr(dataset, self.file_extension)

        RESPONSE = self.request.RESPONSE
        RESPONSE.setHeader("Content-disposition", filename)
        RESPONSE.setHeader(
            "Content-Type", "%s;charset=utf-8" % self.content_type
        )
        RESPONSE.setHeader("Content-Length", len(output))

        return output
Example #24
0
    def test_transport(self):
        in_ = BytesIO(utf_8_encode('{"test_input":1}\n// END\n')[0])
        out = BytesIO()

        xp = TacoTransport(in_, out)

        xp.write({'test_output': 2})

        r = utf_8_decode(out.getvalue())[0]

        self.assertEqual(r, "{\"test_output\": 2}\n// END\n")

        r = xp.read()

        self.assertEqual(r, {'test_input': 1})
def countsyll(instring):
    """This function counts the number of characters in a tamil string
        This is done by ignoring the vowel additions. If one uses the len
        function, the sample string has a length of 17 - but there are actually
        only 11 characters"""
    s = codecs.utf_8_encode(instring)
    print s
    x = codecs.utf_8_decode(s[0])[0]    
    print repr(x)
    syllen = 0
    vowels = [u'\u0bbe',u'\u0bbf',u'\u0bc0',
                u'\u0bc1',u'\u0bc2',u'\u0bc6',
                u'\u0bc7',u'\u0bc8',u'\u0bca',
                u'\u0bcb',u'\u0bcc',u'\u0bcd',]
    for y in x:
        if y not in vowels:
            syllen += 1    
    return syllen
Example #26
0
def progress(*args):
    level = len(traceback.extract_stack())
    sys.stderr.write(" "*level)
    for a in args:
        i = 0
        a = unicode(a)
        while 1:
##    lineCount[0] += 1
            i = a.find("\n", i)
            if i < 0: break
            a = a[:i+1] + (" "*level) + a[i+1:]
            i = i+1
        q = utf_8_encode(u"%s " % (a,))[0]
        sys.stderr.write(q)
##        if lineCount[0] > 20:
##            lineCount[0] = 0
##            sys.stdin.readline()
    sys.stderr.write("\n")
Example #27
0
def progress(*args):
    """Logs a debug statement?
    
    """
    level = len(traceback.extract_stack())
    sys.stderr.write(" " * level)
    for a in args:
        i = 0
        a = unicode(a)
        
        # Indent all the levels of each of the args.
        while 1:
##    lineCount[0] += 1
            i = a.find("\n", i)
            if i < 0: break
            a = a[:i+1] + (" " * level) + a[i+1:]
            i = i + 1
        
        q = utf_8_encode(u"%s " % (a,))[0]
        sys.stderr.write(q)
##        if lineCount[0] > 20:
##            lineCount[0] = 0
##            sys.stdin.readline()
    sys.stderr.write("\n")
Example #28
0
 def on_lineEdit1_textChanged(self):
     req = unicode(self.lineEdit1.text())
     self.lineEditK.setText(roma(req))
     if len(req) == 0:
         return
     latin1 = codecs.utf_8_encode(req)[0]
     self.listWidget.clear()
     v = os.popen("grep '" + latin1 + "' edict.utf8").readlines()
     for entry in [codecs.utf_8_decode(line[:-1])[0] for line in v[:150]]:
         if '['+req+']' in entry or entry.startswith(req + " "):
             # exact match => move to top and color in gray
             self.listWidget.insertItem(0, entry)
             self.listWidget.item(0).setBackground(self.pen)
         else:
             self.listWidget.addItem(entry)
     if len(req) == 1 and req in D_unichar:
         infos_unicode = D_unichar[req]
         entry = "\n".join(["%s: %s" % x for x in (sorted(infos_unicode, reverse=True))])[4:]
         self.listWidget.insertItem(0, entry)
         self.listWidget.item(0).setBackground(self.penUnicode)
         self.listWidget.item(0).setFont(self.smallfont)
     self.stackedWidget.setCurrentIndex(0)
     if req.lower() == "samuel":
         QtGui.QMessageBox.information(None, "Spoiled!", u"Dans cette série, il meurt à la fin", "TA GUEULE")
Example #29
0
			def encode(self, input, final=False):
				return codecs.utf_8_encode(input, self.errors)[0]
Example #30
0
def latin1_to_utf8(text):
    "helper to convert when needed from latin input"
    return utf_8_encode(latin_1_decode(text)[0])[0]
Example #31
0
    def send(self,
             url,
             method="GET",
             payload=None,
             headers=None,
             cookie=None,
             auth=None,
             content=''):

        # initialize url service
        urlib = self.framework.urlib(url)
        if urlib.scheme == '':
            url = urlib.sub_service("http")

        # get random user agent
        if self.rand_agent:
            self.user_agent = self.framework.rand_uagent().get
        # Makes a web request and returns a response object.
        if method.upper() != "POST" and content:
            raise RequestException(
                "Invalid content type for the %s method: %s" %
                (method, content))
        # Prime local mutable variables to prevent persistence
        if payload is None:
            payload = {}
        if headers is None:
            headers = {}
        if auth is None:
            auth = ()

        # Set request arguments
        # Process user-agent header
        headers["User-Agent"] = self.user_agent
        if not headers["User-Agent"]:
            headers["User-Agent"] = self.user_agent
        # process payload
        if content.upper() == "JSON":
            headers["Content-Type"] = "application/json"
            payload = json.dumps(payload)
        else:
            payload = urlencode(encode_payload(payload))
        # process basic authentication
        if len(auth) == 2:
            authorization = b64encode(
                utf_8_encode('%s:%s' % (auth[0], auth[1]))).replace('\n', '')
            headers["Authorization"] = "Basic %s" % (authorization)
        # Process socket timeout
        if self.timeout:
            socket.setdefaulttimeout(self.timeout)

        # Set handlers
        # Declare handlers list according to debug setting
        handlers = [
            request.HTTPHandler(debuglevel=1),
            request.HTTPSHandler(debuglevel=1)
        ] if self.debug else []
        # Process cookie handler
        if cookie is not None and cookie != {}:
            if isinstance(cookie, dict):
                str_cookie = ""
                for i in cookie:
                    str_cookie += "%s=%s; " % (i, cookie[i])
                headers["Cookie"] = str_cookie
            else:
                handlers.append(request.HTTPCookieProcessor(cookie))

        # Process redirect and add handler
        if not self.redirect:
            handlers.append(NoRedirectHandler)
        # Process proxies and add handler
        if self.proxy:
            proxies = {"http": self.proxy, "https": self.proxy}
            handlers.append(request.ProxyHandler(proxies))

        # Install opener
        opener = request.build_opener(*handlers)
        request.install_opener(opener)
        # Process method and make request
        if method == "GET":
            if payload:
                url = "%s?%s" % (url, payload)
            req = request.Request(url, headers=headers)
        elif method == "POST":
            req = request.Request(url, data=payload, headers=headers)
        elif method == "HEAD":
            if payload:
                url = "%s?%s" % (url, payload)
            req = request.Request(url, headers=headers)
            req.get_method = lambda: "HEAD"
        else:
            raise RequestException(
                "Request method \'%s\' is not a supported method." % (method))

        try:
            resp = request.urlopen(req)
        except request.HTTPError as e:
            resp = e

        # Build and return response object
        return ResponseObject(resp, cookie)
 def _s2b(string):
     """Python 3: convert the string to binary data"""
     return codecs.utf_8_encode(string)[0]
Example #33
0
 def encode(input, errors='strict'):
     return (codecs.BOM_UTF8 +
             codecs.utf_8_encode(input, errors)[0], len(input))
Example #34
0
# LOCALIZATION NOTE (getting_started):
# link title for http://www.mozilla.com/en-US/firefox/central/
#define getting_started %s

# LOCALIZATION NOTE (firefox_heading):
# Firefox links folder name
#define firefox_heading %s

# LOCALIZATION NOTE (firefox_help):
# link title for http://www.mozilla.com/en-US/firefox/help/
#define firefox_help %s

# LOCALIZATION NOTE (firefox_customize):
# link title for http://www.mozilla.com/en-US/firefox/customize/
#define firefox_customize %s

# LOCALIZATION NOTE (firefox_community):
# link title for http://www.mozilla.com/en-US/firefox/community/
#define firefox_community %s

# LOCALIZATION NOTE (firefox_about):
# link title for http://www.mozilla.com/en-US/about/
#define firefox_about %s

#unfilter emptyLines'''

strings = tuple(e.val for e in p if ll.search(e.key))

print codecs.utf_8_encode(template % strings)[0]
Example #35
0
def encode(input, errors='strict'):
    return (codecs.BOM_UTF8 + codecs.utf_8_encode(input, errors)[0], len(input))
Example #36
0
 def encode_utf8(x):
     return codecs.utf_8_encode(x)[0]
Example #37
0
 def encode(self, input, final=False):
     return codecs.utf_8_encode(input, self.errors)[0]
Example #38
0
def utf8ise(s):
	return codecs.utf_8_encode(s)[0]