Skip to content

Commit

Permalink
Generate config files in TOML
Browse files Browse the repository at this point in the history
This PR provides coala-quickstart
the flexibity to generate configuration
files in TOML

Closes coala#336
  • Loading branch information
PrajwalM2212 committed Aug 19, 2019
1 parent 5ece789 commit 06ebf38
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .ci/appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ test_script:
python setup.py install

- python -m pip install
git+https://github.com/coala/coala#egg=coala
git+https://github.com/PrajwalM2212/coala.git@writer#egg=coala
git+https://github.com/coala/coala-bears#egg=coala-bears


Expand Down
3 changes: 2 additions & 1 deletion .ci/deps.python-packages.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ function Install-coala {

Checkpoint-Pip-Constraints

Install-Pip-Requirement 'git+https://github.com/coala/coala#egg=coala'
Install-Pip-Requirement 'git+https://github.com/PrajwalM2212/coala.git@writer#egg=coala
'

if (!($stop_at -eq 'coala-bears')) {
Write-Output "Installing coala-bears"
Expand Down
5 changes: 3 additions & 2 deletions .moban.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ entry_points:
- coala-quickstart = coala_quickstart.coala_quickstart:main

dependencies:
- 'git+https://github.com/coala/coala#egg=coala'
- 'git+https://github.com/coala/coala-bears#egg=coala-bears'
- git+https://gitlab.com/coala/coala-utils#egg=coala-utils
- git+https://github.com/coala/coala-bears#egg=coala-bears
- git+https://github.com/PrajwalM2212/coala.git@writer#egg=coala
- gemfileparser~=0.6.2
- pyjsparser~=2.4.5

Expand Down
19 changes: 16 additions & 3 deletions coala_quickstart/coala_quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
remove_unusable_bears,
)
from coala_quickstart.generation.Settings import (
generate_settings, write_coafile)
generate_settings, write_coafile, write_toml_file)
from coala_quickstart.generation.SettingsClass import (
collect_bear_settings)
from coala_quickstart.green_mode.green_mode_core import green_mode
Expand All @@ -52,6 +52,11 @@ def _get_arg_parser():
'-C', '--non-interactive', const=True, action='store_const',
help='run coala-quickstart in non interactive mode')

arg_parser.add_argument('-T', '--toml-config', const=True,
action='store_const',
help='generate TOML config file '
'from coala-quickstart')

arg_parser.add_argument(
'--ci', action='store_const', dest='non_interactive', const=True,
help='continuous integration run, alias for `--non-interactive`')
Expand Down Expand Up @@ -86,7 +91,7 @@ def _get_arg_parser():


def main():
global MAX_ARGS_GREEN_MODE, MAX_VALUES_GREEN_MODE
global MAX_ARGS_GREEN_MODE, MAX_VALUES_GREEN_MODE, IN_TOML
arg_parser = _get_arg_parser()
args = arg_parser.parse_args()

Expand All @@ -104,6 +109,10 @@ def main():
MAX_ARGS_GREEN_MODE = args.max_args
if args.max_values:
MAX_VALUES_GREEN_MODE = args.max_values
if args.toml_config:
IN_TOML = True
else:
IN_TOML = False

if not args.green_mode and (args.max_args or args.max_values):
logging.warning(' --max-args and --max-values can be used '
Expand Down Expand Up @@ -143,6 +152,7 @@ def main():
project_dir, ignore_globs, relevant_bears, bear_settings_obj,
MAX_ARGS_GREEN_MODE,
MAX_VALUES_GREEN_MODE,
IN_TOML,
project_files,
printer,
)
Expand All @@ -163,4 +173,7 @@ def main():
extracted_information,
args.incomplete_sections)

write_coafile(printer, project_dir, settings)
if args.toml_config:
write_toml_file(printer, project_dir, settings)
else:
write_coafile(printer, project_dir, settings)
32 changes: 31 additions & 1 deletion coala_quickstart/generation/Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
from collections import OrderedDict
from datetime import date

from coalib.output.ConfigConverter import ConfigConverter
from coalib.settings.SectionFilling import fill_settings
from coala_quickstart.generation.SettingsFilling import (
fill_section, acquire_settings)
from coala_quickstart.generation.Utilities import (
split_by_language, get_extensions)
from coalib.settings.Section import Section
from coalib.output.ConfWriter import ConfWriter
from tomlkit import comment


def generate_section(section_name, extensions_used, bears):
Expand Down Expand Up @@ -153,7 +155,7 @@ def write_coafile(printer, project_dir, settings):
coafile = os.path.join(project_dir, '.coafile')
if os.path.isfile(coafile):
printer.print("'" + coafile + "' already exists.\nThe settings will be"
" written to '" + coafile + ".new'",
" written to '" + coafile + ".new'",
color='yellow')
coafile = coafile + '.new'

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

printer.print("'" + coafile + "' successfully generated.", color='green')


def write_toml_file(printer, project_dir, settings):
"""
Writes the .coafile.toml to disk.
:param printer:
A ``ConsolePrinter`` object used for console interactions.
:param project_dir:
Full path of the user's project directory.
:param settings:
A dict with section name as key and a ``Section`` object as value.
"""
generation_date = date.today().strftime('%d %b %Y')
generation_comment = ('Generated by coala-quickstart on '
'{date}.\n'.format(date=generation_date))

toml_file = os.path.join(project_dir, '.coafile.toml')
if os.path.isfile(toml_file):
printer.print("'" + toml_file + "' already exists.\nThe settings will"
" be written to '" +
".coafile.new.toml'",
color='yellow')
toml_file = '.coafile.new.toml'
writer = ConfigConverter(toml_file)
writer.document.add(comment(generation_comment))
writer.coafile_to_toml(settings)
printer.print("'" + toml_file + "' successfully generated.", color='green')
85 changes: 58 additions & 27 deletions coala_quickstart/green_mode/green_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import operator
import os
import sys
from collections import OrderedDict
from copy import deepcopy
from pathlib import Path

Expand All @@ -11,36 +12,33 @@
get_all_args,
get_extensions,
get_yaml_contents,
peek,
split_by_language,
)
from coala_quickstart.generation.SettingsClass import (
SettingTypes,
)
)

from coala_quickstart.green_mode.file_aggregator import (
aggregate_files,
)
)
from coala_quickstart.green_mode.Setting import (
find_max_min_of_setting,
)
)
from coala_quickstart.generation.Settings import (
generate_ignore_field,
)
)
from coala_quickstart.green_mode.QuickstartBear import (
QuickstartBear,
)
)
from coala_utils.string_processing.Core import (
escape,
)
)
from coalib.bears.GlobalBear import GlobalBear
from coalib.output.ConfWriter import ConfWriter
from coalib.output.ConfigConverter import ConfigConverter
from coalib.processes.Processing import (
get_file_dict,
yield_ignore_ranges,
)
)
from coalib.settings.Section import Section


settings_key = 'green_mode_infinite_value_settings'
_CI_PYTEST_ACTIVE = os.environ.get('CI') and os.environ.get('PYTEST')
_PYTHON_VERSION_MINOR = sys.version_info[0:2]
Expand Down Expand Up @@ -72,20 +70,20 @@ def initialize_project_data(dir, ignore_globs):
"""
files_dirs = os.listdir(dir)
# files_dirs holds names of both files and dirs.
dir_name = dir[dir.rfind(os.sep)+1:]
dir_name = dir[dir.rfind(os.sep) + 1:]
final_data = []

for i in files_dirs:
to_continue = False
for glob in ignore_globs:
if fnmatch.fnmatch(dir+i, glob):
if fnmatch.fnmatch(dir + i, glob):
to_continue = True
if to_continue is True:
continue
if os.path.isfile(dir+i):
if os.path.isfile(dir + i):
final_data.append(i)
else:
look_into_dir = dir+i+os.sep
look_into_dir = dir + i + os.sep
data = initialize_project_data(look_into_dir,
ignore_globs)
final_data.append({i: data})
Expand Down Expand Up @@ -114,7 +112,7 @@ def generate_complete_filename_list(contents, project_dir):
file_names_list.append(prefix + item)
else:
file_names_list += generate_complete_filename_list(
item[next(iter(item))], prefix+next(iter(item)))
item[next(iter(item))], prefix + next(iter(item)))
return file_names_list


Expand Down Expand Up @@ -213,7 +211,7 @@ def get_setting_type(setting, bear, dir=None):
"""
__location__ = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__))) if (
dir is None) else dir
dir is None) else dir
bear_settings = get_yaml_contents(os.path.join(
__location__, 'bear_settings.yaml'))
for type_setting in bear_settings:
Expand Down Expand Up @@ -481,8 +479,8 @@ def bear_test_fun(bears, bear_settings_obj, file_dict, ignore_ranges,
bear, file_dict, file_names, lang, non_op_kwargs,
ignore_ranges, 'non-op', printer,
jobs=jobs,
)
if len(op_kwargs) < op_args_limit and not(
)
if len(op_kwargs) < op_args_limit and not (
True in [len(value) > value_to_op_args_limit
for key, value in op_kwargs.items()]):
unified_kwargs = dict(non_op_kwargs)
Expand All @@ -492,7 +490,7 @@ def bear_test_fun(bears, bear_settings_obj, file_dict, ignore_ranges,
unified_kwargs, ignore_ranges, 'unified',
printer,
jobs=jobs,
)
)
else:
unified_file_results = None
final_non_op_results.append(non_op_file_results)
Expand Down Expand Up @@ -597,8 +595,31 @@ def write_sections(self, sections):
self.write_section(individual_section)


def write_toml_sections(self, sections):
sections_dict = OrderedDict()

if not sections['all'] == []:
all_section = sections['all'][0]
ignore_all = all_section['ignore']
sections_dict[all_section.name] = all_section
del sections['all']
else:
all_section = ''
ignore_all = ''

for section in sections:
for individual_section in sections[section]:
individual_section.defaults = all_section
if not ignore_all == '':
individual_section['ignore'] = str(
ignore_all) + ', ' + str(individual_section['ignore'])
sections_dict[individual_section.name] = individual_section
self.coafile_to_toml(sections_dict)


def generate_green_mode_sections(data, project_dir, project_files,
ignore_globs, printer=None, suffix=''):
ignore_globs, in_toml, printer=None,
suffix=''):
"""
Generates the section objects for the green_mode.
:param data:
Expand All @@ -610,6 +631,8 @@ def generate_green_mode_sections(data, project_dir, project_files,
List of paths to only the files inside the project directory.
:param ignore_globs:
The globs of files to ignore.
:param in_toml:
Decides whether to generate configuration files in toml or not
:param printer:
The ConsolePrinter object.
:param suffix:
Expand Down Expand Up @@ -658,8 +681,16 @@ def generate_green_mode_sections(data, project_dir, project_files,
new_bear_sections.append(section)
all_sections[bear.__name__] = new_bear_sections

coafile = os.path.join(project_dir, '.coafile.green' + suffix)
writer = ConfWriter(coafile)
write_sections(writer, all_sections)
writer.close()
printer.print("'" + coafile + "' successfully generated.", color='green')
if in_toml:
toml_file = os.path.join(project_dir, '.coafile.green.toml' + suffix)
writer = ConfigConverter(toml_file)
write_toml_sections(writer, all_sections)
printer.print("'" + toml_file + "' successfully generated.",
color='green')
else:
coafile = os.path.join(project_dir, '.coafile.green' + suffix)
writer = ConfWriter(coafile)
write_sections(writer, all_sections)
writer.close()
printer.print("'" + coafile + "' successfully generated.",
color='green')
7 changes: 5 additions & 2 deletions coala_quickstart/green_mode/green_mode_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def green_mode(project_dir: str, ignore_globs, bears, bear_settings_obj,
op_args_limit, value_to_op_args_limit, project_files,
op_args_limit, value_to_op_args_limit, in_toml, project_files,
printer=None):
"""
Runs the green mode of coala-quickstart.
Expand All @@ -44,6 +44,8 @@ def green_mode(project_dir: str, ignore_globs, bears, bear_settings_obj,
about whether a setting takes a boolean value or any other value.
:param op_args_limit:
The maximum number of optional bear arguments allowed for guessing.
:param in_toml:
Decides whether to generate configuration file in toml or not
:param project_files:
The list of files in the project.
:param value_to_op_args_limit:
Expand Down Expand Up @@ -95,7 +97,8 @@ def green_mode(project_dir: str, ignore_globs, bears, bear_settings_obj,
settings_unified[bear] = settings_non_op[bear]

generate_green_mode_sections(
settings_unified, project_dir, project_files, ignore_globs, printer)
settings_unified, project_dir, project_files, ignore_globs,
in_toml, printer)

# Final Dump.
dump_yaml_to_file(project_data, project_data_contents)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
git+https://github.com/coala/coala#egg=coala
git+https://gitlab.com/coala/coala-utils#egg=coala-utils
git+https://github.com/coala/coala-bears#egg=coala-bears
git+https://github.com/PrajwalM2212/coala.git@writer#egg=coala
gemfileparser~=0.6.2
pyjsparser~=2.4.5
Loading

0 comments on commit 06ebf38

Please sign in to comment.