3
3
import operator
4
4
import os
5
5
import sys
6
+ from collections import OrderedDict
6
7
from copy import deepcopy
7
8
from pathlib import Path
8
9
11
12
get_all_args ,
12
13
get_extensions ,
13
14
get_yaml_contents ,
14
- peek ,
15
15
split_by_language ,
16
- )
17
- from coala_quickstart .generation .SettingsClass import (
18
- SettingTypes ,
19
- )
16
+ )
17
+
20
18
from coala_quickstart .green_mode .file_aggregator import (
21
19
aggregate_files ,
22
- )
20
+ )
23
21
from coala_quickstart .green_mode .Setting import (
24
22
find_max_min_of_setting ,
25
- )
23
+ )
26
24
from coala_quickstart .generation .Settings import (
27
25
generate_ignore_field ,
28
- )
26
+ )
29
27
from coala_quickstart .green_mode .QuickstartBear import (
30
28
QuickstartBear ,
31
- )
29
+ )
32
30
from coala_utils .string_processing .Core import (
33
31
escape ,
34
- )
32
+ )
35
33
from coalib .bears .GlobalBear import GlobalBear
36
34
from coalib .output .ConfWriter import ConfWriter
35
+ from coalib .output .ConfigConverter import ConfigConverter
37
36
from coalib .processes .Processing import (
38
37
get_file_dict ,
39
38
yield_ignore_ranges ,
40
- )
39
+ )
41
40
from coalib .settings .Section import Section
42
41
43
-
44
42
settings_key = 'green_mode_infinite_value_settings'
45
43
_CI_PYTEST_ACTIVE = os .environ .get ('CI' ) and os .environ .get ('PYTEST' )
46
44
_PYTHON_VERSION_MINOR = sys .version_info [0 :2 ]
@@ -72,20 +70,20 @@ def initialize_project_data(dir, ignore_globs):
72
70
"""
73
71
files_dirs = os .listdir (dir )
74
72
# 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 :]
76
74
final_data = []
77
75
78
76
for i in files_dirs :
79
77
to_continue = False
80
78
for glob in ignore_globs :
81
- if fnmatch .fnmatch (dir + i , glob ):
79
+ if fnmatch .fnmatch (dir + i , glob ):
82
80
to_continue = True
83
81
if to_continue is True :
84
82
continue
85
- if os .path .isfile (dir + i ):
83
+ if os .path .isfile (dir + i ):
86
84
final_data .append (i )
87
85
else :
88
- look_into_dir = dir + i + os .sep
86
+ look_into_dir = dir + i + os .sep
89
87
data = initialize_project_data (look_into_dir ,
90
88
ignore_globs )
91
89
final_data .append ({i : data })
@@ -114,7 +112,7 @@ def generate_complete_filename_list(contents, project_dir):
114
112
file_names_list .append (prefix + item )
115
113
else :
116
114
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 )))
118
116
return file_names_list
119
117
120
118
@@ -213,7 +211,7 @@ def get_setting_type(setting, bear, dir=None):
213
211
"""
214
212
__location__ = os .path .realpath (
215
213
os .path .join (os .getcwd (), os .path .dirname (__file__ ))) if (
216
- dir is None ) else dir
214
+ dir is None ) else dir
217
215
bear_settings = get_yaml_contents (os .path .join (
218
216
__location__ , 'bear_settings.yaml' ))
219
217
for type_setting in bear_settings :
@@ -481,8 +479,8 @@ def bear_test_fun(bears, bear_settings_obj, file_dict, ignore_ranges,
481
479
bear , file_dict , file_names , lang , non_op_kwargs ,
482
480
ignore_ranges , 'non-op' , printer ,
483
481
jobs = jobs ,
484
- )
485
- if len (op_kwargs ) < op_args_limit and not (
482
+ )
483
+ if len (op_kwargs ) < op_args_limit and not (
486
484
True in [len (value ) > value_to_op_args_limit
487
485
for key , value in op_kwargs .items ()]):
488
486
unified_kwargs = dict (non_op_kwargs )
@@ -492,7 +490,7 @@ def bear_test_fun(bears, bear_settings_obj, file_dict, ignore_ranges,
492
490
unified_kwargs , ignore_ranges , 'unified' ,
493
491
printer ,
494
492
jobs = jobs ,
495
- )
493
+ )
496
494
else :
497
495
unified_file_results = None
498
496
final_non_op_results .append (non_op_file_results )
@@ -597,8 +595,31 @@ def write_sections(self, sections):
597
595
self .write_section (individual_section )
598
596
599
597
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
+
600
620
def generate_green_mode_sections (data , project_dir , project_files ,
601
- ignore_globs , printer = None , suffix = '' ):
621
+ ignore_globs , in_toml , printer = None ,
622
+ suffix = '' ):
602
623
"""
603
624
Generates the section objects for the green_mode.
604
625
:param data:
@@ -610,6 +631,8 @@ def generate_green_mode_sections(data, project_dir, project_files,
610
631
List of paths to only the files inside the project directory.
611
632
:param ignore_globs:
612
633
The globs of files to ignore.
634
+ :param in_toml:
635
+ Decides whether to generate configuration files in toml or not
613
636
:param printer:
614
637
The ConsolePrinter object.
615
638
:param suffix:
@@ -658,8 +681,16 @@ def generate_green_mode_sections(data, project_dir, project_files,
658
681
new_bear_sections .append (section )
659
682
all_sections [bear .__name__ ] = new_bear_sections
660
683
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