def get_setting_type(setting, bear, dir=None):
    """
    Retrieves the type of setting according to cEP0022.md
    and seperates the other settings from generation/SettingsClass
    into type2, type3, type4 or unguessable at the moment based on
    the data in bear_settings.yaml.
    :param setting:
        The setting name.
    :param bear:
        The bear class to which the setting belongs.
    :param dir:
        The directory where to look for `bear_settings.yaml`, defaults
        to the `green_mode` directory.
    :return:
        - The type of setting according to bear_settings.yaml.
        - The list of acceptable values in case of type3 setting
          or the value guessed by the QuickstartBear which fits
          the project or the default value in case of unguessable
          settings.
    """
    __location__ = os.path.realpath(
        os.path.join(os.getcwd(), os.path.dirname(__file__))) if (
        dir is None) else dir
    bear_settings = get_yaml_contents(os.path.join(
        __location__, 'bear_settings.yaml'))
    for type_setting in bear_settings:
        for bear_names in bear_settings[type_setting]:
            if bear_names in str(bear):
                if setting in bear_settings[type_setting][bear_names]:
                    return (type_setting,
                            bear_settings[type_setting][bear_names][setting])
def get_setting_type(setting, bear, dir=None):
    """
    Retrieves the type of setting according to cEP0022.md
    and seperates the other settings from generation/SettingsClass
    into type2, type3, type4 or unguessable at the moment based on
    the data in bear_settings.yaml.
    :param setting:
        The setting name.
    :param bear:
        The bear class to which the setting belongs.
    :param dir:
        The directory where to look for `bear_settings.yaml`, defaults
        to the `green_mode` directory.
    :return:
        - The type of setting according to bear_settings.yaml.
        - The list of acceptable values in case of type3 setting
          or the value guessed by the QuickstartBear which fits
          the project or the default value in case of unguessable
          settings.
    """
    __location__ = os.path.realpath(
        os.path.join(os.getcwd(), os.path.dirname(__file__))) if (
        dir is None) else dir
    bear_settings = get_yaml_contents(os.path.join(
        __location__, 'bear_settings.yaml'))
    for type_setting in bear_settings:
        for bear_names in bear_settings[type_setting]:
            if bear_names in str(bear):
                if setting in bear_settings[type_setting][bear_names]:
                    return (type_setting,
                            bear_settings[type_setting][bear_names][setting])
 def test_get_yaml_contents(self):
     project_data = 'example_.project_data.yaml'
     full_path = str(Path(__file__).parent / project_data)
     yaml_contents = get_yaml_contents(full_path)
     test_yaml_contents = {
         'dir_structure': ['.coafile',
                           'example_file_1',
                           {'example_folder_1': ['example_file_2',
                                                 {'example_nested_folder_1':
                                                  ['example_file_3']},
                                                 {'example_nested_folder_2':
                                                  ['example_file_4']},
                                                 'example_file_5']},
                           'example_file_6']}
     self.assertEqual(yaml_contents, test_yaml_contents)
Esempio n. 4
0
 def test_get_yaml_contents(self):
     project_data = 'example_.project_data.yaml'
     full_path = str(Path(__file__).parent / project_data)
     yaml_contents = get_yaml_contents(full_path)
     test_yaml_contents = {
         'dir_structure': [
             '.coafile', 'example_file_1', {
                 'example_folder_1': [
                     'example_file_2', {
                         'example_nested_folder_1': ['example_file_3']
                     }, {
                         'example_nested_folder_2': ['example_file_4']
                     }, 'example_file_5'
                 ]
             }, 'example_file_6'
         ]
     }
     self.assertEqual(yaml_contents, test_yaml_contents)
def run_quickstartbear(contents, project_dir):
    """
    Runs the QuickstartBear which pareses the file_dict
    to get the exact value of some settings which can attain
    any infinite amount of values.
    :param contents:
        The python object written to 'PROJECT_DATA' which
        contains the directory structure and values of some
        settings which can attain an infinite set of values.
    :param project_dir:
        The project directory from which to get the files for the
        QuickstartBear to run.
    :return:
        - An updated contents value after guessing values of certain
          settings.
        - Collection of SourceRange objects indicating the parts of
          code to ignore.
        - The complete file dict contains file names as keys and file
          contents as values to those keys.
        - The complete file name list from the project directory and sub
          directories.
    """
    section = Section('green_mode')
    quickstartbear_obj = QuickstartBear(section, None)

    complete_filename_list = generate_complete_filename_list(
        contents['dir_structure'], project_dir)
    complete_file_dict = get_file_dict(complete_filename_list,
                                       allow_raw_files=True)
    ignore_ranges = list(yield_ignore_ranges(complete_file_dict))
    find_max = ['max_lines_per_file', 'max_line_length']
    find_min = ['min_lines_per_file']
    for key in complete_file_dict:
        return_val = quickstartbear_obj.execute(
            filename=key, file=complete_file_dict[key])
        return_val = return_val[0]
        # eg. return_val = {'setting_name': value, ...}
        if return_val is not None:
            for setting in find_max:
                contents = find_max_min_of_setting(
                    setting, return_val[setting], contents,
                    operator.gt)

            for setting in find_min:
                contents = find_max_min_of_setting(
                    setting, return_val[setting], contents,
                    operator.lt)

    bear_settings = get_yaml_contents(str(
        Path(__file__).parent / 'bear_settings.yaml'))['type2']
    full_dict = {}

    for small_dict in bear_settings.values():
        full_dict.update(small_dict)

    resort_to_default_settings = deepcopy(find_max)
    resort_to_default_settings += find_min

    for setting_name in resort_to_default_settings:
        default_val = full_dict[setting_name]
        insert_index = -1
        for index, dict_ in enumerate(contents[settings_key]):
            if setting_name in dict_:
                current_val = dict_[setting_name]
                insert_index = index
                break
        if 'current_val' in locals() and current_val < default_val:
            contents[settings_key][insert_index][setting_name] = default_val

    return (contents, ignore_ranges, complete_file_dict,
            complete_filename_list)
Esempio n. 6
0
def green_mode(project_dir: str, ignore_globs, bears, bear_settings_obj,
               op_args_limit, value_to_op_args_limit, project_files,
               printer=None):
    """
    Runs the green mode of coala-quickstart.

    Generates '.project_data.yaml' which contains the files and directory
    structure of the project, runs the QuickstartBear which guesses some values
    of settings the can take an infinite set of values by parsing the
    file_dict and appends to `.project_data.yaml`. Runs some further linting
    options based on file names etc. Calls the methods which test out whether
    a setting value is green for a bear i.e. does not point out any error in
    the code base and further generates sections and writes the green config
    file for the project.
    :param project_dir:
        The project directory.
    :param ignore_globs:
        The globs of the files to ignore from the linting process.
    :param bears:
        The bears from Constants.GREEN_MODE_COMPATIBLE_BEAR_LIST along
        with Constants.IMPORTANT_BEAR_LIST.
    :param bear_settings_obj:
        The object of SettingsClass/BearSettings which stores the metadata
        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 project_files:
        The list of files in the project.
    :param value_to_op_args_limit:
        The maximum number of values to run the bear again and again for
        a optional setting.
    """
    from coala_quickstart.green_mode.filename_operations import (
        check_filename_prefix_postfix)
    ignore_globs.append(os.path.join(project_dir, '.git', '**'))
    project_data = project_dir + os.sep + PROJECT_DATA

    # Currently as a temporary measure, recreating the file at each run from
    # scratch, as there is no mechanism created uptil now to reuse this data.
    if os.path.isfile(project_data):
        os.remove(project_data)

    if not os.path.isfile(project_data):
        new_data = initialize_project_data(project_dir + os.sep, ignore_globs)
        data_to_dump = {'dir_structure': new_data}
        dump_yaml_to_file(project_data, data_to_dump)

    # Operations before the running of QuickstartBear are done over here.
    # Eg. do operations on filenames over here.
    project_data_contents = get_yaml_contents(project_data)
    project_data_contents = check_filename_prefix_postfix(
        project_data_contents)

    # Run QuickstartBear
    (project_data_contents, ignore_ranges, file_dict,
     file_names) = run_quickstartbear(
        project_data_contents, project_dir)

    final_non_op_results, final_unified_results = bear_test_fun(
        bears, bear_settings_obj, file_dict,
        ignore_ranges, project_data_contents, file_names,
        op_args_limit, value_to_op_args_limit, printer)

    # Call to create `.coafile` goes over here.
    settings_non_op = generate_data_struct_for_sections(
        final_non_op_results)
    settings_unified = generate_data_struct_for_sections(
        final_unified_results)

    # Combine the settings for the sections due to the missed out bears in
    # unified results due to the limitations on maximum number of optionanl
    # arguments and the values to those arguments that can be supplied.
    for bear in settings_non_op:
        if bear not in settings_unified:
            settings_unified[bear] = settings_non_op[bear]

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

    # Final Dump.
    dump_yaml_to_file(project_data, project_data_contents)

    # Delete .project_data.yaml for now as there is currently no mechanism
    # added to reuse this data.
    os.remove(project_data)
def run_quickstartbear(contents, project_dir):
    """
    Runs the QuickstartBear which pareses the file_dict
    to get the exact value of some settings which can attain
    any infinite amount of values.
    :param contents:
        The python object written to 'PROJECT_DATA' which
        contains the directory structure and values of some
        settings which can attain an infinite set of values.
    :param project_dir:
        The project directory from which to get the files for the
        QuickstartBear to run.
    :return:
        - An updated contents value after guessing values of certain
          settings.
        - Collection of SourceRange objects indicating the parts of
          code to ignore.
        - The complete file dict contains file names as keys and file
          contents as values to those keys.
        - The complete file name list from the project directory and sub
          directories.
    """
    section = Section('green_mode')
    quickstartbear_obj = QuickstartBear(section, None)

    complete_filename_list = generate_complete_filename_list(
        contents['dir_structure'], project_dir)
    complete_file_dict = get_file_dict(complete_filename_list,
                                       allow_raw_files=True)
    ignore_ranges = list(yield_ignore_ranges(complete_file_dict))
    find_max = ['max_lines_per_file', 'max_line_length']
    find_min = ['min_lines_per_file']
    for key in complete_file_dict:
        return_val = quickstartbear_obj.execute(
            filename=key, file=complete_file_dict[key])
        return_val = return_val[0]
        # eg. return_val = {'setting_name': value, ...}
        if return_val is not None:
            for setting in find_max:
                contents = find_max_min_of_setting(
                    setting, return_val[setting], contents,
                    operator.gt)

            for setting in find_min:
                contents = find_max_min_of_setting(
                    setting, return_val[setting], contents,
                    operator.lt)

    bear_settings = get_yaml_contents(str(
        Path(__file__).parent / 'bear_settings.yaml'))['type2']
    full_dict = {}

    for small_dict in bear_settings.values():
        full_dict.update(small_dict)

    resort_to_default_settings = deepcopy(find_max)
    resort_to_default_settings += find_min

    for setting_name in resort_to_default_settings:
        default_val = full_dict[setting_name]
        insert_index = -1
        for index, dict_ in enumerate(contents[settings_key]):
            if setting_name in dict_:
                current_val = dict_[setting_name]
                insert_index = index
                break
        if 'current_val' in locals() and current_val < default_val:
            contents[settings_key][insert_index][setting_name] = default_val

    return (contents, ignore_ranges, complete_file_dict,
            complete_filename_list)