Added count command
authorDmitriy Morozov <dmitriy@mrzv.org>
Wed, 03 Sep 2008 14:25:49 -0700
changeset 14 53e32c952ebd
parent 13 53b93fa00f44
child 15 73c27c2afa65
Added count command
alexandria.py
--- a/alexandria.py	Tue Jul 15 08:49:46 2008 -0400
+++ b/alexandria.py	Wed Sep 03 14:25:49 2008 -0700
@@ -116,6 +116,29 @@
         _show_paper(p)
         print
 
+def count(args, options):
+    """returns the count of papers matching given criteria in the database"""
+    papers = Paper.query
+
+    # Refactor with _set_options()
+    for label_with_commas in (options.labels or []):
+        labels = label_with_commas.split(',')
+        for label in labels:
+            label = unicode(label.strip())
+            label = label.replace('*', '%')             # allow for glob style-pattern
+            papers = papers.filter(Paper.tags.any(Tag.name.like(label)))
+
+    for author_with_commas in (options.authors or []):
+        authors = author_with_commas.split(',')
+        for author in authors:
+            author = unicode(author.strip())
+            an = AuthorNickname.get_by(name = author)
+            if an: a = an.author.name
+            else:  a = author.replace('*', '%')
+            papers = papers.filter(Paper.authors.any(Author.name.like(a)))
+
+    print "Count:", papers.count()
+
 def authors(args, options):
     """List authors, add, and remove aliases
   ax authors                 - list authors
@@ -265,7 +288,7 @@
         return None
 
 
-commands = [add, list, authors, update, view, remove, labels, info]
+commands = [add, list, authors, update, view, remove, labels, info, count]
 
 
 if __name__ == "__main__":