Skip to content

Commit

Permalink
Add green_mode.py
Browse files Browse the repository at this point in the history
This file is the center of --green-mode and contains the underlying
core of this mode of coala-quickstart.

* green_mode(): The master method which calls all the underlying
  methods in this file and many others. Some broad actions that it
  performs:
  * Creates `.project_data.yaml` to store the directory and file
    structure of the project, deletes it in the end. Further commits
    may change this behaviour and add code to reuse this data
    generated.
  * Calls the QuickstartBear to traverse the file_dict and guess some
  	setting values on its own.
  * Calls the methods to provide some other information about settings
  	of some bears which can't be detected by traversing the file_dict
  	eg. settings based on the names of files.
  * Calls the methods to test each bear for each file for all the
    values for each setting until a green setting is found for a bear.
  * Will call the methods to create section object and write `.coafile`
  	in further commits.

* bear_test_fun(): Calls the methods to get all possible combinations
  of values to bear settings and supply these to other underlying
  methods to test them for each bear.

* run_test_on_each_bear(): Prints a message about which bear, the tests
  are running on and calls local_bear_test() or global_bear_test()
  depending on the type of bear to guess the green setting values
  from the combination of all values.

* local_bear_test(), global_bear_test(): Run coala repetitively on the
  bear in a multiprocessing environment and return the green settings
  found.

* check_bear_results(): Checks whether the result objects returned by
  the bear are 0 or if they lie in the ignore scope of the code to
  testify whether the current bear settings are green.

* get_kwargs(): Produces combinations of all setting values for the
  bears which are given one by one to the bear to run upon.

* get_type_of_setting(): Detects the type of a bear argument, i.e.
  whether it accepts boolean values or something like int which falls
  under the category of infinite set of values, whether some setting
  requires a config file of a specific linter or whether an argument
  accepts some discrete set of values.

* run_quickstartbear(): Runs QuickstartBear to guess some setting
  values from the file_dict and write the results to
  `.project_data.yaml`.

* find_min_of_setting()/find_max_of_setting(): Are helper functions
  for bear settings that run in coordination with per file results of
  QuickstartBear to get the project minima/maxima value of that
  particular setting.

* generate_complete_filename_list(): From the file/directory structure
  written to the `.project_data.yaml` gets only the list of files.

* initialize_project_data(): Initializes the `.project_data.yaml` by
  writing to the file/directory structure of the project with file
  names as items of a list but directories as dicts again as sub items
  of a list.

* Some other helper methods for some basic operations are also created.

Apart from the changes to green_mode.py a method that is added to
Utilities.py is:

contained_in(): Detects whether the first argument which must be
a SourceRange object is contained absolutely inside the
other SourceRange object with closed boundaries.

Tests are added for each method except the final green_mode() method.

coala_quickstart.py: The arguments:
* MAX_NUM_OF_OPTIONAL_ARGS_ALLOWED_FOR_GREEN_MODE
* MAX_NUM_OF_VALUES_OF_OPTIONAL_ARGS_ALLOWED_FOR_GREEN_MODE
are introduced.
  • Loading branch information
ishanSrt committed Aug 11, 2018
1 parent fe76d27 commit e06b9b5
Show file tree
Hide file tree
Showing 14 changed files with 1,110 additions and 4 deletions.
1 change: 1 addition & 0 deletions .nocover.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ nocover_file_globs:
- coala_quickstart/info_extractors/EditorconfigParsing.py
- coala_quickstart/info_extractors/GemfileInfoExtractor.py
- coala_quickstart/info_extractors/GruntfileInfoExtractor.py
- coala_quickstart/green_mode/green_mode_core.py

nocover_regexes:
# coala_quickstart.py
Expand Down
14 changes: 12 additions & 2 deletions coala_quickstart/coala_quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
from coala_quickstart.generation.Settings import (
generate_settings, write_coafile)
from coala_quickstart.generation.SettingsClass import (
collect_bear_settings)
collect_bear_settings, build_bear_settings)
from coala_quickstart.green_mode.green_mode_core import green_mode

MAX_NUM_OF_OPTIONAL_ARGS_ALLOWED_FOR_GREEN_MODE = 5
MAX_NUM_OF_VALUES_OF_OPTIONAL_ARGS_ALLOWED_FOR_GREEN_MODE = 5


def _get_arg_parser():
Expand Down Expand Up @@ -110,7 +114,13 @@ def main():
used_languages, printer, arg_parser, extracted_information)

if args.green_mode:
collect_bear_settings(relevant_bears)
build_bear_settings(relevant_bears)
bear_settings_obj = collect_bear_settings(relevant_bears)
green_mode(
project_dir, ignore_globs, relevant_bears, bear_settings_obj,
MAX_NUM_OF_OPTIONAL_ARGS_ALLOWED_FOR_GREEN_MODE,
MAX_NUM_OF_VALUES_OF_OPTIONAL_ARGS_ALLOWED_FOR_GREEN_MODE,
printer)

print_relevant_bears(printer, relevant_bears)

Expand Down
5 changes: 5 additions & 0 deletions coala_quickstart/generation/SettingsClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,8 @@ def collect_bear_settings(bears):
for bear in bears[language]:
bear_settings_obj.append(BearSettings(bear))
return bear_settings_obj


def build_bear_settings(bears):
pass # pragma: no cover
# TODO build bear_settings.yaml on the fly when the bear args are annotated
69 changes: 69 additions & 0 deletions coala_quickstart/generation/Utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,72 @@ def peek(iterable):
except StopIteration:
return None
return first, itertools.chain([first], iterable)


def contained_in(smaller, bigger):
"""
Takes in two SourceRange objects and checks whether
the first one lies inside the other one.
:param smaller:
The SourceRange object that needs to be checked whether
it is inside the other one.
:param bigger:
The SourceRange object that needs to be checked whether
it contains the other one.
:return:
True if smaller is inside the bigger else false.
"""
smaller_file = smaller.start.file
bigger_file = bigger.start.file

smaller_start_line = smaller.start.line
smaller_start_column = smaller.start.column
smaller_end_line = smaller.end.line
smaller_end_column = smaller.end.column

bigger_start_line = bigger.start.line
bigger_start_column = bigger.start.column
bigger_end_line = bigger.end.line
bigger_end_column = bigger.end.column

if None in [smaller_start_line, smaller_start_column,
smaller_end_line, smaller_end_column,
bigger_start_line, bigger_start_column,
bigger_end_line, bigger_end_column]:
return False

if not smaller_file == bigger_file:
return False

if smaller_start_line < bigger_start_line:
return False

if smaller_end_line > bigger_end_line:
return False

if smaller_start_line > bigger_start_line and (
smaller_end_line < bigger_end_line):
return True

same_start_line = True if (
smaller_start_line == bigger_start_line) else False

same_end_line = True if (
smaller_end_line == bigger_end_line) else False

if same_start_line and same_end_line:
if smaller_start_column < bigger_start_column:
return False
if smaller_end_column > bigger_end_column:
return False
return True

if same_start_line:
if smaller_start_column < bigger_start_column:
return False
return True

assert same_end_line
if smaller_end_column > bigger_end_column:
return False
return True
2 changes: 1 addition & 1 deletion coala_quickstart/green_mode/filename_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _discover_prefixes(self, node, prefix, min_length, len, min_files):
current_prefix = ''.join(prefix)+node.character
to_delete = []
for i in self.prefixes:
if i in current_prefix:
if i in current_prefix: # pragma: no cover
to_delete.append(i)
for i in to_delete:
self.prefixes.pop(i)
Expand Down
Loading

0 comments on commit e06b9b5

Please sign in to comment.