src/cm/utils/string_utils.py
author Simon Descarpentries <sid@sopinspace.com>
Thu, 13 Mar 2014 18:19:43 +0100
changeset 613 a0b695aace0a
parent 236 725653080973
permissions -rw-r--r--
File field of Upload from file is now mandatory. Integrates 1sts IRI improvements in JavaScript test-suite. Continue testing error messages of mandatory fields, up to 704 tests.

import chardet
import re

def to_unicode(input):
    if type(input) == str:
        res = None
        encodings = ['utf8', 'latin1']
        doc_enc = chardet.detect(input)['encoding']
        if doc_enc:
            encodings = [doc_enc,] + encodings  
        for encoding in encodings:
            try:
                res = unicode(input, encoding)
                break;
            except UnicodeDecodeError:
                pass
        if not res:
            raise Exception('UnicodeDecodeError: could not decode')
        return res
    return input

# strip carriage returns
def strip_cr(input):
    return re.sub('\r\n|\r|\n', '\n', input)