示例#1
0
文件: generic.py 项目: code2u/jsb
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)
示例#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
示例#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)
示例#4
0
文件: generic.py 项目: code2u/jsb
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
示例#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
示例#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
示例#7
0
文件: generic.py 项目: code2u/jsb
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)
示例#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)
示例#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
示例#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