Debian Codegraph @

jenkins_jobs/formatter.py

raw
1 #!/usr/bin/env python
2 # Copyright (C) 2015 OpenStack, LLC.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 # Manage interpolation of JJB variables into template strings.
17
18 import _string
19 import logging
20 import re
21 from string import Formatter
22
23 from jinja2 import Undefined
24 from jinja2.exceptions import UndefinedError
25
26 from .errors import JenkinsJobsException
27
28 logger = logging.getLogger(__name__)
29
30
31 class CustomFormatter(Formatter):
32 """
33 Custom formatter to allow non-existing key references when formatting a
34 string
35 """
36
37 _expr = r"""
38 (?<!{){({{)* # non-pair opening {
39 (?:obj:)? # obj:
40 (?P<key>\w+) # key
41 (?:\|(?P<default>[^}]*))? # default fallback
42 }(}})*(?!}) # non-pair closing }
43 """
44 _matcher = re.compile(_expr, re.VERBOSE)
45 _whole_matcher = re.compile(f"^{_expr}$", re.VERBOSE)
46
47 def __init__(self, allow_empty=False):
48 super().__init__()
49 self.allow_empty = allow_empty
50
51 def vformat(self, format_string, args, kwargs):
52 # Special case of returning the object preserving it's type if the entire string
53 # matches a single parameter.
54 result = self._whole_matcher.match(format_string)
55 if result is not None:
56 try:
57 value = kwargs[result.group("key")]
58 except KeyError:
59 pass
60 else:
61 if not isinstance(value, Undefined):
62 return value
63
64 # handle multiple fields within string via a callback to re.sub()
65 def re_replace(match):
66 key = match.group("key")
67 default = match.group("default")
68
69 if default is not None:
70 if key not in kwargs or isinstance(kwargs[key], Undefined):
71 return default
72 else:
73 return "{%s}" % key
74 return match.group(0)
75
76 format_string = self._matcher.sub(re_replace, format_string)
77
78 try:
79 return super().vformat(format_string, args, kwargs)
80 except (JenkinsJobsException, UndefinedError, ValueError) as x:
81 if len(format_string) > 40:
82 short_fmt = format_string[:80] + "..."
83 else:
84 short_fmt = format_string
85 raise JenkinsJobsException(f"While formatting string {short_fmt!r}: {x}")
86
87 def enum_required_params(self, format_string):
88 def re_replace(match):
89 key = match.group("key")
90 return "{%s}" % key
91
92 prepared_format_string = self._matcher.sub(re_replace, format_string)
93 for literal_text, field_name, format_spec, conversion in self.parse(
94 prepared_format_string
95 ):
96 if field_name is None:
97 continue
98 arg_used, rest = _string.formatter_field_name_split(field_name)
99 if arg_used == "" or type(arg_used) is int:
100 raise JenkinsJobsException(
101 f"Positional format arguments are not supported: {format_string!r}"
102 )
103 yield arg_used
104
105 def enum_param_defaults(self, format_string):
106 for match in self._matcher.finditer(format_string):
107 key = match.group("key")
108 default = match.group("default")
109 if default is not None:
110 yield (key, default)
111
112 def get_value(self, key, args, kwargs):
113 try:
114 return super().get_value(key, args, kwargs)
115 except KeyError:
116 if self.allow_empty:
117 logger.debug(
118 "Found uninitialized key %s, replaced with empty string", key
119 )
120 return ""
121 raise JenkinsJobsException(f"Missing parameter: {key!r}")
122
123
124 def enum_str_format_required_params(format, pos):
125 formatter = CustomFormatter()
126 try:
127 yield from formatter.enum_required_params(str(format))
128 except JenkinsJobsException as x:
129 raise x.with_pos(pos)
130
131
132 def enum_str_format_param_defaults(format):
133 formatter = CustomFormatter()
134 yield from formatter.enum_param_defaults(str(format))