File: error.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 (196 lines) | stat: -rw-r--r-- 6,731 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#! /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.
##
##############################################################################

"""
Errors, exceptions, warnings, etc.
"""

import sys
import re
import warnings
import inspect
import subprocess

class ImmutableTaxonNamespaceError(TypeError):
    def __init__(self, message):
        TypeError.__init__(self, message)

class DataParseError(Exception):

    def __init__(self,
            message=None,
            line_num=None,
            col_num=None,
            filename=None,
            stream=None):
        Exception.__init__(self)
        self.line_num = line_num
        self.col_num = col_num
        self.message = message
        self.stream = stream
        self.filename = None
        self.decorate_with_name(filename=filename, stream=stream)

    def decorate_with_name(self,
            filename=None,
            stream=None):
        if filename is not None:
            self.filename = filename
        if stream is not None:
            try:
                self.filename = stream.name
            except AttributeError:
                pass

    def __str__(self):
        f, l, c = "", "", ""
        if self.filename:
            f =  " '{}'".format(self.filename)
        if self.line_num is not None:
            l =  " on line {}".format(self.line_num)
        if self.col_num is not None:
            c =  " at column {}".format(self.col_num)
        return "Error parsing data source{}{}{}: {}".format(f, l, c, self.message)

class UnsupportedSchemaError(NotImplementedError):

    def __init__(self, *args, **kwargs):
        NotImplementedError.__init__(self, *args, **kwargs)

class UnspecifiedSchemaError(Exception):

    def __init__(self, *args, **kwargs):
        Exception.__init__(self, *args, **kwargs)

class UnspecifiedSourceError(Exception):

    def __init__(self, *args, **kwargs):
        Exception.__init__(self, *args, **kwargs)

class TooManyArgumentsError(TypeError):

    def __init__(self,
            message=None,
            func_name=None,
            max_args=None,
            args=None):
        if message is None and (func_name is not None and max_args):
            message = "{}() takes a maximum of {} arguments ({} given)".format(func_name, max_args, len(args))
        TypeError.__init__(self, message)

class InvalidArgumentValueError(ValueError):

    def __init__(self, message=None, func_name=None, arg=None):
        if message is None and (func_name is not None and arg is not None):
            message = "{}() does not accept objects of type '{}' as an argument".format(func_name, arg.__class__.__name__)
        ValueError.__init__(self, message)

class MultipleInitializationSourceError(TypeError):
    def __init__(self, message=None, class_name=None, arg=None):
        if message is None and (class_name is not None and arg is not None):
            message = "{}() does not accept data 'stream' or 'schema' arguments when initializing with another object".format(class_name)
        TypeError.__init__(self, message)

class TaxonNamespaceIdentityError(ValueError):
    def __init__(self, o1, o2):
        message = "Non-identical taxon namespace references: {} is not {}".format(
                "<TaxonNamespace object at {}>".format(str(hex(id((o1.taxon_namespace))))),
                "<TaxonNamespace object at {}>".format(str(hex(id((o2.taxon_namespace))))),
                )
        ValueError.__init__(self, message)

class SingleTaxonAssemblageException(ValueError):
    def __init__(self, message=None):
        ValueError.__init__(self, message)

class NullAssemblageException(ValueError):
    def __init__(self, message=None):
        ValueError.__init__(self, message)

class MixedRootingError(ValueError):
    def __init__(self, message=None):
        ValueError.__init__(self, message)

class TaxonNamespaceReconstructionError(ValueError):
    def __init__(self, message=None):
        ValueError.__init__(self, message)

class UltrametricityError(ValueError):
    def __init__(self, message=None):
        ValueError.__init__(self, message)

class ProcessFailedException(Exception):
    """Exception to be raised when branching process results in all lineages going extinct."""
    def __init__(self, *args, **kwargs):
        Exception.__init__(self, *args, **kwargs)

class TreeSimTotalExtinctionException(ProcessFailedException):
    """Exception to be raised when branching process results in all lineages going extinct."""
    def __init__(self, *args, **kwargs):
        ProcessFailedException.__init__(self, *args, **kwargs)

class NullLeafSetException(Exception):

    def __init__(self, *args, **kwargs):
        Exception.__init__(self, *args, **kwargs)

class SeedNodeDeletionException(Exception):

    def __init__(self, *args, **kwargs):
        Exception.__init__(self, *args, **kwargs)

class InvalidMultispeciesCoalescentStructureError(Exception):

    def __init__(self, *args, **kwargs):
        Exception.__init__(self, *args, **kwargs)

class ExternalServiceError(Exception):

    def __init__(self,
            service_name,
            invocation_command,
            service_input,
            returncode,
            stdout,
            stderr):
        self.service_name = service_name
        self.invocation_command = invocation_command
        self.service_input = service_input
        self.returncode = returncode
        self.stdout = stdout
        self.stderr = stderr
        self.message = self.compose_message()

    def __str__(self):
        return self.compose_message()

    def compose_message(self):
        parts = []
        parts.append("- Service process {} exited with return code: {}".format(self.service_name, self.returncode))
        parts.append("- Service invoked with command:")
        parts.append("   {}".format(self.invocation_command))
        parts.append("<<<<<<< (SERVICE STANDARD INPUT)")
        parts.append(self.service_input)
        parts.append(">>>>>>>")
        parts.append("<<<<<<< (SERVICE STANDARD OUTPUT)")
        parts.append(self.stdout)
        parts.append(">>>>>>>")
        parts.append("<<<<<<< (SERVICE STANDARD ERROR)")
        parts.append(self.stderr)
        parts.append(">>>>>>>")
        return "\n".join(parts)