gstlal
1.13.0
Toggle main menu visibility
Loading...
Searching...
No Matches
python
utilities
admin.py
1
"""Collection of tools that are useful for repo administration such as:
2
3
- deprecation warnings
4
- renames / backwards compatibility
5
- etc
6
"""
7
8
import
datetime
9
import
functools
10
import
inspect
11
import
types
12
import
warnings
13
from
typing
import
Union, List
14
15
COPYRIGHT_ATTR =
'__copyright__'
16
COPYRIGHT_PATTERN =
"""Copyright (C) {start_year:d}--{end_year:d} {authors}
17
18
This program is free software; you can redistribute it and/or modify it
19
under the terms of the GNU General Public License as published by the
20
Free Software Foundation; either version 2 of the License, or (at your
21
option) any later version.
22
23
This program is distributed in the hope that it will be useful, but
24
WITHOUT ANY WARRANTY; without even the implied warranty of
25
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
26
Public License for more details.
27
28
You should have received a copy of the GNU General Public License along
29
with this program; if not, write to the Free Software Foundation, Inc.,
30
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31
"""
32
DEPRECATION_PATTERN =
'{kind} {name} is deprecated{reason}'
33
34
35
def
add_copyright
(authors: List[str], start_year: int, end_year: int =
None
):
36
"""Add copyright stub to module under __copyright__ attribute
37
38
Args:
39
authors:
40
List[str], list of author names
41
start_year:
42
int, starting year
43
end_year:
44
int, default None, ending year. If not specified uses current year
45
46
Returns:
47
None
48
"""
49
text = COPYRIGHT_PATTERN.format(authors=
', '
.join(authors),
50
start_year=start_year,
51
end_year=datetime.datetime.today().year
if
end_year
is
None
else
end_year)
52
53
# Get module that called this function
54
frm = inspect.stack()[1]
55
mod = inspect.getmodule(frm[0])
56
57
# Set copyright on module
58
setattr(mod, COPYRIGHT_ATTR, text)
59
60
61
def
deprecated
(reason: str =
None
):
62
"""Deprecation decorator factory, can be used to decorate a class or function
63
64
Args:
65
reason:
66
str, string to display as part of deprecation warning
67
68
Returns:
69
Function, the decorator
70
"""
71
72
def
deprecation_decorator(to_decorate: Union[types.FunctionType, type]):
73
if
inspect.isclass(to_decorate):
74
kind =
'Class'
75
elif
inspect.isfunction(to_decorate):
76
kind =
'Function'
77
else
:
78
raise
ValueError(
'Unable to decorate object of type: {}'
.format(type(to_decorate)))
79
80
@functools.wraps(to_decorate)
81
def
decorated(*args, **kwargs):
82
with
warnings.catch_warnings():
83
warnings.simplefilter(
'always'
, DeprecationWarning)
84
warnings.warn(
85
DEPRECATION_PATTERN.format(name=to_decorate.__name__,
86
kind=kind,
87
reason=
': '
+ reason
if
reason
is
not
None
else
''
),
88
category=DeprecationWarning,
89
stacklevel=2
90
)
91
warnings.simplefilter(
'default'
, DeprecationWarning)
92
return
to_decorate(*args, **kwargs)
93
94
return
decorated
95
96
return
deprecation_decorator
python.utilities.admin.add_copyright
add_copyright(List[str] authors, int start_year, int end_year=None)
Definition
admin.py:35
python.utilities.admin.deprecated
deprecated(str reason=None)
Definition
admin.py:61
Generated by
1.17.0