--- a/alexandria.py Mon Apr 28 17:23:09 2008 -0400
+++ b/alexandria.py Wed Apr 30 10:41:18 2008 -0400
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import os, sys, os.path
+import hashlib
from optparse import OptionParser
from models import Author, Paper, Tag, AuthorNickname, initDatabase, session
@@ -21,17 +22,30 @@
return (True, os.path.join(directory, db_filename))
def add(args, options):
- p = Paper(title = unicode(options.title))
+ path = args[0]
+ if not os.path.exists(path):
+ print "Path %s does not exist. Cannot add paper"
+ return
+
+ path = os.path.abspath(path)
+ m = hashlib.md5()
+ fd = open(path, 'r')
+ m.update(fd.read())
+ fd.close()
+ p = Paper(title = unicode(options.title), path = path, md5 = m.hexdigest())
for label in options.labels:
t = Tag.get_by_or_init(name = unicode(label))
t.papers.append(p)
- for author in options.authors:
- an = AuthorNickname.get_by(name = unicode(author))
- if an: a = an.author
- else: a = Author.get_by_or_init(name = unicode(author))
- a.papers.append(p)
+ for author_with_commas in options.authors:
+ authors = author_with_commas.split(',')
+ for author in authors:
+ author = author.strip()
+ an = AuthorNickname.get_by(name = unicode(author))
+ if an: a = an.author
+ else: a = Author.get_by_or_init(name = unicode(author))
+ a.papers.append(p)
session.flush()
_show_paper(p)
@@ -53,15 +67,21 @@
for an in AuthorNickname.query.all():
print ' %s: %s' % (an.name, an.author.name)
+def _sort_authors(authors):
+ authors.sort() # FIXME: deal with firstname lastname issues
+
def _show_paper(paper):
print paper.title
- for author in paper.authors[:-1]:
+ authors = [str(a) for a in paper.authors]
+ _sort_authors(authors)
+ for author in authors[:-1]:
print '%s,' % author,
- print '%s' % paper.authors[-1]
+ print '%s' % authors[-1]
print 'Labels:',
for tag in paper.tags:
print tag,
print
+ print "Path: %s" % paper.path
if __name__ == "__main__":
usage = '%s COMMAND OPTIONS\n' % sys.argv[0]
@@ -82,6 +102,7 @@
found, path = find_database(options.database)
initDatabase(path, not found)
+ if len(args) == 0: sys.exit()
if args[0] == 'add':
add(args[1:], options)
elif args[0] == 'list':
--- a/models.py Mon Apr 28 17:23:09 2008 -0400
+++ b/models.py Wed Apr 30 10:41:18 2008 -0400
@@ -21,6 +21,7 @@
tags = ManyToMany('Tag')
path = Field(Text)
+ md5 = Field(Text(64))
def __repr__(self): return self.title