Changeset 4802


Ignore:
Timestamp:
01/13/12 21:17:01 (4 months ago)
Author:
alxf
Message:

Add support for python3 to python bindings.

Location:
libcaca/trunk/python
Files:
19 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/python/caca/__init__.py

    r4398 r4802  
    1515""" Libcaca Python bindings """ 
    1616 
     17#standard modules 
     18import locale 
     19import sys 
    1720import ctypes 
    1821from ctypes.util import find_library 
     
    2124    _lib = ctypes.cdll.LoadLibrary(find_library('caca')) 
    2225else: 
    23     raise ImportError, \ 
    24         "Can't find shared library, you need to install libcaca in your path !" 
     26    raise ImportError( 
     27        "Can't find shared library, you need to install libcaca in your path !") 
    2528 
    26 from common import * 
     29#functions to handle string/bytes in python3+ 
     30if sys.version_info[0:2] >= (3, 0): 
     31    _PYTHON3 = True 
     32else: 
     33    _PYTHON3 = False 
    2734 
     35def _str_to_bytes(the_string): 
     36    """ Translate string to bytes type for python 3. 
     37    """ 
     38    return bytes(the_string, locale.getlocale()[1]) 
     39 
     40def _bytes_to_str(the_bytes): 
     41    """ Translate bytes to string type for python 3. 
     42    """ 
     43    return the_bytes.decode(locale.getlocale()[1]) 
     44 
     45from .common import * 
     46 
  • libcaca/trunk/python/caca/canvas.py

    r4800 r4802  
    1919 
    2020from caca import _lib, utf8_to_utf32, utf32_to_utf8 
     21from caca import _PYTHON3, _str_to_bytes, _bytes_to_str 
    2122from caca.font import _Font 
    2223 
     
    4849 
    4950        return _lib.caca_free_canvas(self) 
    50  
    51     def get_width(self): 
    52         raise CanvasError, "You can't use model canvas directly" 
    53  
    54     def get_height(self): 
    55         raise CanvasError, "You can't use model canvas directly" 
    5651 
    5752class Canvas(_Canvas): 
     
    7166            try: 
    7267                self._cv = _lib.caca_create_canvas(width, height) 
    73             except ctypes.ArgumentError, e: 
     68            except ctypes.ArgumentError: 
    7469                self._cv = 0 
    7570                raise CanvasError("Specified width or height is invalid") 
     
    9287        """ Not implemented. 
    9388        """ 
    94         raise CanvasError, "Not implemented" 
     89        raise CanvasError("Not implemented") 
    9590 
    9691    def unmanage(self, *args, **kw): 
    9792        """ Not implemented. 
    9893        """ 
    99         raise CanvasError, "Not implemented" 
     94        raise CanvasError("Not implemented") 
    10095 
    10196    def set_size(self, width, height): 
     
    112107        try: 
    113108            ret = _lib.caca_set_canvas_size(self, width, height) 
    114         except ctypes.ArgumentError, e: 
     109        except ctypes.ArgumentError: 
    115110            raise CanvasError("Specified width or height is invalid") 
    116111        else: 
     
    147142        """ Not implemented. 
    148143        """ 
    149         raise CanvasError, "Not implemented" 
     144        raise CanvasError("Not implemented") 
    150145 
    151146    def get_attrs(self, *args, **kw): 
    152147        """ Not implemented. 
    153148        """ 
    154         raise CanvasError, "Not implemented" 
     149        raise CanvasError("Not implemented") 
    155150 
    156151    def gotoxy(self, x, y): 
     
    166161        try: 
    167162            ret = _lib.caca_gotoxy(self, x, y) 
    168         except ctypes.ArgumentError, e: 
     163        except ctypes.ArgumentError: 
    169164            raise CanvasError("specified coordinate X or Y is invalid") 
    170165        else: 
     
    210205            try: 
    211206                ret =  _lib.caca_put_char(self, x, y, ch) 
    212             except ctypes.ArgumentError, e: 
     207            except ctypes.ArgumentError: 
    213208                raise CanvasError("specified coordinate X or Y is invalid") 
    214209            else: 
     
    250245        _lib.caca_put_str.restype  = ctypes.c_int 
    251246 
    252         if not isinstance(s, str): 
    253             raise CanvasError("Specified string is invalid") 
    254         else: 
    255             try: 
    256                 ret = _lib.caca_put_str(self, x, y, s) 
    257             except ctypes.ArgumentError: 
    258                 raise CanvasError("Specified coordinate X or Y is invalid") 
    259             else: 
    260                 return ret 
     247        if _PYTHON3 and isinstance(s, str): 
     248            s = _str_to_bytes(s) 
     249 
     250        try: 
     251            ret = _lib.caca_put_str(self, x, y, s) 
     252        except ctypes.ArgumentError: 
     253            raise CanvasError("Invalid argument") 
     254        else: 
     255            return ret 
    261256 
    262257    def printf(self, x, y, fmt, *args): 
     
    273268        _lib.caca_printf.restype  = ctypes.c_int 
    274269 
    275         if not isinstance(fmt, str): 
    276             raise CanvasError("Specified formated string is invalid") 
    277         else: 
    278             try: 
    279                 ret = _lib.caca_printf(self, x, y, fmt, *args) 
    280             except ctypes.ArgumentError: 
    281                 raise CanvasError("Specified coordinate X or Y is invalid") 
    282             else: 
    283                 return ret 
     270        if _PYTHON3 and isinstance(fmt, str): 
     271            fmt = _str_to_bytes(fmt) 
     272 
     273        if _PYTHON3: 
     274            nargs = [] 
     275            for arg in args[:]: 
     276                if isinstance(arg, str): 
     277                    nargs.append(_str_to_bytes(arg)) 
     278                else: 
     279                    nargs.append(arg) 
     280        else: 
     281            nargs = args 
     282 
     283        try: 
     284            ret = _lib.caca_printf(self, x, y, fmt, *nargs) 
     285        except ctypes.ArgumentError: 
     286            raise CanvasError("Specified coordinate X or Y is invalid") 
     287        else: 
     288            return ret 
    284289 
    285290    def vprintf(self, *args, **kw): 
    286291        """ Not implemented. 
    287292        """ 
    288         raise CanvasError, "Not implemented" 
     293        raise CanvasError("Not implemented") 
    289294 
    290295    def clear(self): 
     
    12311236        _lib.caca_get_frame_name.restype  = ctypes.c_char_p 
    12321237 
    1233         return _lib.caca_get_frame_name(self) 
     1238        if _PYTHON3: 
     1239            return _bytes_to_str(_lib.caca_get_frame_name(self)) 
     1240        else: 
     1241            return _lib.caca_get_frame_name(self) 
    12341242 
    12351243    def set_frame_name(self, name): 
     
    12401248        _lib.caca_set_frame_name.argtypes = [_Canvas, ctypes.c_char_p] 
    12411249        _lib.caca_set_frame_name.restype  = ctypes.c_int 
     1250 
     1251        if _PYTHON3 and isinstance(name, str): 
     1252            name = _str_to_bytes(name) 
    12421253 
    12431254        try: 
     
    13051316              - utf8: import UTF-8 files with ANSI colour codes. 
    13061317        """ 
    1307         length = ctypes.c_size_t(len(data)) 
    13081318 
    13091319        _lib.caca_import_canvas_from_memory.argtypes = [ 
     
    13131323        _lib.caca_import_canvas_from_memory.restype  = ctypes.c_int 
    13141324 
    1315         if not isinstance(fmt, str): 
    1316             raise CanvasError("Invalid format requested") 
     1325        if _PYTHON3 and isinstance(data, str): 
     1326            data = _str_to_bytes(data) 
     1327        if _PYTHON3 and isinstance(fmt, str): 
     1328            fmt = _str_to_bytes(fmt) 
     1329 
     1330        length = ctypes.c_size_t(len(data)) 
    13171331 
    13181332        try: 
     
    13481362        _lib.caca_import_canvas_from_file.restype  = ctypes.c_int 
    13491363 
    1350         if not isinstance(fmt, str): 
    1351             raise CanvasError("Invalid format requested") 
     1364        if _PYTHON3 and isinstance(filename, str): 
     1365            filename = _str_to_bytes(filename) 
     1366        if _PYTHON3 and isinstance(fmt, str): 
     1367            fmt = _str_to_bytes(fmt) 
    13521368 
    13531369        try: 
     
    13921408        _lib.caca_import_area_from_memory.restype  = ctypes.c_int 
    13931409 
    1394         if not isinstance(fmt, str): 
    1395             raise CanvasError("Invalid format requested") 
    1396         elif not isinstance(data, str): 
    1397             raise CanvasError("Given data are invalid") 
     1410        if _PYTHON3 and isinstance(data, str): 
     1411            data = _str_to_bytes(data) 
     1412        if _PYTHON3 and isinstance(fmt, str): 
     1413            fmt = _str_to_bytes(fmt) 
    13981414 
    13991415        try: 
     
    14341450        _lib.caca_import_area_from_file.restype  = ctypes.c_int 
    14351451 
    1436         if not isinstance(fmt, str): 
    1437             raise CanvasError("Invalid format requested") 
    1438         elif not isinstance(filename, str): 
    1439             raise CanvasError("Invalid filename requested") 
     1452        if _PYTHON3 and isinstance(filename, str): 
     1453            filename = _str_to_bytes(filename) 
     1454        if _PYTHON3 and isinstance(fmt, str): 
     1455            fmt = _str_to_bytes(fmt) 
    14401456 
    14411457        try: 
     
    14771493                _Canvas, ctypes.c_char_p, p_size_t 
    14781494            ] 
    1479         _lib.caca_export_canvas_to_memory.restype  = ctypes.POINTER(ctypes.c_char_p) 
     1495        _lib.caca_export_canvas_to_memory.restype  = ctypes.POINTER( 
     1496                                                        ctypes.c_char_p) 
    14801497 
    14811498        p = ctypes.c_size_t() 
     1499 
     1500        if _PYTHON3 and isinstance(fmt, str): 
     1501            fmt = _str_to_bytes(fmt) 
    14821502 
    14831503        try: 
     
    14941514                                      " buffer") 
    14951515            else: 
    1496                 return ctypes.string_at(ret, p.value) 
     1516                if _PYTHON3: 
     1517                    return _bytes_to_str(ctypes.string_at(ret, p.value)) 
     1518                else: 
     1519                    return ctypes.string_at(ret, p.value) 
    14971520 
    14981521    def export_area_to_memory(self, x, y, width, height, fmt): 
     
    15261549        p = ctypes.c_size_t() 
    15271550 
    1528         if not isinstance(fmt, str): 
    1529             raise CanvasError("Invalid format requested") 
     1551        if _PYTHON3 and isinstance(fmt, str): 
     1552            fmt = _str_to_bytes(fmt) 
    15301553 
    15311554        try: 
     
    15431566                                      " buffer") 
    15441567            else: 
    1545                 return ctypes.string_at(ret, p.value) 
     1568                if _PYTHON3: 
     1569                    return _bytes_to_str(ctypes.string_at(ret, p.value)) 
     1570                else: 
     1571                    return ctypes.string_at(ret, p.value) 
    15461572 
    15471573    def set_figfont(self, filename): 
     
    15531579        _lib.caca_canvas_set_figfont.restype  = ctypes.c_int 
    15541580 
    1555         if not isinstance(filename, str): 
    1556             raise CanvasError("Invalid filename requested") 
    1557         else: 
    1558             return _lib.caca_canvas_set_figfont(self, filename) 
     1581        if _PYTHON3 and isinstance(filename, str): 
     1582            filename = _str_to_bytes(filename) 
     1583 
     1584        return _lib.caca_canvas_set_figfont(self, filename) 
    15591585 
    15601586    def put_figchar(self, ch): 
     
    15661592        _lib.caca_put_figchar.restype  = ctypes.c_int 
    15671593 
    1568         if not isinstance(ch, str): 
    1569             raise CanvasError("Specified character is invalid") 
    1570         else: 
    1571             try: 
    1572                 ch = ord(ch) 
    1573             except TypeError: 
    1574                 ch = utf8_to_utf32(ch) 
     1594        if _PYTHON3 and isinstance(ch, str): 
     1595            ch = _str_to_bytes(ch) 
     1596 
     1597        try: 
     1598            ch = ord(ch) 
     1599        except TypeError: 
     1600            ch = utf8_to_utf32(ch) 
    15751601 
    15761602        return _lib.caca_put_figchar(self, ch) 
  • libcaca/trunk/python/caca/common.py

    r4774 r4802  
    1717import ctypes 
    1818 
    19 from caca import _lib 
     19from caca import _lib, _PYTHON3, _bytes_to_str 
    2020 
    2121#color constants 
     
    117117    _lib.caca_get_version.restype = ctypes.c_char_p 
    118118 
    119     return _lib.caca_get_version() 
     119    if _PYTHON3: 
     120        return _bytes_to_str(_lib.caca_get_version()) 
     121    else: 
     122        return _lib.caca_get_version() 
    120123 
    121124def get_display_driver_list(): 
     
    129132    for item in _lib.caca_get_display_driver_list(): 
    130133        if item is not None and item != "": 
    131             tmplst.append(item) 
     134            if _PYTHON3: 
     135                tmplst.append(_bytes_to_str(item)) 
     136            else: 
     137                tmplst.append(item) 
    132138        else: 
    133139            #memory error occured otherwise 
    134140            break 
    135141 
    136     for i in xrange(0, len(tmplst)): 
     142    for i in range(0, len(tmplst)): 
    137143        if i % 2 == 0: 
    138144            retlst.append((tmplst[i], tmplst[i+1])) 
     
    151157    for item in _lib.caca_get_export_list(): 
    152158        if item is not None and item != "": 
    153             tmplst.append(item) 
     159            if _PYTHON3: 
     160                tmplst.append(_bytes_to_str(item)) 
     161            else: 
     162                tmplst.append(item) 
    154163        else: 
    155164            #memory error occured otherwise 
    156165            break 
    157166 
    158     for i in xrange(0, len(tmplst)): 
     167    for i in range(0, len(tmplst)): 
    159168        if i % 2 == 0: 
    160169            retlst.append((tmplst[i], tmplst[i+1])) 
     
    176185            if item == "": 
    177186                if not autodetect: 
    178                     tmplst.append("\"\"") 
     187                    if _PYTHON3: 
     188                        tmplst.append(_bytes_to_str("\"\"")) 
     189                    else: 
     190                        tmplst.append("\"\"") 
    179191                    autodetect = True 
    180192                else: 
     
    182194                    break 
    183195            else: 
    184                 tmplst.append(item) 
     196                if _PYTHON3: 
     197                    tmplst.append(_bytes_to_str(item)) 
     198                else: 
     199                    tmplst.append(item) 
    185200        else: 
    186201            #memory error occured otherwise 
    187202            break 
    188203 
    189     for i in xrange(0, len(tmplst)): 
     204    for i in range(0, len(tmplst)): 
    190205        if i % 2 == 0: 
    191206            retlst.append((tmplst[i], tmplst[i+1])) 
     
    203218    for item in _lib.caca_get_font_list(): 
    204219        if item is not None and item != "": 
    205             fl.append(item) 
     220            if _PYTHON3: 
     221                fl.append(_bytes_to_str(item)) 
     222            else: 
     223                fl.append(item) 
    206224        else: 
    207225            #memory error occured otherwise 
     
    294312    _lib.caca_utf32_to_utf8(buf, ch) 
    295313 
    296     return buf.raw.rstrip('\x00') 
     314    if _PYTHON3: 
     315        return _bytes_to_str(buf.raw).replace('\x00', '') 
     316    else: 
     317        return buf.raw.replace('\x00', '') 
    297318 
    298319def utf32_to_cp437(ch): 
  • libcaca/trunk/python/caca/display.py

    r4769 r4802  
    1717import ctypes 
    1818 
    19 from caca import _lib 
     19from caca import _lib, _PYTHON3, _str_to_bytes 
    2020from caca.canvas import _Canvas, Canvas 
    2121 
     
    6262                _Canvas, ctypes.c_char_p 
    6363            ] 
     64            if _PYTHON3 and isinstance(driver, str): 
     65                driver = _str_to_bytes(driver) 
     66 
    6467            self._dp = _lib.caca_create_display_with_driver(cv, driver) 
    6568 
    6669        if self._dp == 0: 
    67             raise DisplayError, "Failed to create display" 
     70            raise DisplayError("Failed to create display") 
    6871 
    6972    def get_driver(self): 
     
    8689        if not driver: 
    8790            driver = ctypes.c_char_p(0) 
     91        else: 
     92            if _PYTHON3 and isinstance(driver, str): 
     93                driver = _str_to_bytes(driver) 
    8894 
    8995        return _lib.caca_set_display_driver(self, driver) 
     
    131137        _lib.caca_set_display_title.restype  = ctypes.c_int 
    132138 
     139        if _PYTHON3 and isinstance(title, str): 
     140            title = _str_to_bytes(title) 
     141 
    133142        return _lib.caca_set_display_title(self, title) 
    134143 
    135144    def set_mouse(self, flag): 
    136         """ Show or hide the mouse pointer. This function works with the ncurses, 
    137         S-Lang and X11 drivers. 
    138  
    139             flag -- 0 hides the pointer, 1 shows the system's default pointer (usually an arrow). 
     145        """ Show or hide the mouse pointer. This function works with the 
     146            ncurses, S-Lang and X11 drivers. 
     147 
     148            flag -- 0 hides the pointer, 1 shows the system's default pointer 
     149                    (usually an arrow) 
    140150        """ 
    141151        _lib.caca_set_mouse.argtypes = [_Display, ctypes.c_int] 
     
    147157        """ Show or hide the cursor, for devices that support such a feature. 
    148158 
    149             flag -- 0 hides the cursor, 1 shows the system's default cursor (usually a white rectangle). 
     159            flag -- 0 hides the cursor, 1 shows the system's default cursor 
     160                    (usually a white rectangle). 
    150161        """ 
    151162 
     
    163174        """ 
    164175 
    165         _lib.caca_get_event.argtypes = [Display, ctypes.c_int, ctypes.POINTER(Event), ctypes.c_int] 
    166  
    167         return _lib.caca_get_event(self, event_mask, ctypes.byref(event), timeout) 
     176        _lib.caca_get_event.argtypes = [ 
     177                Display, ctypes.c_int, ctypes.POINTER(Event), ctypes.c_int 
     178            ] 
     179 
     180        return _lib.caca_get_event(self, event_mask, ctypes.byref(event), 
     181                                         timeout) 
    168182 
    169183    def get_mouse_x(self): 
     
    217231        """ Not implemented. 
    218232        """ 
    219         raise DisplayError, "Not implemented" 
     233        raise DisplayError("Not implemented") 
    220234 
    221235    def get_key_utf8(self): 
  • libcaca/trunk/python/caca/dither.py

    r4707 r4802  
    7272 
    7373        if self._dither == 0: 
    74             raise DitherError, "Failed to create dither object" 
     74            raise DitherError("Failed to create dither object") 
    7575 
    7676    def set_palette(self, red, green, blue, alpha): 
     
    8383            alpha   -- array of 256 alpha values 
    8484        """ 
    85         raise DitherError, "Not implemented" 
     85        raise DitherError("Not implemented") 
    8686 
    8787    def set_brightness(self, brightness): 
  • libcaca/trunk/python/caca/font.py

    r4770 r4802  
    1818import errno 
    1919 
    20 from caca import _lib 
     20from caca import _lib, _PYTHON3, _str_to_bytes 
    2121 
    2222class _Font(object): 
     
    6565        _lib.caca_load_font.restype = ctypes.c_int 
    6666 
     67        if _PYTHON3: 
     68            font = _str_to_bytes(font) 
     69 
    6770        self._font = _lib.caca_load_font(font, size) 
    6871        if self._font == 0: 
  • libcaca/trunk/python/examples/blit.py

    r4711 r4802  
    4444        cv = Canvas(0, 0) 
    4545        dp = Display(cv) 
    46     except (CanvasError, DisplayError), err: 
     46    except (CanvasError, DisplayError) as err: 
    4747        sys.stderr.write("%s\n" % err) 
    4848        sys.exit(2) 
     
    5151    sprite.set_color_ansi(caca.COLOR_LIGHTRED, caca.COLOR_BLACK) 
    5252    sprite.import_from_memory(THE_PIG, "text") 
    53     sprite.set_handle(sprite.get_width()/2, sprite.get_height()/2) 
     53    sprite.set_handle(sprite.get_width()//2, sprite.get_height()//2) 
    5454 
    5555    cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE) 
    5656    cv.put_str(0, 0, "Centered sprite") 
    57     cv.blit(cv.get_width()/2, cv.get_height()/2, sprite, NullCanvas()) 
     57    cv.blit(cv.get_width()//2, cv.get_height()//2, sprite, NullCanvas()) 
    5858 
    5959    dp.refresh() 
  • libcaca/trunk/python/examples/colors.py

    r4712 r4802  
    2929        cv = Canvas(80, 24) 
    3030        dp = Display(cv) 
    31     except (CanvasError, DisplayError), err: 
     31    except (CanvasError, DisplayError) as err: 
    3232        sys.stderr.write("%s\n" % err) 
    3333        sys.exit(127) 
     
    3636    cv.clear() 
    3737 
    38     for i in xrange(0, 16): 
     38    for i in range(0, 16): 
    3939        if i >= 8: 
    4040            y = i + 3 
     
    4545        cv.printf(3, y, "ANSI %i", i) 
    4646 
    47         for j in xrange(0, 16): 
     47        for j in range(0, 16): 
    4848            if j >= 8: 
    4949                x = 13 + (j * 4) 
  • libcaca/trunk/python/examples/drawing.py

    r4398 r4802  
    5252        array_x = ctypes.c_int * (len(x) + 1) 
    5353        array_y = ctypes.c_int * (len(y) + 1) 
    54         ax = array_x(*x) 
    55         ay = array_y(*y) 
     54        array_xy = [ (x, y) for x, y in zip(array_x(*x), array_y(*y))] 
    5655        if thin: 
    57             self.cv.draw_thin_polyline(ax, ay, len(x) + 1) 
     56            self.cv.draw_thin_polyline(array_xy) 
    5857        else: 
    59             self.cv.draw_polyline(ax, ay, len(x) + 1, '#') 
     58            self.cv.draw_polyline(array_xy, '#') 
    6059 
    6160    def do_circle(self): 
    62         x = self.cv.get_width() / 2 
    63         y = self.cv.get_height() / 2 
     61        x = self.cv.get_width() // 2 
     62        y = self.cv.get_height() // 2 
    6463        radius = 5 
    6564        self.cv.draw_circle(x, y, radius, '@') 
    6665 
    6766    def do_ellipse(self, thin=False): 
    68         x = self.cv.get_width() / 2 
    69         y = self.cv.get_height() / 2 
     67        x = self.cv.get_width() // 2 
     68        y = self.cv.get_height() // 2 
    7069        a = 7 
    7170        b = 3 
  • libcaca/trunk/python/examples/driver.py

    r4712 r4802  
    3232        cv = Canvas(0, 0) 
    3333        dp = Display(cv) 
    34     except (CanvasError, DisplayError), err: 
     34    except (CanvasError, DisplayError) as err: 
    3535        sys.stderr.write("%s\n" % err) 
    3636        sys.exit(127) 
  • libcaca/trunk/python/examples/event.py

    r4712 r4802  
    7171        cv = Canvas(80, 24) 
    7272        dp = Display(cv) 
    73     except (CanvasError, DisplayError), err: 
     73    except (CanvasError, DisplayError) as err: 
    7474        sys.stderr.write("%s\n" % err) 
    7575        sys.exit(127) 
     
    116116        #print previous events 
    117117        cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK) 
    118         counts = range(0, len(events) - 1) 
     118        counts = list(range(0, len(events)-1)) 
    119119        counts.reverse() 
    120120        if len(events) > 1: 
  • libcaca/trunk/python/examples/figfont.py

    r4772 r4802  
    1717# 
    1818 
     19import codecs 
    1920import os 
    2021import sys 
     
    2627    """ Main function. """ 
    2728 
    28     color = 0 
    2929 
    3030    if len(sys.argv) < 3: 
     
    3535    try: 
    3636        cv = Canvas(0, 0) 
    37     except CanvasError, err: 
     37    except CanvasError as err: 
    3838        sys.stderr.write("%s\n" % err) 
    3939        sys.exit(2) 
     
    4343        sys.exit(2) 
    4444 
    45     for c in sys.argv[2].decode('utf8'): 
    46         color += 4 
    47         cv.set_color_ansi(1+(color % 15), caca.COLOR_TRANSPARENT) 
     45    if sys.version_info[0:2] >= (3,0): 
     46        word = sys.argv[2] 
     47    else: 
     48        word = codecs.decode(sys.argv[2], "utf8") 
     49    for c in word: 
    4850        cv.put_figchar(c) 
    4951 
  • libcaca/trunk/python/examples/font.py

    r4770 r4802  
    3131    try: 
    3232        cv = Canvas(8, 2) 
    33     except CanvasError, err: 
     33    except CanvasError as err: 
    3434        sys.stderr.write("%s\n" % err) 
    3535        sys.exit(127) 
     
    4949    try: 
    5050        f = Font(fonts[0]) 
    51     except FontError, err: 
     51    except FontError as err: 
    5252        sys.stderr.write("%s\n" % err) 
    5353        sys.exit(127) 
     
    6262    try: 
    6363        dp = Display(cv) 
    64     except DisplayError, err: 
     64    except DisplayError as err: 
    6565        sys.stderr.write("%s\n" % err) 
    6666        sys.exit(127) 
    6767 
    68     if sys.byteorder == 'big': 
    69         dit = Dither(32, w, h, 4 * w, 0xff0000, 0xff00, 0xff, 0xff000000) 
     68    try: 
     69        if sys.byteorder == 'big': 
     70            dit = Dither(32, w, h, 4 * w, 0xff0000, 0xff00, 0xff, 0xff000000) 
     71        else: 
     72            dit = Dither(32, w, h, 4 * w, 0xff00, 0xff0000, 0xff000000, 0xff) 
     73 
     74        dit.bitmap(cv, 0, 0, cv.get_width(), cv.get_height(), buf) 
     75    except DitherError as err: 
     76        sys.stderr.write("%s\n" % err) 
     77        sys.exit(127) 
    7078    else: 
    71         dit = Dither(32, w, h, 4 * w, 0xff00, 0xff0000, 0xff000000, 0xff) 
    72  
    73     dit.bitmap(cv, 0, 0, cv.get_width(), cv.get_height(), buf) 
    74     dp.refresh() 
     79        dp.refresh() 
    7580 
    7681    dp.get_event(caca.EVENT_KEY_PRESS, Event(), -1) 
  • libcaca/trunk/python/examples/frames.py

    r4711 r4802  
    2828    try: 
    2929        cv = Canvas(0, 0) 
    30     except CanvasError, err: 
     30    except CanvasError as err: 
    3131        sys.stderr.write("%s\n" % err) 
    3232        sys.exit(2) 
     
    4747        cv.fill_box(0, 0, 40, 15, ':') 
    4848        cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE) 
    49         cv.put_str(idx * 5 / 2, idx, "カカ") 
     49        cv.put_str((idx * 5) // 2, idx, "カカ") 
    5050        cv.set_color_ansi(caca.COLOR_DEFAULT, caca.COLOR_TRANSPARENT) 
    5151 
     
    5656    try: 
    5757        dp = Display(cv) 
    58     except DisplayError, err: 
     58    except DisplayError as err: 
    5959        sys.stderr.write("%s\n" % err) 
    6060        sys.exit(2) 
  • libcaca/trunk/python/examples/gol.py

    r4398 r4802  
    3131        self.height = height 
    3232 
    33         for i in xrange(0, self.height): 
     33        for i in range(0, self.height): 
    3434            self.array.append([]) 
    35             for j in xrange(0, self.width): 
     35            for j in range(0, self.width): 
    3636                self.array[i].append([]) 
    3737 
     
    6969        n = 0 
    7070 
    71         for i in xrange(0, self.height): 
    72             for j in xrange(0, self.width): 
     71        for i in range(0, self.height): 
     72            for j in range(0, self.width): 
    7373                if self.get(i, j): 
    7474                    n += 1 
     
    8888    def nextCycle(self): 
    8989        self.cycle += 1 
    90         for x in xrange(0, self.ca.height): 
    91             for y in xrange(0, self.ca.width): 
     90        for x in range(0, self.ca.height): 
     91            for y in range(0, self.ca.width): 
    9292                if self.ca.get(x, y): 
    9393                    if self.ca.neighbors(x, y) >= 2 and self.ca.neighbors(x, y) <= 3: 
     
    102102                else: 
    103103                    self.cbuf.set(x, y, 0) 
    104         for x in xrange(0, self.ca.height): 
    105             for y in xrange(0, self.ca.width): 
     104        for x in range(0, self.ca.height): 
     105            for y in range(0, self.ca.width): 
    106106                self.ca.set(x, y, self.cbuf.get(x, y)) 
    107107 
     
    110110 
    111111    def randomCells(self): 
    112         for x in xrange(0, self.ca.height): 
    113             for y in xrange(0, self.ca.width): 
     112        for x in range(0, self.ca.height): 
     113            for y in range(0, self.ca.width): 
    114114                self.ca.set(x, y, random.randint(0, 1)) 
    115115 
     
    122122        cv.put_str(0, cv.get_height()-1, "generation: %d, population: %d" % (self.cycle, self.ca.population())) 
    123123        cv.set_color_ansi(caca.COLOR_DEFAULT, caca.COLOR_BLACK) 
    124         posx = (cv.get_height() - self.height) / 2 
    125         posy = (cv.get_width() - self.width) / 2 
    126         for x in xrange(0, self.ca.height): 
    127             for y in xrange(0, self.ca.width): 
     124        posx = (cv.get_height() - self.height) // 2 
     125        posy = (cv.get_width() - self.width) // 2 
     126        for x in range(0, self.ca.height): 
     127            for y in range(0, self.ca.width): 
    128128                if self.ca.get(x, y): 
    129                     cv.put_str(posy+y, posx+x, '@') 
     129                    cv.put_str(posy+y, posx+x, "@") 
    130130 
    131131    def zeroCells(self): 
    132         for x in xrange(0, self.ca.height): 
    133             for y in xrange(0, self.ca.width): 
     132        for x in range(0, self.ca.height): 
     133            for y in range(0, self.ca.width): 
    134134                self.ca.set(x, y, 0) 
    135135 
  • libcaca/trunk/python/examples/text.py

    r4711 r4802  
    4242        pig.import_from_memory(STRING, "text") 
    4343        cv = Canvas(pig.get_width() * 2, pig.get_height() * 2) 
    44     except CanvasError, err: 
     44    except CanvasError as err: 
    4545        sys.stderr.write("%s\n" % err) 
    4646        sys.exit(2) 
     
    5656    cv.blit(pig.get_width(), pig.get_height(), pig, NullCanvas()) 
    5757 
    58     for j in xrange(0, cv.get_height()): 
    59         for i in xrange(0, cv.get_width(), 2): 
     58    for j in range(0, cv.get_height()): 
     59        for i in range(0, cv.get_width(), 2): 
    6060            cv.set_color_ansi(caca.COLOR_LIGHTBLUE + (i + j) % 6, 
    6161                              caca.COLOR_DEFAULT) 
     
    6565            cv.put_attr(i+1, j, a) 
    6666 
    67     sys.stdout.write(cv.export_to_memory("utf8")) 
     67    print("%s" % cv.export_to_memory('utf8')) 
    6868    cv.rotate_left() 
    69     sys.stdout.write(cv.export_to_memory("utf8")) 
     69    print("%s" % cv.export_to_memory('utf8')) 
    7070 
    7171if __name__ == "__main__": 
  • libcaca/trunk/python/setup.py

    r4801 r4802  
    44# 
    55 
     6import sys 
    67from setuptools import setup 
    78 
    8 import caca 
     9try: 
     10    import caca 
     11except ImportError as err: 
     12    sys.stderr.write("FATAL: %s\n" % str(err)) 
     13    sys.exit(127) 
     14 
     15version_string=caca.get_version() 
    916 
    1017setup( 
     
    1219    author='Alex Foulon', 
    1320    author_email='alxf@lavabit.com', 
    14     version=caca.get_version(), 
     21    version=version_string, 
    1522    packages=['caca'], 
    1623    package_dir={ 
  • libcaca/trunk/python/test/__init__.py

    r4801 r4802  
    1919 
    2020#test modules 
    21 import canvas 
     21from . import canvas 
    2222 
    2323#create modules test suite 
  • libcaca/trunk/python/test/canvas.py

    r4801 r4802  
    7575        self.assertEqual(10, cv.put_str(0, 0, "teststring")) 
    7676        liststring = [] 
    77         for i in xrange(0, 10): 
     77        for i in range(0, 10): 
    7878            liststring.append(cv.get_char(i, 0)) 
    7979 
     
    8989        self.assertEqual(10, cv.printf(0, 0, "%s%s", word1, word2)) 
    9090        liststring = [] 
    91         for i in xrange(0, 10): 
     91        for i in range(0, 10): 
    9292            liststring.append(cv.get_char(i, 0)) 
    9393        self.assertEqual("teststring", "".join(liststring)) 
Note: See TracChangeset for help on using the changeset viewer.