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:
objectThe operator class for Loki’s linter functionality
It allows to check
Sourcefileobjects for compliance to rules specified as subclasses ofGenericRule.- Parameters:
 reporter (
Reporter) – The reporter instance to be used for problem reporting.rules (list of
GenericRuleor 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
configdict
- check(sourcefile, overwrite_rules=None, overwrite_config=None, **kwargs)
 Check the given
sourcefileand compile aFileReport.The file report is then stored in the
Reportergiven 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_reportthat 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:
TransformationApply
Linteras aTransformationtoSourcefileThe
FileReportis stored in the ``trafo_data` in anItemobject, if it is provided totransform_file(), e.g., during aSchedulertraversal.- Parameters:
 
- traverse_file_graph = True
 
- transform_file(sourcefile, **kwargs)
 Defines the transformation to apply to
Sourcefileitems.For transformations that modify
Sourcefileobjects, 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
Linteraccording toconfigand check the rules inrulesDepending on the given config values, this will use a
Schedulerto 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
basediroption is given as the discovery path to theScheduler. SeeSchedulerConfigfor more details on the available config options.See
JunitXmlHandlerandViolationFileHandlerfor more details on the output file options.The
rulesoption in the config allows selecting only certain rules out of the providedrulesargument.In addition,
configtakes for scheduler the following options:{ 'scheduler': <SchedulerConfig values> }
If the
schedulerkey 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
includeandexcludeoptions are provided tofind_pathsto discover files that should be linted.- Parameters:
 rules (list of
GenericRuleor 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
GenericHandlerto use during linting
- Returns:
 The number of checked files
- Return type: