gstlal 1.13.0
Loading...
Searching...
No Matches
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
8import datetime
9import functools
10import inspect
11import types
12import warnings
13from typing import Union, List
14
15COPYRIGHT_ATTR = '__copyright__'
16COPYRIGHT_PATTERN = """Copyright (C) {start_year:d}--{end_year:d} {authors}
17
18This program is free software; you can redistribute it and/or modify it
19under the terms of the GNU General Public License as published by the
20Free Software Foundation; either version 2 of the License, or (at your
21option) any later version.
22
23This program is distributed in the hope that it will be useful, but
24WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
26Public License for more details.
27
28You should have received a copy of the GNU General Public License along
29with this program; if not, write to the Free Software Foundation, Inc.,
3051 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31"""
32DEPRECATION_PATTERN = '{kind} {name} is deprecated{reason}'
33
34
35def 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
61def 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
add_copyright(List[str] authors, int start_year, int end_year=None)
Definition admin.py:35
deprecated(str reason=None)
Definition admin.py:61