33import operator
44import os
55import sys
6+ from collections import OrderedDict
67from copy import deepcopy
78from pathlib import Path
89
1112 get_all_args ,
1213 get_extensions ,
1314 get_yaml_contents ,
14- peek ,
1515 split_by_language ,
16- )
17- from coala_quickstart .generation .SettingsClass import (
18- SettingTypes ,
19- )
16+ )
17+
2018from coala_quickstart .green_mode .file_aggregator import (
2119 aggregate_files ,
22- )
20+ )
2321from coala_quickstart .green_mode .Setting import (
2422 find_max_min_of_setting ,
25- )
23+ )
2624from coala_quickstart .generation .Settings import (
2725 generate_ignore_field ,
28- )
26+ )
2927from coala_quickstart .green_mode .QuickstartBear import (
3028 QuickstartBear ,
31- )
29+ )
3230from coala_utils .string_processing .Core import (
3331 escape ,
34- )
32+ )
3533from coalib .bears .GlobalBear import GlobalBear
3634from coalib .output .ConfWriter import ConfWriter
35+ from coalib .output .ConfigConverter import ConfigConverter
3736from coalib .processes .Processing import (
3837 get_file_dict ,
3938 yield_ignore_ranges ,
40- )
39+ )
4140from coalib .settings .Section import Section
4241
43-
4442settings_key = 'green_mode_infinite_value_settings'
4543_CI_PYTEST_ACTIVE = os .environ .get ('CI' ) and os .environ .get ('PYTEST' )
4644_PYTHON_VERSION_MINOR = sys .version_info [0 :2 ]
@@ -72,20 +70,20 @@ def initialize_project_data(dir, ignore_globs):
7270 """
7371 files_dirs = os .listdir (dir )
7472 # files_dirs holds names of both files and dirs.
75- dir_name = dir [dir .rfind (os .sep )+ 1 :]
73+ dir_name = dir [dir .rfind (os .sep ) + 1 :]
7674 final_data = []
7775
7876 for i in files_dirs :
7977 to_continue = False
8078 for glob in ignore_globs :
81- if fnmatch .fnmatch (dir + i , glob ):
79+ if fnmatch .fnmatch (dir + i , glob ):
8280 to_continue = True
8381 if to_continue is True :
8482 continue
85- if os .path .isfile (dir + i ):
83+ if os .path .isfile (dir + i ):
8684 final_data .append (i )
8785 else :
88- look_into_dir = dir + i + os .sep
86+ look_into_dir = dir + i + os .sep
8987 data = initialize_project_data (look_into_dir ,
9088 ignore_globs )
9189 final_data .append ({i : data })
@@ -114,7 +112,7 @@ def generate_complete_filename_list(contents, project_dir):
114112 file_names_list .append (prefix + item )
115113 else :
116114 file_names_list += generate_complete_filename_list (
117- item [next (iter (item ))], prefix + next (iter (item )))
115+ item [next (iter (item ))], prefix + next (iter (item )))
118116 return file_names_list
119117
120118
@@ -213,7 +211,7 @@ def get_setting_type(setting, bear, dir=None):
213211 """
214212 __location__ = os .path .realpath (
215213 os .path .join (os .getcwd (), os .path .dirname (__file__ ))) if (
216- dir is None ) else dir
214+ dir is None ) else dir
217215 bear_settings = get_yaml_contents (os .path .join (
218216 __location__ , 'bear_settings.yaml' ))
219217 for type_setting in bear_settings :
@@ -481,8 +479,8 @@ def bear_test_fun(bears, bear_settings_obj, file_dict, ignore_ranges,
481479 bear , file_dict , file_names , lang , non_op_kwargs ,
482480 ignore_ranges , 'non-op' , printer ,
483481 jobs = jobs ,
484- )
485- if len (op_kwargs ) < op_args_limit and not (
482+ )
483+ if len (op_kwargs ) < op_args_limit and not (
486484 True in [len (value ) > value_to_op_args_limit
487485 for key , value in op_kwargs .items ()]):
488486 unified_kwargs = dict (non_op_kwargs )
@@ -492,7 +490,7 @@ def bear_test_fun(bears, bear_settings_obj, file_dict, ignore_ranges,
492490 unified_kwargs , ignore_ranges , 'unified' ,
493491 printer ,
494492 jobs = jobs ,
495- )
493+ )
496494 else :
497495 unified_file_results = None
498496 final_non_op_results .append (non_op_file_results )
@@ -597,8 +595,31 @@ def write_sections(self, sections):
597595 self .write_section (individual_section )
598596
599597
598+ def write_toml_sections (self , sections ):
599+ sections_dict = OrderedDict ()
600+
601+ if not sections ['all' ] == []:
602+ all_section = sections ['all' ][0 ]
603+ ignore_all = all_section ['ignore' ]
604+ sections_dict [all_section .name ] = all_section
605+ del sections ['all' ]
606+ else :
607+ all_section = ''
608+ ignore_all = ''
609+
610+ for section in sections :
611+ for individual_section in sections [section ]:
612+ individual_section .defaults = all_section
613+ if not ignore_all == '' :
614+ individual_section ['ignore' ] = str (
615+ ignore_all ) + ', ' + str (individual_section ['ignore' ])
616+ sections_dict [individual_section .name ] = individual_section
617+ self .coafile_to_toml (sections_dict )
618+
619+
600620def generate_green_mode_sections (data , project_dir , project_files ,
601- ignore_globs , printer = None , suffix = '' ):
621+ ignore_globs , in_toml , printer = None ,
622+ suffix = '' ):
602623 """
603624 Generates the section objects for the green_mode.
604625 :param data:
@@ -610,6 +631,8 @@ def generate_green_mode_sections(data, project_dir, project_files,
610631 List of paths to only the files inside the project directory.
611632 :param ignore_globs:
612633 The globs of files to ignore.
634+ :param in_toml:
635+ Decides whether to generate configuration files in toml or not
613636 :param printer:
614637 The ConsolePrinter object.
615638 :param suffix:
@@ -658,8 +681,16 @@ def generate_green_mode_sections(data, project_dir, project_files,
658681 new_bear_sections .append (section )
659682 all_sections [bear .__name__ ] = new_bear_sections
660683
661- coafile = os .path .join (project_dir , '.coafile.green' + suffix )
662- writer = ConfWriter (coafile )
663- write_sections (writer , all_sections )
664- writer .close ()
665- printer .print ("'" + coafile + "' successfully generated." , color = 'green' )
684+ if in_toml :
685+ toml_file = os .path .join (project_dir , '.coafile.green.toml' + suffix )
686+ writer = ConfigConverter (toml_file )
687+ write_toml_sections (writer , all_sections )
688+ printer .print ("'" + toml_file + "' successfully generated." ,
689+ color = 'green' )
690+ else :
691+ coafile = os .path .join (project_dir , '.coafile.green' + suffix )
692+ writer = ConfWriter (coafile )
693+ write_sections (writer , all_sections )
694+ writer .close ()
695+ printer .print ("'" + coafile + "' successfully generated." ,
696+ color = 'green' )
0 commit comments