Ejemplo n.º 1
0
    def run(self):

        karm = KArm()
        karm.load(self.storage)
        bc = Basecamp(self.url, self.user, self.password)

        userId = self.getUserId(bc)

        updated = False
        # now let's go through the collected data
        # and decide what to do with each entry
        for project_id, project in karm.todos.items():
            if project.x_kde_ktimetracker_bctype != "project":
                continue

            if self.cmdutil.debug:
                print "Processing project: <%s> [%s]... " % (project.summary, project_id)
            pSessionTime = getSessionTime(project)
            if pSessionTime is not None:
                # log time for project, aka create time entry for project
                summary = raw_input(
                    "    Please, enter time entry (%s) "
                    "description for project "
                    "(<Enter> to skip comment): " % prettyTime(pSessionTime)
                )
                entry = bc.createTimeEntryForProject(
                    int(project_id), bcTime(pSessionTime), date=self.date, person_id=userId, description=summary
                )
                if self.cmdutil.debug:
                    print "    Add time to project: (%s)" % bcTime(pSessionTime)
                # update karm file
                project.x_kde_ktimetracker_totalsessiontime = "0"
                updated = True
                if self.cmdutil.debug:
                    print "    Reset project's session time to 0"

            # now loop through the project's todo lists
            for list_id, todolist in project.todos.items():
                if todolist.x_kde_ktimetracker_bctype == "todolist":
                    # go deeper to find todos
                    for todo_id, todo in todolist.todos.items():
                        if todo.x_kde_ktimetracker_bctype != "todoitem":
                            # create todo in basecamp
                            todo_id = str(bc.createTodoItem(int(list_id), content=todo.summary, notify=True).id)
                            todolist.delete(todo.uid)
                            todo.uid = todo_id
                            todolist.add(todo)
                            todo.x_kde_ktimetracker_bctype = "todoitem"
                            updated = True

                        # check for todos time entry
                        pSessionTime = getSessionTime(todo)
                        if pSessionTime is not None:
                            # log time for todo item
                            summary = raw_input(
                                "    Please, enter time entry (%s) "
                                "description for todo item <%s> "
                                "(<Enter> to skip comment): " % (prettyTime(pSessionTime), todo.summary)
                            )
                            entry = bc.createTimeEntryForTodoItem(
                                int(todo_id),
                                bcTime(pSessionTime),
                                date=self.date,
                                person_id=userId,
                                description=summary,
                            )
                            if self.cmdutil.debug:
                                print "    Added time for todo item <%s>: " "(%s)" % (
                                    bcTime(pSessionTime),
                                    todo.summary,
                                )
                            # update karm file
                            todo.x_kde_ktimetracker_totalsessiontime = "0"
                            updated = True
                            if self.cmdutil.debug:
                                print "    Reset todo's session time to 0"

                        # then check whether todo has more time entries inside
                        for entry_id, entry in todo.todos.items():
                            # log time if such exists
                            pSessionTime = getSessionTime(entry)
                            if pSessionTime is not None:
                                # log time for todo item
                                summary = entry.summary
                                dummy = bc.createTimeEntryForTodoItem(
                                    int(todo_id),
                                    bcTime(pSessionTime),
                                    date=self.date,
                                    person_id=userId,
                                    description=summary,
                                )
                                if self.cmdutil.debug:
                                    print "    Added time for todo item " "<%s>: (%s)" % (
                                        bcTime(pSessionTime),
                                        todo.summary,
                                    )
                                # update karm file
                                entry.x_kde_ktimetracker_totalsessiontime = "0"
                                updated = True

                            # delete time entry if it is checked
                            if entry.isCompleted():
                                todo.delete(entry_id)
                                updated = True

                        # delete todo itself if it is checked as done
                        if todo.isCompleted() and not todo.todos:
                            # check todo item as done in basecamp
                            if bc.completeTodoItem(int(todo_id)) and self.cmdutil.debug:
                                print "    Checked as done todo item " "[%s]:" % todo_id
                            todolist.delete(todo_id)
                            updated = True
                            if self.cmdutil.debug:
                                print "    Deleted todo item " "<%s>" % todo.summary
                else:
                    # possibly we've got time entry for project
                    # with entry comment as todo body
                    pSessionTime = getSessionTime(todolist)
                    if pSessionTime is not None:
                        # log time for project
                        summary = todolist.summary
                        entry = bc.createTimeEntryForProject(
                            int(project_id), bcTime(pSessionTime), date=self.date, person_id=userId, description=summary
                        )
                        if self.cmdutil.debug:
                            print "    Added time for project: (%s)" % bcTime(pSessionTime)
                        # update karm file
                        todolist.x_kde_ktimetracker_totalsessiontime = "0"
                        updated = True

                # after logging time delete such kind of todo
                # in case it is checked as done
                if todolist.isCompleted() and not todolist.todos:
                    project.delete(list_id)
                    updated = True
                    if self.cmdutil.debug:
                        print "    Deleted list [%s]" % list_id

            # check whether project has any lists
            # if not then delete this project from karm
            if project.isCompleted() and not project.todos:
                karm.delete(project_id)
                updated = True
                if self.cmdutil.debug:
                    print "    Deleted project [%s]" % project_id

        # dump karm storage if something has changed
        if updated:
            karm.dump()
            if self.cmdutil.debug:
                print "Done..."
        elif self.cmdutil.debug:
            print "Nothing has changed..."