Coverage for /wheeldirectory/casa-6.7.0-12-py3.10.el8/lib/py/lib/python3.10/site-packages/casatools/__cerberus__/utils.py: 81%

54 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-10-31 19:53 +0000

1from __future__ import absolute_import 

2 

3from collections.abc import Mapping, Sequence 

4 

5from casatools.__cerberus__.platform import _int_types, _str_type 

6 

7 

8def compare_paths_lt(x, y): 

9 for i in range(min(len(x), len(y))): 

10 if isinstance(x[i], type(y[i])): 

11 if x[i] != y[i]: 

12 return x[i] < y[i] 

13 elif isinstance(x[i], _int_types): 

14 return True 

15 elif isinstance(y[i], _int_types): 

16 return False 

17 return len(x) < len(y) 

18 

19 

20def drop_item_from_tuple(t, i): 

21 return t[:i] + t[i + 1:] 

22 

23 

24def get_Validator_class(): 

25 global Validator 

26 if 'Validator' not in globals(): 

27 from casatools.__cerberus__.validator import Validator 

28 return Validator 

29 

30 

31def mapping_hash(schema): 

32 return hash(mapping_to_frozenset(schema)) 

33 

34 

35def mapping_to_frozenset(mapping): 

36 """ Be aware that this treats any sequence type with the equal members as 

37 equal. As it is used to identify equality of schemas, this can be 

38 considered okay as definitions are semantically equal regardless the 

39 container type. """ 

40 mapping = mapping.copy() 

41 for key, value in mapping.items(): 

42 if isinstance(value, Mapping): 

43 mapping[key] = mapping_to_frozenset(value) 

44 elif isinstance(value, Sequence): 

45 value = list(value) 

46 for i, item in enumerate(value): 

47 if isinstance(item, Mapping): 

48 value[i] = mapping_to_frozenset(item) 

49 mapping[key] = tuple(value) 

50 return frozenset(mapping.items()) 

51 

52 

53def isclass(obj): 

54 try: 

55 issubclass(obj, object) 

56 except TypeError: 

57 return False 

58 else: 

59 return True 

60 

61 

62def quote_string(value): 

63 if isinstance(value, _str_type): 

64 return '"%s"' % value 

65 else: 

66 return value 

67 

68 

69def validator_factory(name, mixin=None, class_dict={}): 

70 """ Dynamically create a :class:`~cerberus.Validator` subclass. 

71 Docstrings of mixin-classes will be added to the resulting 

72 class' one if ``__doc__`` is not in :obj:`class_dict`. 

73 

74 :param name: The name of the new class. 

75 :type name: :class:`str` 

76 :param mixin: Class(es) with mixin-methods. 

77 :type mixin: :class:`tuple` of or a single :term:`class` 

78 :param class_dict: Attributes for the new class. 

79 :type class_dict: :class:`dict` 

80 :return: The created class. 

81 """ 

82 Validator = get_Validator_class() 

83 

84 if mixin is None: 

85 bases = (Validator,) 

86 elif isinstance(mixin, tuple): 

87 bases = (Validator,) + mixin 

88 else: 

89 bases = (Validator, mixin) 

90 

91 docstrings = [x.__doc__ for x in bases if x.__doc__] 

92 if len(docstrings) > 1 and '__doc__' not in class_dict: 

93 class_dict.update({'__doc__': '\n'.join(docstrings)}) 

94 

95 return type(name, bases, class_dict)