Exemplo n.º 1
0
    def test_charmapencode(self):
        if test_support.due_to_ironpython_bug("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=303938"):
            return
        # For charmap encodings the replacement string will be
        # mapped through the encoding again. This means, that
        # to be able to use e.g. the "replace" handler, the
        # charmap has to have a mapping for "?".
        charmap = dict([ (ord(c), 2*c.upper()) for c in "abcdefgh"])
        sin = u"abc"
        sout = "AABBCC"
        self.assertEquals(codecs.charmap_encode(sin, "strict", charmap)[0], sout)

        sin = u"abcA"
        self.assertRaises(UnicodeError, codecs.charmap_encode, sin, "strict", charmap)

        charmap[ord("?")] = "XYZ"
        sin = u"abcDEF"
        sout = "AABBCCXYZXYZXYZ"
        self.assertEquals(codecs.charmap_encode(sin, "replace", charmap)[0], sout)

        charmap[ord("?")] = u"XYZ"
        self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)

        charmap[ord("?")] = u"XYZ"
        self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
Exemplo n.º 2
0
    def test_charmap_decode_1(self):
        import codecs
        assert codecs.charmap_encode(u'xxx') == ('xxx', 3)
        assert codecs.charmap_encode(u'xxx', 'strict', {ord('x'): 'XX'}) == ('XXXXXX', 3)

        res = codecs.charmap_decode("\x00\x01\x02", "replace", u"ab")
        assert res == (u"ab\ufffd", 3)
        res = codecs.charmap_decode("\x00\x01\x02", "replace", u"ab\ufffe")
        assert res == (u'ab\ufffd', 3)
Exemplo n.º 3
0
    def test_charmap_decode_1(self):
        import codecs

        assert codecs.charmap_encode(u"xxx") == ("xxx", 3)
        assert codecs.charmap_encode(u"xxx", "strict", {ord("x"): "XX"}) == ("XXXXXX", 3)

        res = codecs.charmap_decode("\x00\x01\x02", "replace", u"ab")
        assert res == (u"ab\ufffd", 3)
        res = codecs.charmap_decode("\x00\x01\x02", "replace", u"ab\ufffe")
        assert res == (u"ab\ufffd", 3)
Exemplo n.º 4
0
    def getPayload( self ):
        print self.challenge
        hash = password( self.passwd, self.challenge )

        packet = "0" + DELIM1 + self.nick + DELIM1 + "6" + DELIM1 + codecs.charmap_encode(hash[ 0 ])[0] + DELIM1 + "96" + \
            DELIM1 + codecs.charmap_encode(hash[ 1 ])[0] + DELIM1 + "2" + DELIM1 + "1" + DELIM1 + "1" + DELIM1 + \
            self.nick + DELIM1
		
        printPacket( packet )
        
        return packet
Exemplo n.º 5
0
 def test_charmap_encode_replace(self):
     charmap = dict([(c, bytes([c, c]).upper()) for c in b"abcdefgh"])
     charmap[ord("?")] = b"XYZ"
     import codecs
     sin = "abcDEF"
     sout = codecs.charmap_encode(sin, "replace", charmap)[0]
     assert sout == b"AABBCCXYZXYZXYZ"
Exemplo n.º 6
0
 def doExecute( self, sock, words ):
     # where is the song file?
     server = self.config.getString( "playing", "server" )
     filename = self.config.getString( "playing", "file" )
     
     # get the file from the server
     conn = httplib.HTTPConnection( server )
     conn.request( "GET", filename )
     response = conn.getresponse()
     if response.status != 200:
         raise Command.CommandError( "Could not access song file" )        
 
     file = response.read()
     
     # parse the XML
     dom = xml.dom.minidom.parseString(file)
     node = dom.getElementsByTagName( "media" )[0]
     artist = node.getElementsByTagName("artist")[0].firstChild.nodeValue.strip()
     song = node.getElementsByTagName( "title" )[0].firstChild.nodeValue.strip()
     album = node.getElementsByTagName( "album" )[0].firstChild.nodeValue.strip()
     year = node.getElementsByTagName( "year" )[0].firstChild.nodeValue.strip()
     
     # show it
     msg = "%(artist)s: %(song)s [%(album)s, %(year)s]" % locals()
     msg = codecs.charmap_encode(msg)[0]
     self.sendMessage( sock, msg )
     
     
     
     
Exemplo n.º 7
0
 def test_charmap_encode_replace(self):
     charmap = dict([ (ord(c), 2*c.upper()) for c in "abcdefgh"])
     charmap[ord("?")] = "XYZ"
     import codecs
     sin = u"abcDEF"
     sout = codecs.charmap_encode(sin, "replace", charmap)[0]
     assert sout == "AABBCCXYZXYZXYZ"
Exemplo n.º 8
0
    def test_charmap_encode(self):
        #Sanity
        self.assertEqual(codecs.charmap_encode("abc"), 
                ('abc', 3))
        self.assertEqual(codecs.charmap_encode("abc", "strict"), 
                ('abc', 3))
        
        self.assertEqual(codecs.charmap_encode("", "strict", {}),
                ('', 0))

        charmap = dict([ (ord(c), c.upper()) for c in "abcdefgh"])
        self.assertEqual(codecs.charmap_encode(u"abc", "strict", charmap),
                ('ABC', 3))

                    
        #Sanity Negative
        self.assertRaises(UnicodeEncodeError, codecs.charmap_encode, "abc", "strict", {})
    def test_charmapencode(self):
        # For charmap encodings the replacement string will be
        # mapped through the encoding again. This means, that
        # to be able to use e.g. the "replace" handler, the
        # charmap has to have a mapping for "?".
        charmap = dict((ord(c), bytes(2*c.upper(), 'ascii')) for c in "abcdefgh")
        sin = "abc"
        sout = b"AABBCC"
        self.assertEquals(codecs.charmap_encode(sin, "strict", charmap)[0], sout)

        sin = "abcA"
        self.assertRaises(UnicodeError, codecs.charmap_encode, sin, "strict", charmap)

        charmap[ord("?")] = b"XYZ"
        sin = "abcDEF"
        sout = b"AABBCCXYZXYZXYZ"
        self.assertEquals(codecs.charmap_encode(sin, "replace", charmap)[0], sout)

        charmap[ord("?")] = "XYZ" # wrong type in mapping
        self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
Exemplo n.º 10
0
def _encode(input,errors='strict'):
    # split to see if we have any 'extended' characters
    runs=unicode_splitter.split(input)
    
    # now iterate through handling any 'multibyte' ourselves
    out_str=list()
    consumed=0
    extended=extended_encode_map.keys()
    for run in runs:
        if len(run)==1 and run[0] in extended:
            out_str.append(extended_indicator+extended_encode_map[run])
            consumed+=1
        else:
            # pass it to the standard encoder
            out,cons=codecs.charmap_encode(run,errors,encoding_table)
            out_str.append(out)
            consumed+=cons
    return (''.join(out_str),consumed)
Exemplo n.º 11
0
 def encode(self, input, errors="strict"):
     return codecs.charmap_encode(input, errors, encoding_table)
Exemplo n.º 12
0
 def encode(self, char, final=False):
     return codecs.charmap_encode(char, self.errors, ENCODING_MAP)[0]
Exemplo n.º 13
0
""" Python Character Mapping Codec generated from 'CP852.TXT' with gencodec.py.
Exemplo n.º 14
0
 def encode(self, input, final=False):
     data, nbytes = codecs.charmap_encode(input, self.errors,
                                          encoding_map)
     return data
Exemplo n.º 15
0
 def encode(self, input, final = False):
     return codecs.charmap_encode(input, self.errors, self.mapping)[0]
Exemplo n.º 16
0
 def encode(self, input, final=False):
     return codecs.charmap_encode(input, self.errors, self.mapping)[0]
Exemplo n.º 17
0
""" Python Character Mapping Codec generated from 'CP860.TXT' with gencodec.py.
Exemplo n.º 18
0
 def encode(self, input, final = False):
     return codecs.charmap_encode(input, self.errors, encoding_table)[0]
Exemplo n.º 19
0
#!/usr/bin/env python3
# encoding: utf-8
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Error handling with character map encodings.
"""

#end_pymotw_header
import codecs
from codecs_invertcaps_charmap import encoding_map

text = 'pi: \u03c0'

for error in ['ignore', 'replace', 'strict']:
    try:
        encoded = codecs.charmap_encode(text, error, encoding_map)
    except UnicodeEncodeError as err:
        encoded = str(err)
    print('{:7}: {}'.format(error, encoded))
Exemplo n.º 20
0
""" Python Character Mapping Codec generated from 'CP857.TXT' with gencodec.py.
Exemplo n.º 21
0
 def encode(self,input,errors='strict'):
     return codecs.charmap_encode(input,errors,encoding_table)
 def encode(self, input, final=False):
   return codecs.charmap_encode(input,self.errors,encmap_hd44780_a02)[0]
 def encode_a02(self,input,errors='strict'):
   return codecs.charmap_encode(input,errors,encmap_hd44780_a02)
Exemplo n.º 24
0
 def test_charmap_build(self):
     decodemap = ''.join([unichr(i).upper() if chr(i).islower() else unichr(i).lower() for i in xrange(256)])
     encodemap = codecs.charmap_build(decodemap)
     self.assertEqual(codecs.charmap_decode(u'Hello World', 'strict', decodemap), ('hELLO wORLD', 11))
     self.assertEqual(codecs.charmap_encode(u'Hello World', 'strict', encodemap), ('hELLO wORLD', 11))
Exemplo n.º 25
0
""" Python Character Mapping Codec generated from 'CP500.TXT' with gencodec.py.
Exemplo n.º 26
0
""" Python Character Mapping Codec generated from 'CP1257.TXT' with gencodec.py.
Exemplo n.º 27
0
 def encode(self, _input, errors='strict'):
     return codecs.charmap_encode(_input, errors, encoding_map)
Exemplo n.º 28
0
""" Python Character Mapping Codec generated from 'KOI8-R.TXT' with gencodec.py.
Exemplo n.º 29
0
""" Python Character Mapping Codec generated from 'CP1006.TXT' with gencodec.py.
Exemplo n.º 30
0
#
# Copyright (c) 2010 Doug Hellmann.  All rights reserved.
#
"""Character mapping encoder
"""
#end_pymotw_header

import codecs
import string

# Map every character to itself
decoding_map = codecs.make_identity_dict(range(256))

# Make a list of pairs of ordinal values for the lower and upper case
# letters
pairs = zip([ ord(c) for c in string.ascii_lowercase],
            [ ord(c) for c in string.ascii_uppercase])

# Modify the mapping to convert upper to lower and lower to upper.
decoding_map.update( dict( (upper, lower) for (lower, upper) in pairs) )
decoding_map.update( dict( (lower, upper) for (lower, upper) in pairs) )

# Create a separate encoding map.
encoding_map = codecs.make_encoding_map(decoding_map)

if __name__ == '__main__':
    print codecs.charmap_encode('abc.DEF', 'strict', encoding_map)
    print codecs.charmap_decode('abc.DEF', 'strict', decoding_map)
    print encoding_map == decoding_map
    
Exemplo n.º 31
0
""" Python Character Mapping Codec generated from 'CP424.TXT' with gencodec.py.
Exemplo n.º 32
0
 def encode(self, input, errors="strict"):
     return codecs.charmap_encode(input, errors, encoding_map)
Exemplo n.º 33
0
""" Python Character Mapping Codec generated from 'CP1256.TXT' with gencodec.py.
#!/usr/bin/python
# -*- coding: utf-8 -*-

#
# Copyright (c) 2008 Doug Hellmann All rights reserved.
#

import codecs
import string

# Map every character to itself
decoding_map = codecs.make_identity_dict(range(256))

# Make a list of pairs of ordinal values for the lower and upper case
# letters
pairs = zip([ord(c) for c in string.ascii_lowercase],
            [ord(c) for c in string.ascii_uppercase])

# Modify the mapping to convert upper to lower and lower to upper.
decoding_map.update(dict((upper, lower) for (lower, upper) in pairs))
decoding_map.update(dict((lower, upper) for (lower, upper) in pairs))

# Create a separate encoding map.
encoding_map = codecs.make_encoding_map(decoding_map)

if __name__ == '__main__':
    print codecs.charmap_encode('abc.DEF', 'strict', encoding_map)
    print codecs.charmap_decode('abc.DEF', 'strict', decoding_map)
    print encoding_map == decoding_map
Exemplo n.º 35
0
""" Python Character Mapping Codec generated from '8859-9.TXT' with gencodec.py.
Exemplo n.º 36
0
 def encode(self, input, final=False):
   return codecs.charmap_encode(input,self.errors,encmap_hd44780_a02)[0]
Exemplo n.º 37
0
""" Python Character Mapping Codec generated from '8859-10.TXT' with gencodec.py.
Exemplo n.º 38
0
 def encode(self, _input, final=False):
     return codecs.charmap_encode(_input, self.errors, encoding_map)[0]
Exemplo n.º 39
0
 def encode(self, char, errors='strict'):
     return codecs.charmap_encode(char, errors, ENCODING_MAP)
Exemplo n.º 40
0
 def encode(self, input, final = False):
     return codecs.charmap_encode(input, self.errors, encoding_table)[0]
 def encode(self, input, errors=error_handling):
     return codecs.charmap_encode(input, errors, encoding_map)
# Map every character to itself
decoding_map = codecs.make_identity_dict( xrange(256) )

# Make a list of pairs of ordinal values for all lcase and ucase letters
pairs = zip( [ord(c) for c in string.ascii_lowercase],
             [ord(c) for c in string.ascii_uppercase] )

# modify the mapping to convert upper to lower and lower to upper
decoding_map.update( dict( (upper, lower) for (lower, upper) in pairs ) )
decoding_map.update( dict( (lower, upper) for (lower, upper) in pairs ) )

# Create a separate encoding map
encoding_map = codecs.make_encoding_map( decoding_map )

print (codecs.charmap_encode('abc.DEF', error_handling, encoding_map)) 
print (codecs.charmap_decode('abc.DEF', error_handling, decoding_map)) 
print encoding_map == decoding_map
print

# by default, char map encoders and decoders support the standard error methods
# since this charmap only includes [a-zA-Z], the u"pi: π" from earlier fails    
for error in ['ignore', 'replace', 'strict']:
    try:
        encoded = codecs.charmap_encode(data, error, encoding_map)
    except UnicodeEncodeError, err:
        encoded = str(err)
    print '{:7} {}'.format(error, encoded)
print

# After defining a en/decoding maps, a few additonal classes have to be set up
Exemplo n.º 43
0
""" Python Character Mapping Codec generated from 'ICELAND.TXT' with gencodec.py.
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import codecs
from codecs_invertcaps_charmap import encoding_map

text = u'pi: π'

for error in [ 'ignore', 'replace', 'strict' ]:
    try:
        encoded = codecs.charmap_encode(text, error, encoding_map)
    except UnicodeEncodeError, err:
        encoded = str(err)
    print '{:7}: {}'.format(error, encoded)
Exemplo n.º 45
0
""" Python Character Mapping Codec generated from 'CP875.TXT' with gencodec.py.
Exemplo n.º 46
0
""" Python Character Mapping Codec generated from '8859-8.TXT' with gencodec.py.
Exemplo n.º 47
0
    def encode(self,input,errors='strict'):

        return codecs.charmap_encode(input,errors,encoding_map)
Exemplo n.º 48
0
""" Python Character Mapping Codec generated from 'LATIN2.TXT' with gencodec.py.