def lookupCb(thing): if not thing: self.stdout.write('No thing found for %s\n' % shortid.encode('utf-8')) return self.getRootCommand()._stdio.teardown() def pre_input_hook(): readline.insert_text(display.display( thing, shortid=False, colored=False)) readline.redisplay() # Unset the hook again readline.set_pre_input_hook(None) readline.set_pre_input_hook(pre_input_hook) line = raw_input("GTD edit> ").decode('utf-8') # Remove edited line from history: # oddly, get_history_item is 1-based, # but remove_history_item is 0-based readline.remove_history_item(readline.get_current_history_length() - 1) self.getRootCommand()._stdio.setup() try: d = parse.parse(line) except ValueError, e: self.stderr.write('Could not parse line: %s\n' % log.getExceptionMessage(e)) return 3
def doLater(self, args): new = parse.parse(u" ".join(args)) if not new.has_key('start'): new['start'] = datetime.datetime.now() newThing = couch.thing_from_dict(new) server = self.getRootCommand().getServer() d = server._db.infoDB(self.getRootCommand().dbName) def infoDBCb(_): createdBy = server.getCreatedBy() if not createdBy: # it's possible we don't have a session yet, try and get # a hoodie createdBy from the database name if server._dbName.startswith('user/'): createdBy = server._dbName[5:] if createdBy: self.debug('createdBy %s' % createdBy) newThing.createdBy = createdBy # we assume it's now hoodie, so if no id is set, set a # hoodie-compatible one that starts with type if not 'id' in new: newThing.setHoodieId() self.debug('set hoodie-compatible id %s' % newThing.id) return server.save(newThing) d.addCallback(infoDBCb) def saveCb(ret): self.stdout.write('Added thing "%s" (%s)\n' % ( newThing.title.encode('utf-8'), ret['id'][::-1].encode('utf-8'))) d.addCallback(saveCb) return d
def doLater(self, args): from mushin.common import parse filter = parse.parse(" ".join(args)) self.debug('parsed filter: %r' % filter) server = self.getRootCommand().getServer() # pick the view giving us the most resolution result = [] found = False for fattribute in ['urgency', 'importance']: if filter.has_key(fattribute): found = True self.debug('viewing on %s' % fattribute) view = views.View(server._db, server._dbName, 'mushin', 'open-things-by-%s' % fattribute, couch.Thing, include_docs=True, key=filter[fattribute]) break # fall back to getting all if not found: self.debug('getting all open things') view = views.View(server._db, server._dbName, 'mushin', 'open-things', couch.Thing, include_docs=True) self.debug('calling view.queryView') d = view.queryView() def viewCb(result): result = list(result) self.debug('viewCb: %d rows returned' % len(result)) # now apply all filters in a row for attribute in ['urgency', 'importance', 'time']: if filter.has_key(attribute) and attribute != fattribute: self.debug('filtering on %s: %s' % ( attribute, filter[attribute])) result = [t for t in result if str(t[attribute]).find(str(filter[attribute])) > -1] # separate because filter has singular, Thing has plural for attribute in ['projects', 'contexts', 'statuses']: if filter.has_key(attribute) and attribute != fattribute: which = filter[attribute] self.debug('filtering on %s: %s' % ( attribute, which)) new = [] for t in result: # multiple values for an attribute should be anded match = True for w in which: targets = getattr(t, attribute, []) # FIXME: lazy matching by partial string-matching # on the str of the list if str(targets).find(w) == -1: match = False break if match: new.append(t) result = new # now filter on title if result and filter['title']: self.debug('filtering on title %s' % filter['title']) result = [t for t in result if t.title.find(filter['title']) > -1] if self.options.count: self.stdout.write('%d open things\n' % len(result)) else: display.Displayer(self.stdout).display_things(result) return 0 def viewEb(failure): self.debug('viewEb, failure %r' % failure) from twisted.web import error as tw_error if failure.check(tw_error.Error): if failure.value.status == 404: self.stderr.write( "CouchDB server doesn't have the views installed.\n" "Please read the README and push the views to the server.\n") return print dir(failure.value) return failure d.addCallback(viewCb) d.addErrback(viewEb) return d
def testWeek(self): d = parse.parse(u"R:1W") self.assertEquals(d['recurrence'], 60 * 60 * 24 * 7)