Coverage for bim2sim/plugins/PluginEnergyPlus/bim2sim_energyplus/utils/utils_hash_function.py: 0%

23 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-17 11:04 +0000

1import hashlib 

2import logging 

3from pathlib import Path 

4 

5logger = logging.getLogger(__name__) 

6 

7 

8def generate_hash(ifc_path) -> str: 

9 """Generate SHA-256 hash for IFC file with cross-platform consistency""" 

10 sha256_hash = hashlib.sha256() 

11 

12 # Read file in binary mode to avoid encoding issues 

13 with open(ifc_path, "rb") as f: 

14 file_content = f.read() 

15 # Normalize line endings to Unix style (\n) for cross-platform consistency 

16 normalized_content = file_content.replace(b'\r\n', b'\n').replace(b'\r', b'\n') 

17 sha256_hash.update(normalized_content) 

18 

19 ifc_hash = sha256_hash.hexdigest() 

20 ifc_filename = Path(ifc_path).name 

21 hash_prefix = "IFC_GEOMETRY_HASH" # prefix 

22 hash_line = f"! {hash_prefix}: {ifc_hash} | IFC_FILENAME: {ifc_filename}\n" 

23 logger.info(f"Generated hash for IFC file {ifc_filename}: {ifc_hash}") 

24 return hash_line 

25 

26 

27def add_hash_into_idf(hash_line, idf_path): 

28 """Add hash with specific prefix for easy search""" 

29 with open(idf_path, 'r', encoding='utf-8') as f: 

30 lines = f.readlines() 

31 # Insert at first line 

32 lines.insert(0, hash_line) 

33 with open(idf_path, 'w', encoding='utf-8') as f: 

34 f.writelines(lines) 

35 

36 logger.info(f"Added hash to IDF file")