Esempio n. 1
0
def fill_settings(sections,
                  acquire_settings,
                  log_printer=None,
                  fill_section_method=fill_section,
                  **kwargs):
    """
    Retrieves all bears and requests missing settings via the given
    acquire_settings method.

    This will retrieve all bears and their dependencies.

    :param sections:            The sections to fill up, modified in place.
    :param acquire_settings:    The method to use for requesting settings. It
                                will get a parameter which is a dictionary with
                                the settings name as key and a list containing
                                a description in [0] and the names of the bears
                                who need this setting in all following indexes.
    :param log_printer:         The log printer to use for logging.
    :param fill_section_method: Method to be used to fill the section settings.
    :param kwargs:              Any other arguments for the fill_section_method
                                can be supplied via kwargs, which are passed
                                directly to the fill_section_method.
    :return:                    A tuple containing (local_bears, global_bears),
                                each of them being a dictionary with the
                                section name as key and as value the bears as a
                                list.
    """
    local_bears = {}
    global_bears = {}

    for section_name, section in sections.items():
        bear_dirs = section.bear_dirs()
        if getattr(section, 'aspects', None):
            section_local_bears, section_global_bears = (
                collect_bears_by_aspects(
                    section.aspects,
                    [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL]))
        else:
            bears = list(section.get('bears', ''))
            section_local_bears, section_global_bears = collect_bears(
                bear_dirs,
                bears,
                [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL])
        section_local_bears = Dependencies.resolve(section_local_bears)
        section_global_bears = Dependencies.resolve(section_global_bears)
        all_bears = copy.deepcopy(section_local_bears)
        all_bears.extend(section_global_bears)
        fill_section_method(section,
                            acquire_settings,
                            None,
                            all_bears,
                            **kwargs)

        local_bears[section_name] = section_local_bears
        global_bears[section_name] = section_global_bears

    return local_bears, global_bears
Esempio n. 2
0
def fill_settings(sections,
                  acquire_settings,
                  log_printer=None,
                  fill_section_method=fill_section,
                  **kwargs):
    """
    Retrieves all bears and requests missing settings via the given
    acquire_settings method.

    This will retrieve all bears and their dependencies.

    :param sections:            The sections to fill up, modified in place.
    :param acquire_settings:    The method to use for requesting settings. It
                                will get a parameter which is a dictionary with
                                the settings name as key and a list containing
                                a description in [0] and the names of the bears
                                who need this setting in all following indexes.
    :param log_printer:         The log printer to use for logging.
    :param fill_section_method: Method to be used to fill the section settings.
    :param kwargs:              Any other arguments for the fill_section_method
                                can be supplied via kwargs, which are passed
                                directly to the fill_section_method.
    :return:                    A tuple containing (local_bears, global_bears),
                                each of them being a dictionary with the
                                section name as key and as value the bears as a
                                list.
    """
    local_bears = {}
    global_bears = {}

    for section_name, section in sections.items():
        bear_dirs = section.bear_dirs()
        if getattr(section, 'aspects', None):
            section_local_bears, section_global_bears = (
                collect_bears_by_aspects(
                    section.aspects,
                    [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL]))
        else:
            bears = list(section.get('bears', ''))
            section_local_bears, section_global_bears = collect_bears(
                bear_dirs,
                bears,
                [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL])
        section_local_bears = Dependencies.resolve(section_local_bears)
        section_global_bears = Dependencies.resolve(section_global_bears)
        all_bears = copy.deepcopy(section_local_bears)
        all_bears.extend(section_global_bears)
        fill_section_method(section,
                            acquire_settings,
                            None,
                            all_bears,
                            **kwargs)

        local_bears[section_name] = section_local_bears
        global_bears[section_name] = section_global_bears

    return local_bears, global_bears
Esempio n. 3
0
    def test_aspect_bear(self):
        with bear_test_module():
            aspects = AspectList([get_aspect('unusedvariable')('py')])
            local_bears, global_bears = collect_bears_by_aspects(
                aspects, [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL])

        self.assertEqual(len(global_bears), 0)
        self.assertEqual(len(local_bears), 1)
        self.assertIs(local_bears[0].name, 'AspectTestBear')
Esempio n. 4
0
    def test_aspect_bear(self):
        with bear_test_module():
            aspects = AspectList([
                get_aspect('unusedglobalvariable')('py'),
                get_aspect('unusedlocalvariable')('py'),
            ])
            local_bears, global_bears = collect_bears_by_aspects(
                aspects,
                [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL])

        self.assertEqual(len(global_bears), 0)
        self.assertEqual(len(local_bears), 1)
        self.assertIs(local_bears[0].name, 'AspectTestBear')
Esempio n. 5
0
    def test_collect_bears_unfulfilled_aspect(self):
        aspects = AspectList([
            get_aspect('unusedvariable')('py'),
        ])

        logger = logging.getLogger()
        with bear_test_module(), self.assertLogs(logger, 'WARNING') as log:
            local_bears, global_bears = collect_bears_by_aspects(
                aspects, [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL])
        self.assertRegex(
            log.output[0], 'coala cannot find bear that could analyze the '
            r'following aspects: \['
            r"'Root\.Redundancy\.UnusedVariable\.UnusedParameter'"
            r'\]')

        self.assertEqual(global_bears, [])
        self.assertEqual(str(local_bears),
                         "[<class 'AspectTestBear.AspectTestBear'>]")
Esempio n. 6
0
    def test_collect_bears_unfulfilled_aspect(self):
        aspects = AspectList([
            get_aspect('unusedvariable')('py'),
        ])

        logger = logging.getLogger()
        with bear_test_module(), self.assertLogs(logger, 'WARNING') as log:
            local_bears, global_bears = collect_bears_by_aspects(
                aspects,
                [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL])
        self.assertRegex(log.output[0],
                         'coala cannot find bear that could analyze the '
                         'following aspects: \['
                         "'Root\.Redundancy\.UnusedVariable\.UnusedParameter'"
                         '\]')

        self.assertEqual(global_bears, [])
        self.assertEqual(str(local_bears),
                         "[<class 'AspectTestBear.AspectTestBear'>]")