Coverage for python/lsst/daf/butler/core/utils.py: 38%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

36 statements  

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21from __future__ import annotations 

22 

23__all__ = ( 

24 "stripIfNotNone", 

25 "transactional", 

26) 

27 

28import fnmatch 

29import functools 

30import logging 

31import re 

32from typing import TYPE_CHECKING, Any, Callable, List, Optional, Pattern, TypeVar, Union 

33 

34from lsst.utils.iteration import ensure_iterable 

35 

36if TYPE_CHECKING: 36 ↛ 37line 36 didn't jump to line 37, because the condition on line 36 was never true

37 from ..registry.wildcards import Ellipsis, EllipsisType 

38 

39 

40_LOG = logging.getLogger(__name__) 

41 

42 

43F = TypeVar("F", bound=Callable) 

44 

45 

46def transactional(func: F) -> F: 

47 """Decorate a method and makes it transactional. 

48 

49 This depends on the class also defining a `transaction` method 

50 that takes no arguments and acts as a context manager. 

51 """ 

52 

53 @functools.wraps(func) 

54 def inner(self: Any, *args: Any, **kwargs: Any) -> Any: 

55 with self.transaction(): 

56 return func(self, *args, **kwargs) 

57 

58 return inner # type: ignore 

59 

60 

61def stripIfNotNone(s: Optional[str]) -> Optional[str]: 

62 """Strip leading and trailing whitespace if the given object is not None. 

63 

64 Parameters 

65 ---------- 

66 s : `str`, optional 

67 Input string. 

68 

69 Returns 

70 ------- 

71 r : `str` or `None` 

72 A string with leading and trailing whitespace stripped if `s` is not 

73 `None`, or `None` if `s` is `None`. 

74 """ 

75 if s is not None: 

76 s = s.strip() 

77 return s 

78 

79 

80def globToRegex( 

81 expressions: Union[str, EllipsisType, None, List[str]] 

82) -> Union[List[Union[str, Pattern]], EllipsisType]: 

83 """Translate glob-style search terms to regex. 

84 

85 If a stand-alone '``*``' is found in ``expressions``, or expressions is 

86 empty or `None`, then the special value ``...`` will be returned, 

87 indicating that any string will match. 

88 

89 Parameters 

90 ---------- 

91 expressions : `str` or `list` [`str`] 

92 A list of glob-style pattern strings to convert. 

93 

94 Returns 

95 ------- 

96 expressions : `list` [`str` or `re.Pattern`] or ``...`` 

97 A list of regex Patterns or simple strings. Returns ``...`` if 

98 the provided expressions would match everything. 

99 """ 

100 if expressions is Ellipsis or expressions is None: 

101 return Ellipsis 

102 expressions = list(ensure_iterable(expressions)) 

103 if not expressions or "*" in expressions: 

104 return Ellipsis 

105 

106 nomagic = re.compile(r"^[\w/\.\-@]+$", re.ASCII) 

107 

108 # Try not to convert simple string to a regex. 

109 results: List[Union[str, Pattern]] = [] 

110 for e in expressions: 

111 res: Union[str, Pattern] 

112 if nomagic.match(e): 

113 res = e 

114 else: 

115 res = re.compile(fnmatch.translate(e)) 

116 results.append(res) 

117 return results