Commits

Virgil Dupras committed b5922cc

Added support for GTimeLog imports.

  • Participants
  • Parent commits b2ed30b

Comments (0)

Files changed (3)

 
 .DS_Store
 *.pyc
+env
 envconf.json
 openhs.db
 data/charts

File templates/addtimelog.htm

 </p>
 <form action="../submitcsvlog/" method="post">
 <select name="csvprofile">
-    <option value="kronox" selected="selected">KronoX</option>
+    <option value="gtimelog" selected="selected">GTimeLog</option>
+    <option value="kronox">KronoX</option>
     <option value="timetracker">Time Tracker Mac</option>
 </select>
 <br/>

File view/account.py

     template = tmplenv.get_template('msg.htm')
     return template.render(msg=msg)
 
+def parse_csv_log(csvlog, parsefunc):
+    dialect = csv.Sniffer().sniff(csvlog)
+    reader = csv.reader(csvlog.split('\n'), dialect)
+    lines = [line for line in reader if line]
+    logs = []
+    for line in lines:
+        try:
+            logs.append(parsefunc(line))
+        except Exception:
+            pass # ignore
+    return logs
+
 def parse_timetracker_line(line):
     TIMEFMT = '%Y-%m-%d %H:%M:%S'
     start_time = datetime.strptime(line[3], TIMEFMT)
     return TimelogData(project_name=project_name, start_time=start_time, stop_time=stop_time,
         task=task, comment=comment)
 
+def parse_gtimelog_log(contents):
+    MSG_SHORTCUTS = { # (project_name, task)
+        'hsdev': ("Hardcoded Software", "Development"),
+        'support': ("Hardcoded Software", "Support"),
+        'hspaper': ("Hardcoded Software", "Paperwork"),
+        'mgdev': ("moneyGuru", "Development"),
+        'mgbug': ("moneyGuru", "Bug Hunting"),
+        'dgdev': ("dupeGuru", "Development"),
+        'dgbug': ("dupeGuru", "Bug Hunting"),
+        'pmdev': ("PdfMasher", "Development"),
+        'pmbug': ("PdfMasher", "Bug Hunting"),
+    }
+    prev_timestamp = None
+    result = []
+    TIMESTAMP_LENGTH = len('0000-00-00 00:00: ')
+    for line in contents.splitlines():
+        try:
+            timestamp = datetime.strptime(line[:TIMESTAMP_LENGTH], '%Y-%m-%d %H:%M: ')
+        except ValueError: # Not a time log, skip
+            continue
+        next_day = prev_timestamp is None or prev_timestamp.date() != timestamp.date()
+        start_time = prev_timestamp
+        stop_time = timestamp
+        prev_timestamp = timestamp
+        if next_day:
+            continue # It's the "arrived" log, it's not a real entry
+        msg = line[TIMESTAMP_LENGTH:].strip()
+        if ' - ' in msg:
+            shortcut, comment = msg.split(' - ')
+        else:
+            shortcut = msg
+            comment = ''
+        if shortcut not in MSG_SHORTCUTS:
+            continue # We ignore messages that don't fit our format
+        project_name, task = MSG_SHORTCUTS[shortcut]
+        result.append(TimelogData(project_name=project_name, start_time=start_time,
+            stop_time=stop_time, task=task, comment=comment))
+    return result
+
 class AccountView:
     @staticmethod
     def getuser(session):
     def submitcsvlog(self, csvprofile, csvlog):
         session = Session()
         dev = self.getuser(session)
-        dialect = csv.Sniffer().sniff(csvlog)
-        reader = csv.reader(csvlog.split('\n'), dialect)
-        lines = [line for line in reader if line]
-        parsefunc = parse_kronox_line if csvprofile == 'kronox' else parse_timetracker_line
-        logs = []
-        for line in lines:
-            try:
-                logs.append(parsefunc(line))
-            except Exception:
-                pass # ignore
+        if csvprofile == 'gtimelog':
+            logs = parse_gtimelog_log(csvlog)
+        else:
+            parsefunc = parse_kronox_line if csvprofile == 'kronox' else parse_timetracker_line
+            logs = parse_csv_log(csvlog, parsefunc)
         added, ignored = Timelog.add(session, dev, logs)
         Cache.clear(session)
         msg = "{0} logs added, {1} ignored.".format(added, ignored)