def fromenc(txt, encoding='utf-8', what=""): """ convert from encoding. """ if not txt: txt = u"" if type(txt) == types.UnicodeType: return txt try: return txt.decode(encoding) except UnicodeDecodeError: logging.debug("%s - can't decode %s - decoding per char" % (whichmodule(), encoding)) return decodeperchar(txt, encoding, what)
def __getattribute__(self, attr): where = whichmodule(1) logging.debug('locking - locking on %s' % where) rlockmanager.acquire(object) res = None try: res = super(Locked, self).__getattribute__(attr) finally: rlockmanager.release(object) return res
def fromenc(txt, encoding='utf-8', what=""): """ convert from encoding. """ if not txt: txt = u"" if type(txt) == types.UnicodeType: return txt try: return txt.decode(encoding) except UnicodeDecodeError: logging.debug(u"%s - can't decode %s - decoding per char" % (whichmodule(), encoding)) return decodeperchar(txt, encoding, what)
def toenc(what, encoding='utf-8'): """ convert to encoding. """ if not what: what= u"" try: w = unicode(what) return w.encode(encoding) except UnicodeDecodeError: logging.debug("%s - can't encode %s to %s" % (whichmodule(2), what, encoding)) raise
def toenc(what, encoding='utf-8'): """ convert to encoding. """ if not what: what= u"" try: w = unicode(what) return w.encode(encoding) except UnicodeDecodeError: logging.debug(u"%s - can't encode %s to %s" % (whichmodule(2), what, encoding)) raise
def decodeperchar(txt, encoding='utf-8', what=""): """ decode a string char by char. strip chars that can't be decoded. """ res = [] nogo = [] for i in txt: try: res.append(i.decode(encoding)) except UnicodeDecodeError: if i not in nogo: nogo.append(i) if nogo: if what: logging.debug("%s: can't decode %s characters to %s" % (what, nogo, encoding)) else: logging.debug("%s - can't decode %s characters to %s" % (whichmodule(), nogo, encoding)) return u"".join(res)
def lockedfunc(*args, **kwargs): """ the locked function. """ where = whichmodule(2) logging.debug('locking - locking on %s' % where) lock.acquire() res = None try: res = func(*args, **kwargs) finally: try: lock.release() logging.debug('locking - releasing %s' % where) except: pass return res