|
1 | | -from __future__ import annotations as _annotations |
| 1 | +from __future__ import annotations |
2 | 2 |
|
3 | 3 | import copy |
4 | | -from typing import TYPE_CHECKING as _TYPE_CHECKING |
| 4 | +from typing import TYPE_CHECKING |
5 | 5 |
|
6 | 6 | import controlman |
7 | 7 | import jinja2 |
|
23 | 23 | from proman.manager.user import UserManager |
24 | 24 | from proman.manager.variable import VariableManager |
25 | 25 |
|
26 | | -if _TYPE_CHECKING: |
| 26 | +if TYPE_CHECKING: |
27 | 27 | from github_contexts import GitHubContext |
28 | 28 | from github_contexts.github.payload.object import Issue, PullRequest |
29 | 29 | from gittidy import Git |
|
35 | 35 | from proman.report import Reporter |
36 | 36 |
|
37 | 37 |
|
| 38 | +def from_metadata_json( |
| 39 | + git_api: Git, |
| 40 | + jinja_env_vars: dict, |
| 41 | + github_context: GitHubContext, |
| 42 | + github_api_actions: GitHubRepoAPI, |
| 43 | + github_api_admin: GitHubRepoAPI, |
| 44 | + github_link: GitHubLink, |
| 45 | + reporter: Reporter, |
| 46 | + zenodo_token: Token, |
| 47 | + zenodo_sandbox_token: Token, |
| 48 | + commit_hash: str | None = None, |
| 49 | +) -> Manager: |
| 50 | + branch_name = git_api.current_branch_name() |
| 51 | + address = f"from the {branch_name} branch of the {repo} repository" |
| 52 | + log_title = f"Metadata Load ({branch_name})" |
| 53 | + err_msg = f"Failed to load metadata file {address}." |
| 54 | + try: |
| 55 | + if commit_hash: |
| 56 | + data = controlman.from_json_file_at_commit( |
| 57 | + git_manager=git_api, |
| 58 | + commit_hash=commit_hash, |
| 59 | + ) |
| 60 | + else: |
| 61 | + data = controlman.from_json_file(repo_path=git_api.repo_path) |
| 62 | + logger.success( |
| 63 | + log_title, |
| 64 | + f"Metadata loaded successfully {address}.", |
| 65 | + ) |
| 66 | + return Manager( |
| 67 | + data=data, |
| 68 | + git_api=git_api, |
| 69 | + jinja_env_vars=jinja_env_vars, |
| 70 | + github_context=github_context, |
| 71 | + github_api_actions=github_api_actions, |
| 72 | + github_api_admin=github_api_admin, |
| 73 | + github_link=github_link, |
| 74 | + zenodo_token=zenodo_token, |
| 75 | + zenodo_sandbox_token=zenodo_sandbox_token, |
| 76 | + reporter=reporter, |
| 77 | + ) |
| 78 | + except controlman.exception.load.ControlManInvalidMetadataError as e: |
| 79 | + logger.critical( |
| 80 | + log_title, |
| 81 | + err_msg, |
| 82 | + e.report.body["problem"].content, |
| 83 | + ) |
| 84 | + reporter.add( |
| 85 | + name="main", |
| 86 | + status="fail", |
| 87 | + summary=f"Failed to load metadata {address}.", |
| 88 | + section="", |
| 89 | + ) |
| 90 | + raise ProManException() |
| 91 | + except controlman.exception.load.ControlManSchemaValidationError as e: |
| 92 | + logger.critical( |
| 93 | + log_title, |
| 94 | + err_msg, |
| 95 | + e.report.body["problem"].content, |
| 96 | + ) |
| 97 | + reporter.add( |
| 98 | + name="main", |
| 99 | + status="fail", |
| 100 | + summary=f"Failed to load metadata {address}.", |
| 101 | + section="", |
| 102 | + ) |
| 103 | + raise ProManException() |
| 104 | + |
| 105 | + |
38 | 106 | class Manager: |
39 | 107 | def __init__( |
40 | 108 | self, |
@@ -222,71 +290,3 @@ def recursive_fill(template, path): |
222 | 290 | @staticmethod |
223 | 291 | def normalize_github_date(date_str: str) -> str: |
224 | 292 | return date.to_internal(date.from_github(date_str)) |
225 | | - |
226 | | - |
227 | | -def from_metadata_json( |
228 | | - git_api: Git, |
229 | | - jinja_env_vars: dict, |
230 | | - github_context: GitHubContext, |
231 | | - github_api_actions: GitHubRepoAPI, |
232 | | - github_api_admin: GitHubRepoAPI, |
233 | | - github_link: GitHubLink, |
234 | | - reporter: Reporter, |
235 | | - zenodo_token: Token, |
236 | | - zenodo_sandbox_token: Token, |
237 | | - commit_hash: str | None = None, |
238 | | -) -> Manager: |
239 | | - branch_name = git_api.current_branch_name() |
240 | | - address = f"from the {branch_name} branch of the {repo} repository" |
241 | | - log_title = f"Metadata Load ({branch_name})" |
242 | | - err_msg = f"Failed to load metadata file {address}." |
243 | | - try: |
244 | | - if commit_hash: |
245 | | - data = controlman.from_json_file_at_commit( |
246 | | - git_manager=git_api, |
247 | | - commit_hash=commit_hash, |
248 | | - ) |
249 | | - else: |
250 | | - data = controlman.from_json_file(repo_path=git_api.repo_path) |
251 | | - logger.success( |
252 | | - log_title, |
253 | | - f"Metadata loaded successfully {address}.", |
254 | | - ) |
255 | | - return Manager( |
256 | | - data=data, |
257 | | - git_api=git_api, |
258 | | - jinja_env_vars=jinja_env_vars, |
259 | | - github_context=github_context, |
260 | | - github_api_actions=github_api_actions, |
261 | | - github_api_admin=github_api_admin, |
262 | | - github_link=github_link, |
263 | | - zenodo_token=zenodo_token, |
264 | | - zenodo_sandbox_token=zenodo_sandbox_token, |
265 | | - reporter=reporter, |
266 | | - ) |
267 | | - except controlman.exception.load.ControlManInvalidMetadataError as e: |
268 | | - logger.critical( |
269 | | - log_title, |
270 | | - err_msg, |
271 | | - e.report.body["problem"].content, |
272 | | - ) |
273 | | - reporter.add( |
274 | | - name="main", |
275 | | - status="fail", |
276 | | - summary=f"Failed to load metadata {address}.", |
277 | | - section="", |
278 | | - ) |
279 | | - raise ProManException() |
280 | | - except controlman.exception.load.ControlManSchemaValidationError as e: |
281 | | - logger.critical( |
282 | | - log_title, |
283 | | - err_msg, |
284 | | - e.report.body["problem"].content, |
285 | | - ) |
286 | | - reporter.add( |
287 | | - name="main", |
288 | | - status="fail", |
289 | | - summary=f"Failed to load metadata {address}.", |
290 | | - section="", |
291 | | - ) |
292 | | - raise ProManException() |
0 commit comments