X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=wx%2Flib%2Futils.py;fp=wx%2Flib%2Futils.py;h=75e80fff64dc591f35718497b19e23ef7996679c;hp=0000000000000000000000000000000000000000;hb=e37b83503aa09d36d7e32327d9bd1f6c42708890;hpb=ddb6b1f242031dc55e52403029ac851cc70bd1f8 diff --git a/wx/lib/utils.py b/wx/lib/utils.py new file mode 100644 index 0000000..75e80ff --- /dev/null +++ b/wx/lib/utils.py @@ -0,0 +1,55 @@ +import wx + +def blank_bmp(w, h, color="white"): + bmp = wx.EmptyBitmap(max(w,1) , max(h,1)) + clear_bmp(bmp, color) + return bmp + +def clear_bmp(bmp, color): + dc = wx.MemoryDC() + dc.SelectObject(bmp) + dc.SetBackground(wx.Brush(color)) + dc.Clear() + + +# Taken and adapt from django.utils.encoding +class GPdf2SwfUnicodeDecodeError(UnicodeDecodeError): + def __init__(self, obj, *args): + self.obj = obj + UnicodeDecodeError.__init__(self, *args) + + def __str__(self): + original = UnicodeDecodeError.__str__(self) + return '%s. You passed in %r (%s)' % (original, self.obj, + type(self.obj)) + +def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): + try: + if not isinstance(s, basestring,): + if hasattr(s, '__unicode__'): + s = unicode(s) + else: + try: + s = unicode(str(s), encoding, errors) + except UnicodeEncodeError: + if not isinstance(s, Exception): + raise + # If we get to here, the caller has passed in an Exception + # subclass populated with non-ASCII data without special + # handling to display as a string. We need to handle this + # without raising a further exception. We do an + # approximation to what the Exception's standard str() + # output should be. + s = ' '.join([force_unicode(arg, encoding, strings_only, + errors) for arg in s]) + elif not isinstance(s, unicode): + # Note: We use .decode() here, instead of unicode(s, encoding, + # errors), so that if s is a SafeString, it ends up being a + # SafeUnicode at the end. + s = s.decode(encoding, errors) + except UnicodeDecodeError, e: + raise GPdf2SwfUnicodeDecodeError(s, *e.args) + #raise UnicodeDecodeError(*e.args) + return s + +