Added config file and ralias command
authorDmitriy Morozov <morozov@cs.duke.edu>
Fri, 13 Jun 2008 14:55:43 -0400
changeset 7 d0e3cd42a3af
parent 6 3f2c0ca0812a
child 8 0c241cd7c989
Added config file and ralias command
alexandria.py
--- a/alexandria.py	Sat May 24 06:23:33 2008 -0400
+++ b/alexandria.py	Fri Jun 13 14:55:43 2008 -0400
@@ -4,6 +4,7 @@
 import hashlib
 import terminal
 from optparse import OptionParser
+from ConfigParser import SafeConfigParser
 from models import Author, Paper, Tag, AuthorNickname, initDatabase, session, asc
 
 db_filename = 'alexandria.db'
@@ -34,7 +35,7 @@
 def add(args, options):
     path = args[0]
     if not os.path.exists(path):
-        print _colorize_string('error', "Path %s does not exist. Cannot add paper" % path)
+        print _colorize_string('error', "Path %s does not exist. Cannot add paper." % path)
         return
 
     m = hashlib.md5()
@@ -43,6 +44,9 @@
     fd.close()
 
     path = os.path.abspath(path)
+    commonpath = os.path.commonprefix([options.commonpath, path])
+    path = path[len(commonpath):]
+    path = path.strip('/')
     p = Paper.get_by(path = path) or Paper.get_by(md5 = m.hexdigest())
     if p is not None:
         print _colorize_string('error', "Paper already exists, use update")
@@ -68,7 +72,7 @@
     if not p: return
     if len(args) > 1:   viewer = args[1]
     else:               viewer = default_viewer
-    os.system('%s %s' % (viewer, p.path))
+    os.system('%s %s' % (viewer, os.path.join(options.commonpath, p.path.strip('/'))))
 
 def remove(args, options):
     if len(args) < 1: return
@@ -111,14 +115,7 @@
         an.author = a
         session.flush()
 
-    print "Nicknames:"
-    for a in Author.query.all():
-        if len(a.nicknames) > 0:
-            print '  ' + a.name + ':',
-            for an in a.nicknames[:-1]:
-                print an.name + ',',
-            #print '%s: %s' % (a.nicknames[-1], a.name)
-            print a.nicknames[-1]
+    _show_nicknames()
 
 def labels(args, options):
     if len(args) >= 2:
@@ -135,6 +132,17 @@
         print '  (%d) %s' % (len(t.papers), t.name)
     session.flush()
 
+def ralias(args, options):
+    if len(args) == 0:
+        print _colorize_string('error', 'Need alias to remove as an argument.')
+
+    an = AuthorNickname.get_by(name = unicode(args[0]))
+    if an:
+        an.delete()
+        session.flush()
+
+    _show_nicknames()
+
 def _set_options(p, options, required = []):
     title = options.title or ('title' in required) and raw_input("Enter title: ")
     if title:
@@ -160,6 +168,16 @@
             else:  a = Author.get_by_or_init(name = author)
             a.papers.append(p)
 
+def _show_nicknames():
+    print "Nicknames:"
+    for a in Author.query.all():
+        if len(a.nicknames) > 0:
+            print '  ' + a.name + ':',
+            for an in a.nicknames[:-1]:
+                print an.name + ',',
+            #print '%s: %s' % (a.nicknames[-1], a.name)
+            print a.nicknames[-1]
+
 def _sort_authors(authors):
     authors.sort()          # FIXME: deal with firstname lastname issues
 
@@ -187,7 +205,8 @@
             (update,    'update paper by hash'),
             (view,      'view paper by hash'),
             (remove,    'remove paper by hash'),
-            (labels,    'rename and/or list labels')
+            (labels,    'rename and/or list labels'),
+            (ralias,    'remove alias'),
            ]
 
 
@@ -198,19 +217,29 @@
         func, description = cmd
         usage += '  %-10s - %s\n' % (func.__name__, description)
     
+    # Parse config
+    config = SafeConfigParser()
+    config.read([os.path.expanduser('~/.alexandria')])
+    dbpath = os.path.expanduser(config.get('paths', 'dbpath'))
+    commonpath = os.path.expanduser(config.get('paths', 'common'))
+
     # Parse options
     parser = OptionParser(usage = usage)
     parser.add_option('-a', '--author', action='append', dest='authors', help='author')
-    parser.add_option('-t', '--title', dest='title', help='title')
-    parser.add_option('-l', '--label', action='append', dest='labels', help='label')
-    parser.add_option('-s', '--hash', dest='hash', help='hash (only for list)')
+    parser.add_option('-t', '--title',  dest='title', help='title')
+    parser.add_option('-l', '--label',  action='append', dest='labels', help='label')
     parser.add_option('-D', '--database', dest='database', help='directory with the database')
     (options, args) = parser.parse_args()
     
     # Find database
-    found, path = find_database(options.database)
+    found, path = find_database(options.database or dbpath)
     initDatabase(path, not found)
     
+    # Augment options
+    options.dbpath = path
+    options.commonpath = commonpath
+    
+    # Find and execute the command
     if len(args) == 0: sys.exit()
     candidates = []
     for cmd in commands: