def add(calendar_name, text, priority, color): setup_color(color) text = " ".join(text) if priority is not None: priority = Priority(priority.upper()) else: prefix_match = PRIORITY_PREFIX_RE.match(text) if prefix_match: priority, text = Task.parse_priority(prefix_match.group(1)), text[prefix_match.end():] else: priority = None calendar = client.get_calendar(calendar_name) try: task = Task.new_task(client, parent=calendar, summary=text) if priority is not None: task.priority = priority task.save() except Exception, e: print "Error saving event: %r" % e return
def listpri(calendar_name, priority, term, color, use_cache): setup_color(color) try: priorities = Task.parse_priority_range(priority) except ValueError: # Assume this wasn't really a priority term.insert(0, priority) priorities = Priority.__named__ calendar, task_lookup = get_tasks(calendar_name, use_cache) term = [t.lower() for t in term] for task_id in sorted_tasks(task_lookup): task = calendar.get_task(task_id) if task.status != "COMPLETED" and task.priority in priorities: search_text = task.summary.lower() if all(task.id.startswith(t) or (t[:-1] not in search_text if t.endswith('-') else t in search_text) for t in term): output_task(task_lookup, task)
def get_tasks(calendar_name, use_cache=None): """gets a calendar and tasks, and returns the tuple of both of them. Loads tasks from cache if necessary""" from_cache = cache_default if use_cache is None else use_cache if from_cache: if cache_dir is None: raise ValueError("Attempt to use cache but cache.dir is not defined in config") calendar = TaskList(client) if calendar._tasks is None: calendar._tasks = short_id.prefix_dict() tasks = calendar._tasks for filename in os.listdir(cache_dir): if filename.endswith(".ics"): with open(os.path.join(cache_dir, filename)) as f: t = Task(client, url=None, data=f.read(), parent=calendar, etag=None) task_id = t.id or (filename.replace(".ics", "")) tasks[task_id] = t else: calendar = client.get_calendar(calendar_name) tasks = calendar.get_tasks() return calendar, tasks