Revision 91c788ec snf-django-lib/snf_django/management/utils.py

b/snf-django-lib/snf_django/management/utils.py
1
# Copyright 2012 - 2013 GRNET S.A. All rights reserved.
1
# Copyright 2012 - 2014 GRNET S.A. All rights reserved.
2 2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
......
33 33

  
34 34
import json
35 35
import csv
36
import functools
37 36
import operator
37
import locale
38
import unicodedata
39

  
38 40
from datetime import datetime
39 41
from django.utils.timesince import timesince, timeuntil
40 42
from django.db.models.query import QuerySet
41
from django.utils.encoding import smart_unicode
43
from django.utils.encoding import smart_unicode, smart_str
44

  
45

  
46
def smart_locale_unicode(s, **kwargs):
47
    """Wrapper around 'smart_unicode' using user's preferred encoding."""
48
    encoding = locale.getpreferredencoding()
49
    return smart_unicode(s, encoding=encoding, **kwargs)
50

  
51

  
52
def smart_locale_str(s, errors='replace', **kwargs):
53
    """Wrapper around 'smart_str' using user's preferred encoding."""
54
    encoding = locale.getpreferredencoding()
55
    return smart_str(s, encoding=encoding, errors=errors, **kwargs)
56

  
57

  
58
def safe_string(s):
59
    """Escape control characters from unicode and string objects."""
60
    if not isinstance(s, basestring):
61
        return s
62
    if isinstance(s, unicode):
63
        return "".join(ch.encode("unicode_escape")
64
                       if unicodedata.category(ch)[0] == "C" else
65
                       ch for ch in s)
66
    return s.encode("string-escape")
42 67

  
43 68

  
44 69
def parse_bool(value, strict=True):

Also available in: Unified diff