1+ import json
2+ import os
3+ import shutil
4+
5+ # Пути к файлам
6+ FILE_PATH = '/home/rompik/Projects/Python/OpenIso/data/settings/OpenIso.json'
7+ BACKUP_PATH = '/home/rompik/Projects/Python/OpenIso/data/settings/OpenIso_backup.json'
8+
9+ # Dictionary of replacements (SKEY -> [New Subgroup, New Description, New Group])
10+ # All descriptions are translated into technical English
11+ MAPPING = {
12+ # Valves & Instrumentation
13+ "ZV" : ["Pressure Relief Valve" , "Safety relief valve for pressure protection. Instrumentation type. ISO 14617-10." , "Valves" ],
14+ "MA" : ["Gate Valve (Manual)" , "Manual gate valve for isolation. ASME B16.34 standard design." , "Valves" ],
15+ "M3" : ["3-Way Valve" , "Three-way diverting/mixing valve. ISO 14617-4." , "Valves" ],
16+ "M4" : ["4-Way Valve" , "Four-way crossover valve. ISO 14617-4." , "Valves" ],
17+ "FI" : ["Flow Indicator" , "Sight flow indicator for visual process monitoring." , "Instrumentation" ],
18+ "HV" : ["Hand Valve" , "Generic manual isolation valve. Handwheel or lever operated." , "Valves" ],
19+ "HA" : ["Valve Actuator" , "Generic valve actuator symbol." , "Valves" ],
20+
21+ # Fittings & Flanges
22+ "COSW" : ["Socket Weld Coupling" , "Full coupling, socket weld (SW) type. ASME B16.11." , "Fittings" ],
23+ "FLSJ" : ["Lap Joint Flange" , "Lap joint flange for use with stub ends. ASME B16.5." , "Flanges" ],
24+ "FLWN" : ["Weld Neck Flange" , "Tapered hub flange for butt welding. ASME B16.5." , "Flanges" ],
25+ "FLBL" : ["Blind Flange" , "Flange used to seal the end of a piping system. ASME B16.5." , "Flanges" ],
26+ "KASW" : ["Socket Weld Cap" , "Pipe cap, socket weld (SW) connection. ASME B16.11." , "Fittings" ],
27+ "NI" : ["Swage Nipple (BW)" , "Concentric swage nipple, butt weld (BW) ends. ASME B16.9." , "Reducers" ],
28+ "CTBW" : ["Concentric Reducer" , "Concentric reducer, butt weld. ASME B16.9 / ISO 5251." , "Reducers" ],
29+ "REBW" : ["Eccentric Reducer" , "Eccentric reducer for horizontal line drainage. ASME B16.9." , "Reducers" ],
30+
31+ # Supports & Misc
32+ "HANG" : ["Pipe Hanger" , "Rigid pipe hanger support for vertical loads." , "Supports" ],
33+ "MP" : ["Magnetic Flowmeter" , "Electromagnetic flow meter. Process instrumentation." , "Instrumentation" ],
34+ "II" : ["Insulating Joint" , "Dielectric insulation kit/gasket for corrosion protection." , "Fittings" ],
35+ "WELD" : ["Weld Point" , "Field or shop weld identification mark." , "Welds" ],
36+
37+ # Spindles (Operators)
38+ "01SP" : ["Standard Handwheel" , "Manual handwheel valve operator." , "Spindles" ],
39+ "02SP" : ["Lever Operator" , "Manual lever/handle for quarter-turn valves." , "Spindles" ],
40+ "10SP" : ["Chain Wheel" , "Chain operated handwheel for high-reach valves." , "Spindles" ],
41+ "11SP" : ["Electric Actuator" , "Motor-operated valve (MOV) electric actuator." , "Spindles" ],
42+ "12SP" : ["Pneumatic Actuator" , "Air-operated valve (AOV) pneumatic actuator." , "Spindles" ],
43+ }
44+
45+ def clean_and_translate_library (file_path ):
46+ if not os .path .exists (file_path ):
47+ print (f"Error: File { file_path } not found." )
48+ return
49+
50+ # Create backup
51+ shutil .copy2 (file_path , BACKUP_PATH )
52+ print (f"Backup created: { BACKUP_PATH } " )
53+
54+ with open (file_path , 'r' , encoding = 'utf-8' ) as f :
55+ data = json .load (f )
56+
57+ updated_count = 0
58+
59+ for skey , params in data .items ():
60+ # 1. Update from mapping
61+ if skey in MAPPING :
62+ new_sub , new_desc , new_group = MAPPING [skey ]
63+ params ['subgroup' ] = new_sub
64+ params ['description' ] = new_desc
65+ params ['skey_group' ] = new_group
66+ updated_count += 1
67+
68+ # 2. General cleanup for remaining Unknowns
69+ else :
70+ if params .get ('subgroup' ) == 'Unknown' :
71+ params ['subgroup' ] = 'Generic Fitting'
72+
73+ if params .get ('skey_group' ) == 'Unknown' :
74+ params ['skey_group' ] = 'Miscellaneous'
75+
76+ if params .get ('description' ) == "" :
77+ params ['description' ] = f"Symbol SKEY: { skey } . Technical specification pending."
78+
79+ # Translate 'All' to English if present
80+ if params .get ('subgroup' ) == 'Все' or params .get ('subgroup' ) == 'All' :
81+ params ['subgroup' ] = 'Universal'
82+
83+ updated_count += 1
84+
85+ # Save the English version
86+ with open (file_path , 'w' , encoding = 'utf-8' ) as f :
87+ json .dump (data , f , indent = 4 , ensure_ascii = False )
88+
89+ print (f"Success! Updated and translated { updated_count } elements." )
90+
91+ if __name__ == "__main__" :
92+ clean_and_translate_library (FILE_PATH )
0 commit comments