-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathrpobject.py
144 lines (117 loc) · 5.68 KB
/
rpobject.py
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
"""
RenderPipeline
Copyright (c) 2014-2016 tobspr <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
# Disable the xxx has no yyy member warning, pylint seems to be
# unable to figure out the Colorama properties, and throws an error
# for each property.
# pylint: disable=no-member
from __future__ import print_function
import sys
# Load and init colorama, used to color the output
from rplibs.colorama import init as init_colorama
from rplibs.colorama import Fore, Style
init_colorama()
class RPObject(object):
""" This is the base class for every object in the render pipeline. It
provides the functions debug, warn, error and fatal for classes which
inherit from this object, including the name of the class when printing out
the message. """
_OUTPUT_LEVEL = 0
_OUTPUT_LEVELS = ["debug", "warning", "error", "fatal"]
@classmethod
def set_output_level(cls, level):
""" Sets the output level, messages with a level below will not be
printed. E.g. if you set the output level to "error", only error and
fatal messages will be shown. """
assert level in RPObject._OUTPUT_LEVELS
RPObject._OUTPUT_LEVEL = RPObject._OUTPUT_LEVELS.index(level)
@staticmethod
def global_debug(context, *args, **kwargs):
""" This method can be used from a static context to print a debug
message. The first argument should be the name of the object / context,
all other arguments should be the message. """
if RPObject._OUTPUT_LEVEL > 0:
return
print(kwargs.get("color", Fore.GREEN) + "[>] " +
context.ljust(25) + " " + Style.RESET_ALL + Fore.WHITE +
' '.join([str(i) for i in args]), Fore.RESET + Style.RESET_ALL)
@staticmethod
def global_warn(context, *args):
""" This method can be used from a static context to print a warning.
The first argument should be the name of the object / context, all
other arguments should be the message. """
if RPObject._OUTPUT_LEVEL > 1:
return
print(Fore.YELLOW + Style.BRIGHT + "[!] " + context.ljust(25) +
Fore.YELLOW + Style.BRIGHT + " " + ' '.join([str(i) for i in args]) +
Fore.RESET + Style.RESET_ALL)
@staticmethod
def global_error(context, *args):
""" This method can be used from a static context to print an error.
The first argument should be the name of the object / context, all
other arguments should be the message. """
if RPObject._OUTPUT_LEVEL > 2:
return
print(Fore.RED + Style.BRIGHT + "\n[!!!] " +
context.ljust(23) + " " + ' '.join([str(i) for i in args]) +
"\n" + Fore.RESET + Style.RESET_ALL)
def __init__(self, name=None):
""" Initiates the RPObject with a given name. The name should be
representative about the class. If no name is given, the classname
is used """
if name is None:
name = self.__class__.__name__
self._debug_name = name
self._debug_color = Fore.GREEN
def _set_debug_color(self, color, style=None):
""" Sets the color used to output debug messages """
self._debug_color = getattr(Fore, color.upper())
if style:
self._debug_color += getattr(Style, style.upper())
@property
def debug_name(self):
""" Returns the name of the debug object """
return self._debug_name
@debug_name.setter
def debug_name(self, name):
""" Renames this object """
self._debug_name = name
def debug(self, *args):
""" Outputs a debug message, something that is not necessarry
but provides useful information for the developer """
self.global_debug(self._debug_name, *args, color=self._debug_color)
def warn(self, *args):
""" Outputs a warning message, something that failed or does
not work, but does not prevent the program from running """
self.global_warn(self._debug_name, *args)
def error(self, *args):
""" Outputs an error message, something really serious.
Hopefully this never get's called! """
self.global_error(self._debug_name, *args)
def fatal(self, *args):
""" Outputs a fatal error message, printing out the errors and then
calling sys.exit to terminate the program. This method should be called
when something failed so hard that the pipeline has to exit. """
# We have to set output level to 0 here, so we can print out errors
RPObject._OUTPUT_LEVEL = 0
self.error(*args)
sys.exit(0)
def __repr__(self):
""" Represents this object. Subclasses can override this """
return self._debug_name