<commit_before>from . import base
from . import messages
from grow.pods import locales
from grow.pods import urls
import fnmatch
import mimetypes
import re
import hashlib
import webob
import os

mimetypes.add_type('application/font-woff', '.woff')
mimetypes.add_type('application/font-woff', '.woff')
mimetypes.add_type('image/bmp', '.cur')
mimetypes.add_type('image/svg+xml', '.svg')
mimetypes.add_type('text/css', '.css')


SKIP_PATTERNS = [
    '**/.**',
]


class Error(Exception):
    pass


class BadStaticFileError(Error):
    pass


class StaticFile(object):

    def __init__(self, pod_path, serving_path, locale=None, localization=None,
                 controller=None, fingerprinted=False, pod=None):
        self.pod = pod
        self.default_locale = pod.podspec.default_locale
        self.locale = pod.normalize_locale(locale)
        self.localization = localization
        self.pod_path = pod_path
        self.serving_path = serving_path
        self.controller = controller
        self.basename = os.path.basename(pod_path)
        self.fingerprinted = fingerprinted
        self.base, self.ext = os.path.splitext(self.basename)

    def __repr__(self):
        if self.locale:
            return "<StaticFile({}, locale='{}')>".format(self.pod_path, self.locale)
        return "<StaticFile({})>".format(self.pod_path)

    def __eq__(self, other):
        return (self.pod_path == other.pod_path and self.pod == other.pod
                and other.locale == self.locale)

    def __ne__(self, other):
        return not self.__eq__(other)

    @property
    def exists(self):
        return self.pod.file_exists(self.pod_path)

    @property
    def modified(self):
        return self.pod.file_modified(self.pod_path)

    @property
    def size(self):
        return self.pod.file_size(self.pod_path)

    @property
    def fingerprint(self):
        return StaticFile._create_fingerprint(self.pod, self.pod_path)

    @staticmethod
    def _create_fingerprint(pod, pod_path):
        md5 = hashlib.md5()
        with pod.open_file(pod_path, 'rb') as fp:
            content = fp.read()
            md5.update(content)
        return md5.hexdigest()

    @staticmethod
    def remove_fingerprint(path):
        base, _ = os.path.splitext(path)
        if base.endswith('.min'):
            return re.sub('(.*)-([a-fA-F\d]{32})\.min\.(.*)', r'\g<1>.min.\g<3>', path)
        return re.sub('(.*)-([a-fA-F\d]{32})\.(.*)', r'\g<1>.\g<3>', path)

    @staticmethod
    def apply_fingerprint(path, fingerprint):
        base, ext = os.path.splitext(path)
        # Special case to preserve ".min.<ext>" extension lockup.
        if base.endswith('.min'):
            base = base[:-4]
            return '{}-{}.min{}'.format(base, fingerprint, ext)
        else:
            return '{}-{}{}'.format(base, fingerprint, ext)

    @property
    def url(self):
        serving_path = self.serving_path
        path_format = self.controller.path_format.replace('{filename}', '')
        if '{fingerprint}' in path_format:
            path_format = path_format.replace('{fingerprint}', self.fingerprint)
        # Determine suffix only after all replacements are made.
        suffix = serving_path.replace(path_format, '')
        if self.localization:
            if self.fingerprinted:
                suffix = StaticFile.remove_fingerprint(suffix)
            localized_pod_path = self.localization['static_dir'] + suffix
            localized_pod_path = localized_pod_path.format(locale=self.locale)
            localized_pod_path = localized_pod_path.replace('//', '/')
            if self.pod.file_exists(localized_pod_path):
                # TODO(jeremydw): Centralize path formatting.
                # Internal paths use Babel locales, serving paths use aliases.
                locale = self.locale.alias if self.locale is not None else self.locale
                localized_serving_path = self.localization['serve_at'] + suffix
                kwargs = {
                    'locale': locale,
                    'root': self.pod.podspec.root,
                }
                if '{fingerprint}' in localized_serving_path:
                    fingerprint = StaticFile._create_fingerprint(
                        self.pod, localized_pod_path)
                    kwargs['fingerprint'] = fingerprint
                localized_serving_path = localized_serving_path.format(**kwargs)
                if self.fingerprinted and localized_serving_path:
                    fingerprint = StaticFile._create_fingerprint(self.pod, localized_pod_path)
                    localized_serving_path = StaticFile.apply_fingerprint(localized_serving_path, fingerprint)
                serving_path = localized_serving_path.replace('//', '/')
        return urls.Url(path=serving_path) if serving_path else None<commit_msg>Fix issue where URLs created via {{g.static}} would not contain correct environment.<commit_after>from . import base
from . import messages
from grow.pods import locales
from grow.pods import urls
import fnmatch
import mimetypes
import re
import hashlib
import webob
import os

mimetypes.add_type('application/font-woff', '.woff')
mimetypes.add_type('application/font-woff', '.woff')
mimetypes.add_type('image/bmp', '.cur')
mimetypes.add_type('image/svg+xml', '.svg')
mimetypes.add_type('text/css', '.css')


SKIP_PATTERNS = [
    '**/.**',
]


class Error(Exception):
    pass


class BadStaticFileError(Error):
    pass


class StaticFile(object):

    def __init__(self, pod_path, serving_path, locale=None, localization=None,
                 controller=None, fingerprinted=False, pod=None):
        self.pod = pod
        self.default_locale = pod.podspec.default_locale
        self.locale = pod.normalize_locale(locale)
        self.localization = localization
        self.pod_path = pod_path
        self.serving_path = serving_path
        self.controller = controller
        self.basename = os.path.basename(pod_path)
        self.fingerprinted = fingerprinted
        self.base, self.ext = os.path.splitext(self.basename)

    def __repr__(self):
        if self.locale:
            return "<StaticFile({}, locale='{}')>".format(self.pod_path, self.locale)
        return "<StaticFile({})>".format(self.pod_path)

    def __eq__(self, other):
        return (self.pod_path == other.pod_path and self.pod == other.pod
                and other.locale == self.locale)

    def __ne__(self, other):
        return not self.__eq__(other)

    @property
    def exists(self):
        return self.pod.file_exists(self.pod_path)

    @property
    def modified(self):
        return self.pod.file_modified(self.pod_path)

    @property
    def size(self):
        return self.pod.file_size(self.pod_path)

    @property
    def fingerprint(self):
        return StaticFile._create_fingerprint(self.pod, self.pod_path)

    @staticmethod
    def _create_fingerprint(pod, pod_path):
        md5 = hashlib.md5()
        with pod.open_file(pod_path, 'rb') as fp:
            content = fp.read()
            md5.update(content)
        return md5.hexdigest()

    @staticmethod
    def remove_fingerprint(path):
        base, _ = os.path.splitext(path)
        if base.endswith('.min'):
            return re.sub('(.*)-([a-fA-F\d]{32})\.min\.(.*)', r'\g<1>.min.\g<3>', path)
        return re.sub('(.*)-([a-fA-F\d]{32})\.(.*)', r'\g<1>.\g<3>', path)

    @staticmethod
    def apply_fingerprint(path, fingerprint):
        base, ext = os.path.splitext(path)
        # Special case to preserve ".min.<ext>" extension lockup.
        if base.endswith('.min'):
            base = base[:-4]
            return '{}-{}.min{}'.format(base, fingerprint, ext)
        else:
            return '{}-{}{}'.format(base, fingerprint, ext)

    @property
    def url(self):
        serving_path = self.serving_path
        path_format = self.controller.path_format.replace('{filename}', '')
        if '{fingerprint}' in path_format:
            path_format = path_format.replace('{fingerprint}', self.fingerprint)
        # Determine suffix only after all replacements are made.
        suffix = serving_path.replace(path_format, '')
        if self.localization:
            if self.fingerprinted:
                suffix = StaticFile.remove_fingerprint(suffix)
            localized_pod_path = self.localization['static_dir'] + suffix
            localized_pod_path = localized_pod_path.format(locale=self.locale)
            localized_pod_path = localized_pod_path.replace('//', '/')
            if self.pod.file_exists(localized_pod_path):
                # TODO(jeremydw): Centralize path formatting.
                # Internal paths use Babel locales, serving paths use aliases.
                locale = self.locale.alias if self.locale is not None else self.locale
                localized_serving_path = self.localization['serve_at'] + suffix
                kwargs = {
                    'locale': locale,
                    'root': self.pod.podspec.root,
                }
                if '{fingerprint}' in localized_serving_path:
                    fingerprint = StaticFile._create_fingerprint(
                        self.pod, localized_pod_path)
                    kwargs['fingerprint'] = fingerprint
                localized_serving_path = localized_serving_path.format(**kwargs)
                if self.fingerprinted and localized_serving_path:
                    fingerprint = StaticFile._create_fingerprint(self.pod, localized_pod_path)
                    localized_serving_path = StaticFile.apply_fingerprint(localized_serving_path, fingerprint)
                serving_path = localized_serving_path.replace('//', '/')
        if serving_path:
            return urls.Url(
                path=serving_path,
                host=self.pod.env.host,
                port=self.pod.env.port,
                scheme=self.pod.env.scheme)
{'fraction_non_alphanumeric': 0.11881388523428803, 'fraction_numerical': 0.0026313126201801436, 'mean_word_length': 2.4205607476635516, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6251362', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2856, 'n_words': 574}