Beispiel #1
0
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)
Beispiel #2
0
 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
Beispiel #3
0
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)
Beispiel #4
0
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
Beispiel #5
0
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
Beispiel #6
0
 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
Beispiel #7
0
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)
Beispiel #8
0
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)
Beispiel #9
0
 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
Beispiel #10
0
 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