2023-12-20 17:17:41 +00:00
|
|
|
import json
|
2024-04-15 18:02:05 +00:00
|
|
|
import logging
|
2023-12-20 17:17:41 +00:00
|
|
|
|
2024-04-05 19:23:50 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-12-20 17:17:41 +00:00
|
|
|
def parse_json(borg_json_output, label):
|
|
|
|
'''
|
|
|
|
Given a Borg JSON output string, parse it as JSON into a dict. Inject the given borgmatic
|
|
|
|
repository label into it and return the dict.
|
2024-04-05 19:23:50 +00:00
|
|
|
|
|
|
|
Raise JSONDecodeError if the JSON output cannot be parsed.
|
2023-12-20 17:17:41 +00:00
|
|
|
'''
|
2024-04-05 19:23:50 +00:00
|
|
|
lines = borg_json_output.splitlines()
|
|
|
|
start_line_index = 0
|
|
|
|
|
|
|
|
# Scan forward to find the first line starting with "{" and assume that's where the JSON starts.
|
|
|
|
for line_index, line in enumerate(lines):
|
|
|
|
if line.startswith('{'):
|
|
|
|
start_line_index = line_index
|
|
|
|
break
|
|
|
|
|
|
|
|
json_data = json.loads('\n'.join(lines[start_line_index:]))
|
2023-12-20 17:17:41 +00:00
|
|
|
|
|
|
|
if 'repository' not in json_data:
|
|
|
|
return json_data
|
|
|
|
|
|
|
|
json_data['repository']['label'] = label or ''
|
|
|
|
|
|
|
|
return json_data
|