1#  Copyright 2008-2015 Nokia Networks
2#  Copyright 2016-     Robot Framework Foundation
3#
4#  Licensed under the Apache License, Version 2.0 (the "License");
5#  you may not use this file except in compliance with the License.
6#  You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10#  Unless required by applicable law or agreed to in writing, software
11#  distributed under the License is distributed on an "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#  See the License for the specific language governing permissions and
14#  limitations under the License.
15
16from collections import Mapping
17from UserDict import UserDict
18from UserString import UserString
19from types import ClassType, NoneType
20
21try:
22    from java.lang import String
23except ImportError:
24    String = ()
25
26from .platform import RERAISED_EXCEPTIONS
27
28
29def is_integer(item):
30    return isinstance(item, (int, long))
31
32
33def is_number(item):
34    return isinstance(item, (int, long, float))
35
36
37def is_bytes(item):
38    return isinstance(item, (bytes, bytearray))
39
40
41def is_string(item):
42    # Returns False with `b'bytes'` on IronPython on purpose. Results of
43    # `isinstance(item, basestring)` would depend on IronPython 2.7.x version.
44    return isinstance(item, (str, unicode))
45
46
47def is_unicode(item):
48    return isinstance(item, unicode)
49
50
51def is_list_like(item):
52    if isinstance(item, (str, unicode, bytes, bytearray, UserString, String,
53                         file)):
54        return False
55    try:
56        iter(item)
57    except RERAISED_EXCEPTIONS:
58        raise
59    except:
60        return False
61    else:
62        return True
63
64
65def is_dict_like(item):
66    return isinstance(item, (Mapping, UserDict))
67
68
69def type_name(item):
70    cls = item.__class__ if hasattr(item, '__class__') else type(item)
71    named_types = {str: 'string', unicode: 'string', bool: 'boolean',
72                   int: 'integer', long: 'integer', NoneType: 'None',
73                   dict: 'dictionary', type: 'class', ClassType: 'class'}
74    return named_types.get(cls, cls.__name__)
75