|
18 | 18 | from .remote_state import S3TerraformRemoteStateRetriever |
19 | 19 |
|
20 | 20 |
|
| 21 | +class ConfigProcessor(object): |
| 22 | + |
| 23 | + def process(self, cwd=None, path=None, filters=(), exclude_keys=(), enclosing_key=None, output_format=yaml, print_data=False, |
| 24 | + output_file=None, skip_interpolations=False, skip_interpolation_validation=False): |
| 25 | + |
| 26 | + path = self.get_relative_path(path) |
| 27 | + |
| 28 | + if skip_interpolations: |
| 29 | + skip_interpolation_validation = True |
| 30 | + |
| 31 | + if cwd is None: |
| 32 | + cwd = os.getcwd() |
| 33 | + |
| 34 | + generator = ConfigGenerator(cwd, path) |
| 35 | + generator.generate_hierarchy() |
| 36 | + generator.process_hierarchy() |
| 37 | + |
| 38 | + if not skip_interpolations: |
| 39 | + generator.resolve_interpolations() |
| 40 | + generator.add_dynamic_data() |
| 41 | + generator.resolve_interpolations() |
| 42 | + |
| 43 | + if len(filters) > 0: |
| 44 | + generator.filter_data(filters) |
| 45 | + |
| 46 | + if len(exclude_keys) > 0: |
| 47 | + generator.exclude_keys(exclude_keys) |
| 48 | + |
| 49 | + if not skip_interpolation_validation: |
| 50 | + generator.validate_interpolations() |
| 51 | + |
| 52 | + data = generator.add_enclosing_key(enclosing_key) if enclosing_key else generator.generated_data |
| 53 | + |
| 54 | + formatted_data = generator.output_data(data, output_format) |
| 55 | + |
| 56 | + if print_data: |
| 57 | + print(formatted_data) |
| 58 | + |
| 59 | + if output_file: |
| 60 | + with open(output_file, 'w') as f: |
| 61 | + f.write(formatted_data) |
| 62 | + |
| 63 | + return data |
| 64 | + |
| 65 | + @staticmethod |
| 66 | + def get_relative_path(self, path): |
| 67 | + cwd = os.path.join(os.getcwd(), '') |
| 68 | + if path.startswith(cwd): |
| 69 | + return path[len(cwd):] |
| 70 | + return path |
| 71 | + |
| 72 | + |
21 | 73 | class ConfigGenerator(object): |
22 | 74 | """ |
23 | 75 | this class is used to create a config generator object which will be used to generate cluster definition files |
@@ -160,55 +212,3 @@ def resolve_interpolations(self): |
160 | 212 |
|
161 | 213 | def validate_interpolations(self): |
162 | 214 | self.interpolation_validator.check_all_interpolations_resolved(self.generated_data) |
163 | | - |
164 | | - |
165 | | -class ConfigProcessor(object): |
166 | | - |
167 | | - def process(self, cwd=None, path=None, filters=(), exclude_keys=(), enclosing_key=None, output_format=yaml, print_data=False, |
168 | | - output_file=None, skip_interpolations=False, skip_interpolation_validation=False): |
169 | | - |
170 | | - path = self.get_relative_path(path) |
171 | | - |
172 | | - if skip_interpolations: |
173 | | - skip_interpolation_validation = True |
174 | | - |
175 | | - if cwd is None: |
176 | | - cwd = os.getcwd() |
177 | | - |
178 | | - generator = ConfigGenerator(cwd, path) |
179 | | - generator.generate_hierarchy() |
180 | | - generator.process_hierarchy() |
181 | | - |
182 | | - if not skip_interpolations: |
183 | | - generator.resolve_interpolations() |
184 | | - generator.add_dynamic_data() |
185 | | - generator.resolve_interpolations() |
186 | | - |
187 | | - if len(filters) > 0: |
188 | | - generator.filter_data(filters) |
189 | | - |
190 | | - if len(exclude_keys) > 0: |
191 | | - generator.exclude_keys(exclude_keys) |
192 | | - |
193 | | - if not skip_interpolation_validation: |
194 | | - generator.validate_interpolations() |
195 | | - |
196 | | - data = generator.add_enclosing_key(enclosing_key) if enclosing_key else generator.generated_data |
197 | | - |
198 | | - formatted_data = generator.output_data(data, output_format) |
199 | | - |
200 | | - if print_data: |
201 | | - print(formatted_data) |
202 | | - |
203 | | - if output_file: |
204 | | - with open(output_file, 'w') as f: |
205 | | - f.write(formatted_data) |
206 | | - |
207 | | - return data |
208 | | - |
209 | | - @staticmethod |
210 | | - def get_relative_path(self, path): |
211 | | - cwd = os.path.join(os.getcwd(), '') |
212 | | - if path.startswith(cwd): |
213 | | - return path[len(cwd):] |
214 | | - return path |
0 commit comments