<commit_before>from common import PluginType
from bs4 import BeautifulSoup
from yapsy.IPlugin import IPlugin
import logging


class MetaTagValidator(IPlugin):
    
    category = PluginType.CHECKER
    id = "seometa"
    
    
    def __init__(self):
        self.__journal = None

    def setJournal(self, journal):
        self.__journal = journal

    def check(self, transaction):
        """Pusti validator, ulozi chyby.
        """
        if 'soup' in transaction.cache and transaction.cache['soup']:
            soup = transaction.cache['soup']
        else:
            soup = BeautifulSoup(transaction.getContent(), 'lxml')
            transaction.cache['soup'] = soup

        for html in soup.find_all('html', limit=1):
            for head in html.find_all('head', limit=1):
                desc = head.find_all('meta', {'name' : 'description'})
                if len(desc) > 1:
                    self.__journal.foundDefect(transaction.idno,
                                               "seo:multidsc",
                                               "Multiple description meta tags found",
                                               str(len(desc)), 0.8)
                elif len(desc) == 0:
                    self.__journal.foundDefect(transaction.idno,
                                               "seo:nodsc",
                                               "No description meta tag found",
                                               "", 0.8)

                tags = head.find_all('meta', {'name' : 'keywords'})
                if len(tags) > 1:
                    self.__journal.foundDefect(transaction.idno,
                                               "seo:multikeys",
                                               "Multiple keywords meta tags found",
                                               str(len(tags)), 0.8)
                elif len(desc) == 0:
                    self.__journal.foundDefect(transaction.idno,
                                               "seo:nokeys",
                                               "No keywords meta tag found",
                                               "", 0.8)
        return
<commit_msg>Refactoring<commit_after>from common import PluginType
from bs4 import BeautifulSoup
from yapsy.IPlugin import IPlugin
import logging


class MetaTagValidator(IPlugin):
    
    category = PluginType.CHECKER
    id = "seometa"
    
    __defects = {"seo:multidsc": "Multiple description meta tags found",
                 "seo:nodsc": "No description meta tag found",
                 "seo:multikeys": "Multiple keywords meta tags found", 
                 "seo:nokeys": "No keywords meta tags found"}
    __severity = 0.8
     
    def __init__(self):
        self.__journal = None

    def setJournal(self, journal):
        self.__journal = journal

    def __classify(self, head, name, idno, zero, multi):
        foo = head.find_all('meta', {'name' : name})
        if len(foo) > 1:
            clas = multi
            dsc = str(len(desc)
        elif len(foo) == 0:
            clas = zero
            dsc = ""

        self.__journal.foundDefect(idno, clas,
                                   MetaTagValidator.__defects[clas],
                                   dsc, MetaTagValidator.__severity)

    def check(self, transaction):
        """Pusti validator, ulozi chyby.
        """
        if 'soup' in transaction.cache and transaction.cache['soup']:
            soup = transaction.cache['soup']
        else:
            soup = BeautifulSoup(transaction.getContent(), 'lxml')
            transaction.cache['soup'] = soup

        for html in soup.find_all('html', limit=1):
            for head in html.find_all('head', limit=1):
                self.__classify(head, 'description', transaction.idno,
                                  "seo:nodsc", "seo:multidsc")

                self.__classify(head, 'keywords', transaction.idno,
                                  "seo:nokeys", "seo:multikeys")
        return

{'fraction_non_alphanumeric': 0.09885172241637544, 'fraction_numerical': 0.0054917623564653024, 'mean_word_length': 1.3640117994100296, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4540887', 'n_tokens_mistral': 1030, 'n_tokens_neox': 990, 'n_words': 242}