<commit_before># coding: utf-8

# imports
import os, re
from time import gmtime

# django imports
from django.template import Library, Node, Variable, VariableDoesNotExist, TemplateSyntaxError
from django.conf import settings
from django.utils.encoding import force_unicode

# filebrowser imports
from filebrowser.settings import MEDIA_ROOT, MEDIA_URL, VERSIONS
from filebrowser.functions import url_to_path, path_to_url, get_version_path, version_generator
from filebrowser.base import FileObject

register = Library()


class VersionNode(Node):
    def __init__(self, src, version_prefix):
        self.src = Variable(src)
        if (version_prefix[0] == version_prefix[-1] and version_prefix[0] in ('"', "'")):
            self.version_prefix = version_prefix[1:-1]
        else:
            self.version_prefix = None
            self.version_prefix_var = Variable(version_prefix)
        
    def render(self, context):
        try:
            source = self.src.resolve(context)
        except VariableDoesNotExist:
            return None
        if self.version_prefix:
            version_prefix = self.version_prefix
        else:
            try:
                version_prefix = self.version_prefix_var.resolve(context)
            except VariableDoesNotExist:
                return None
        try:
            source = force_unicode(source)
            version_path = get_version_path(url_to_path(source), version_prefix)
            if not os.path.isfile(os.path.join(MEDIA_ROOT, version_path)):
                # create version
                version_path = version_generator(url_to_path(source), version_prefix)
            elif os.path.getmtime(os.path.join(MEDIA_ROOT, url_to_path(source))) > os.path.getmtime(os.path.join(MEDIA_ROOT, version_path)):
                # recreate version if original image was updated
                version_path = version_generator(url_to_path(source), version_prefix, force=True)
            return path_to_url(version_path)
        except:
            return ""


def version(parser, token):
    """
    Displaying a version of an existing Image according to the predefined VERSIONS settings (see filebrowser settings).
    {% version field_name version_prefix %}
    
    Use {% version my_image 'medium' %} in order to display the medium-size
    version of an Image stored in a field name my_image.
    
    version_prefix can be a string or a variable. if version_prefix is a string, use quotes.
    """
    
    try:
        tag, src, version_prefix = token.split_contents()
    except:
        raise TemplateSyntaxError, "%s tag requires 2 arguments" % token.contents.split()[0]
    if (version_prefix[0] == version_prefix[-1] and version_prefix[0] in ('"', "'")) and version_prefix.lower()[1:-1] not in VERSIONS:
        raise TemplateSyntaxError, "%s tag received bad version_prefix %s" % (tag, version_prefix)
    return VersionNode(src, version_prefix)


class VersionObjectNode(Node):
    def __init__(self, src, version_prefix, var_name):
        self.var_name = var_name
        self.src = Variable(src)
        if (version_prefix[0] == version_prefix[-1] and version_prefix[0] in ('"', "'")):
            self.version_prefix = version_prefix[1:-1]
        else:
            self.version_prefix = None
            self.version_prefix_var = Variable(version_prefix)
    
    def render(self, context):
        try:
            source = self.src.resolve(context)
        except VariableDoesNotExist:
            return None
        if self.version_prefix:
            version_prefix = self.version_prefix
        else:
            try:
                version_prefix = self.version_prefix_var.resolve(context)
            except VariableDoesNotExist:
                return None
        try:
            source = force_unicode(source)
            version_path = get_version_path(url_to_path(source), version_prefix)
            if not os.path.isfile(os.path.join(MEDIA_ROOT, version_path)):
                # create version
                version_path = version_generator(url_to_path(source), version_prefix)
            elif os.path.getmtime(os.path.join(MEDIA_ROOT, url_to_path(source))) > os.path.getmtime(os.path.join(MEDIA_ROOT, version_path)):
                # recreate version if original image was updated
                version_path = version_generator(url_to_path(source), version_prefix, force=True)
            context[self.var_name] = FileObject(version_path)
        except:
            context[self.var_name] = ""
        return ''<commit_msg>fixed some unicode bugs;<commit_after># coding: utf-8

# imports
import os, re
from time import gmtime

# django imports
from django.template import Library, Node, Variable, VariableDoesNotExist, TemplateSyntaxError
from django.conf import settings
from django.utils.encoding import force_unicode
from django.utils.encoding import smart_str

# filebrowser imports
from filebrowser.settings import MEDIA_ROOT, MEDIA_URL, VERSIONS
from filebrowser.functions import url_to_path, path_to_url, get_version_path, version_generator
from filebrowser.base import FileObject

register = Library()


class VersionNode(Node):
    def __init__(self, src, version_prefix):
        self.src = Variable(src)
        if (version_prefix[0] == version_prefix[-1] and version_prefix[0] in ('"', "'")):
            self.version_prefix = version_prefix[1:-1]
        else:
            self.version_prefix = None
            self.version_prefix_var = Variable(version_prefix)
        
    def render(self, context):
        try:
            source = self.src.resolve(context)
        except VariableDoesNotExist:
            return None
        if self.version_prefix:
            version_prefix = self.version_prefix
        else:
            try:
                version_prefix = self.version_prefix_var.resolve(context)
            except VariableDoesNotExist:
                return None
        try:
            source = force_unicode(source)
            version_path = get_version_path(url_to_path(source), version_prefix)
            if not os.path.isfile(smart_str(os.path.join(MEDIA_ROOT, version_path))):
                # create version
                version_path = version_generator(url_to_path(source), version_prefix)
            elif os.path.getmtime(os.path.join(MEDIA_ROOT, url_to_path(source))) > os.path.getmtime(os.path.join(MEDIA_ROOT, version_path)):
                # recreate version if original image was updated
                version_path = version_generator(url_to_path(source), version_prefix, force=True)
            return path_to_url(version_path)
        except:
            return ""


def version(parser, token):
    """
    Displaying a version of an existing Image according to the predefined VERSIONS settings (see filebrowser settings).
    {% version field_name version_prefix %}
    
    Use {% version my_image 'medium' %} in order to display the medium-size
    version of an Image stored in a field name my_image.
    
    version_prefix can be a string or a variable. if version_prefix is a string, use quotes.
    """
    
    try:
        tag, src, version_prefix = token.split_contents()
    except:
        raise TemplateSyntaxError, "%s tag requires 2 arguments" % token.contents.split()[0]
    if (version_prefix[0] == version_prefix[-1] and version_prefix[0] in ('"', "'")) and version_prefix.lower()[1:-1] not in VERSIONS:
        raise TemplateSyntaxError, "%s tag received bad version_prefix %s" % (tag, version_prefix)
    return VersionNode(src, version_prefix)


class VersionObjectNode(Node):
    def __init__(self, src, version_prefix, var_name):
        self.var_name = var_name
        self.src = Variable(src)
        if (version_prefix[0] == version_prefix[-1] and version_prefix[0] in ('"', "'")):
            self.version_prefix = version_prefix[1:-1]
        else:
            self.version_prefix = None
            self.version_prefix_var = Variable(version_prefix)
    
    def render(self, context):
        try:
            source = self.src.resolve(context)
        except VariableDoesNotExist:
            return None
        if self.version_prefix:
            version_prefix = self.version_prefix
        else:
            try:
                version_prefix = self.version_prefix_var.resolve(context)
            except VariableDoesNotExist:
                return None
        try:
            source = force_unicode(source)
            version_path = get_version_path(url_to_path(source), version_prefix)
            if not os.path.isfile(smart_str(os.path.join(MEDIA_ROOT, version_path))):
                # create version
                version_path = version_generator(url_to_path(source), version_prefix)
            elif os.path.getmtime(os.path.join(MEDIA_ROOT, url_to_path(source))) > os.path.getmtime(os.path.join(MEDIA_ROOT, version_path)):
                # recreate version if original image was updated
                version_path = version_generator(url_to_path(source), version_prefix, force=True)
            context[self.var_name] = FileObject(version_path)
        except:
            context[self.var_name] = ""
        return ''
{'fraction_non_alphanumeric': 0.08678997482762395, 'fraction_numerical': 0.00394002407792492, 'mean_word_length': 2.6669341894060996, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1068774', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2506, 'n_words': 617}