TimeTracker/TimeTrackEntry.py

Fri, 19 Oct 2012 15:54:12 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 19 Oct 2012 15:54:12 +0200
changeset 2
058c6a316ca8
parent 1
a0beac325e5a
child 3
ce9309868f8a
permissions
-rw-r--r--

Implemented the next step.

# -*- coding: utf-8 -*-

# Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the time track entry class.
"""

from PyQt4.QtCore import Qt, QDateTime


class TimeTrackEntry(object):
    """
    Class implementing the time track entry.
    """
    LineMarker = "Entry: "
    Separator = "@@"
    
    def __init__(self):
        """
        Constructor
        """
        self.__entryMembersCount = 4
        self.__startDateTime = QDateTime()      # start date and time
        self.__duration = 0                     # duration in minutes
        self.__task = ""                        # task specification
        self.__comment = ""                     # comment string
        self.__valid = False                    # flag for a valid entry
        
        self.__continueDateTime = QDateTime()
        self.__paused = False
    
    def __lt__(self, other):
        """
        Special method implementing the less than function.
        
        @param other reference to the other object (TimeTrackEntry)
        """
        return self.__startDateTime < other.getStartDateTime()
    
    def toString(self):
        """
        Public method to get a string representation of the entry.
        
        @return string representation of the entry (string)
        """
        if self.__valid:
            dataLine = TimeTrackEntry.Separator.join([
                self.__startDateTime.toString(Qt.ISODate),
                str(self.__duration),
                self.__task,
                self.__comment,
            ])
            return "{0}{1}".format(TimeTrackEntry.LineMarker, dataLine)
        else:
            return ""
    
    def fromString(self, line):
        """
        Public method to populate the entry from the given string.
        
        @param line stringified entry data as generated by toString() (string)
        @return flag indicating valid data
        """
        if not line.startswith(TimeTrackEntry.LineMarker):
            return False
        
        line = line.replace(TimeTrackEntry.LineMarker, "")
        dataList = line.split(TimeTrackEntry.Separator)
        if len(dataList) != self.__entryMembersCount:
            return False
        
        dt = QDateTime.fromString(dataList[0], Qt.ISODate)
        if not dt.isValid():
            return False
        self.__startDateTime = dt
        
        try:
            dt = int(dataList[1])
        except ValueError:
            return False
        self.__duration = dt
        
        self.__task = dataList[2]
        self.__comment = dataList[3]
        
        self.__valid = True
        return True
    
    def isValid(self):
        """
        Public method to check the validity of the entry.
        
        @return validity of the entry (boolean)
        """
        return self.__valid
    
    def start(self):
        """
        Public method to set the start time of this entry.
        """
        self.__startDateTime = QDateTime.currentDateTime()
        self.__continueDateTime = QDateTime(self.__startDateTime)
    
    def stop(self):
        """
        Public method to stop this entry.
        """
        if not self.__paused:
            minutes = self.__calculateDuration(
                self.__continueDateTime, QDateTime.currentDateTime())
            self.__duration += minutes
        
        if self.__duration >= 2:
            self.__valid = True
        else:
            self.__duration = 0
            self.__valid = False
    
    def pause(self):
        """
        Public method to pause the entry.
        """
        if not self.__paused:
            minutes = self.__calculateDuration(
                self.__continueDateTime, QDateTime.currentDateTime())
            self.__duration += minutes
            self.__paused = True
    
    def continue_(self):
        """
        Public method to continue the entry.
        """
        if self.__paused:
            self.__continueDateTime = QDateTime(self.__startDateTime)
            self.__paused = False
    
    def isPaused(self):
        """
        Public method to check for a paused state.
        
        @return flag indicating a paused state (boolean)
        """
        return self.__paused
    
    def __calculateDuration(self, start, stop):
        """
        Private method to calculate the duration in minutes.
        
        @param start start date and time (QDateTime)
        @param stop end date and time (QDateTime)
        @return duration in minutes (int)
        """
        secs = start.secsTo(stop)
        minutes = secs // 60
        secsRemaining = secs % 60
        if secsRemaining >= 30:
            minutes += 1
        
        return minutes
    
    def setTask(self, description):
        """
        Public method to set the task description.
        
        @param description task description (string)
        """
        self.__task = description.replace("\r\n", " ")\
                                 .replace("\n", " ")\
                                 .replace("\r", " ")
    
    def setComment(self, comment):
        """
        Public method to set a comment.
        
        @param comment comment to set (string)
        """
        self.__comment = comment.replace("\r\n", " ")\
                                .replace("\n", " ")\
                                .replace("\r", " ")
    
    def getEntryData(self):
        """
        Public method to get the entry data.
        
        @return entry data as a tuple of start date (string), start time (string),
            duration (integer), task (string), comment (string) and flag indicating
            a paused state (boolean)
        """
        return (
            self.__startDateTime.toString("yyyy-MM-dd"),
            self.__startDateTime.toString("hh:mm"),
            self.__duration,
            self.__task,
            self.__comment,
            self.__paused,
        )
    
    def getStartDateTime(self):
        """
        Public method to get the start date and time.
        
        @return start date and time (QDateTime)
        """
        return self.__startDateTime

eric ide

mercurial