lookout.core.analyzer

Module Contents

class lookout.core.analyzer.ReferencePointer

Bases:typing.NamedTuple()

We redefine ReferencePointer because Protocol Buffers message objects suck.

static from_pb(refptr:ApiReferencePointer)

Convert from ReferencePointer defined in protocol buffers to our own Python-friendly pointer.

to_pb(self)

Convert to ReferencePointer defined in protocol buffers.

lookout.core.analyzer.UnicodeFile
lookout.core.analyzer.UnicodeChange
class lookout.core.analyzer.AnalyzerModel(log_level:int=logging.INFO, **kwargs)

Bases:modelforge.Model

All models used in Analyzer-s must derive from this base class.

NAME
VENDOR = <unknown>
DESCRIPTION = <unknown>
classmethod generate(cls, analyzer:Type['Analyzer'], ptr:ReferencePointer)

Initialize the model (__init__ does not do the real work to allow load()).

Parameters:
  • analyzer – Bound type of the Analyzer. Not instance!
  • ptr – Git repository state pointer.
Returns:

self

dump(self)

Satisfy the upstream’s abstract method.

Returns:summary text of the model.
save(self, output:Union[str, BinaryIO], series:Optional[str]='Lookout', deps:Iterable=tuple(), create_missing_dirs:bool=True)

Serialize the model to a file.

Parameters:
  • output – Path to the file or a file object.
  • series – Name of the model series.
  • deps – List of the dependencies.
  • create_missing_dirs – create missing directories in output path if the output is a path.
Returns:

self

class lookout.core.analyzer.DummyAnalyzerModel

Bases:lookout.core.analyzer.AnalyzerModel

Stub for stateless analyzers.

NAME = dummy
VENDOR = public domain
DESCRIPTION = Model that does not contain anything is acts as a stub for stateless analyzers.
class lookout.core.analyzer.Analyzer(model:AnalyzerModel, url:str, config:Mapping[str, Any])

Interface of all the analyzers. Each analyzer uses a model to run the analysis and generates a model as the result of the training.

version allows to version the models. It is checked in the model repository and if it does not match, a new model is trained. model_type points to the specific derivative of AnalyzerModel - type of the model used in analyze() and generated in train().

version
model_type
name
vendor
analyze(self, ptr_from:ReferencePointer, ptr_to:ReferencePointer, data_service:'lookout.core.data_requests.DataService', **data)

Run the analysis on the specified Git repository state.

This is called on Review events. It must return the list of Comment-s - found review suggestions.

Parameters:
  • ptr_from – The Git revision of the fork point. Exists in both the original and the forked repositories.
  • ptr_to – The Git revision to analyze. Exists only in the forked repository.
  • data_service – The channel to the data service in Lookout server to query for UASTs, file contents, etc.
  • data – Extra data passed into the method. Used by the decorators to simplify the data retrieval.
Returns:

List of found review suggestions. Refer to lookout/core/server/sdk/service_analyzer.proto.

classmethod train(cls, ptr:ReferencePointer, config:Mapping[str, Any], data_service:'lookout.core.data_requests.DataService', **data)

Generate a new model on top of the specified source code.

Parameters:
  • ptr – Git repository state pointer.
  • config – Configuration of the training of unspecified structure.
  • data_service – The channel to the data service in Lookout server to query for UASTs, file contents, etc.
  • data – Extra data passed into the method. Used by the decorators to simplify the data retrieval.
Returns:

Instance of AnalyzerModel (model_type, to be precise).

classmethod construct_model(cls, ptr:ReferencePointer)

Produce a new empty model associated with this analyzer.

Parameters:ptr – state of Git repository which is used to generate the model.
Returns:Instance of the model.
classmethod check_training_required(cls, old_model:AnalyzerModel, ptr:ReferencePointer, config:Mapping[str, Any], data_service:'lookout.core.data_requests.DataService', **data)

Decide whether we need to train the model or re-use the old one.

Parameters:
  • old_model – Previously trained model. It is never None.
  • ptr – Git repository state pointer.
  • config – Configuration of the training of unspecified structure.
  • data_service – The channel to the data service in Lookout server to query for UASTs, file contents, etc.
  • data – Extra data passed into the method. Used by the decorators to simplify the data retrieval.
Returns:

True (required) or False (not required)