coalib.bears package

Submodules

coalib.bears.BEAR_KIND module

coalib.bears.Bear module

class coalib.bears.Bear.Bear(*args, **kwargs)

Bases: pyprint.Printer.Printer, coalib.output.printers.LogPrinter.LogPrinter

A bear contains the actual subroutine that is responsible for checking source code for certain specifications. However it can actually do whatever it wants with the files it gets. If you are missing some Result type, feel free to contact us and/or help us extending the coalib.

This is the base class for every bear. If you want to write an bear, you will probably want to look at the GlobalBear and LocalBear classes that inherit from this class. In any case you’ll want to overwrite at least the run method. You can send debug/warning/error messages through the debug(), warn(), err() functions. These will send the appropriate messages so that they are outputted. Be aware that if you use err(), you are expected to also terminate the bear run-through immediately.

If you need some setup or teardown for your bear, feel free to overwrite the set_up() and tear_down() functions. They will be invoked before/after every run invocation.

Settings are available at all times through self.section.

classmethod check_prerequisites()

Checks whether needed runtime prerequisites of the bear are satisfied.

This function gets executed at construction and returns True by default.

Section value requirements shall be checked inside the run method.

Returns:True if prerequisites are satisfied, else False or a string that serves a more detailed description of what’s missing.
execute(*args, **kwargs)
get_config_dir()

Gives the directory where the configuration file is

Returns:Directory of the config file
static get_dependencies()

Retrieves bear classes that are to be executed before this bear gets executed. The results of these bears will then be passed to the run method as a dict via the dependency_results argument. The dict will have the name of the Bear as key and the list of its results as results.

Returns:A list of bear classes.
classmethod get_metadata()
Returns:Metadata for the run function. However parameters like self or parameters implicitly used by coala (e.g. filename for local bears) are already removed.
classmethod get_non_optional_settings()

This method has to determine which settings are needed by this bear. The user will be prompted for needed settings that are not available in the settings file so don’t include settings where a default value would do.

Returns:A dictionary of needed settings as keys and a tuple of help text and annotation as values
static kind()
Returns:The kind of the bear
log_message(log_message, timestamp=None, **kwargs)
classmethod missing_dependencies(lst)

Checks if the given list contains all dependencies.

Parameters:lst – A list of all already resolved bear classes (not instances).
Returns:A list of missing dependencies.
run(*args, dependency_results=None, **kwargs)
run_bear_from_section(args, kwargs)

coalib.bears.GlobalBear module

class coalib.bears.GlobalBear.GlobalBear(file_dict, section, message_queue, timeout=0)

Bases: coalib.bears.Bear.Bear

A GlobalBear is able to analyze semantic facts across several file.

The results of a GlobalBear will be presented grouped by the origin Bear. Therefore Results spanning above multiple files are allowed and will be handled right.

If you only look at one file at once anyway a LocalBear is better for your needs. (And better for performance and usability for both user and developer.)

static kind()
run(*args, dependency_results=None, **kwargs)

Handles all files in file_dict.

Returns:A list of Result type.

coalib.bears.LocalBear module

class coalib.bears.LocalBear.LocalBear(*args, **kwargs)

Bases: coalib.bears.Bear.Bear

A LocalBear is a Bear that analyzes only one file at once. It therefore can not analyze semantical facts over multiple files.

This has the advantage that it can be highly parrallelized. In addition, the results from multiple bears for one file can be shown together for that file, which is better to grasp for the user. coala takes care of all that.

Examples for LocalBear’s could be:

  • A SpaceConsistencyBear that checks every line for trailing whitespaces, tabs, etc.
  • A VariableNameBear that checks variable names and constant names for certain conditions
classmethod get_metadata()
static kind()
run(filename, file, *args, dependency_results=None, **kwargs)

Handles the given file.

Parameters:
  • filename – The filename of the file
  • file – The file contents as string array
Returns:

A list of Result

Module contents