<commit_before># -*- coding: utf-8 -*-
#
# (c) 2014 <NAME> <<EMAIL>>
#
# See LICENSE comming with the source of 'trex' for details.
#

import re

from datetime import date, timedelta

from django.utils.encoding import force_unicode


class ZeitEntry(object):

    """
    Representation of one entry in a zeiterfassung.txt
    """

    def __init__(self, line_number, day, month, year, hours, minutes, state,
                 user, workpackage, description):
        self.line_nr = line_number
        self.day = day
        self.month = month
        self.year = year
        self.hours = hours
        self.minutes = minutes
        self.state = state
        self.user = user
        self.wp = workpackage
        self.desc = description or ""

    def add_desc(self, desc):
        self.desc = u"%s %s" % (force_unicode(self.desc), force_unicode(desc))

    def __str__(self):
        return u"%s.%s.%s %s:%s %s %s [%s] %s" % (
            self.day, self.month, self.year,
            self.hours, self.minutes,
            self.get_state(), self.get_user(),
            self.get_workpackage(), self.get_description())

    def get_duration(self):
        # requires python 2.7
        return timedelta(minutes=self.minutes, hours=self.hours).total_seconds()

    def get_date(self):
        return date(self.year, self.month, self.day)

    def get_state(self):
        return self.state

    def get_description(self):
        return self.desc

    def get_workpackage(self):
        return self.wp

    def get_user(self):
        return self.user

    def get_line_number(self):
        return self.line_nr


class Zeiterfassung(object):

    """
    Reads entries from a zeiterfassung.txt formatted iterable
    """

    zeitline = re.compile(
        "(?P<day>[0-9]{2})\.(?P<month>[0-9]{2})\.(?P<year>[0-9]{4})\s+"
        "(?P<hour>\d+)\:(?P<minutes>\d{2})h\s(?P<state>.)\s+(?P<user>\w+)\s+"
        "\[(?P<ap>[a-zA-Z0-9_\-]+)\]\s+(?P<desc>.*)"
    )

    zeitline_comment = re.compile("\s+(?P<desc>.*)")

    def __init__(self, zeiterfassung, encoding="utf-8"):
        self.zeiterfassung = zeiterfassung
        self.encoding = encoding

    def read(self):
        entry = None
        for nr, line in enumerate(self.zeiterfassung, start=1):
            zmatch = self.zeitline.match(line)
            zcmatch = self.zeitline_comment.match(line)
            if zmatch:<commit_msg>Add a ZeiterfassungEntry to_dict methods<commit_after># -*- coding: utf-8 -*-
#
# (c) 2014 <NAME> <<EMAIL>>
#
# See LICENSE comming with the source of 'trex' for details.
#

import re

from datetime import date, timedelta

from django.utils.encoding import force_unicode


class ZeitEntry(object):

    """
    Representation of one entry in a zeiterfassung.txt
    """

    def __init__(self, line_number, day, month, year, hours, minutes, state,
                 user, workpackage, description):
        self.line_nr = line_number
        self.day = day
        self.month = month
        self.year = year
        self.hours = hours
        self.minutes = minutes
        self.state = state
        self.user = user
        self.wp = workpackage
        self.desc = description or ""

    def add_desc(self, desc):
        self.desc = u"%s %s" % (force_unicode(self.desc), force_unicode(desc))

    def __str__(self):
        return u"%s.%s.%s %s:%s %s %s [%s] %s" % (
            self.day, self.month, self.year,
            self.hours, self.minutes,
            self.get_state(), self.get_user(),
            self.get_workpackage(), self.get_description())

    def get_duration(self):
        # requires python 2.7
        return timedelta(minutes=self.minutes, hours=self.hours).total_seconds()

    def get_date(self):
        return date(self.year, self.month, self.day)

    def get_state(self):
        return self.state

    def get_description(self):
        return self.desc

    def get_workpackage(self):
        return self.wp

    def get_user(self):
        return self.user

    def get_line_number(self):
        return self.line_nr

    def to_dict(self):
        return {
            "line_nr": self.get_line_number(),
            "day": self.day,
            "month": self.month,
            "year": self.year,
            "hours": self.hours,
            "minutes": self.minutes,
            "state": self.get_state(),
            "description": self.get_description(),
            "user": self.get_user(),
            "workpackage": self.get_workpackage(),
        }


class Zeiterfassung(object):

    """
    Reads entries from a zeiterfassung.txt formatted iterable
    """

    zeitline = re.compile(
        "(?P<day>[0-9]{2})\.(?P<month>[0-9]{2})\.(?P<year>[0-9]{4})\s+"
        "(?P<hour>\d+)\:(?P<minutes>\d{2})h\s(?P<state>.)\s+(?P<user>\w+)\s+"
        "\[(?P<ap>[a-zA-Z0-9_\-]+)\]\s+(?P<desc>.*)"
    )

    zeitline_comment = re.compile("\s+(?P<desc>.*)")

    def __init__(self, zeiterfassung, encoding="utf-8"):
        self.zeiterfassung = zeiterfassung
        self.encoding = encoding

    def read(self):
        entry = None
        for nr, line in enumerate(self.zeiterfassung, start=1):
            zmatch = self.zeitline.match(line)
            zcmatch = self.zeitline_comment.match(line)
            if zmatch:
{'fraction_non_alphanumeric': 0.14723809523809525, 'fraction_numerical': 0.008, 'mean_word_length': 2.2798251093066835, 'pattern_counts': {'":': 10, '<': 29, '<?xml version=': 0, '>': 29, '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': '4365396', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1701, 'n_words': 366}