Browse Source

Add support for python3 to python bindings.

tags/v0.99.beta18
Alex Foulon alxf 13 years ago
parent
commit
769eab719d
19 changed files with 244 additions and 148 deletions
  1. +22
    -3
      python/caca/__init__.py
  2. +87
    -61
      python/caca/canvas.py
  3. +32
    -11
      python/caca/common.py
  4. +23
    -9
      python/caca/display.py
  5. +2
    -2
      python/caca/dither.py
  6. +4
    -1
      python/caca/font.py
  7. +3
    -3
      python/examples/blit.py
  8. +3
    -3
      python/examples/colors.py
  9. +7
    -8
      python/examples/drawing.py
  10. +1
    -1
      python/examples/driver.py
  11. +2
    -2
      python/examples/event.py
  12. +7
    -5
      python/examples/figfont.py
  13. +14
    -9
      python/examples/font.py
  14. +3
    -3
      python/examples/frames.py
  15. +17
    -17
      python/examples/gol.py
  16. +5
    -5
      python/examples/text.py
  17. +9
    -2
      python/setup.py
  18. +1
    -1
      python/test/__init__.py
  19. +2
    -2
      python/test/canvas.py

+ 22
- 3
python/caca/__init__.py View File

@@ -14,14 +14,33 @@

""" Libcaca Python bindings """

#standard modules
import locale
import sys
import ctypes
from ctypes.util import find_library

if find_library('caca') is not None:
_lib = ctypes.cdll.LoadLibrary(find_library('caca'))
else:
raise ImportError, \
"Can't find shared library, you need to install libcaca in your path !"
raise ImportError(
"Can't find shared library, you need to install libcaca in your path !")

from common import *
#functions to handle string/bytes in python3+
if sys.version_info[0:2] >= (3, 0):
_PYTHON3 = True
else:
_PYTHON3 = False

def _str_to_bytes(the_string):
""" Translate string to bytes type for python 3.
"""
return bytes(the_string, locale.getlocale()[1])

def _bytes_to_str(the_bytes):
""" Translate bytes to string type for python 3.
"""
return the_bytes.decode(locale.getlocale()[1])

from .common import *


+ 87
- 61
python/caca/canvas.py View File

@@ -18,6 +18,7 @@ import ctypes
import errno

from caca import _lib, utf8_to_utf32, utf32_to_utf8
from caca import _PYTHON3, _str_to_bytes, _bytes_to_str
from caca.font import _Font

class _Canvas(object):
@@ -48,12 +49,6 @@ class _Canvas(object):

return _lib.caca_free_canvas(self)

def get_width(self):
raise CanvasError, "You can't use model canvas directly"

def get_height(self):
raise CanvasError, "You can't use model canvas directly"

class Canvas(_Canvas):
""" Canvas object, methods are libcaca functions with canvas_t as
first parameter.
@@ -70,7 +65,7 @@ class Canvas(_Canvas):
if pointer is None:
try:
self._cv = _lib.caca_create_canvas(width, height)
except ctypes.ArgumentError, e:
except ctypes.ArgumentError:
self._cv = 0
raise CanvasError("Specified width or height is invalid")
else:
@@ -91,12 +86,12 @@ class Canvas(_Canvas):
def manage(self, *args, **kw):
""" Not implemented.
"""
raise CanvasError, "Not implemented"
raise CanvasError("Not implemented")

def unmanage(self, *args, **kw):
""" Not implemented.
"""
raise CanvasError, "Not implemented"
raise CanvasError("Not implemented")

def set_size(self, width, height):
""" Resize a canvas.
@@ -111,7 +106,7 @@ class Canvas(_Canvas):

try:
ret = _lib.caca_set_canvas_size(self, width, height)
except ctypes.ArgumentError, e:
except ctypes.ArgumentError:
raise CanvasError("Specified width or height is invalid")
else:
if ret == -1:
@@ -146,12 +141,12 @@ class Canvas(_Canvas):
def get_chars(self, *args, **kw):
""" Not implemented.
"""
raise CanvasError, "Not implemented"
raise CanvasError("Not implemented")

def get_attrs(self, *args, **kw):
""" Not implemented.
"""
raise CanvasError, "Not implemented"
raise CanvasError("Not implemented")

def gotoxy(self, x, y):
""" Set cursor position. Setting the cursor position outside the canvas
@@ -165,7 +160,7 @@ class Canvas(_Canvas):

try:
ret = _lib.caca_gotoxy(self, x, y)
except ctypes.ArgumentError, e:
except ctypes.ArgumentError:
raise CanvasError("specified coordinate X or Y is invalid")
else:
return ret
@@ -209,7 +204,7 @@ class Canvas(_Canvas):

try:
ret = _lib.caca_put_char(self, x, y, ch)
except ctypes.ArgumentError, e:
except ctypes.ArgumentError:
raise CanvasError("specified coordinate X or Y is invalid")
else:
return ret
@@ -249,15 +244,15 @@ class Canvas(_Canvas):
]
_lib.caca_put_str.restype = ctypes.c_int

if not isinstance(s, str):
raise CanvasError("Specified string is invalid")
if _PYTHON3 and isinstance(s, str):
s = _str_to_bytes(s)

try:
ret = _lib.caca_put_str(self, x, y, s)
except ctypes.ArgumentError:
raise CanvasError("Invalid argument")
else:
try:
ret = _lib.caca_put_str(self, x, y, s)
except ctypes.ArgumentError:
raise CanvasError("Specified coordinate X or Y is invalid")
else:
return ret
return ret

def printf(self, x, y, fmt, *args):
""" Print a formated string.
@@ -272,20 +267,30 @@ class Canvas(_Canvas):
]
_lib.caca_printf.restype = ctypes.c_int

if not isinstance(fmt, str):
raise CanvasError("Specified formated string is invalid")
if _PYTHON3 and isinstance(fmt, str):
fmt = _str_to_bytes(fmt)

if _PYTHON3:
nargs = []
for arg in args[:]:
if isinstance(arg, str):
nargs.append(_str_to_bytes(arg))
else:
nargs.append(arg)
else:
try:
ret = _lib.caca_printf(self, x, y, fmt, *args)
except ctypes.ArgumentError:
raise CanvasError("Specified coordinate X or Y is invalid")
else:
return ret
nargs = args

try:
ret = _lib.caca_printf(self, x, y, fmt, *nargs)
except ctypes.ArgumentError:
raise CanvasError("Specified coordinate X or Y is invalid")
else:
return ret

def vprintf(self, *args, **kw):
""" Not implemented.
"""
raise CanvasError, "Not implemented"
raise CanvasError("Not implemented")

def clear(self):
""" Clear the canvas.
@@ -1230,7 +1235,10 @@ class Canvas(_Canvas):
_lib.caca_get_frame_name.argtypes = [_Canvas]
_lib.caca_get_frame_name.restype = ctypes.c_char_p

return _lib.caca_get_frame_name(self)
if _PYTHON3:
return _bytes_to_str(_lib.caca_get_frame_name(self))
else:
return _lib.caca_get_frame_name(self)

def set_frame_name(self, name):
""" Set the current frame's name.
@@ -1240,6 +1248,9 @@ class Canvas(_Canvas):
_lib.caca_set_frame_name.argtypes = [_Canvas, ctypes.c_char_p]
_lib.caca_set_frame_name.restype = ctypes.c_int

if _PYTHON3 and isinstance(name, str):
name = _str_to_bytes(name)

try:
ret = _lib.caca_set_frame_name(self, name)
except ctypes.ArgumentError:
@@ -1304,7 +1315,6 @@ class Canvas(_Canvas):
- ansi: import ANSI files.
- utf8: import UTF-8 files with ANSI colour codes.
"""
length = ctypes.c_size_t(len(data))

_lib.caca_import_canvas_from_memory.argtypes = [
Canvas, ctypes.c_char_p,
@@ -1312,8 +1322,12 @@ class Canvas(_Canvas):
]
_lib.caca_import_canvas_from_memory.restype = ctypes.c_int

if not isinstance(fmt, str):
raise CanvasError("Invalid format requested")
if _PYTHON3 and isinstance(data, str):
data = _str_to_bytes(data)
if _PYTHON3 and isinstance(fmt, str):
fmt = _str_to_bytes(fmt)

length = ctypes.c_size_t(len(data))

try:
ret = _lib.caca_import_canvas_from_memory(self, data, length, fmt)
@@ -1347,8 +1361,10 @@ class Canvas(_Canvas):
]
_lib.caca_import_canvas_from_file.restype = ctypes.c_int

if not isinstance(fmt, str):
raise CanvasError("Invalid format requested")
if _PYTHON3 and isinstance(filename, str):
filename = _str_to_bytes(filename)
if _PYTHON3 and isinstance(fmt, str):
fmt = _str_to_bytes(fmt)

try:
ret = _lib.caca_import_canvas_from_file(self, filename, fmt)
@@ -1391,10 +1407,10 @@ class Canvas(_Canvas):
]
_lib.caca_import_area_from_memory.restype = ctypes.c_int

if not isinstance(fmt, str):
raise CanvasError("Invalid format requested")
elif not isinstance(data, str):
raise CanvasError("Given data are invalid")
if _PYTHON3 and isinstance(data, str):
data = _str_to_bytes(data)
if _PYTHON3 and isinstance(fmt, str):
fmt = _str_to_bytes(fmt)

try:
ret = _lib.caca_import_area_from_memory(self, x, y,
@@ -1433,10 +1449,10 @@ class Canvas(_Canvas):
]
_lib.caca_import_area_from_file.restype = ctypes.c_int

if not isinstance(fmt, str):
raise CanvasError("Invalid format requested")
elif not isinstance(filename, str):
raise CanvasError("Invalid filename requested")
if _PYTHON3 and isinstance(filename, str):
filename = _str_to_bytes(filename)
if _PYTHON3 and isinstance(fmt, str):
fmt = _str_to_bytes(fmt)

try:
ret = _lib.caca_import_area_from_file(self, x, y, filename, fmt)
@@ -1476,10 +1492,14 @@ class Canvas(_Canvas):
_lib.caca_export_canvas_to_memory.argtypes = [
_Canvas, ctypes.c_char_p, p_size_t
]
_lib.caca_export_canvas_to_memory.restype = ctypes.POINTER(ctypes.c_char_p)
_lib.caca_export_canvas_to_memory.restype = ctypes.POINTER(
ctypes.c_char_p)

p = ctypes.c_size_t()

if _PYTHON3 and isinstance(fmt, str):
fmt = _str_to_bytes(fmt)

try:
ret = _lib.caca_export_canvas_to_memory(self, fmt, p)
except ctypes.ArgumentError:
@@ -1493,7 +1513,10 @@ class Canvas(_Canvas):
raise CanvasError("Not enough memory to allocate output"
" buffer")
else:
return ctypes.string_at(ret, p.value)
if _PYTHON3:
return _bytes_to_str(ctypes.string_at(ret, p.value))
else:
return ctypes.string_at(ret, p.value)

def export_area_to_memory(self, x, y, width, height, fmt):
""" Export a canvas portion into a foreign format.
@@ -1525,8 +1548,8 @@ class Canvas(_Canvas):

p = ctypes.c_size_t()

if not isinstance(fmt, str):
raise CanvasError("Invalid format requested")
if _PYTHON3 and isinstance(fmt, str):
fmt = _str_to_bytes(fmt)

try:
ret = _lib.caca_export_area_to_memory(self, x, y, width, height,
@@ -1542,7 +1565,10 @@ class Canvas(_Canvas):
raise CanvasError("Not enough memory to allocate output"
" buffer")
else:
return ctypes.string_at(ret, p.value)
if _PYTHON3:
return _bytes_to_str(ctypes.string_at(ret, p.value))
else:
return ctypes.string_at(ret, p.value)

def set_figfont(self, filename):
""" Load a figfont and attach it to a canvas.
@@ -1552,10 +1578,10 @@ class Canvas(_Canvas):
_lib.caca_canvas_set_figfont.argtypes = [_Canvas, ctypes.c_char_p]
_lib.caca_canvas_set_figfont.restype = ctypes.c_int

if not isinstance(filename, str):
raise CanvasError("Invalid filename requested")
else:
return _lib.caca_canvas_set_figfont(self, filename)
if _PYTHON3 and isinstance(filename, str):
filename = _str_to_bytes(filename)
return _lib.caca_canvas_set_figfont(self, filename)

def put_figchar(self, ch):
""" Paste a character using the current figfont.
@@ -1565,13 +1591,13 @@ class Canvas(_Canvas):
_lib.caca_put_figchar.argtypes = [_Canvas, ctypes.c_uint32]
_lib.caca_put_figchar.restype = ctypes.c_int

if not isinstance(ch, str):
raise CanvasError("Specified character is invalid")
else:
try:
ch = ord(ch)
except TypeError:
ch = utf8_to_utf32(ch)
if _PYTHON3 and isinstance(ch, str):
ch = _str_to_bytes(ch)
try:
ch = ord(ch)
except TypeError:
ch = utf8_to_utf32(ch)

return _lib.caca_put_figchar(self, ch)



+ 32
- 11
python/caca/common.py View File

@@ -16,7 +16,7 @@

import ctypes

from caca import _lib
from caca import _lib, _PYTHON3, _bytes_to_str

#color constants
COLOR_BLACK = 0x00
@@ -116,7 +116,10 @@ def get_version():
"""
_lib.caca_get_version.restype = ctypes.c_char_p

return _lib.caca_get_version()
if _PYTHON3:
return _bytes_to_str(_lib.caca_get_version())
else:
return _lib.caca_get_version()

def get_display_driver_list():
""" Return a list of available drivers as tuple (name, description).
@@ -128,12 +131,15 @@ def get_display_driver_list():

for item in _lib.caca_get_display_driver_list():
if item is not None and item != "":
tmplst.append(item)
if _PYTHON3:
tmplst.append(_bytes_to_str(item))
else:
tmplst.append(item)
else:
#memory error occured otherwise
break

for i in xrange(0, len(tmplst)):
for i in range(0, len(tmplst)):
if i % 2 == 0:
retlst.append((tmplst[i], tmplst[i+1]))

@@ -150,12 +156,15 @@ def get_export_list():

for item in _lib.caca_get_export_list():
if item is not None and item != "":
tmplst.append(item)
if _PYTHON3:
tmplst.append(_bytes_to_str(item))
else:
tmplst.append(item)
else:
#memory error occured otherwise
break

for i in xrange(0, len(tmplst)):
for i in range(0, len(tmplst)):
if i % 2 == 0:
retlst.append((tmplst[i], tmplst[i+1]))

@@ -175,18 +184,24 @@ def get_import_list():
if item is not None:
if item == "":
if not autodetect:
tmplst.append("\"\"")
if _PYTHON3:
tmplst.append(_bytes_to_str("\"\""))
else:
tmplst.append("\"\"")
autodetect = True
else:
#memory error occured otherwise
break
else:
tmplst.append(item)
if _PYTHON3:
tmplst.append(_bytes_to_str(item))
else:
tmplst.append(item)
else:
#memory error occured otherwise
break

for i in xrange(0, len(tmplst)):
for i in range(0, len(tmplst)):
if i % 2 == 0:
retlst.append((tmplst[i], tmplst[i+1]))

@@ -202,7 +217,10 @@ def get_font_list():

for item in _lib.caca_get_font_list():
if item is not None and item != "":
fl.append(item)
if _PYTHON3:
fl.append(_bytes_to_str(item))
else:
fl.append(item)
else:
#memory error occured otherwise
break
@@ -293,7 +311,10 @@ def utf32_to_utf8(ch):
buf = ctypes.c_buffer(7)
_lib.caca_utf32_to_utf8(buf, ch)

return buf.raw.rstrip('\x00')
if _PYTHON3:
return _bytes_to_str(buf.raw).replace('\x00', '')
else:
return buf.raw.replace('\x00', '')

def utf32_to_cp437(ch):
""" Convert a UTF-32 character to CP437.


+ 23
- 9
python/caca/display.py View File

@@ -16,7 +16,7 @@

import ctypes

from caca import _lib
from caca import _lib, _PYTHON3, _str_to_bytes
from caca.canvas import _Canvas, Canvas

class _Display(object):
@@ -61,10 +61,13 @@ class Display(_Display):
_lib.caca_create_display_with_driver.argtypes = [
_Canvas, ctypes.c_char_p
]
if _PYTHON3 and isinstance(driver, str):
driver = _str_to_bytes(driver)

self._dp = _lib.caca_create_display_with_driver(cv, driver)

if self._dp == 0:
raise DisplayError, "Failed to create display"
raise DisplayError("Failed to create display")

def get_driver(self):
""" Return the caca graphical context's current output driver.
@@ -85,6 +88,9 @@ class Display(_Display):

if not driver:
driver = ctypes.c_char_p(0)
else:
if _PYTHON3 and isinstance(driver, str):
driver = _str_to_bytes(driver)

return _lib.caca_set_display_driver(self, driver)

@@ -130,13 +136,17 @@ class Display(_Display):
_lib.caca_set_display_title.argtypes = [_Display, ctypes.c_char_p]
_lib.caca_set_display_title.restype = ctypes.c_int

if _PYTHON3 and isinstance(title, str):
title = _str_to_bytes(title)

return _lib.caca_set_display_title(self, title)

def set_mouse(self, flag):
""" Show or hide the mouse pointer. This function works with the ncurses,
S-Lang and X11 drivers.
""" Show or hide the mouse pointer. This function works with the
ncurses, S-Lang and X11 drivers.

flag -- 0 hides the pointer, 1 shows the system's default pointer (usually an arrow).
flag -- 0 hides the pointer, 1 shows the system's default pointer
(usually an arrow)
"""
_lib.caca_set_mouse.argtypes = [_Display, ctypes.c_int]
_lib.caca_set_mouse.restype = ctypes.c_int
@@ -146,7 +156,8 @@ class Display(_Display):
def set_cursor(self, flag):
""" Show or hide the cursor, for devices that support such a feature.

flag -- 0 hides the cursor, 1 shows the system's default cursor (usually a white rectangle).
flag -- 0 hides the cursor, 1 shows the system's default cursor
(usually a white rectangle).
"""

_lib.caca_set_cursor.argtypes = [Display, ctypes.c_int]
@@ -162,9 +173,12 @@ class Display(_Display):
tiemout -- a timeout value in microseconds
"""

_lib.caca_get_event.argtypes = [Display, ctypes.c_int, ctypes.POINTER(Event), ctypes.c_int]
_lib.caca_get_event.argtypes = [
Display, ctypes.c_int, ctypes.POINTER(Event), ctypes.c_int
]

return _lib.caca_get_event(self, event_mask, ctypes.byref(event), timeout)
return _lib.caca_get_event(self, event_mask, ctypes.byref(event),
timeout)

def get_mouse_x(self):
""" Return the X mouse coordinate.
@@ -216,7 +230,7 @@ class Event(ctypes.Structure):
def get_key_utf32(self):
""" Not implemented.
"""
raise DisplayError, "Not implemented"
raise DisplayError("Not implemented")

def get_key_utf8(self):
""" Return a key press or key release event's UTF-8 value


+ 2
- 2
python/caca/dither.py View File

@@ -71,7 +71,7 @@ class Dither(_Dither):
rmask, gmask, bmask, amask)

if self._dither == 0:
raise DitherError, "Failed to create dither object"
raise DitherError("Failed to create dither object")

def set_palette(self, red, green, blue, alpha):
""" Set the palette of an 8 bits per pixel bitmap. Values should be
@@ -82,7 +82,7 @@ class Dither(_Dither):
blue -- array of 256 blue values
alpha -- array of 256 alpha values
"""
raise DitherError, "Not implemented"
raise DitherError("Not implemented")

def set_brightness(self, brightness):
""" Set the brightness of the dither object.


+ 4
- 1
python/caca/font.py View File

@@ -17,7 +17,7 @@
import ctypes
import errno

from caca import _lib
from caca import _lib, _PYTHON3, _str_to_bytes

class _Font(object):
""" Model for Font object.
@@ -64,6 +64,9 @@ class Font(_Font):

_lib.caca_load_font.restype = ctypes.c_int

if _PYTHON3:
font = _str_to_bytes(font)

self._font = _lib.caca_load_font(font, size)
if self._font == 0:
err = ctypes.c_int.in_dll(_lib, "errno")


+ 3
- 3
python/examples/blit.py View File

@@ -43,18 +43,18 @@ def main():
try:
cv = Canvas(0, 0)
dp = Display(cv)
except (CanvasError, DisplayError), err:
except (CanvasError, DisplayError) as err:
sys.stderr.write("%s\n" % err)
sys.exit(2)

sprite = Canvas(0, 0)
sprite.set_color_ansi(caca.COLOR_LIGHTRED, caca.COLOR_BLACK)
sprite.import_from_memory(THE_PIG, "text")
sprite.set_handle(sprite.get_width()/2, sprite.get_height()/2)
sprite.set_handle(sprite.get_width()//2, sprite.get_height()//2)

cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
cv.put_str(0, 0, "Centered sprite")
cv.blit(cv.get_width()/2, cv.get_height()/2, sprite, NullCanvas())
cv.blit(cv.get_width()//2, cv.get_height()//2, sprite, NullCanvas())

dp.refresh()
dp.get_event(caca.EVENT_KEY_PRESS, Event(), -1)


+ 3
- 3
python/examples/colors.py View File

@@ -28,14 +28,14 @@ def main():
try:
cv = Canvas(80, 24)
dp = Display(cv)
except (CanvasError, DisplayError), err:
except (CanvasError, DisplayError) as err:
sys.stderr.write("%s\n" % err)
sys.exit(127)

cv.set_color_ansi(caca.COLOR_LIGHTGRAY, caca.COLOR_BLACK)
cv.clear()

for i in xrange(0, 16):
for i in range(0, 16):
if i >= 8:
y = i + 3
else:
@@ -44,7 +44,7 @@ def main():
cv.set_color_ansi(caca.COLOR_LIGHTGRAY, caca.COLOR_BLACK)
cv.printf(3, y, "ANSI %i", i)

for j in xrange(0, 16):
for j in range(0, 16):
if j >= 8:
x = 13 + (j * 4)
else:


+ 7
- 8
python/examples/drawing.py View File

@@ -51,22 +51,21 @@ class Drawing(object):
y = [0, self.cv.get_height(), 0]
array_x = ctypes.c_int * (len(x) + 1)
array_y = ctypes.c_int * (len(y) + 1)
ax = array_x(*x)
ay = array_y(*y)
array_xy = [ (x, y) for x, y in zip(array_x(*x), array_y(*y))]
if thin:
self.cv.draw_thin_polyline(ax, ay, len(x) + 1)
self.cv.draw_thin_polyline(array_xy)
else:
self.cv.draw_polyline(ax, ay, len(x) + 1, '#')
self.cv.draw_polyline(array_xy, '#')

def do_circle(self):
x = self.cv.get_width() / 2
y = self.cv.get_height() / 2
x = self.cv.get_width() // 2
y = self.cv.get_height() // 2
radius = 5
self.cv.draw_circle(x, y, radius, '@')

def do_ellipse(self, thin=False):
x = self.cv.get_width() / 2
y = self.cv.get_height() / 2
x = self.cv.get_width() // 2
y = self.cv.get_height() // 2
a = 7
b = 3
if thin:


+ 1
- 1
python/examples/driver.py View File

@@ -31,7 +31,7 @@ def main():
try:
cv = Canvas(0, 0)
dp = Display(cv)
except (CanvasError, DisplayError), err:
except (CanvasError, DisplayError) as err:
sys.stderr.write("%s\n" % err)
sys.exit(127)



+ 2
- 2
python/examples/event.py View File

@@ -70,7 +70,7 @@ def main():
try:
cv = Canvas(80, 24)
dp = Display(cv)
except (CanvasError, DisplayError), err:
except (CanvasError, DisplayError) as err:
sys.stderr.write("%s\n" % err)
sys.exit(127)

@@ -115,7 +115,7 @@ def main():

#print previous events
cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLACK)
counts = range(0, len(events) - 1)
counts = list(range(0, len(events)-1))
counts.reverse()
if len(events) > 1:
j = 0


+ 7
- 5
python/examples/figfont.py View File

@@ -16,6 +16,7 @@
# http://sam.zoy.org/wtfpl/COPYING for more details.
#

import codecs
import os
import sys

@@ -25,7 +26,6 @@ from caca.canvas import Canvas, CanvasError
def main():
""" Main function. """

color = 0

if len(sys.argv) < 3:
sys.stderr.write("Usage: %s <figfont file> <word>\n" \
@@ -34,7 +34,7 @@ def main():

try:
cv = Canvas(0, 0)
except CanvasError, err:
except CanvasError as err:
sys.stderr.write("%s\n" % err)
sys.exit(2)

@@ -42,9 +42,11 @@ def main():
sys.stderr.write("Could not open font...\n")
sys.exit(2)

for c in sys.argv[2].decode('utf8'):
color += 4
cv.set_color_ansi(1+(color % 15), caca.COLOR_TRANSPARENT)
if sys.version_info[0:2] >= (3,0):
word = sys.argv[2]
else:
word = codecs.decode(sys.argv[2], "utf8")
for c in word:
cv.put_figchar(c)

sys.stderr.write(cv.export_to_memory("utf8"))


+ 14
- 9
python/examples/font.py View File

@@ -30,7 +30,7 @@ def main():

try:
cv = Canvas(8, 2)
except CanvasError, err:
except CanvasError as err:
sys.stderr.write("%s\n" % err)
sys.exit(127)

@@ -48,7 +48,7 @@ def main():

try:
f = Font(fonts[0])
except FontError, err:
except FontError as err:
sys.stderr.write("%s\n" % err)
sys.exit(127)

@@ -61,17 +61,22 @@ def main():
cv.set_size(80, 32)
try:
dp = Display(cv)
except DisplayError, err:
except DisplayError as err:
sys.stderr.write("%s\n" % err)
sys.exit(127)

if sys.byteorder == 'big':
dit = Dither(32, w, h, 4 * w, 0xff0000, 0xff00, 0xff, 0xff000000)
else:
dit = Dither(32, w, h, 4 * w, 0xff00, 0xff0000, 0xff000000, 0xff)
try:
if sys.byteorder == 'big':
dit = Dither(32, w, h, 4 * w, 0xff0000, 0xff00, 0xff, 0xff000000)
else:
dit = Dither(32, w, h, 4 * w, 0xff00, 0xff0000, 0xff000000, 0xff)

dit.bitmap(cv, 0, 0, cv.get_width(), cv.get_height(), buf)
dp.refresh()
dit.bitmap(cv, 0, 0, cv.get_width(), cv.get_height(), buf)
except DitherError as err:
sys.stderr.write("%s\n" % err)
sys.exit(127)
else:
dp.refresh()

dp.get_event(caca.EVENT_KEY_PRESS, Event(), -1)



+ 3
- 3
python/examples/frames.py View File

@@ -27,7 +27,7 @@ def main():

try:
cv = Canvas(0, 0)
except CanvasError, err:
except CanvasError as err:
sys.stderr.write("%s\n" % err)
sys.exit(2)

@@ -46,7 +46,7 @@ def main():
cv.set_color_ansi(caca.COLOR_WHITE, idx)
cv.fill_box(0, 0, 40, 15, ':')
cv.set_color_ansi(caca.COLOR_WHITE, caca.COLOR_BLUE)
cv.put_str(idx * 5 / 2, idx, "カカ")
cv.put_str((idx * 5) // 2, idx, "カカ")
cv.set_color_ansi(caca.COLOR_DEFAULT, caca.COLOR_TRANSPARENT)

cv.set_size(41, 16)
@@ -55,7 +55,7 @@ def main():

try:
dp = Display(cv)
except DisplayError, err:
except DisplayError as err:
sys.stderr.write("%s\n" % err)
sys.exit(2)



+ 17
- 17
python/examples/gol.py View File

@@ -30,9 +30,9 @@ class CellArray(object):
self.width = width
self.height = height

for i in xrange(0, self.height):
for i in range(0, self.height):
self.array.append([])
for j in xrange(0, self.width):
for j in range(0, self.width):
self.array[i].append([])

def get(self, x, y):
@@ -68,8 +68,8 @@ class CellArray(object):
def population(self):
n = 0

for i in xrange(0, self.height):
for j in xrange(0, self.width):
for i in range(0, self.height):
for j in range(0, self.width):
if self.get(i, j):
n += 1

@@ -87,8 +87,8 @@ class CellApp(object):

def nextCycle(self):
self.cycle += 1
for x in xrange(0, self.ca.height):
for y in xrange(0, self.ca.width):
for x in range(0, self.ca.height):
for y in range(0, self.ca.width):
if self.ca.get(x, y):
if self.ca.neighbors(x, y) >= 2 and self.ca.neighbors(x, y) <= 3:
self.cbuf.set(x, y, 1)
@@ -101,16 +101,16 @@ class CellApp(object):
self.cbuf.set(x, y, 0)
else:
self.cbuf.set(x, y, 0)
for x in xrange(0, self.ca.height):
for y in xrange(0, self.ca.width):
for x in range(0, self.ca.height):
for y in range(0, self.ca.width):
self.ca.set(x, y, self.cbuf.get(x, y))

def resetCycle(self):
self.cycle = 0

def randomCells(self):
for x in xrange(0, self.ca.height):
for y in xrange(0, self.ca.width):
for x in range(0, self.ca.height):
for y in range(0, self.ca.width):
self.ca.set(x, y, random.randint(0, 1))

def renderCells(self, cv):
@@ -121,16 +121,16 @@ class CellApp(object):
cv.put_str(0, cv.get_height()-1, " "*cv.get_width())
cv.put_str(0, cv.get_height()-1, "generation: %d, population: %d" % (self.cycle, self.ca.population()))
cv.set_color_ansi(caca.COLOR_DEFAULT, caca.COLOR_BLACK)
posx = (cv.get_height() - self.height) / 2
posy = (cv.get_width() - self.width) / 2
for x in xrange(0, self.ca.height):
for y in xrange(0, self.ca.width):
posx = (cv.get_height() - self.height) // 2
posy = (cv.get_width() - self.width) // 2
for x in range(0, self.ca.height):
for y in range(0, self.ca.width):
if self.ca.get(x, y):
cv.put_str(posy+y, posx+x, '@')
cv.put_str(posy+y, posx+x, "@")

def zeroCells(self):
for x in xrange(0, self.ca.height):
for y in xrange(0, self.ca.width):
for x in range(0, self.ca.height):
for y in range(0, self.ca.width):
self.ca.set(x, y, 0)

if __name__ == "__main__":


+ 5
- 5
python/examples/text.py View File

@@ -41,7 +41,7 @@ def main():
pig = Canvas(0, 0)
pig.import_from_memory(STRING, "text")
cv = Canvas(pig.get_width() * 2, pig.get_height() * 2)
except CanvasError, err:
except CanvasError as err:
sys.stderr.write("%s\n" % err)
sys.exit(2)

@@ -55,8 +55,8 @@ def main():
pig.rotate_180()
cv.blit(pig.get_width(), pig.get_height(), pig, NullCanvas())

for j in xrange(0, cv.get_height()):
for i in xrange(0, cv.get_width(), 2):
for j in range(0, cv.get_height()):
for i in range(0, cv.get_width(), 2):
cv.set_color_ansi(caca.COLOR_LIGHTBLUE + (i + j) % 6,
caca.COLOR_DEFAULT)

@@ -64,9 +64,9 @@ def main():
cv.put_attr(i, j, a)
cv.put_attr(i+1, j, a)

sys.stdout.write(cv.export_to_memory("utf8"))
print("%s" % cv.export_to_memory('utf8'))
cv.rotate_left()
sys.stdout.write(cv.export_to_memory("utf8"))
print("%s" % cv.export_to_memory('utf8'))

if __name__ == "__main__":
main()


+ 9
- 2
python/setup.py View File

@@ -3,15 +3,22 @@
# Minimal setup.py script
#

import sys
from setuptools import setup

import caca
try:
import caca
except ImportError as err:
sys.stderr.write("FATAL: %s\n" % str(err))
sys.exit(127)

version_string=caca.get_version()

setup(
name='caca',
author='Alex Foulon',
author_email='alxf@lavabit.com',
version=caca.get_version(),
version=version_string,
packages=['caca'],
package_dir={
'caca': 'caca',


+ 1
- 1
python/test/__init__.py View File

@@ -18,7 +18,7 @@
import unittest

#test modules
import canvas
from . import canvas

#create modules test suite
canvas_t = unittest.TestSuite()


+ 2
- 2
python/test/canvas.py View File

@@ -74,7 +74,7 @@ class CanvasTestCase(unittest.TestCase):
cv = Canvas(10, 1)
self.assertEqual(10, cv.put_str(0, 0, "teststring"))
liststring = []
for i in xrange(0, 10):
for i in range(0, 10):
liststring.append(cv.get_char(i, 0))

self.assertEqual("teststring", "".join(liststring))
@@ -88,7 +88,7 @@ class CanvasTestCase(unittest.TestCase):
cv = Canvas(10, 1)
self.assertEqual(10, cv.printf(0, 0, "%s%s", word1, word2))
liststring = []
for i in xrange(0, 10):
for i in range(0, 10):
liststring.append(cv.get_char(i, 0))
self.assertEqual("teststring", "".join(liststring))
self.assertRaises(CanvasError, cv.printf, 0, 0, 111)


Loading…
Cancel
Save