Update can update paper's path
authorDmitriy Morozov <morozov@cs.duke.edu>
Mon, 14 Jul 2008 13:09:22 -0400
changeset 12 71fba4d319b2
parent 11 ebb3a597f1d5
child 13 53b93fa00f44
Update can update paper's path
alexandria.py
--- a/alexandria.py	Mon Jul 14 12:56:57 2008 -0400
+++ b/alexandria.py	Mon Jul 14 13:09:22 2008 -0400
@@ -44,10 +44,7 @@
     m.update(fd.read())
     fd.close()
 
-    path = os.path.abspath(path)
-    commonpath = os.path.commonprefix([options.commonpath, path])
-    path = path[len(commonpath):]
-    path = path.strip('/')
+    path = _short_path(path, options)
     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")
@@ -64,6 +61,7 @@
 def update(args, options):
     """update paper by hash"""
     p = Paper.query.filter(Paper.md5.startswith(args[0])).one()
+    if len(args) > 1: p.path = _short_path(args[1], options)
     _set_options(p, options)
     session.flush()
     _show_paper(p)
@@ -166,10 +164,7 @@
     if len(args) == 0:
         print _colorize_string('error', 'Need path to check as an argument.')
 
-    path = os.path.abspath(args[0])
-    commonpath = os.path.commonprefix([options.commonpath, path])
-    path = path[len(commonpath):]
-    path = path.strip('/')
+    path = _short_path(args[0], options)
     paper = Paper.get_by(path = path)
 
     if paper:
@@ -177,6 +172,13 @@
     else:
         print "No such path %s found" % _colorize_string('path', path)
 
+def _short_path(path, options):
+    path = os.path.abspath(path)
+    commonpath = os.path.commonprefix([options.commonpath, path])
+    path = path[len(commonpath):]
+    path = path.strip('/')
+    return path
+
 def _set_options(p, options, required = []):
     title = options.title or ('title' in required) and raw_input("Enter title: ")
     if title: