1# ex: python3 sort_yaml_functions.py 2# ex: must be within yaml directory 3import yaml 4import os 5 6 7def sort_yaml_functions(yaml_file): 8 with open(yaml_file, "r") as f: 9 yaml_data = yaml.safe_load(f) 10 11 if "functions" in yaml_data: 12 yaml_data["functions"].sort(key=lambda x: x["name"]) 13 14 class IndentYamlListDumper(yaml.Dumper): 15 def increase_indent(self, flow=False, indentless=False): 16 return super(IndentYamlListDumper, self).increase_indent(flow, False) 17 18 with open(yaml_file, "w") as f: 19 yaml.dump( 20 yaml_data, 21 f, 22 Dumper=IndentYamlListDumper, 23 default_flow_style=False, 24 sort_keys=False, 25 ) 26 27 28def main(): 29 current_directory = os.getcwd() 30 yaml_files = [ 31 file for file in os.listdir(current_directory) if file.endswith(".yaml") 32 ] 33 34 for yaml_file in yaml_files: 35 sort_yaml_functions(yaml_file) 36 37 38if __name__ == "__main__": 39 main() 40