def run(self): karm = KArm() bc = Basecamp(self.url, self.user, self.password) counter = 0 # add projects as todo items to karm projects = bc.getProjects() todolists = bc.getTodoLists() if self.active_projects: active_ids = [todolist.project_id for todolist in todolists] projects = [project for project in projects if project.id in active_ids] for project in projects: karm.add(Todo( str(project.id), project.name, x_kde_ktimetracker_bctype='project' )) if self.cmdutil.debug: print "Added project: <%s>" % project.name # add todolists with their todo items to karm for todolist in todolists: project_id = str(todolist.project_id) task = karm.todos[project_id].add(Todo( str(todolist.id), todolist.name, related_to=project_id, x_kde_ktimetracker_bctype='todolist' )) if self.cmdutil.debug: print " Added todo list: <%s>" % todolist.name # add todo items eventually for todo in todolist.todo_items: hours = 0.0 if self.fetch_time: for time_entry in bc.getEntriesForTodoItem(todo.id): hours += float(time_entry.hours) task.add(Todo( str(todo.id), todo.content, related_to=str(todolist.id), x_kde_ktimetracker_bctype='todoitem', x_kde_ktimetracker_totaltasktime=str(int(hours * 60.0)) )) if self.cmdutil.debug: print " Added todo item: <%s>" % todo.content counter += 1 # dump result karm.dump(self.storage) if self.cmdutil.debug: print 'Added %d todo items...' % counter print 'Done...'
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..."
def run(self): karm = KArm() karm.load(self.storage) bc = Basecamp(self.url, self.user, self.password) # flag that will show at the end whether something has been # updated eventually and if we really need to dump new storage updated = False # update projects projects = bc.getProjects() todolists = bc.getTodoLists() if self.active_projects: active_ids = [todolist.project_id for todolist in todolists] projects = [project for project in projects if project.id in active_ids] # firstly update projects for project in projects: task = karm.todos.get(str(project.id), None) if task is None: karm.add(Todo( str(project.id), project.name, x_kde_ktimetracker_bctype='project' )) updated = True if self.cmdutil.debug: print "Added project: <%s> [%d]" % (project.name, project.id) else: if task.summary != project.name: task.summary = project.name updated = True if self.cmdutil.debug: print "Updated project: <%s> [%d]" % (project.name, project.id) # then update todo lists with their todo items for todolist in todolists: project_id = str(todolist.project_id) task = karm.todos[project_id].todos.get(str(todolist.id), None) if task is None: task = karm.todos[project_id].add(Todo( str(todolist.id), todolist.name, related_to=project_id, x_kde_ktimetracker_bctype='todolist' )) updated = True if self.cmdutil.debug: print "Added todo list to project <%s> [%d]: <%s> [%d]" % ( karm.todos[project_id].summary, todolist.project_id, todolist.name, todolist.id ) else: if task.summary != todolist.name: task.summary = todolist.name updated = True if self.cmdutil.debug: print "Updated todo list: <%s> [%d] " \ "(project <%s> [%d])" % ( todolist.name, todolist.id, karm.todos[project_id].summary, todolist.project_id ) for todo in todolist.todo_items: new_todo = False subtask = task.todos.get(str(todo.id), None) if subtask is None: new_todo = True subtask = task.add(Todo( str(todo.id), todo.content, related_to=task.uid, x_kde_ktimetracker_bctype='todoitem' )) updated = True if self.cmdutil.debug: print "Added todo item <%s> to <%s> [%d] todolist " \ "(project <%s> [%d])" % ( todo.content, todolist.name, todolist.id, karm.todos[project_id].summary, todolist.project_id ) else: # TODO: todo.content string should be escaped # before comparison if subtask.summary != todo.content: subtask.summary = todo.content updated = True if self.cmdutil.debug: print "Updated todoitem #%d " \ "from <%s> todolist [%d] " \ "(<%s> project [%d])" % ( todolist.todo_items.index(todo), todolist.name, todolist.id, karm.todos[project_id].summary, todolist.project_id ) # update todo item's time if self.update_time: if subtask.x_kde_ktimetracker_totalsessiontime is None: subtask.x_kde_ktimetracker_totalsessiontime = '0' if subtask.x_kde_ktimetracker_totaltasktime is None: subtask.x_kde_ktimetracker_totaltasktime = '0' sessionMinutes = int(subtask.x_kde_ktimetracker_totalsessiontime) totalMinutes = int(subtask.x_kde_ktimetracker_totaltasktime) # fetch total todo item's time from basecamp hours = 0.0 for time_entry in bc.getEntriesForTodoItem(todo.id): hours += float(time_entry.hours) if int(hours * 60.0) != totalMinutes - sessionMinutes: subtask.x_kde_ktimetracker_totaltasktime = str( sessionMinutes + int(hours * 60.0) ) updated = True if new_todo and self.cmdutil.debug: print "Added total time to a newly created <%s> " \ "todo item to <%s> todo list [%d] " \ "(project <%s> [%d])" % ( todo.content, todolist.name, todolist.id, karm.todos[project_id].summary, todolist.project_id ) else: if self.cmdutil.debug: print "Updated todoitem's <%s> total time " \ "(todolist <%s> [%d], " \ "project <%s> [%d])" % ( todo.content, todolist.name, todolist.id, karm.todos[project_id].summary, todolist.project_id ) # dump result if something has changed if updated: karm.dump() if self.cmdutil.debug: print "Done..." elif self.cmdutil.debug: print "Nothing has changed..."