Debian Codegraph @

jenkins_jobs/errors.py

raw
1 """Exception classes for jenkins_jobs errors"""
2
3 import inspect
4 from dataclasses import dataclass
5
6 from .position import Pos
7
8
9 def is_sequence(arg):
10 return not hasattr(arg, "strip") and (
11 hasattr(arg, "__getitem__") or hasattr(arg, "__iter__")
12 )
13
14
15 def context_lines(message, pos):
16 if not pos:
17 return [message]
18 snippet_lines = [line.rstrip() for line in pos.snippet.splitlines()]
19 return [
20 f"{pos.path}:{pos.line + 1}:{pos.column + 1}: {message}",
21 *snippet_lines,
22 ]
23
24
25 @dataclass
26 class Context:
27 message: str
28 pos: Pos
29
30 @property
31 def lines(self):
32 return context_lines(self.message, self.pos)
33
34
35 class JenkinsJobsException(Exception):
36 def __init__(self, message, pos=None, ctx=None):
37 super().__init__(message)
38 self.pos = pos
39 self.ctx = ctx or [] # Context list
40
41 @property
42 def message(self):
43 return self.args[0]
44
45 def with_pos(self, pos):
46 return JenkinsJobsException(self.message, pos, self.ctx)
47
48 def with_context(self, message, pos, ctx=None):
49 return JenkinsJobsException(
50 self.message, self.pos, [*(ctx or []), Context(message, pos), *self.ctx]
51 )
52
53 def with_ctx_list(self, ctx):
54 return JenkinsJobsException(self.message, self.pos, [*ctx, *self.ctx])
55
56 @property
57 def lines(self):
58 ctx_lines = []
59 for ctx in self.ctx:
60 ctx_lines += ctx.lines
61 return [*ctx_lines, *context_lines(self.message, self.pos)]
62
63
64 class ModuleError(JenkinsJobsException):
65 def get_module_name(self):
66 frame = inspect.currentframe()
67 module_name = "<unresolved>"
68 while frame:
69 # XML generation called via dispatch
70 co_name = frame.f_code.co_name
71 if co_name == "run":
72 break
73 if co_name == "dispatch":
74 data = frame.f_locals
75 module_name = "%s.%s" % (data["component_type"], data["name"])
76 break
77 # XML generation done directly by class using gen_xml or root_xml
78 if co_name == "gen_xml" or co_name == "root_xml":
79 data = frame.f_locals["data"]
80 module_name = next(iter(data.keys()))
81 break
82 frame = frame.f_back
83
84 return module_name
85
86
87 class InvalidAttributeError(ModuleError):
88 def __init__(self, attribute_name, value, valid_values=None, pos=None, ctx=None):
89 message = "'{0}' is an invalid value for attribute {1}.{2}".format(
90 value, self.get_module_name(), attribute_name
91 )
92
93 if is_sequence(valid_values):
94 message += "\nValid values include: {0}".format(
95 ", ".join("'{0}'".format(value) for value in valid_values)
96 )
97
98 super().__init__(message, pos, ctx)
99
100
101 class MissingAttributeError(ModuleError):
102 def __init__(self, missing_attribute, module_name=None, pos=None, ctx=None):
103 module = module_name or self.get_module_name()
104 if is_sequence(missing_attribute):
105 message = "One of {0} must be present in '{1}'".format(
106 ", ".join("'{0}'".format(value) for value in missing_attribute), module
107 )
108 else:
109 message = "Missing {0} from an instance of '{1}'".format(
110 missing_attribute, module
111 )
112
113 super().__init__(message, pos, ctx)
114
115
116 class AttributeConflictError(ModuleError):
117 def __init__(self, attribute_name, attributes_in_conflict, module_name=None):
118 module = module_name or self.get_module_name()
119 message = "Attribute '{0}' can not be used together with {1} in {2}".format(
120 attribute_name,
121 ", ".join("'{0}'".format(value) for value in attributes_in_conflict),
122 module,
123 )
124
125 super(AttributeConflictError, self).__init__(message)
126
127
128 class YAMLFormatError(JenkinsJobsException):
129 pass
130
131
132 class JJBConfigException(JenkinsJobsException):
133 pass