Commands are dispatched from a list + added "labels" command
authorDmitriy Morozov <morozov@cs.duke.edu>
Wed, 21 May 2008 08:44:59 -0400
changeset 4 2a543aeac83d
parent 3 1626ee683a86
child 5 35552b6cdc51
Commands are dispatched from a list + added "labels" command
alexandria.py
models.py
--- a/alexandria.py	Sat May 17 04:35:06 2008 -0400
+++ b/alexandria.py	Wed May 21 08:44:59 2008 -0400
@@ -4,7 +4,7 @@
 import hashlib
 import terminal
 from optparse import OptionParser
-from models import Author, Paper, Tag, AuthorNickname, initDatabase, session
+from models import Author, Paper, Tag, AuthorNickname, initDatabase, session, asc
 
 db_filename = 'alexandria.db'
 term = terminal.TerminalController()
@@ -87,7 +87,7 @@
         labels = label_with_commas.split(',')
         for label in labels:
             label = unicode(label.strip())
-            papers = papers.filter(Paper.tags.any(name = label))
+            papers = papers.filter(Paper.tags.any(Tag.name.startswith(label)))
 
     for author_with_commas in (options.authors or []):
         authors = author_with_commas.split(',')
@@ -98,6 +98,7 @@
             else:  a = Author.get_by_or_init(name = author)
             papers = papers.filter(Paper.authors.any(name = unicode(a))) 
 
+    print
     for p in papers.all():
         _show_paper(p)
         print
@@ -118,6 +119,15 @@
             #print '%s: %s' % (a.nicknames[-1], a.name)
             print a.nicknames[-1]
 
+def labels(args, options):
+    print "Labels:"
+    for t in Tag.query.order_by(asc(Tag.name)).all():
+        if len(t.papers) == 0:                  # clean the database
+            t.delete()
+            continue
+        print '  (%d) %s' % (len(t.papers), t.name)
+    session.flush()
+
 def _set_options(p, options, required = []):
     title = options.title or ('title' in required) and raw_input("Enter title: ")
     if title:
@@ -163,15 +173,23 @@
 def _colorize_string(clr, str):
     return '%s%s%s' % (color[clr], str, color['normal'])
 
+commands = [
+            (add,       'add a paper to the database'),
+            (list,      'list papers in the database'),
+            (alias,     'add or list author nicknames'),
+            (update,    'update paper by hash'),
+            (view,      'view paper by hash'),
+            (remove,    'remove paper by hash'),
+            (labels,    'list labels')
+           ]
+
+
 if __name__ == "__main__":
     usage =  '%s COMMAND OPTIONS\n' % sys.argv[0]
     usage += 'Commands:\n'
-    usage += '  add        - add a paper to the database\n'
-    usage += '  list       - list papers in the database\n'
-    usage += '  alias      - add or list author nicknames\n'
-    usage += '  update     - update paper by hash\n'
-    usage += '  view       - view paper by hash\n'
-    usage += '  remove     - remove paper by hash'
+    for cmd in commands:
+        func, description = cmd
+        usage += '  %-10s - %s\n' % (func.__name__, description)
     
     # Parse options
     parser = OptionParser(usage = usage)
@@ -187,15 +205,7 @@
     initDatabase(path, not found)
     
     if len(args) == 0: sys.exit()
-    if 'add'.startswith(args[0]):
-        add(args[1:], options)
-    elif 'list'.startswith(args[0]):
-        list(args[1:], options)
-    elif 'alias'.startswith(args[0]):
-        alias(args[1:], options)
-    elif 'update'.startswith(args[0]):
-        update(args[1:], options)
-    elif 'view'.startswith(args[0]):
-        view(args[1:], options)
-    elif 'remove'.startswith(args[0]):
-        remove(args[1:], options)
+    for cmd in commands:
+        func, description = cmd
+        if func.__name__.startswith(args[0]):
+            func(args[1:], options)
--- a/models.py	Sat May 17 04:35:06 2008 -0400
+++ b/models.py	Wed May 21 08:44:59 2008 -0400
@@ -1,4 +1,5 @@
 from elixir import *
+from sqlalchemy import asc
 import os.path
 
 class Author(Entity):