<commit_before># coding: utf-8

import sys, os
import time
import datetime
from django.utils.translation import ugettext as _, ugettext_lazy
from modoboa.lib import parameters

def str2Time(y, M, d, h="0", m="0", s="0"):
    """Date conversion

    Returns a date and a time in seconds from the epoch.

    :return: an integer
    """
    try:
        if not M.isdigit():
            local = time.strptime("%s %s %s %s:%s:%s" % (y, M, d, h, m, s), \
                                      "%Y %b %d %H:%M:%S")
        else:
            local = time.strptime("%s %s %s %s:%s:%s" % (y, M, d, h, m, s), \
                                      "%Y %m %d %H:%M:%S")
    except ValueError:
        print >>sys.stderr, "Error: failed to convert date and time"
        return 0

    return int(time.mktime(local))

class Grapher(object):
    def __init__(self):
        self.rrd_rootdir = parameters.get_admin("RRD_ROOTDIR")
        self.img_rootdir = parameters.get_admin("IMG_ROOTDIR")

    def process(self, target, suffix, start, end, tpl):
        import rrdtool

        rrdfile = "%s/%s.rrd" % (self.rrd_rootdir, target)
        if not os.path.exists(rrdfile):
            return
        ext = "png"
        path = "%s/%s_%s_%s_%s.%s" % (self.img_rootdir, tpl['name'],
                                      target, tpl['cf'], suffix, ext)
        start = str(start)
        end = str(end)
        defs = []
        lines = []
        defs.append('COMMENT: %s %s %s %s\\c' % ("From", start, "to", end))
        defs.append('COMMENT:\\s')
        for v, d in tpl["vars"].iteritems():
            defs += [str('DEF:%s=%s:%s:%s' % (v, rrdfile, v, tpl['cf'])),
                     str('CDEF:%spm=%s,60,*' % (v, v)),
                     str('VDEF:%s_total=%s,TOTAL' % (v, v))]
            type = d.has_key("type") and d["type"] or "LINE"
            lines += [str("%s:%spm%s:%s:STACK" % (type, v, d["color"],
                                                  (d["legend"].encode("utf8")).ljust(20)))]
            lines.append('GPRINT:%s_total:%s%%7.0lf%%s\\l' % (v, _("Total").encode('utf-8')))

        params = defs + lines
        rrdtool.graph(str(path),
                      "--imgformat", "PNG",
                      "--width", tpl["width"],
                      "--height", tpl["height"],
                      "--start", start,
                      "--end", end,
                      "--vertical-label", tpl["vertlabel"].encode("utf-8"),
                      "--lower-limit", "0",
                      "--slope-mode",
                      "--units-exponent", "0",
                      "--title", tpl["title"].encode("utf-8"),
                      *params)

        if not os.path.exists(path):
            print "[graph] Impossible to create %s graph" %path

    def make_defaults(self, target, tpl):
        end = "%d" % int(time.mktime(time.localtime()))
        self.process(target, "day", "-1day", end, tpl)
        self.process(target, "week", "-1week", end, tpl)
        self.process(target, "month", "-1month", end, tpl)
        self.process(target, "year", "-1year", end, tpl)
        
        <commit_msg>Forgot this one into the previous commit :p<commit_after># coding: utf-8

import sys, os
import time
import datetime
from django.utils.translation import ugettext as _, ugettext_lazy
from modoboa.lib import parameters

def str2Time(y, M, d, h="0", m="0", s="0"):
    """Date conversion

    Returns a date and a time in seconds from the epoch.

    :return: an integer
    """
    try:
        if not M.isdigit():
            local = time.strptime("%s %s %s %s:%s:%s" % (y, M, d, h, m, s), \
                                      "%Y %b %d %H:%M:%S")
        else:
            local = time.strptime("%s %s %s %s:%s:%s" % (y, M, d, h, m, s), \
                                      "%Y %m %d %H:%M:%S")
    except ValueError:
        print >>sys.stderr, "Error: failed to convert date and time"
        return 0

    return int(time.mktime(local))

class Grapher(object):
    def __init__(self):
        self.rrd_rootdir = parameters.get_admin("RRD_ROOTDIR")
        self.img_rootdir = parameters.get_admin("IMG_ROOTDIR")

    def process(self, target, suffix, start, end, graph_tpl):
        import rrdtool

        rrdfile = "%s/%s.rrd" % (self.rrd_rootdir, target)
        if not os.path.exists(rrdfile):
            return
        ext = "png"
        path = "%s/%s_%s_%s_%s.%s" % (self.img_rootdir, graph_tpl.display_name,
                                      target, graph_tpl.cf, suffix, ext)
        start = str(start)
        end = str(end)
        defs = []
        lines = []
        defs.append('COMMENT: %s %s %s %s\\c' % ("From", start, "to", end))
        defs.append('COMMENT:\\s')
        for v, d in graph_tpl.vars.iteritems():
            defs += [str('DEF:%s=%s:%s:%s' % (v, rrdfile, v, graph_tpl.cf)),
                     str('CDEF:%spm=%s,60,*' % (v, v)),
                     str('VDEF:%s_total=%s,TOTAL' % (v, v))]
            type = d.has_key("type") and d["type"] or "LINE"
            lines += [str("%s:%spm%s:%s:STACK" % (type, v, d["color"],
                                                  (d["legend"].encode("utf8")).ljust(20)))]
            lines.append('GPRINT:%s_total:%s%%7.0lf%%s\\l' % (v, _("Total").encode('utf-8')))

        params = defs + lines
        rrdtool.graph(str(path),
                      "--imgformat", "PNG",
                      "--width", str(graph_tpl.width),
                      "--height", str(graph_tpl.height),
                      "--start", start,
                      "--end", end,
                      "--vertical-label", graph_tpl.vertlabel.encode("utf-8"),
                      "--lower-limit", "0",
                      "--slope-mode",
                      "--units-exponent", "0",
                      "--title", graph_tpl.title.encode("utf-8"),
                      *params)

        if not os.path.exists(path):
            print "[graph] Impossible to create %s graph" %path

    def make_defaults(self, target, graph_tpl):
        end = "%d" % int(time.mktime(time.localtime()))
        self.process(target, "day", "-1day", end, graph_tpl)
        self.process(target, "week", "-1week", end, graph_tpl)
        self.process(target, "month", "-1month", end, graph_tpl)
        self.process(target, "year", "-1year", end, graph_tpl)
        
        
{'fraction_non_alphanumeric': 0.17657232704402515, 'fraction_numerical': 0.006918238993710692, 'mean_word_length': 1.693056731583404, '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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6701201', 'n_tokens_mistral': 2100, 'n_tokens_neox': 2058, 'n_words': 483}