1+ #!/usr/bin/env python3
2+ import yaml
3+ import sys
4+
5+ def add_ids_to_document (doc ):
6+ """Add sequential IDs to rollouts and constraints in a YAML document"""
7+ rollout_id = 1
8+ constraint_id = 1
9+
10+ # Add IDs to rollouts in flags
11+ if 'flags' in doc :
12+ for flag in doc ['flags' ]:
13+ if 'rollouts' in flag and flag ['rollouts' ]:
14+ for rollout in flag ['rollouts' ]:
15+ if 'id' not in rollout :
16+ rollout ['id' ] = str (rollout_id )
17+ rollout_id += 1
18+
19+ # Add IDs to constraints in segments
20+ if 'segments' in doc :
21+ for segment in doc ['segments' ]:
22+ if 'constraints' in segment and segment ['constraints' ]:
23+ for constraint in segment ['constraints' ]:
24+ if 'id' not in constraint :
25+ constraint ['id' ] = str (constraint_id )
26+ constraint_id += 1
27+
28+ return doc
29+
30+ def process_file (filepath ):
31+ """Process a YAML file to add IDs"""
32+ with open (filepath , 'r' ) as f :
33+ doc = yaml .safe_load (f )
34+
35+ doc = add_ids_to_document (doc )
36+
37+ with open (filepath , 'w' ) as f :
38+ yaml .dump (doc , f , default_flow_style = False , sort_keys = False , allow_unicode = True )
39+
40+ print (f"Processed { filepath } " )
41+
42+ if __name__ == "__main__" :
43+ if len (sys .argv ) < 2 :
44+ print ("Usage: python add_ids.py <yaml_file>" )
45+ sys .exit (1 )
46+
47+ for filepath in sys .argv [1 :]:
48+ process_file (filepath )
0 commit comments