<commit_before>from __future__ import print_function, division, absolute_import
from PIL import Image, ImageChops
import browserstack_screenshots
import requests
import os
from glob import glob
from ntpath import basename
import json
import time
import shutil
import logging

from diffbrowsers.browsers import test_browsers


VIEWS = [
    'glyphs-all',
    'glyphs-missing',
    'glyphs-new',
    'glyphs-modified',
    'waterfall',
]


URL_GF_REGRESSION = 'http://192.168.127.12'


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class UnknownGFRegressionViewError(Exception):
    def __init__(self):
        super(UnknownGFRegressionViewError, self).__init__(
        'View is not valid. Choose from [%s]' % ', '.join(VIEWS)
    )

class DiffBrowsers(object):
    """Class to control GF Regression and Browser Stack api"""
    def __init__(self, auth, dst_dir, fonts_after, fonts_before,
                 browsers=test_browsers['all_browsers']):
        self.auth = auth
        self.views = set()
        self.fonts_before = fonts_before
        self.fonts_after = fonts_after

        self.dst_dir = dst_dir
        self.uid = None   
        self.browsers = browsers
        self.report = {'views': {},
                       'fonts': map(os.path.basename, self.fonts_after)}

        self._mkdir(self.dst_dir)
        self._post_fonts_to_test()

    def diff_view(self, view, pt=None, gen_gifs=False):
        """Diff a GF Regression view"""
        logger.info('Generating diff images for view %s' % view)
        fonts_before_url, fonts_after_url = self._build_view_urls(view, pt)

        view_dir = '%s_%spt' % (view, pt) if pt else view
        view_path = os.path.join(self.dst_dir, view_dir)
        self._mkdir(view_path, overwrite=True)

        fonts_before_path = os.path.join(view_path, 'fonts_before')
        self._mkdir(fonts_before_path, overwrite=True)
        self._generate_images(fonts_before_url, fonts_before_path)

        fonts_after_path = os.path.join(view_path, 'fonts_after')
        self._mkdir(fonts_after_path, overwrite=True)
        self._generate_images(fonts_after_url, fonts_after_path)

        if gen_gifs:
            gif_dir = os.path.join(view_path, 'gifs')
            self._mkdir(gif_dir, overwrite=True)
            self._gen_gifs(fonts_before_path, fonts_after_path, gif_dir)

        diff_dir = os.path.join(view_path, 'diff')
        self._mkdir(diff_dir, overwrite=True)
        comparison = self._compare_images(fonts_before_path, fonts_after_path, diff_dir)
        self.report['views'][view] = comparison
        return comparison

    def _post_fonts_to_test(self):
        """Post fonts using the GF Regression api.

        If test_against_gf is True. font_set_2 is ignored and the fonts
        are downloaded from Google Fonts instead."""
        logger.info("Posting fonts to GF Regression")<commit_msg>Include pt size if specified<commit_after>from __future__ import print_function, division, absolute_import
from PIL import Image, ImageChops
import browserstack_screenshots
import requests
import os
from glob import glob
from ntpath import basename
import json
import time
import shutil
import logging

from diffbrowsers.browsers import test_browsers


VIEWS = [
    'glyphs-all',
    'glyphs-missing',
    'glyphs-new',
    'glyphs-modified',
    'waterfall',
]


URL_GF_REGRESSION = 'http://192.168.127.12'


logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class UnknownGFRegressionViewError(Exception):
    def __init__(self):
        super(UnknownGFRegressionViewError, self).__init__(
        'View is not valid. Choose from [%s]' % ', '.join(VIEWS)
    )

class DiffBrowsers(object):
    """Class to control GF Regression and Browser Stack api"""
    def __init__(self, auth, dst_dir, fonts_after, fonts_before,
                 browsers=test_browsers['all_browsers']):
        self.auth = auth
        self.views = set()
        self.fonts_before = fonts_before
        self.fonts_after = fonts_after

        self.dst_dir = dst_dir
        self.uid = None   
        self.browsers = browsers
        self.report = {'views': {},
                       'fonts': map(os.path.basename, self.fonts_after)}

        self._mkdir(self.dst_dir)
        self._post_fonts_to_test()

    def diff_view(self, view, pt=None, gen_gifs=False):
        """Diff a GF Regression view"""
        logger.info('Generating diff images for view %s' % view)
        fonts_before_url, fonts_after_url = self._build_view_urls(view, pt)

        view_dir = '%s_%spt' % (view, pt) if pt else view
        view_path = os.path.join(self.dst_dir, view_dir)
        self._mkdir(view_path, overwrite=True)

        fonts_before_path = os.path.join(view_path, 'fonts_before')
        self._mkdir(fonts_before_path, overwrite=True)
        self._generate_images(fonts_before_url, fonts_before_path)

        fonts_after_path = os.path.join(view_path, 'fonts_after')
        self._mkdir(fonts_after_path, overwrite=True)
        self._generate_images(fonts_after_url, fonts_after_path)

        if gen_gifs:
            gif_dir = os.path.join(view_path, 'gifs')
            self._mkdir(gif_dir, overwrite=True)
            self._gen_gifs(fonts_before_path, fonts_after_path, gif_dir)

        diff_dir = os.path.join(view_path, 'diff')
        self._mkdir(diff_dir, overwrite=True)
        comparison = self._compare_images(fonts_before_path, fonts_after_path, diff_dir)
        r_view = '{}_{}pt'.format(view, pt) if pt else view
        self.report['views'][r_view] = comparison
        return comparison

    def _post_fonts_to_test(self):
        """Post fonts using the GF Regression api.

        If test_against_gf is True. font_set_2 is ignored and the fonts
        are downloaded from Google Fonts instead."""
        logger.info("Posting fonts to GF Regression")
{'fraction_non_alphanumeric': 0.0975983648441492, 'fraction_numerical': 0.004087889626980072, 'mean_word_length': 3.129395218002813, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5979273', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1815, 'n_words': 414}