Skip to content

Commit eab0786

Browse files
committed
toml mode
1 parent 5ece789 commit eab0786

File tree

8 files changed

+288
-51
lines changed

8 files changed

+288
-51
lines changed

.moban.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ entry_points:
2828
- coala-quickstart = coala_quickstart.coala_quickstart:main
2929

3030
dependencies:
31-
- 'git+https://github.com/coala/coala#egg=coala'
32-
- 'git+https://github.com/coala/coala-bears#egg=coala-bears'
31+
- git+https://gitlab.com/coala/coala-utils#egg=coala-utils
32+
- git+https://github.com/coala/coala-bears#egg=coala-bears
33+
- git+https://github.com/PrajwalM2212/coala.git@writer#egg=coala
3334
- gemfileparser~=0.6.2
3435
- pyjsparser~=2.4.5
3536

coala_quickstart/coala_quickstart.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
remove_unusable_bears,
2727
)
2828
from coala_quickstart.generation.Settings import (
29-
generate_settings, write_coafile)
29+
generate_settings, write_coafile, write_toml_file)
3030
from coala_quickstart.generation.SettingsClass import (
3131
collect_bear_settings)
3232
from coala_quickstart.green_mode.green_mode_core import green_mode
@@ -52,6 +52,11 @@ def _get_arg_parser():
5252
'-C', '--non-interactive', const=True, action='store_const',
5353
help='run coala-quickstart in non interactive mode')
5454

55+
arg_parser.add_argument('-T', '--toml-config', const=True,
56+
action='store_const',
57+
help='generate TOML config file '
58+
'from coala-quickstart')
59+
5560
arg_parser.add_argument(
5661
'--ci', action='store_const', dest='non_interactive', const=True,
5762
help='continuous integration run, alias for `--non-interactive`')
@@ -86,7 +91,7 @@ def _get_arg_parser():
8691

8792

8893
def main():
89-
global MAX_ARGS_GREEN_MODE, MAX_VALUES_GREEN_MODE
94+
global MAX_ARGS_GREEN_MODE, MAX_VALUES_GREEN_MODE, IN_TOML
9095
arg_parser = _get_arg_parser()
9196
args = arg_parser.parse_args()
9297

@@ -104,6 +109,10 @@ def main():
104109
MAX_ARGS_GREEN_MODE = args.max_args
105110
if args.max_values:
106111
MAX_VALUES_GREEN_MODE = args.max_values
112+
if args.toml_config:
113+
IN_TOML = True
114+
else:
115+
IN_TOML = False
107116

108117
if not args.green_mode and (args.max_args or args.max_values):
109118
logging.warning(' --max-args and --max-values can be used '
@@ -143,6 +152,7 @@ def main():
143152
project_dir, ignore_globs, relevant_bears, bear_settings_obj,
144153
MAX_ARGS_GREEN_MODE,
145154
MAX_VALUES_GREEN_MODE,
155+
IN_TOML,
146156
project_files,
147157
printer,
148158
)
@@ -163,4 +173,7 @@ def main():
163173
extracted_information,
164174
args.incomplete_sections)
165175

166-
write_coafile(printer, project_dir, settings)
176+
if args.toml_config:
177+
write_toml_file(printer, project_dir, settings)
178+
else:
179+
write_coafile(printer, project_dir, settings)

coala_quickstart/generation/Settings.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
from collections import OrderedDict
33
from datetime import date
44

5+
from coalib.output.ConfigConverter import ConfigConverter
56
from coalib.settings.SectionFilling import fill_settings
67
from coala_quickstart.generation.SettingsFilling import (
78
fill_section, acquire_settings)
89
from coala_quickstart.generation.Utilities import (
910
split_by_language, get_extensions)
1011
from coalib.settings.Section import Section
1112
from coalib.output.ConfWriter import ConfWriter
13+
from tomlkit import comment
1214

1315

1416
def generate_section(section_name, extensions_used, bears):
@@ -153,7 +155,7 @@ def write_coafile(printer, project_dir, settings):
153155
coafile = os.path.join(project_dir, '.coafile')
154156
if os.path.isfile(coafile):
155157
printer.print("'" + coafile + "' already exists.\nThe settings will be"
156-
" written to '" + coafile + ".new'",
158+
" written to '" + coafile + ".new'",
157159
color='yellow')
158160
coafile = coafile + '.new'
159161

@@ -163,3 +165,31 @@ def write_coafile(printer, project_dir, settings):
163165
writer.close()
164166

165167
printer.print("'" + coafile + "' successfully generated.", color='green')
168+
169+
170+
def write_toml_file(printer, project_dir, settings):
171+
"""
172+
Writes the .coafile.toml to disk.
173+
174+
:param printer:
175+
A ``ConsolePrinter`` object used for console interactions.
176+
:param project_dir:
177+
Full path of the user's project directory.
178+
:param settings:
179+
A dict with section name as key and a ``Section`` object as value.
180+
"""
181+
generation_date = date.today().strftime('%d %b %Y')
182+
generation_comment = ('Generated by coala-quickstart on '
183+
'{date}.\n'.format(date=generation_date))
184+
185+
toml_file = os.path.join(project_dir, '.coafile.toml')
186+
if os.path.isfile(toml_file):
187+
printer.print("'" + toml_file + "' already exists.\nThe settings will"
188+
" be written to '" +
189+
".coafile.new.toml'",
190+
color='yellow')
191+
toml_file = '.coafile.new.toml'
192+
writer = ConfigConverter(toml_file)
193+
writer.document.add(comment(generation_comment))
194+
writer.coafile_to_toml(settings)
195+
printer.print("'" + toml_file + "' successfully generated.", color='green')

coala_quickstart/green_mode/green_mode.py

+58-27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import operator
44
import os
55
import sys
6+
from collections import OrderedDict
67
from copy import deepcopy
78
from pathlib import Path
89

@@ -11,36 +12,33 @@
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+
2018
from coala_quickstart.green_mode.file_aggregator import (
2119
aggregate_files,
22-
)
20+
)
2321
from coala_quickstart.green_mode.Setting import (
2422
find_max_min_of_setting,
25-
)
23+
)
2624
from coala_quickstart.generation.Settings import (
2725
generate_ignore_field,
28-
)
26+
)
2927
from coala_quickstart.green_mode.QuickstartBear import (
3028
QuickstartBear,
31-
)
29+
)
3230
from coala_utils.string_processing.Core import (
3331
escape,
34-
)
32+
)
3533
from coalib.bears.GlobalBear import GlobalBear
3634
from coalib.output.ConfWriter import ConfWriter
35+
from coalib.output.ConfigConverter import ConfigConverter
3736
from coalib.processes.Processing import (
3837
get_file_dict,
3938
yield_ignore_ranges,
40-
)
39+
)
4140
from coalib.settings.Section import Section
4241

43-
4442
settings_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+
600620
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=''):
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')

coala_quickstart/green_mode/green_mode_core.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
def green_mode(project_dir: str, ignore_globs, bears, bear_settings_obj,
22-
op_args_limit, value_to_op_args_limit, project_files,
22+
op_args_limit, value_to_op_args_limit, in_toml, project_files,
2323
printer=None):
2424
"""
2525
Runs the green mode of coala-quickstart.
@@ -44,6 +44,8 @@ def green_mode(project_dir: str, ignore_globs, bears, bear_settings_obj,
4444
about whether a setting takes a boolean value or any other value.
4545
:param op_args_limit:
4646
The maximum number of optional bear arguments allowed for guessing.
47+
:param in_toml:
48+
Decides whether to generate configuration file in toml or not
4749
:param project_files:
4850
The list of files in the project.
4951
:param value_to_op_args_limit:
@@ -95,7 +97,8 @@ def green_mode(project_dir: str, ignore_globs, bears, bear_settings_obj,
9597
settings_unified[bear] = settings_non_op[bear]
9698

9799
generate_green_mode_sections(
98-
settings_unified, project_dir, project_files, ignore_globs, printer)
100+
settings_unified, project_dir, project_files, ignore_globs,
101+
in_toml, printer)
99102

100103
# Final Dump.
101104
dump_yaml_to_file(project_data, project_data_contents)

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
git+https://github.com/coala/coala#egg=coala
1+
git+https://gitlab.com/coala/coala-utils#egg=coala-utils
22
git+https://github.com/coala/coala-bears#egg=coala-bears
3+
git+https://github.com/PrajwalM2212/coala.git@writer#egg=coala
34
gemfileparser~=0.6.2
45
pyjsparser~=2.4.5

tests/generation/BearsTest.py

+27
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,19 @@ def test_bears_allow_incomplete_sections_mode(self):
346346
os.remove('.coafile')
347347
os.chdir(orig_cwd)
348348

349+
def test_bears_allow_incomplete_sections_toml_mode(self):
350+
sys.argv.append('--ci')
351+
sys.argv.append('-T')
352+
sys.argv.append('--allow-incomplete-sections')
353+
orig_cwd = os.getcwd()
354+
os.chdir(os.path.dirname(os.path.realpath(__file__)))
355+
os.chdir("bears_ci_testfiles")
356+
with retrieve_stdout() as custom_stdout:
357+
main()
358+
self.assertNotIn("usable",
359+
custom_stdout.getvalue())
360+
os.chdir(orig_cwd)
361+
349362
def test_bears_ci_mode(self):
350363
sys.argv.append('--ci')
351364
orig_cwd = os.getcwd()
@@ -358,6 +371,20 @@ def test_bears_ci_mode(self):
358371
os.remove('.coafile')
359372
os.chdir(orig_cwd)
360373

374+
def test_bears_ci_toml_mode(self):
375+
sys.argv.append('--ci')
376+
sys.argv.append('-T')
377+
orig_cwd = os.getcwd()
378+
os.chdir(os.path.dirname(os.path.realpath(__file__)))
379+
os.chdir("bears_ci_testfiles")
380+
with retrieve_stdout() as custom_stdout:
381+
main()
382+
self.assertIn("usable",
383+
custom_stdout.getvalue())
384+
os.remove('.coafile.toml')
385+
os.remove('.coafile.new.toml')
386+
os.chdir(orig_cwd)
387+
361388
def test_bears_no_filter_by_capability_mode(self):
362389
languages = []
363390
with bear_test_module():

0 commit comments

Comments
 (0)