File: snippet.py

package info (click to toggle)
ruby-spamcheck 1.0.0%2Bgit20220819.662e6bf-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 620 kB
  • sloc: python: 828; ruby: 422; makefile: 40
file content (35 lines) | stat: -rw-r--r-- 1,064 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""Process an snippet to determine if it is spam or not."""
import api.v1.spamcheck_pb2 as spam

from app import logger, ValidationError
from app.spammable import Spammable
from server.interceptors import SpamCheckContext


log = logger.logger

# Expecting a module to exist in the directory specified by the ml_classifiers config option.
# i.e {ml_classifiers}/snippet/ml
try:
    from snippet import classifier
except ModuleNotFoundError as exp:
    log.warning("snippet ML classifier not loaded", extra={"error": exp})
    classifier = None  # pylint: disable=invalid-name


def validate(snippet: spam.Snippet) -> None:
    """Validate that snippet contains required fields.

    Raises:
        ValidationError
    """
    if not snippet.title:
        raise ValidationError("Snippet title is required ")


class Snippet(Spammable):
    """Analyze a GitLab snippet to determine if it is spam."""

    def __init__(self, snippet: spam.Snippet, context: SpamCheckContext) -> None:
        validate(snippet)
        super().__init__(snippet, context, classifier)