loki.lint.linter
Linter
operator class definition to drive rule checking for
Sourcefile
objects
Functions
|
Check the file at |
|
Construct a |
|
Discover files relative to |
|
Discover files relative to |
Classes
|
The operator class for Loki's linter functionality |
|
Apply |
- class Linter(reporter, rules, config=None)
Bases:
object
The operator class for Loki’s linter functionality
It allows to check
Sourcefile
objects for compliance to rules specified as subclasses ofGenericRule
.- Parameters:
reporter (
Reporter
) – The reporter instance to be used for problem reporting.rules (list of
GenericRule
or a Python module) – List of rules to check files against or a module that contains the rules.config (dict, optional) – Configuration (e.g., from config file) to change behaviour of rules.
- static lookup_rules(rules_module, rule_names=None)
Obtain all available rule classes in a module
- static default_config(rules)
Return default configuration for a list of rules
- update_config(config)
Update the stored configuration using the given
config
dict
- check(sourcefile, overwrite_rules=None, overwrite_config=None, **kwargs)
Check the given
sourcefile
and compile aFileReport
.The file report is then stored in the
Reporter
given while creating theLinter
. Additionally, the file report is returned, e.g., to use it wihtfix()
.- Parameters:
sourcefile (
Sourcefile
) – The source file to check.overwrite_rules (list of rules, optional) – List of rules to check. This overwrites the stored list of rules.
overwrite_config (dict, optional) – Configuration that is used to update the stored configuration.
- Returns:
The report for this file containing any discovered violations.
- Return type:
- fix(sourcefile, file_report, backup_suffix=None, overwrite_config=None)
Fix all rule violations in
file_report
that were reported by fixable rules and write them into the original file- Parameters:
sourcefile (
Sourcefile
) – The source file to fix.file_report (
FileReport
) – The report created bycheck()
for that file.backup_suffix (str, optional) – Create a copy of the original file using this file name suffix.
overwrite_config (dict, optional) – Configuration that is used to update the stored configuration.
- class LinterTransformation(linter, key=None, **kwargs)
Bases:
Transformation
Apply
Linter
as aTransformation
toSourcefile
The
FileReport
is stored in the ``trafo_data` in anItem
object, if it is provided totransform_file()
, e.g., during aScheduler
traversal.- Parameters:
- traverse_file_graph = True
- transform_file(sourcefile, **kwargs)
Defines the transformation to apply to
Sourcefile
items.For transformations that modify
Sourcefile
objects, this method should be implemented. It gets called via the dispatch methodapply()
.- Parameters:
sourcefile (
Sourcefile
) – The sourcefile to be transformed.**kwargs (optional) – Keyword arguments for the transformation.
- lint_files(rules, config, handlers=None)
Construct a
Linter
according toconfig
and check the rules inrules
Depending on the given config values, this will use a
Scheduler
to discover files and drive the linting, or apply glob-based file discovery and apply linting to each of them.Common config options include:
{ 'basedir': <some file path>, 'max_workers': <n>, # Optional: use multiple workers 'fix': <True|False>, # Optional: attempt automatic fixing of rule violations 'backup_suffix': <suffix>, # Optional: Backup original file with given suffix 'junitxml_file': <some file path>, # Optional: write JunitXML-output of lint results 'violations_file': <some file path>, # Optional: write a YAML file containing violations 'rules': ['SomeRule', 'AnotherRule', ...], # Optional: select only these rules 'SomeRule': <rule options>, # Optional: configuration values for individual rules }
The
basedir
option is given as the discovery path to theScheduler
. SeeSchedulerConfig
for more details on the available config options.See
JunitXmlHandler
andViolationFileHandler
for more details on the output file options.The
rules
option in the config allows selecting only certain rules out of the providedrules
argument.In addition,
config
takes for scheduler the following options:{ 'scheduler': <SchedulerConfig values> }
If the
scheduler
key is found inconfig
, the scheduler-based linting is automatically enabled.For glob-based file discovery, the config takes the following options:
{ 'include': [<some pattern>, <another pattern>, ...] 'exclude': [<some pattern>] # Optional }
The
include
andexclude
options are provided tofind_paths
to discover files that should be linted.- Parameters:
rules (list of
GenericRule
or a Python module) – List of rules to check files against or a module that contains the rules.config (dict) – Configuration for file discovery/scheduler and linting rules
handlers (list, optional) – Additional instances of
GenericHandler
to use during linting
- Returns:
The number of checked files
- Return type: