<commit_before>#!/usr/bin/env python3
# -*- coding: utf8 -*-
################################################################################
##
## Copyright (C) 2012 Typhos
##
## This Source Code Form is subject to the terms of the Mozilla Public
## License, v. 2.0. If a copy of the MPL was not distributed with this
## file, You can obtain one at http://mozilla.org/MPL/2.0/.
##
################################################################################

__all__ = ["PartialEmote", "CustomEmote", "NormalEmote"]

import bplib

class BaseEmote:
    def __init__(self, name, suffix, css):
        self.name = name
        self.suffix = suffix
        self.css = css

    @property
    def name_pair(self):
        return (self.name, self.suffix)

    def __repr__(self):
        if self.suffix:
            return "Emote(%r, %r)" % (self.name, self.suffix)
        else:
            return "Emote(%r)" % (self.name)

class PartialEmote(BaseEmote):
    pass

class Emote(BaseEmote):
    def __init__(self, name, suffix, css, is_nsfw=False, custom_selector=None):
        BaseEmote.__init__(self, name, suffix, css)
        self.is_nsfw = is_nsfw
        self.custom_selector = custom_selector

    def selector(self):
        if self.custom_selector:
            return self.custom_selector

        name = self.name.replace("!", "_excl_").replace(":", "_colon_")
        return ".bpmote-" + bplib.combine_name_pair((name, self.suffix)).lstrip("/").lower()

class CustomEmote(Emote):
    @classmethod
    def load(cls, data):
        name = data.pop("Name")
        suffix = data.pop("Suffix", None)
        css = data.pop("CSS", {})
        is_nsfw = data.pop("NSFW", False)
        custom_selector = data.pop("Selector", None)
        return cls(name, suffix, css, is_nsfw, custom_selector)

    def dump(self):
        data = {
            "Name": self.name,
            "CSS": self.css
            }

        if self.suffix:
            data["Suffix"] = self.suffix
        if self.is_nsfw:
            data["NSFW"] = True
        if self.custom_selector:
            data["Selector"] = self.custom_selector

        return data

    def to_css(self):
        return self.css.copy()

# For lack of a better name
class NormalEmote(Emote):
    def __init__(self, name, suffix, css, image_url, size, offset,
                 is_nsfw=False, custom_selector=None, disable_css_gen=False):
        Emote.__init__(self, name, suffix, css, is_nsfw, custom_selector)
        self.image_url = image_url
        self.size = size
        self.offset = offset
        self.disable_css_gen = disable_css_gen

    @classmethod
    def load(cls, image_url, data):
        name = data.pop("Name")
        suffix = data.pop("Suffix", None)
        css = data.pop("CSS", {})
        is_nsfw = data.pop("NSFW", False)
        custom_selector = data.pop("Selector", None)
        size = data.pop("Size")
        offset = data.pop("Offset")
        disable_css_gen = data.pop("NoCSS", False)
        return cls(name, suffix, css, image_url, size, offset, is_nsfw, custom_selector, disable_css_gen)

    def dump(self):
        data = {
            "Name": self.name,
            "Size": list(self.size),
            "Offset": list(self.offset)
            }

        if self.suffix:
            data["Suffix"] = self.suffix
        if self.css:
            data["CSS"] = self.css
        if self.is_nsfw:
            data["NSFW"] = True
        if self.custom_selector:
            data["Selector"] = self.custom_selector

        return data

    def to_css(self):
        assert not self.disable_css_gen
        css = {
            "display": "block",
            "clear": "none",
            "float": "left",
            # YouTube sets a background property with an !important on it, which
            # interferes with global emotes. We need to override that in Firefox.
            #
            # As yet, none of these other properties require the same consideration.
            "background-image": "url(%s)" % (self.image_url),
            "background-position": "%spx %spx" % (self.offset[0], self.offset[1]),
            "width": "%spx" % (self.size[0]),
            "height": "%spx" % (self.size[1])
            }
        css.update(self.css)
        return css<commit_msg>Comment fix<commit_after>#!/usr/bin/env python3
# -*- coding: utf8 -*-
################################################################################
##
## Copyright (C) 2012 Typhos
##
## This Source Code Form is subject to the terms of the Mozilla Public
## License, v. 2.0. If a copy of the MPL was not distributed with this
## file, You can obtain one at http://mozilla.org/MPL/2.0/.
##
################################################################################

__all__ = ["PartialEmote", "CustomEmote", "NormalEmote"]

import bplib

class BaseEmote:
    def __init__(self, name, suffix, css):
        self.name = name
        self.suffix = suffix
        self.css = css

    @property
    def name_pair(self):
        return (self.name, self.suffix)

    def __repr__(self):
        if self.suffix:
            return "Emote(%r, %r)" % (self.name, self.suffix)
        else:
            return "Emote(%r)" % (self.name)

class PartialEmote(BaseEmote):
    pass

class Emote(BaseEmote):
    def __init__(self, name, suffix, css, is_nsfw=False, custom_selector=None):
        BaseEmote.__init__(self, name, suffix, css)
        self.is_nsfw = is_nsfw
        self.custom_selector = custom_selector

    def selector(self):
        if self.custom_selector:
            return self.custom_selector

        name = self.name.replace("!", "_excl_").replace(":", "_colon_")
        return ".bpmote-" + bplib.combine_name_pair((name, self.suffix)).lstrip("/").lower()

class CustomEmote(Emote):
    @classmethod
    def load(cls, data):
        name = data.pop("Name")
        suffix = data.pop("Suffix", None)
        css = data.pop("CSS", {})
        is_nsfw = data.pop("NSFW", False)
        custom_selector = data.pop("Selector", None)
        return cls(name, suffix, css, is_nsfw, custom_selector)

    def dump(self):
        data = {
            "Name": self.name,
            "CSS": self.css
            }

        if self.suffix:
            data["Suffix"] = self.suffix
        if self.is_nsfw:
            data["NSFW"] = True
        if self.custom_selector:
            data["Selector"] = self.custom_selector

        return data

    def to_css(self):
        return self.css.copy()

# For lack of a better name
class NormalEmote(Emote):
    def __init__(self, name, suffix, css, image_url, size, offset,
                 is_nsfw=False, custom_selector=None, disable_css_gen=False):
        Emote.__init__(self, name, suffix, css, is_nsfw, custom_selector)
        self.image_url = image_url
        self.size = size
        self.offset = offset
        self.disable_css_gen = disable_css_gen

    @classmethod
    def load(cls, image_url, data):
        name = data.pop("Name")
        suffix = data.pop("Suffix", None)
        css = data.pop("CSS", {})
        is_nsfw = data.pop("NSFW", False)
        custom_selector = data.pop("Selector", None)
        size = data.pop("Size")
        offset = data.pop("Offset")
        disable_css_gen = data.pop("NoCSS", False)
        return cls(name, suffix, css, image_url, size, offset, is_nsfw, custom_selector, disable_css_gen)

    def dump(self):
        data = {
            "Name": self.name,
            "Size": list(self.size),
            "Offset": list(self.offset)
            }

        if self.suffix:
            data["Suffix"] = self.suffix
        if self.css:
            data["CSS"] = self.css
        if self.is_nsfw:
            data["NSFW"] = True
        if self.custom_selector:
            data["Selector"] = self.custom_selector

        return data

    def to_css(self):
        assert not self.disable_css_gen
        css = {
            "display": "block",
            "clear": "none",
            "float": "left",
            "background-image": "url(%s)" % (self.image_url),
            "width": "%spx" % (self.size[0]),
            "height": "%spx" % (self.size[1])
            "background-position": "%spx %spx" % (self.offset[0], self.offset[1]),
            }
        css.update(self.css)
        return css
{'fraction_non_alphanumeric': 0.16622372553756945, 'fraction_numerical': 0.0033824595312877508, 'mean_word_length': 2.264589905362776, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4389088', 'n_tokens_mistral': 2533, 'n_tokens_neox': 2469, 'n_words': 609}