coalib.output package

Submodules

coalib.output.ConfWriter module

class coalib.output.ConfWriter.ConfWriter(file_name, key_value_delimiters=('=', ), comment_separators=('#', ), key_delimiters=(', ', ' '), section_name_surroundings=mappingproxy({'[': ']'}), section_override_delimiters=('.', ), unsavable_keys=('save', ), key_value_append_delimiters=('+=', ))[source]

Bases: pyprint.ClosableObject.ClosableObject

static is_comment(key)[source]
write_section(section)[source]
write_sections(sections)[source]

coalib.output.ConsoleInteraction module

class coalib.output.ConsoleInteraction.BackgroundMessageStyle[source]

Bases: pygments.style.Style

styles = {Token.Name.Function.Magic: '', Token.Literal.String.Char: '', Token.Error: '', Token.Text.Whitespace: '', Token.Literal.String.Symbol: '', Token.Literal.String.Other: '', Token.Name.Exception: '', Token.Operator: '', Token.Name.Variable.Global: '', Token.Name.Attribute: '', Token.Name.Variable.Class: '', Token.Literal: '', Token.Name.Label: '', Token.Generic.Subheading: '', Token.Generic.Heading: '', Token.Comment.Preproc: '', Token.Literal.String.Escape: '', Token.Literal.String.Regex: '', Token.Name.Variable.Magic: '', Token.Text: '', Token.Name.Variable: '', Token.Name: '', Token.Comment.Hashbang: '', Token.Literal.Number.Integer.Long: '', Token.Keyword.Constant: '', Token.Literal.String.Interpol: '', Token.Name.Constant: '', Token.Literal.Number: '', Token.Other: '', Token.Literal.String.Backtick: '', Token.Punctuation: '', Token.Comment.Special: '', Token.Name.Class: '', Token.Literal.String: '', Token.Generic.Emph: '', Token.Keyword.Declaration: '', Token.Keyword.Reserved: '', Token.Keyword.Namespace: '', Token.Operator.Word: '', Token.Comment.Single: '', Token.Literal.String.Delimiter: '', Token.Keyword.Type: '', Token.Name.Builtin: '', Token.Literal.String.Doc: '', Token.Generic.Strong: '', Token.Literal.Number.Bin: '', Token.Literal.String.Heredoc: '', Token.Name.Builtin.Pseudo: '', Token.Generic.Output: '', Token.Literal.Number.Hex: '', Token.Literal.Number.Float: '', Token.Literal.Number.Oct: '', Token.Literal.Date: '', Token.Comment.Multiline: '', Token.Generic.Traceback: '', Token.Escape: '', Token.Generic: '', Token.Keyword: '', Token.Keyword.Pseudo: '', Token.Generic.Error: '', Token.Literal.String.Double: '', Token.Name.Variable.Instance: '', Token.Name.Tag: '', Token.Literal.Number.Integer: '', Token.Name.Property: '', Token.Name.Entity: '', Token.Comment.PreprocFile: '', Token.Name.Function: '', Token: 'bold bg:#eee #111', Token.Generic.Deleted: '', Token.Name.Other: '', Token.Name.Decorator: '', Token.Name.Namespace: '', Token.Generic.Inserted: '', Token.Generic.Prompt: '', Token.Literal.String.Single: '', Token.Literal.String.Affix: '', Token.Comment: ''}
class coalib.output.ConsoleInteraction.BackgroundSourceRangeStyle[source]

Bases: pygments.style.Style

styles = {Token.Name.Function.Magic: '', Token.Literal.String.Char: '', Token.Error: '', Token.Text.Whitespace: '', Token.Literal.String.Symbol: '', Token.Literal.String.Other: '', Token.Name.Exception: '', Token.Operator: '', Token.Name.Variable.Global: '', Token.Name.Attribute: '', Token.Name.Variable.Class: '', Token.Literal: '', Token.Name.Label: '', Token.Generic.Subheading: '', Token.Generic.Heading: '', Token.Comment.Preproc: '', Token.Literal.String.Escape: '', Token.Literal.String.Regex: '', Token.Name.Variable.Magic: '', Token.Text: '', Token.Name.Variable: '', Token.Name: '', Token.Comment.Hashbang: '', Token.Literal.Number.Integer.Long: '', Token.Keyword.Constant: '', Token.Literal.String.Interpol: '', Token.Name.Constant: '', Token.Literal.Number: '', Token.Other: '', Token.Literal.String.Backtick: '', Token.Punctuation: '', Token.Comment.Special: '', Token.Name.Class: '', Token.Literal.String: '', Token.Generic.Emph: '', Token.Keyword.Declaration: '', Token.Keyword.Reserved: '', Token.Keyword.Namespace: '', Token.Operator.Word: '', Token.Comment.Single: '', Token.Literal.String.Delimiter: '', Token.Keyword.Type: '', Token.Name.Builtin: '', Token.Literal.String.Doc: '', Token.Generic.Strong: '', Token.Literal.Number.Bin: '', Token.Literal.String.Heredoc: '', Token.Name.Builtin.Pseudo: '', Token.Generic.Output: '', Token.Literal.Number.Hex: '', Token.Literal.Number.Float: '', Token.Literal.Number.Oct: '', Token.Literal.Date: '', Token.Comment.Multiline: '', Token.Generic.Traceback: '', Token.Escape: '', Token.Generic: '', Token.Keyword: '', Token.Keyword.Pseudo: '', Token.Generic.Error: '', Token.Literal.String.Double: '', Token.Name.Variable.Instance: '', Token.Name.Tag: '', Token.Literal.Number.Integer: '', Token.Name.Property: '', Token.Name.Entity: '', Token.Comment.PreprocFile: '', Token.Name.Function: '', Token: 'bold bg:#BB4D3E #111', Token.Generic.Deleted: '', Token.Name.Other: '', Token.Name.Decorator: '', Token.Name.Namespace: '', Token.Generic.Inserted: '', Token.Generic.Prompt: '', Token.Literal.String.Single: '', Token.Literal.String.Affix: '', Token.Comment: ''}
class coalib.output.ConsoleInteraction.NoColorStyle[source]

Bases: pygments.style.Style

styles = {Token.Name.Function.Magic: '', Token.Literal.String.Char: '', Token.Error: '', Token.Text.Whitespace: '', Token.Literal.String.Symbol: '', Token.Literal.String.Other: '', Token.Name.Exception: '', Token.Operator: '', Token.Name.Variable.Global: '', Token.Name.Attribute: '', Token.Name.Variable.Class: '', Token.Literal: '', Token.Name.Label: '', Token.Generic.Subheading: '', Token.Generic.Heading: '', Token.Comment.Preproc: '', Token.Literal.String.Escape: '', Token.Literal.String.Regex: '', Token.Name.Variable.Magic: '', Token.Text: '', Token.Name.Variable: '', Token.Name: '', Token.Comment.Hashbang: '', Token.Literal.Number.Integer.Long: '', Token.Keyword.Constant: '', Token.Literal.String.Interpol: '', Token.Name.Constant: '', Token.Literal.Number: '', Token.Other: '', Token.Literal.String.Backtick: '', Token.Punctuation: '', Token.Comment.Special: '', Token.Name.Class: '', Token.Literal.String: '', Token.Generic.Emph: '', Token.Keyword.Declaration: '', Token.Keyword.Reserved: '', Token.Keyword.Namespace: '', Token.Operator.Word: '', Token.Comment.Single: '', Token.Literal.String.Delimiter: '', Token.Keyword.Type: '', Token.Name.Builtin: '', Token.Literal.String.Doc: '', Token.Generic.Strong: '', Token.Literal.Number.Bin: '', Token.Literal.String.Heredoc: '', Token.Name.Builtin.Pseudo: '', Token.Generic.Output: '', Token.Literal.Number.Hex: '', Token.Literal.Number.Float: '', Token.Literal.Number.Oct: '', Token.Literal.Date: '', Token.Comment.Multiline: '', Token.Generic.Traceback: '', Token.Escape: '', Token.Generic: '', Token.Keyword: '', Token.Keyword.Pseudo: '', Token.Generic.Error: '', Token.Literal.String.Double: '', Token.Name.Variable.Instance: '', Token.Name.Tag: '', Token.Literal.Number.Integer: '', Token.Name.Property: '', Token.Name.Entity: '', Token.Comment.PreprocFile: '', Token.Name.Function: '', Token: 'noinherit', Token.Generic.Deleted: '', Token.Name.Other: '', Token.Name.Decorator: '', Token.Name.Namespace: '', Token.Generic.Inserted: '', Token.Generic.Prompt: '', Token.Literal.String.Single: '', Token.Literal.String.Affix: '', Token.Comment: ''}
coalib.output.ConsoleInteraction.acquire_actions_and_apply(console_printer, section, file_diff_dict, result, file_dict, cli_actions=None, apply_single=False)[source]

Acquires applicable actions and applies them.

Parameters:
  • console_printer – Object to print messages on the console.
  • section – Name of section to which the result belongs.
  • file_diff_dict – Dictionary containing filenames as keys and Diff objects as values.
  • result – A derivative of Result.
  • file_dict – A dictionary containing all files with filename as key.
  • apply_single – The action that should be applied for all results. If it’s not selected, has a value of False.
  • cli_actions – The list of cli actions available.
coalib.output.ConsoleInteraction.acquire_settings(log_printer, settings_names_dict, section)[source]

This method prompts the user for the given settings.

Parameters:
  • log_printer – Printer responsible for logging the messages. This is needed to comply with the interface.
  • settings_names_dict

    A dictionary with the settings name as key and a list containing a description in [0] and the name of the bears who need this setting in [1] and following.

    Example:
{"UseTabs": ["describes whether tabs should be used instead of spaces",
             "SpaceConsistencyBear",
             "SomeOtherBear"]}
Parameters:section – The section the action corresponds to.
Returns:A dictionary with the settings name as key and the given value as value.
coalib.output.ConsoleInteraction.ask_for_action_and_apply(console_printer, section, metadata_list, action_dict, failed_actions, result, file_diff_dict, file_dict, applied_actions, apply_single=False)[source]

Asks the user for an action and applies it.

Parameters:
  • console_printer – Object to print messages on the console.
  • section – Currently active section.
  • metadata_list – Contains metadata for all the actions.
  • action_dict – Contains the action names as keys and their references as values.
  • failed_actions – A set of all actions that have failed. A failed action remains in the list until it is successfully executed.
  • result – Result corresponding to the actions.
  • file_diff_dict – If it is an action which applies a patch, this contains the diff of the patch to be applied to the file with filename as keys.
  • file_dict – Dictionary with filename as keys and its contents as values.
  • apply_single – The action that should be applied for all results. If it’s not selected, has a value of False.
  • applied_actions – A dictionary that contains the result, file_dict, file_diff_dict and the section for an action.
Returns:

Returns a boolean value. True will be returned, if it makes sense that the user may choose to execute another action, False otherwise. If apply_single ist set, always return False.

coalib.output.ConsoleInteraction.choose_action(console_printer, actions, apply_single=False)[source]

Presents the actions available to the user and takes as input the action the user wants to choose.

Parameters:
  • console_printer – Object to print messages on the console.
  • actions – Actions available to the user.
  • apply_single – The action that should be applied for all results. If it’s not selected, has a value of False.
Returns:

Return a tuple of lists, a list with the names of actions that needs to be applied and a list with with the description of the actions.

coalib.output.ConsoleInteraction.color_letter(console_printer, line)[source]
coalib.output.ConsoleInteraction.format_lines(lines, symbol='', line_nr='')[source]
coalib.output.ConsoleInteraction.get_action_info(section, action, failed_actions)[source]

Gets all the required Settings for an action. It updates the section with the Settings.

Parameters:
  • section – The section the action corresponds to.
  • action – The action to get the info for.
  • failed_actions – A set of all actions that have failed. A failed action remains in the list until it is successfully executed.
Returns:

Action name and the updated section.

coalib.output.ConsoleInteraction.highlight_text(no_color, text, style, lexer=<pygments.lexers.TextLexer>)[source]
coalib.output.ConsoleInteraction.nothing_done(log_printer=None)[source]

Will be called after processing a coafile when nothing had to be done, i.e. no section was enabled/targeted.

Parameters:log_printer – A LogPrinter object.
coalib.output.ConsoleInteraction.print_affected_files(console_printer, log_printer, result, file_dict)[source]

Prints all the affected files and affected lines within them.

Parameters:
  • console_printer – Object to print messages on the console.
  • log_printer – Printer responsible for logging the messages.
  • result – The result to print the context for.
  • file_dict – A dictionary containing all files with filename as key.
coalib.output.ConsoleInteraction.print_affected_lines(console_printer, file_dict, sourcerange)[source]

Prints the lines affected by the bears.

Parameters:
  • console_printer – Object to print messages on the console.
  • file_dict – A dictionary containing all files with filename as key.
  • sourcerange – The SourceRange object referring to the related lines to print.
coalib.output.ConsoleInteraction.print_bears(bears, show_description, show_params, console_printer, args=None)[source]

Presents all bears being used in a stylized manner.

Parameters:
  • bears – It’s a dictionary with bears as keys and list of sections containing those bears as values.
  • show_description – True if the main description of the bears should be shown.
  • show_params – True if the parameters and their description should be shown.
  • console_printer – Object to print messages on the console.
  • args – Args passed to coala command.
coalib.output.ConsoleInteraction.print_bears_formatted(bears, format=None)[source]
coalib.output.ConsoleInteraction.print_diffs_info(diffs, printer)[source]

Prints diffs information (number of additions and deletions) to the console.

Parameters:
  • diffs – List of Diff objects containing corresponding diff info.
  • printer – Object responsible for printing diffs on console.
coalib.output.ConsoleInteraction.print_lines(console_printer, file_dict, sourcerange)[source]

Prints the lines between the current and the result line. If needed they will be shortened.

Parameters:
  • console_printer – Object to print messages on the console.
  • file_dict – A dictionary containing all files as values with filenames as key.
  • sourcerange – The SourceRange object referring to the related lines to print.
coalib.output.ConsoleInteraction.print_result(console_printer, section, file_diff_dict, result, file_dict, interactive=True, apply_single=False)[source]

Prints the result to console.

Parameters:
  • console_printer – Object to print messages on the console.
  • section – Name of section to which the result belongs.
  • file_diff_dict – Dictionary containing filenames as keys and Diff objects as values.
  • result – A derivative of Result.
  • file_dict – A dictionary containing all files with filename as key.
  • apply_single – The action that should be applied for all results. If it’s not selected, has a value of False.
  • interactive – Variable to check whether or not to offer the user actions interactively.
coalib.output.ConsoleInteraction.print_results(log_printer, section, result_list, file_dict, file_diff_dict, console_printer, apply_single=False)[source]

Prints all the results in a section.

Parameters:
  • log_printer – Printer responsible for logging the messages.
  • section – The section to which the results belong to.
  • result_list – List containing the results
  • file_dict – A dictionary containing all files with filename as key.
  • file_diff_dict – A dictionary that contains filenames as keys and diff objects as values.
  • apply_single – The action that should be applied for all results. If it’s not selected, has a value of False.
  • console_printer – Object to print messages on the console.
coalib.output.ConsoleInteraction.print_results_formatted(log_printer, section, result_list, file_dict, *args)[source]

Prints results through the format string from the format setting done by user.

Parameters:
  • log_printer – Printer responsible for logging the messages.
  • section – The section to which the results belong.
  • result_list – List of Result objects containing the corresponding results.
coalib.output.ConsoleInteraction.print_results_no_input(log_printer, section, result_list, file_dict, file_diff_dict, console_printer, apply_single=False)[source]

Prints all non interactive results in a section

Parameters:
  • log_printer – Printer responsible for logging the messages.
  • section – The section to which the results belong to.
  • result_list – List containing the results
  • file_dict – A dictionary containing all files with filename as key.
  • file_diff_dict – A dictionary that contains filenames as keys and diff objects as values.
  • apply_single – The action that should be applied for all results. If it’s not selected, has a value of False.
  • console_printer – Object to print messages on the console.
coalib.output.ConsoleInteraction.print_section_beginning(console_printer, section)[source]

Will be called after initialization current_section in begin_section()

Parameters:
  • console_printer – Object to print messages on the console.
  • section – The section that will get executed now.
coalib.output.ConsoleInteraction.require_setting(setting_name, arr, section)[source]

This method is responsible for prompting a user about a missing setting and taking its value as input from the user.

Parameters:
  • setting_name – Name of the setting missing
  • arr – A list containing a description in [0] and the name of the bears who need this setting in [1] and following.
  • section – The section the action corresponds to.
coalib.output.ConsoleInteraction.show_bear(bear, show_description, show_params, console_printer)[source]

Displays all information about a bear.

Parameters:
  • bear – The bear to be displayed.
  • show_description – True if the main description should be shown.
  • show_params – True if the details should be shown.
  • console_printer – Object to print messages on the console.
coalib.output.ConsoleInteraction.show_bears(local_bears, global_bears, show_description, show_params, console_printer, args=None)[source]

Extracts all the bears from each enabled section or the sections in the targets and passes a dictionary to the show_bears_callback method.

Parameters:
  • local_bears – Dictionary of local bears with section names as keys and bear list as values.
  • global_bears – Dictionary of global bears with section names as keys and bear list as values.
  • show_description – True if the main description of the bears should be shown.
  • show_params – True if the parameters and their description should be shown.
  • console_printer – Object to print messages on the console.
  • args – Args passed to coala command.
coalib.output.ConsoleInteraction.show_enumeration(console_printer, title, items, indentation, no_items_text)[source]

This function takes as input an iterable object (preferably a list or a dict) and prints it in a stylized format. If the iterable object is empty, it prints a specific statement given by the user. An e.g :

<indentation>Title: <indentation> * Item 1 <indentation> * Item 2

Parameters:
  • console_printer – Object to print messages on the console.
  • title – Title of the text to be printed
  • items – The iterable object.
  • indentation – Number of spaces to indent every line by.
  • no_items_text – Text printed when iterable object is empty.
coalib.output.ConsoleInteraction.show_language_bears_capabilities(language_bears_capabilities, console_printer)[source]

Displays what the bears can detect and fix.

Parameters:
  • language_bears_capabilities – Dictionary with languages as keys and their bears’ capabilities as values. The capabilities are stored in a tuple of two elements where the first one represents what the bears can detect, and the second one what they can fix.
  • console_printer – Object to print messages on the console.
coalib.output.ConsoleInteraction.try_to_apply_action(action_name, chosen_action, console_printer, section, metadata_list, action_dict, failed_actions, result, file_diff_dict, file_dict, applied_actions)[source]

Try to apply the given action.

Parameters:
  • action_name – The name of the action.
  • choose_action – The action object that will be applied.
  • console_printer – Object to print messages on the console.
  • section – Currently active section.
  • metadata_list – Contains metadata for all the actions.
  • action_dict – Contains the action names as keys and their references as values.
  • failed_actions – A set of all actions that have failed. A failed action remains in the list until it is successfully executed.
  • result – Result corresponding to the actions.
  • file_diff_dict – If it is an action which applies a patch, this contains the diff of the patch to be applied to the file with filename as keys.
  • applied_actions – A dictionary that contains the result, file_dict, file_diff_dict and the section for an action.
  • file_dict – Dictionary with filename as keys and its contents as values.

coalib.output.Interactions module

coalib.output.Interactions.fail_acquire_settings(log_printer, settings_names_dict, section=None)[source]

This method throws an exception if any setting needs to be acquired.

Parameters:
  • log_printer – Printer responsible for logging the messages.
  • settings_names_dict – A dictionary with the settings name as key and a list containing a description in [0] and the name of the bears who need this setting in [1] and following.
  • section – This parameter is deprecated and is no longer required.
Raises:
  • AssertionError – If any setting is required.
  • TypeError – If settings_names_dict is not a dictionary.

coalib.output.JSONEncoder module

coalib.output.JSONEncoder.create_json_encoder(**kwargs)[source]

coalib.output.Logging module

class coalib.output.Logging.CounterHandler(level=0)[source]

Bases: logging.Handler

A logging handler which counts the number of calls for each logging level.

classmethod emit(record)[source]
classmethod get_num_calls_for_level(level)[source]

Returns the number of calls registered for a given log level.

classmethod reset()[source]

Reset the counter to 0 for all levels

class coalib.output.Logging.JSONFormatter(fmt=None, datefmt=None, style='%')[source]

Bases: logging.Formatter

JSON formatter for python logging.

static format(record)[source]
coalib.output.Logging.configure_json_logging()[source]

Configures logging for JSON. :return: Returns a StringIO that captures the logs as JSON.

coalib.output.Logging.configure_logging(color=True)[source]

Configures the logging with hard coded dictionary.

Module contents