--- a/TimeTracker/TimeTrackEntry.py Thu Jun 03 18:02:18 2021 +0200 +++ b/TimeTracker/TimeTrackEntry.py Fri Jun 04 16:40:54 2021 +0200 @@ -7,7 +7,7 @@ Module implementing the time track entry class. """ -from PyQt5.QtCore import Qt, QDateTime, QTime +from PyQt6.QtCore import Qt, QDateTime, QTime class TimeTrackEntry: @@ -21,7 +21,8 @@ """ Constructor - @param plugin reference to the plugin object (TimeTrackerPlugin) + @param plugin reference to the plugin object + @type TimeTrackerPlugin """ self.__plugin = plugin @@ -40,71 +41,118 @@ """ Special method implementing the less than function. - @param other reference to the other object (TimeTrackEntry) - @return flag indicating that self is less than other (boolean) + @param other reference to the other object + @type TimeTrackEntry + @return flag indicating that self is less than other + @rtype bool """ return self.__startDateTime < other.getStartDateTime() - def toString(self): + def toDict(self): """ - Public method to get a string representation of the entry. + Public method to convert the time track entry into a dictionary. - @return string representation of the entry (string) + @return dictionary containing the time track entry data + @rtype dict """ if self.__valid: - dataLine = TimeTrackEntry.Separator.join([ - str(self.__id), - self.__startDateTime.toString(Qt.ISODate), - str(self.__duration), - self.__task, - self.__comment, - ]) - return "{0}{1}".format(TimeTrackEntry.LineMarker, dataLine) + return { + "id": self.__id, + "start": self.__startDateTime.toString(Qt.DateFormat.ISODate), + "duration": self.__duration, + "task": self.__task, + "comment": self.__comment, + } else: - return "" + return {} - def fromString(self, line): + def fromDict(self, data): """ - Public method to populate the entry from the given string. + Public method to populate the time track entry from a dictionary. - @param line stringified entry data as generated by toString() (string) - @return ID of the tracker entry; -1 indicates an error (integer) + @param data dictionary containing the time track entry data + @type dict + @return ID of the tracker entry; -1 indicates an error + @rtype int """ - if not line.startswith(TimeTrackEntry.LineMarker): + if len(data) != self.__entryMembersCount: return -1 - line = line.replace(TimeTrackEntry.LineMarker, "") - dataList = line.split(TimeTrackEntry.Separator) - if len(dataList) != self.__entryMembersCount: - return -1 + self.__id = data["id"] - try: - self.__id = int(dataList[0]) - except ValueError: - return -1 - - dt = QDateTime.fromString(dataList[1], Qt.ISODate) + dt = QDateTime.fromString(data["start"], Qt.DateFormat.ISODate) if not dt.isValid(): return -1 self.__startDateTime = dt - try: - dt = int(dataList[2]) - except ValueError: - return -1 - self.__duration = dt - - self.__task = dataList[3] - self.__comment = dataList[4] + self.__duration = data["duration"] + self.__task = data["task"] + self.__comment = data["comment"] self.__valid = True return self.__id - + +## 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([ +## str(self.__id), +## self.__startDateTime.toString(Qt.DateFormat.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 ID of the tracker entry; -1 indicates an error (integer) +## """ +## if not line.startswith(TimeTrackEntry.LineMarker): +## return -1 +## +## line = line.replace(TimeTrackEntry.LineMarker, "") +## dataList = line.split(TimeTrackEntry.Separator) +## if len(dataList) != self.__entryMembersCount: +## return -1 +## +## try: +## self.__id = int(dataList[0]) +## except ValueError: +## return -1 +## +## dt = QDateTime.fromString(dataList[1], Qt.DateFormat.ISODate) +## if not dt.isValid(): +## return -1 +## self.__startDateTime = dt +## +## try: +## dt = int(dataList[2]) +## except ValueError: +## return -1 +## self.__duration = dt +## +## self.__task = dataList[3] +## self.__comment = dataList[4] +## +## self.__valid = True +## return self.__id +## def isValid(self): """ Public method to check the validity of the entry. - @return validity of the entry (boolean) + @return validity of the entry + @rtype bool """ return self.__valid @@ -152,7 +200,8 @@ """ Public method to check for a paused state. - @return flag indicating a paused state (boolean) + @return flag indicating a paused state + @rtype bool """ return self.__paused @@ -160,7 +209,8 @@ """ Private method to get the current date and time without milliseconds. - @return current date and time (QDateTime) + @return current date and time + @rtype QDateTime """ dt = QDateTime.currentDateTime() t = dt.time() @@ -172,9 +222,12 @@ """ 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) + @param start start date and time + @type QDateTime + @param stop end date and time + @type QDateTime + @return duration in minutes + @rtype int """ secs = start.secsTo(stop) minutes = secs // 60 @@ -188,7 +241,8 @@ """ Public method to get the ID of the entry. - @return ID of the entry (integer) + @return ID of the entry + @rtype int """ return self.__id @@ -196,7 +250,8 @@ """ Public method to assign an ID to the entry. - @param eid ID for the entry (integer) + @param eid ID for the entry + @type int """ self.__id = eid @@ -204,7 +259,8 @@ """ Public method to get the start date and time. - @return start date and time (QDateTime) + @return start date and time + @rtype QDateTime """ return self.__startDateTime @@ -212,7 +268,8 @@ """ Public method to set the start date and time. - @param startDateTime start date and time (QDateTime) + @param startDateTime start date and time + @type QDateTime """ if startDateTime.isValid(): self.__startDateTime = startDateTime @@ -226,7 +283,8 @@ """ Public slot to get the duration. - @return duration (integer) + @return duration + @rtype int """ return self.__duration @@ -234,7 +292,8 @@ """ Public method to set the duration. - @param duration duration in minutes (integer) + @param duration duration in minutes + @type int """ if duration >= self.__plugin.getPreferences("MinimumDuration"): self.__duration = duration @@ -248,8 +307,9 @@ """ Public method to add a duration. - @param duration duration to be added in minutes (integer). Negative - values are ignored. + @param duration duration to be added in minutes. Negative values are + ignored. + @type int """ if duration > 0: self.__duration += duration @@ -258,7 +318,8 @@ """ Public method to get the task description. - @return task description (string) + @return task description + @rtype str """ return self.__task @@ -266,7 +327,8 @@ """ Public method to set the task description. - @param description task description (string) + @param description task description + @type str """ self.__task = ( description.replace("\r\n", " ").replace("\n", " ") @@ -277,7 +339,8 @@ """ Public method to get the comment. - @return comment (string) + @return comment + @rtype str """ return self.__comment @@ -285,7 +348,8 @@ """ Public method to set a comment. - @param comment comment to set (string) + @param comment comment to set + @type str """ self.__comment = ( comment.replace("\r\n", " ").replace("\n", " ").replace("\r", " ") @@ -295,16 +359,19 @@ """ 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 entry data as a dictionary with keys 'id', 'paused', + 'start_date', 'start_time', 'duration', 'task' and 'comment' + containing the entry ID, a flag indicating a paused + state, the start date as a string, the start time as a string, + the duration, the task and a comment + @rtype dict """ - return ( - self.__id, - self.__startDateTime.toString("yyyy-MM-dd"), - self.__startDateTime.toString("hh:mm"), - self.__duration, - self.__task, - self.__comment, - self.__paused, - ) + return { + "id": self.__id, + "paused": self.__paused, + "start_date": self.__startDateTime.toString("yyyy-MM-dd"), + "start_time": self.__startDateTime.toString("hh:mm:ss"), + "duration": self.__duration, + "task": self.__task, + "comment": self.__comment, + }