Passed
Pull Request — dev (#1344)
by
unknown
02:18
created

convert_oem_to_v2.run_conversion()   B

Complexity

Conditions 8

Size

Total Lines 40
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 40
rs 7.2933
c 0
b 0
f 0
cc 8
nop 0
1
#!/usr/bin/env python3
2
3
"""
4
Script to convert metadata files from OEMetadata-1.x to OEMetadata-2.0 format.
5
6
Requires at least oemetadata in version OEP-1.5.2.
7
"""
8
9
from pathlib import Path
10
import json
11
import logging
12
13
from omi.conversion import convert_metadata
14
15
logger = logging.getLogger(__name__)
16
17
# Metadata input/output directories
18
SOURCE_DIR = Path(__file__).parent.parent / "results"
19
TARGET_DIR = Path(__file__).parent.parent / "results" / "converted"
20
21
# OMI target version for metadata version conversion
22
TARGET_VERSION = "OEMetadata-2.0"
23
24
25
def run_conversion():
26
    source_dir = SOURCE_DIR
27
    target_dir = TARGET_DIR
28
    target_version = TARGET_VERSION
29
30
    if not target_version:
31
        logger.error("TARGET_VERSION is not defined in settings.py")
32
        return
33
34
    if not source_dir.exists():
35
        logger.error(f"Source directory does not exist: {source_dir}")
36
        return
37
38
    target_dir.mkdir(parents=True, exist_ok=True)
39
    logger.info("Starting metadata conversion")
40
    logger.debug(f"Source: {source_dir}")
41
    logger.debug(f"Target: {target_dir}")
42
    logger.debug(f"Target version: {target_version}")
43
44
    for json_file in sorted(source_dir.glob("*.json")):
45
        if not json_file.is_file():
46
            continue
47
48
        logger.info(f"Converting {json_file.name}")
49
        try:
50
            with open(json_file, "r", encoding="utf-8") as f:
51
                metadata = json.load(f)
52
53
            converted = convert_metadata(metadata, target_version)
54
55
            output_path = target_dir / json_file.name
56
            with open(output_path, "w", encoding="utf-8") as f:
57
                json.dump(converted, f, ensure_ascii=False, indent=2)
58
59
            logger.info(f"Saved converted metadata: {output_path}")
60
61
        except Exception:
62
            logger.exception(f"Error converting file: {json_file.name}")
63
64
    logger.info("Metadata conversion completed.")
65
66
67
if __name__ == "__main__":
68
    logging.basicConfig(
69
        level=logging.INFO,
70
        format="%(asctime)s [%(levelname)s] %(message)s",
71
    )
72
    run_conversion()
73