<commit_before># Copyright 2010 <NAME> <<EMAIL>>
#
# This file is part of the Pykeg package of the Kegbot project.
# For more information on Pykeg or Kegbot, see http://kegbot.org/
#
# Pykeg is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Pykeg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pykeg.  If not, see <http://www.gnu.org/licenses/>.

import copy
import pytz

from django.conf import settings
from django.core.urlresolvers import reverse
from django.template import Library
from django.template import Node
from django.template import VariableDoesNotExist
from django.template import TemplateSyntaxError
from django.template import Variable
from django.template.defaultfilters import pluralize
from django.utils.safestring import mark_safe

from kegbot.util import kbjson
from kegbot.util import units
from kegbot.util import util

from pykeg.core import models
from pykeg.web.charts import charts

register = Library()

@register.inclusion_tag('kegweb/mugshot_box.html', takes_context=True)
def mugshot_box(context, user, boxsize=0):
    c = copy.copy(context)
    c['user'] = user
    c['boxsize'] = boxsize
    return c

@register.inclusion_tag('kegweb/picture-gallery.html', takes_context=True)
def gallery(context, picture_or_pictures, thumb_size='span2', gallery_id=''):
    c = copy.copy(context)
    if not hasattr(picture_or_pictures, '__iter__'):
        c['gallery_pictures'] = [picture_or_pictures]
    else:
        c['gallery_pictures'] = picture_or_pictures
    c['thumb_size'] = thumb_size
    c['gallery_id'] = gallery_id
    return c

@register.inclusion_tag('kegweb/badge.html')
def badge(amount, caption, style='', is_volume=False, do_pluralize=False):
    if is_volume:
        amount = mark_safe(VolumeNode.format(amount, 'mL'))
    if do_pluralize:
        caption += pluralize(amount)
    return {
        'badge_amount': amount,
        'badge_caption': caption,
        'badge_style': style,
    }


### navitem

@register.tag('navitem')
def navitem(parser, token):
    """{% navitem <viewname> <title> [exact] %}"""
    tokens = token.split_contents()
    if len(tokens) < 3:
        raise TemplateSyntaxError, '%s requires at least 3 tokens' % tokens[0]
    return NavitemNode(*tokens[1:])

class NavitemNode(Node):
    def __init__(self, *args):
        self._viewname = args[0]
        self._title = args[1]
        self._exact = 'exact' in args[2:]

    def render(self, context):
        viewname = Variable(self._viewname).resolve(context)
        title = Variable(self._title).resolve(context)
        if viewname.startswith('/'):
            urlbase = viewname
        else:
            urlbase = reverse(viewname)

        request_path = context['request_path']

        if self._exact:
            active = (request_path == urlbase)
        else:
            active = request_path.startswith(urlbase)
        if active:
            res = '<li class="active">'
        else:
            res = '<li>'
        res += '<a href="%s">%s</a></li>' % (urlbase, title)
        return res


### timeago

@register.tag('timeago')
def timeago(parser, token):
    """{% timeago <timestamp> %}"""
    tokens = token.contents.split()
    if len(tokens) != 2:
        raise TemplateSyntaxError, '%s requires 2 tokens' % tokens[0]
    return TimeagoNode(tokens[1])

class TimeagoNode(Node):
    def __init__(self, timestamp_varname):
        self._timestamp_varname = timestamp_varname

    def render(self, context):
        tv = Variable(self._timestamp_varname)
        ts = tv.resolve(context)

        # Try to set time zone information.
        if settings.TIME_ZONE and not settings.USE_TZ:
            try:
                tz = pytz.timezone(settings.TIME_ZONE)
                ts = tz.localize(ts)
            except pytz.UnknownTimeZoneError:
                pass

        iso = ts.isoformat()
        alt = ts.strftime("%A, %B %d, %Y %I:%M%p")
        return '<abbr class="timeago" title="%s">%s</abbr>' % (iso, alt)


### temperature

@register.tag('temperature')
def volumetag(parser, token):
    """{% temperature <temp_c> %}"""
    tokens = token.contents.split()
    if len(tokens) < 2:
        raise TemplateSyntaxError, '%s requires at least 2 tokens' % tokens[0]
    return TemperatureNode(tokens[1])

class TemperatureNode(Node):
    TEMPLATE = "%(amount)s&deg; %(unit)s"

    def __init__(self, varname):<commit_msg>timeago: show the tooltip time string in local zone.<commit_after># Copyright 2010 <NAME> <<EMAIL>>
#
# This file is part of the Pykeg package of the Kegbot project.
# For more information on Pykeg or Kegbot, see http://kegbot.org/
#
# Pykeg is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Pykeg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pykeg.  If not, see <http://www.gnu.org/licenses/>.

import copy
import pytz

from django.conf import settings
from django.core.urlresolvers import reverse
from django.template import Library
from django.template import Node
from django.template import VariableDoesNotExist
from django.template import TemplateSyntaxError
from django.template import Variable
from django.template.defaultfilters import pluralize
from django.utils import timezone
from django.utils.safestring import mark_safe

from kegbot.util import kbjson
from kegbot.util import units
from kegbot.util import util

from pykeg.core import models
from pykeg.web.charts import charts

register = Library()

@register.inclusion_tag('kegweb/mugshot_box.html', takes_context=True)
def mugshot_box(context, user, boxsize=0):
    c = copy.copy(context)
    c['user'] = user
    c['boxsize'] = boxsize
    return c

@register.inclusion_tag('kegweb/picture-gallery.html', takes_context=True)
def gallery(context, picture_or_pictures, thumb_size='span2', gallery_id=''):
    c = copy.copy(context)
    if not hasattr(picture_or_pictures, '__iter__'):
        c['gallery_pictures'] = [picture_or_pictures]
    else:
        c['gallery_pictures'] = picture_or_pictures
    c['thumb_size'] = thumb_size
    c['gallery_id'] = gallery_id
    return c

@register.inclusion_tag('kegweb/badge.html')
def badge(amount, caption, style='', is_volume=False, do_pluralize=False):
    if is_volume:
        amount = mark_safe(VolumeNode.format(amount, 'mL'))
    if do_pluralize:
        caption += pluralize(amount)
    return {
        'badge_amount': amount,
        'badge_caption': caption,
        'badge_style': style,
    }


### navitem

@register.tag('navitem')
def navitem(parser, token):
    """{% navitem <viewname> <title> [exact] %}"""
    tokens = token.split_contents()
    if len(tokens) < 3:
        raise TemplateSyntaxError, '%s requires at least 3 tokens' % tokens[0]
    return NavitemNode(*tokens[1:])

class NavitemNode(Node):
    def __init__(self, *args):
        self._viewname = args[0]
        self._title = args[1]
        self._exact = 'exact' in args[2:]

    def render(self, context):
        viewname = Variable(self._viewname).resolve(context)
        title = Variable(self._title).resolve(context)
        if viewname.startswith('/'):
            urlbase = viewname
        else:
            urlbase = reverse(viewname)

        request_path = context['request_path']

        if self._exact:
            active = (request_path == urlbase)
        else:
            active = request_path.startswith(urlbase)
        if active:
            res = '<li class="active">'
        else:
            res = '<li>'
        res += '<a href="%s">%s</a></li>' % (urlbase, title)
        return res


### timeago

@register.tag('timeago')
def timeago(parser, token):
    """{% timeago <timestamp> %}"""
    tokens = token.contents.split()
    if len(tokens) != 2:
        raise TemplateSyntaxError, '%s requires 2 tokens' % tokens[0]
    return TimeagoNode(tokens[1])

class TimeagoNode(Node):
    def __init__(self, timestamp_varname):
        self._timestamp_varname = timestamp_varname

    def render(self, context):
        tv = Variable(self._timestamp_varname)
        ts = tv.resolve(context)

        # Try to set time zone information.
        if settings.TIME_ZONE and not settings.USE_TZ:
            try:
                tz = pytz.timezone(settings.TIME_ZONE)
                ts = tz.localize(ts)
            except pytz.UnknownTimeZoneError:
                pass

        iso = ts.isoformat()
        alt = timezone.localtime(ts).strftime("%A, %B %d, %Y %I:%M%p")
        return '<abbr class="timeago" title="%s">%s</abbr>' % (iso, alt)


### temperature

@register.tag('temperature')
def volumetag(parser, token):
    """{% temperature <temp_c> %}"""
    tokens = token.contents.split()
    if len(tokens) < 2:
        raise TemplateSyntaxError, '%s requires at least 2 tokens' % tokens[0]
    return TemperatureNode(tokens[1])

class TemperatureNode(Node):
    TEMPLATE = "%(amount)s&deg; %(unit)s"

    def __init__(self, varname):
{'fraction_non_alphanumeric': 0.11447949850991676, 'fraction_numerical': 0.004521631898057754, 'mean_word_length': 3.2721685689201054, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3095136', 'n_tokens_mistral': 3070, 'n_tokens_neox': 2929, 'n_words': 853}