def __init__(self, pre_padding: int = 3, log_printer=ConsolePrinter()): """ A ConsoleInteractor uses the Console to interact with the user. :param output: "stdout" or "stderr". :param pre_padding: Number of code lines to show before a result as context. """ Interactor.__init__(self, log_printer=log_printer) ConsolePrinter.__init__(self) self.pre_padding = pre_padding
def create_params_from_section(self, section, log_printer=ConsolePrinter()): """ Create a params dictionary for this function that holds all values the function needs plus optional ones that are available. :param section: The section to retrieve the values from. :return: A dictionary. Unfold it with ** to pass it to the function. """ # Import Section only as needed to avoid circular dependency from coalib.settings.Section import Section if not isinstance(section, Section): raise TypeError("The 'section' parameter should be a " "coalib.settings.Section instance.") params = {} for param in self.non_optional_params: desc, annotation = self.non_optional_params[param] params[param] = self._get_param(param, section, annotation, log_printer) for param in self.optional_params: if param in section: desc, annotation, default = self.optional_params[param] params[param] = self._get_param(param, section, annotation, log_printer) return params
def test_logging(self): uut = Section("test", log_printer=NullPrinter()) uut.append(Setting(key="log_TYPE", value="conSole")) uut.retrieve_logging_objects() self.assertIsInstance(uut.log_printer, ConsolePrinter) self.assertIsInstance(uut.interactor, ConsoleInteractor) uut = Section("test", log_printer=ConsolePrinter()) uut.append(Setting(key="log_TYPE", value="NONE")) uut.retrieve_logging_objects() self.assertIsInstance(uut.log_printer, NullPrinter) uut = Section("test", log_printer=NullPrinter()) uut.append(Setting(key="log_TYPE", value="./invalid path/@#$%^&*()_")) uut.retrieve_logging_objects() # This should throw a warning self.assertIsInstance(uut.log_printer, ConsolePrinter) self.assertEqual(uut.log_printer.log_level, LOG_LEVEL.WARNING) uut.append(Setting(key="LOG_LEVEL", value="DEBUG")) uut.retrieve_logging_objects() # This should throw a warning self.assertEqual(uut.log_printer.log_level, LOG_LEVEL.DEBUG) filename = tempfile.gettempdir( ) + os.path.sep + "testcoalasectiontestfile~" uut = Section("test", log_printer=NullPrinter()) uut.append(Setting(key="log_TYPE", value=filename)) uut.retrieve_logging_objects() self.assertIsInstance(uut.log_printer, FilePrinter) del uut os.remove(filename)
def collect_bears(bear_dirs, bear_names, kinds, log_printer=ConsolePrinter()): """ Collect all bears from bear directories that have a matching kind. :param bear_dirs: directories that can contain bears :param bear_names: names of bears :param kinds: list of bear kinds to be collected :param log_printer: log_printer to handle logging :return: list of matching bear classes """ return list(icollect_bears(bear_dirs, bear_names, kinds, log_printer))
def test_pickling(self): outputfile = os.path.join(tempfile.gettempdir(), "ConsolePrinterPickleTestFile") with open(outputfile, "wb") as f: pickle.dump(ConsolePrinter(), f) with open(outputfile, "rb") as f: obj = pickle.load(f) self.assertIsInstance(obj, ConsolePrinter) obj.print("test") # print will use the output param, this will ensure that the printer is valid os.remove(outputfile)
def __init__(self, bear_kinds, flat_bear_dirs=[], rec_bear_dirs=[StringConstants.coalib_bears_root], bear_names=None, ignored_bears=None, ignored_bear_dirs=None, regex="", log_printer=ConsolePrinter()): """ The BearCollector searches the filesystem for python files containing Bears. It extracts the Bear classes (not instances) out of this files so the caller can instantiate them as he likes. :param bear_kinds: the KINDs of bears to be collected :param flat_bear_dirs: list of strings: directories from which bears should be collected (flat) :param rec_bear_dirs: list of strings: directories from which bears should be collected (recursive) :param bear_names: list of strings: names of bears that should be collected. :param ignored_bears: list of strings: names of bears that should not be collected, even if they match a regex. Default is none. :param ignored_bear_dirs: list of strings: directories from which bears should not be collected. Overrides anything else. :param regex: regex that match bears to be collected. :param log_printer: LogPrinter to handle logging of debug, warning and error messages """ if bear_names is None: bear_names = [] if ignored_bears is None: ignored_bears = [] if ignored_bear_dirs is None: ignored_bear_dirs = [] if not isinstance(bear_kinds, list): raise TypeError("bear_kinds should be of type list") if not isinstance(bear_names, list): raise TypeError("bear_names should be of type list") if not isinstance(ignored_bears, list): raise TypeError("ignored should be of type list") if not isinstance(regex, str): raise TypeError("regex should be of type string") FileCollector.__init__(self, flat_dirs=flat_bear_dirs, rec_dirs=rec_bear_dirs, allowed_types=["py"], ignored_dirs=ignored_bear_dirs, log_printer=log_printer) self._bear_kinds = bear_kinds self._bear_names = bear_names self._ignored_bears = ignored_bears self._regex = self.prepare_regex(regex)
def icollect_bears(bear_dirs, bear_names, kinds, log_printer=ConsolePrinter()): """ Collect all bears from bear directories that have a matching kind. :param bear_dirs: directories that can contain bears :param bear_names: names of bears :param kinds: list of bear kinds to be collected :param log_printer: log_printer to handle logging :return: iterator that yields bear classes """ for bear_dir in icollect(bear_dirs, files=False): for bear_name in bear_names: for matching_file in iglob( os.path.join(bear_dir, bear_name + '.py')): try: for bear in _import_bears(matching_file, kinds): yield bear except: log_printer.warn( _("Unable to collect bears from {file}. " "Probably the file is malformed or " "the module code raises an exception.").format( file=matching_file))
class ConsolePrinterTestCase(unittest.TestCase): def test_printing(self): self.uut = ConsolePrinter() self.uut.print("\ntest", "message", color="green") self.uut.print("\ntest", "message", color="greeeeen") self.uut.print("\ntest", "message") def test_pickling(self): outputfile = os.path.join(tempfile.gettempdir(), "ConsolePrinterPickleTestFile") with open(outputfile, "wb") as f: pickle.dump(ConsolePrinter(), f) with open(outputfile, "rb") as f: obj = pickle.load(f) self.assertIsInstance(obj, ConsolePrinter) obj.print("test") # print will use the output param, this will ensure that the printer is valid os.remove(outputfile)
class ConsolePrinterTest(unittest.TestCase): def test_printing(self): self.uut = ConsolePrinter() self.uut.print("\ntest", "message", color="green") self.uut.print("\ntest", "message", color="greeeeen") self.uut.print("\ntest", "message") def test_pickling(self): outputfile = os.path.join(tempfile.gettempdir(), "ConsolePrinterPickleTestFile") with open(outputfile, "wb") as f: pickle.dump(ConsolePrinter(), f) with open(outputfile, "rb") as f: obj = pickle.load(f) self.assertIsInstance(obj, ConsolePrinter) obj.print("test") # print will use the output param, this will ensure # that the printer is valid os.remove(outputfile)
def test_printing(self): self.uut = ConsolePrinter() self.uut.print("\ntest", "message", color="green") self.uut.print("\ntest", "message", color="greeeeen") self.uut.print("\ntest", "message")
def print_results(log_printer, section, result_list, file_dict, file_diff_dict, color=True, pre_padding=3): """ Print all the results in a section. :param log_printer: Printer responsible for logging the messages. :param section: The section to which the results belong to. :param result_list: List containing the results :param file_dict: A dictionary containing all files with filename as key. :param file_diff_dict: A dictionary that contains filenames as keys and diff objects as values. :param color: Boolean variable to print the results in color or not. :param pre_padding: No of lines of file to print before the result line. Default value is 3. """ if not isinstance(result_list, list): raise TypeError("result_list should be of type list") if not isinstance(file_dict, dict): raise TypeError("file_dict should be of type dict") if not isinstance(file_diff_dict, dict): raise TypeError("file_diff_dict should be of type dict") # We can't use None since we need line 109 be executed if file of first # result is None console_printer = ConsolePrinter(print_colored=color) current_file = False current_line = 0 for result in sorted(result_list): if result.file != current_file: if result.file in file_dict or result.file is None: current_file = result.file current_line = 0 console_printer.print("\n\n{}".format(current_file if current_file is not None else STR_PROJECT_WIDE), color=FILE_NAME_COLOR) else: log_printer.warn(_("A result ({}) cannot be printed " "because it refers to a file that " "doesn't seem to " "exist.").format(str(result))) continue if result.line_nr is not None: if current_file is None: raise AssertionError("A result with a line_nr should also " "have a file.") if result.line_nr < current_line: # pragma: no cover raise AssertionError("The sorting of the results doesn't " "work correctly.") if len(file_dict[result.file]) < result.line_nr - 1: console_printer.print(format_line(line=STR_LINE_DOESNT_EXIST)) else: print_lines(console_printer, pre_padding, file_dict, current_line, result.line_nr, result.file) current_line = result.line_nr print_result(console_printer, log_printer, section, file_diff_dict, result, file_dict)
def __init__(self, log_printer=ConsolePrinter()): SectionCreatable.__init__(self) self.log_printer = log_printer self.file_diff_dict = {} self.current_section = None
def __init__(self, message_queue, log_printer=ConsolePrinter()): threading.Thread.__init__(self) self.running = True self.message_queue = message_queue self.log_printer = log_printer
def __init__(self, files=[], regex="", flat_dirs=[], rec_dirs=[], allowed_types=None, ignored_files=[], ignored_dirs=[], log_printer=ConsolePrinter()): """ :param files: Absolute path to files that will always be collected if accessible :param regex: Regex to match with files to collect :param flat_dirs: list of strings: directories from which files should be collected, excluding sub directories :param rec_dirs: list of strings: directories from which files should be collected, including sub directories :param allowed_types: list of strings: file types that should be collected. The default value of None will result in all file types being collected. :param ignored_files: list of strings: files that should be ignored. :param ignored_dirs: list of strings: directories that should be ignored. :param log_printer: LogPrinter to handle logging """ if not isinstance(log_printer, LogPrinter): raise TypeError("log_printer should be an instance of LogPrinter") if not isinstance(files, list): raise TypeError("files should be of type list") if not isinstance(regex, str): raise TypeError("regex should be of type string") if not isinstance(flat_dirs, list): raise TypeError("flat_dirs should be of type list") if not isinstance(rec_dirs, list): raise TypeError("rec_dirs should be of type list") if not (isinstance(allowed_types, list) or allowed_types is None): raise TypeError("allowed should be of type list or None") if not isinstance(ignored_files, list): raise TypeError("ignored should be of type list") if not isinstance(ignored_dirs, list): raise TypeError("ignored should be of type list") Collector.__init__(self) self.log_printer = log_printer self._regex = self.prepare_regex(regex) self._prelim_files = [os.path.abspath(a_file) for a_file in files] self._prelim_flat_dirs = [ os.path.abspath(f_dir) for f_dir in flat_dirs ] self._prelim_rec_dirs = [os.path.abspath(r_dir) for r_dir in rec_dirs] self.unfolded = False self._files = [] self._flat_dirs = [] self._rec_dirs = [] if allowed_types is not None: self._allowed_types = [ t.lower().lstrip('.') for t in allowed_types ] else: self._allowed_types = None self._ignored_files = [os.path.abspath(path) for path in ignored_files] self._ignored_dirs = [os.path.abspath(path) for path in ignored_dirs]