Passed
Push — master ( 6e7477...67b0f7 )
by
unknown
03:06
created

to_ascii()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
1
# -*- coding: utf-8 -*-
2
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
3
# contributor license agreements.  See the NOTICE file distributed with
4
# this work for additional information regarding copyright ownership.
5
# The ASF licenses this file to You under the Apache License, Version 2.0
6
# (the "License"); you may not use this file except in compliance with
7
# the License.  You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
17
import six
18
19
__all__ = [
20
    'to_unicode',
21
    'to_ascii'
22
]
23
24
25
def to_unicode(value):
26
    """
27
    Ensure that the provided text value is represented as unicode.
28
29
    :param value: Value to convert.
30
    :type value: ``str`` or ``unicode``
31
32
    :rtype: ``unicode``
33
    """
34
    if not isinstance(value, six.string_types):
35
        raise ValueError('Value "%s" must be a string.' % (value))
36
37
    if not isinstance(value, six.text_type):
38
        value = six.u(value)
39
40
    return value
41
42
43
def to_ascii(value):
44
    """
45
    Function which encodes the provided bytes / string to ASCII encoding ignoring any errors
46
    which could come up when trying to encode a non-ascii value.
47
    """
48
    return value.decode('ascii', errors='ignore')
49