LALInference 4.1.9.1-eeff03c
deprecation.py
Go to the documentation of this file.
1import inspect
2import os
3import re
4import sys
5import warnings
6import textwrap
7
8__all__ = ('warn',)
9
10
11# Python <3.7 hides all DeprecationWarnings by default, whereas Python 3.7
12# shows DeprecationWarnings that occur in main script (__main__) or in
13# an interactive session. This subclass produces the Python 3.7 behavior.
14class MovedWarning(DeprecationWarning):
15 pass
16
17
18warnings.filterwarnings(
19 'default', category=MovedWarning, module='__main__')
20
21
22def rewrap(s):
23 return textwrap.fill(re.sub(r'\s+', ' ', s))
24
25
26def warn(new=None, old=None, kind='module'):
27 if old is None:
28 old = inspect.getmodule(inspect.currentframe().f_back.f_code).__name__
29 kind = 'module'
30 if old == '__main__':
31 old = os.path.basename(sys.argv[0])
32 old.replace('.py', '')
33 kind = 'tool'
34
35 if new is None:
36 new = old.replace('lalinference', 'ligo.skymap')
37
38 kwargs = dict(old=old, new=new, kind=kind)
39
40 message = '"{}" has moved to ligo.skymap.\n\n'.format(old)
41
42 message += rewrap(
43 '''The {old} {kind} from LALInference has been replaced by the {new}
44 {kind} from the ligo.skymap package. The old {kind} is no longer
45 tested and supported and will be removed in a future version of
46 LALInference. Please update to the ligo.skymap package.'''.format(
47 **kwargs))
48
49 python_version = sys.version_info[:2]
50 min_python_version = (3, 5)
51 if python_version < min_python_version:
52 message += '\n\n'
53 message += rewrap(
54 '''The ligo.skymap package requires Python {min_major}.{min_minor}
55 or newer. You have Python {major}.{minor}. Before installing
56 ligo.skymap, please update to Python {min_major}.{min_minor} or
57 newer.'''.format(
58 major=python_version[0],
59 minor=python_version[1],
60 min_major=min_python_version[0],
61 min_minor=min_python_version[1]))
62
63 message += '\n\n'
64 message += rewrap(
65 '''You can install ligo.skymap with pip by running this command in your
66 shell:''')
67
68 message += '\n\n'
69 message += ' pip install ligo.skymap'
70 message += '\n'
71
72 warnings.warn(message, MovedWarning, stacklevel=3)
def warn(new=None, old=None, kind='module')
Definition: deprecation.py:26