TimeTracker/TimeTracker.py

changeset 9
1c7fd3151ba2
parent 8
17d90f9425fc
child 10
64acf4452ac0
--- a/TimeTracker/TimeTracker.py	Sat Oct 20 22:01:38 2012 +0200
+++ b/TimeTracker/TimeTracker.py	Sat Oct 20 22:46:24 2012 +0200
@@ -145,6 +145,72 @@
                             """ saved.</p><p>Reason: {1}</p>""").format(
                     self.__trackerFilePath, str(err)))
     
+    def importTrackerEntries(self, fname):
+        """
+        Public slot to import tracker entries from a file.
+        
+        @param fname name of the file to import (string)
+        """
+        try:
+            f = open(fname, "r", encoding="utf-8")
+            data = f.read()
+            f.close()
+        except (IOError, OSError) as err:
+            E5MessageBox.critical(self.__ui,
+                self.trUtf8("Import Time Tracker File"),
+                self.trUtf8("""<p>The time tracker file <b>{0}</b> could not be"""
+                            """ read.</p><p>Reason: {1}</p>""").format(
+                    fname, str(err)))
+            return
+        
+        invalidCount = 0
+        duplicateCount = 0
+        entries = []
+        for line in data.splitlines():
+            entry = TimeTrackEntry(self.__plugin)
+            eid = entry.fromString(line.strip())
+            if eid > -1:
+                entries.append(entry)
+            else:
+                invalidCount += 1
+        
+        if not self.__plugin.getPreferences("AllowDuplicates"):
+            startDateTimes = [
+                entry.getStartDateTime() for entry in self.__entries.values()]
+            for entry in entries[:]:
+                if entry.getStartDateTime() in startDateTimes:
+                    entries.remove(entry)
+                    duplicateCount += 1
+        
+        if len(self.__entries.keys()):
+            nextID = max(self.__entries.keys()) + 1
+        else:
+            nextID = 0
+        for entry in entries:
+            entry.setID(nextID)
+            self.__entries[nextID] = entry
+            nextID += 1
+        
+        if invalidCount != 0 or duplicateCount != 0:
+            if invalidCount != 0 and duplicateCount != 0:
+                msg = self.tr("""<p>The time tracker file <b>{0}</b> contained"""
+                    """ %n invalid entries.""",
+                    "", invalidCount).format(fname)
+                msg += " " + self.tr("""%n duplicate entries were detected.""",
+                    "", duplicateCount)
+            elif duplicateCount != 0:
+                msg = self.tr("""<p>The time tracker file <b>{0}</b> contained"""
+                    """ %n duplicate entries.""",
+                    "", duplicateCount).format(fname)
+            elif invalidCount != 0:
+                msg = self.tr("""<p>The time tracker file <b>{0}</b> contained"""
+                    """ %n invalid entries.""",
+                    "", invalidCount).format(fname)
+            msg += " " + self.tr("""These have been ignored.""")
+            E5MessageBox.information(self.__ui,
+                self.trUtf8("Import Time Tracker File"),
+                msg)
+    
     def pauseTrackerEntry(self):
         """
         Public method to pause the current tracker entry.

eric ide

mercurial