File: deprecate.py

package info (click to toggle)
python-dendropy 4.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 68,392 kB
  • ctags: 3,947
  • sloc: python: 41,840; xml: 1,400; makefile: 15
file content (74 lines) | stat: -rw-r--r-- 2,609 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /usr/bin/env python

##############################################################################
##  DendroPy Phylogenetic Computing Library.
##
##  Copyright 2010-2015 Jeet Sukumaran and Mark T. Holder.
##  All rights reserved.
##
##  See "LICENSE.rst" for terms and conditions of usage.
##
##  If you use this work or any portion thereof in published work,
##  please cite it as:
##
##     Sukumaran, J. and M. T. Holder. 2010. DendroPy: a Python library
##     for phylogenetic computing. Bioinformatics 26: 1569-1571.
##
##############################################################################

"""
Handling deprecation warnings and messages correctly.
"""

import os
import warnings
from dendropy.utility import metavar

DEPRECATION_WARNING_FILTER = None
_DEPRECATION_WARNINGS_CONFIGURED = False

class CriticalDeprecationWarning(UserWarning):
    pass

def configure_deprecation_warning_behavior(warning_filter=None):
    global DEPRECATION_WARNING_FILTER
    global _DEPRECATION_WARNINGS_CONFIGURED
    if warning_filter is None:
        warning_filter = os.environ.get(metavar.DEPRECATION_WARNING_FILTER, "default")
    DEPRECATION_WARNING_FILTER = warning_filter
    warnings.simplefilter(DEPRECATION_WARNING_FILTER,
            CriticalDeprecationWarning)
    _DEPRECATION_WARNINGS_CONFIGURED = True

def _initialize_deprecation_warnings():
    global _DEPRECATION_WARNINGS_CONFIGURED
    if not _DEPRECATION_WARNINGS_CONFIGURED:
        configure_deprecation_warning_behavior()

def dendropy_deprecation_warning(**kwargs):
    _initialize_deprecation_warnings()
    leader = "  # "
    stacklevel = kwargs.pop("stacklevel", 3)
    if "message" in kwargs:
        message = kwargs["message"]
    elif "old_construct" in kwargs or "new_construct" in kwargs:
        message = []
        message.append("")
        if "preamble" in kwargs:
            message.append(leader + kwargs["preamble"])
        message.append(leader + "Instead of:")
        for construct in kwargs["old_construct"].split("\n"):
            message.append(leader + "    {}".format(construct))
        message.append(leader + "Use:")
        for construct in kwargs["new_construct"].split("\n"):
            message.append(leader + "    {}".format(construct))
        if "epilog" in kwargs:
            message.append(leader + kwargs["epilog"])
        message = "\n".join(message)
    _initialize_deprecation_warnings()
    old_formatwarning = warnings.formatwarning
    warnings.warn(
            message=message,
            category=CriticalDeprecationWarning,
            stacklevel=stacklevel,
            )