from m_lib.defenc import default_encoding
from m_librarian.db import Author, Book, Extension, Genre, Language, open_db
+from m_librarian.download import download
from m_librarian.search import mk_search_conditions, \
search_authors, search_books, \
search_extensions, search_genres, search_languages
return False
+def print_count(count):
+ print _("Found").encode(default_encoding), ":", count
+
+
def _search_authors(case_sensitive, search_type, args):
if (args.surname or args.name or args.misc_name) and args.fullname:
sys.stderr.write(
authors = search_authors(search_type, case_sensitive, values, expressions,
orderBy=('surname', 'name', 'misc_name'))
if args.count:
- print authors.count()
+ print_count(authors.count())
return
+ count = 0
for author in authors:
- names = filter(None, (author.surname, author.name, author.misc_name))
- fullname = u' '.join(names)
- print fullname.encode(default_encoding), \
+ print author.fullname.encode(default_encoding), \
(u"(%s: %d)" % (_('books'), author.count))\
.encode(default_encoding),
- if args.details >= 1:
+ if args.verbose >= 1:
print "(id=%d)" % author.id,
print
+ count += 1
+ print_count(count)
def _search_books(case_sensitive, search_type, args):
books = search_books(search_type, case_sensitive, values, join_expressions,
orderBy=('series', 'ser_no', 'title'))
if args.count:
- print books.count()
+ print_count(books.count())
+ return
+ if args.get:
+ count = books.count()
+ if count != 1:
+ sys.stderr.write("There must be exactly 1 book for --get; "
+ "(found %d).\n" % count)
+ sys.exit(1)
+ book = books[0]
+ download(book, args.path)
return
+ count = 0
for book in books:
print book.title.encode(default_encoding),
- if args.details >= 1:
+ if args.verbose >= 1:
print "(id=%d)" % book.id,
print
- if args.details >= 1:
+ if args.verbose >= 1:
print " ", _("Author(s)").encode(default_encoding), ":",
for author in book.authors:
- names = filter(None,
- (author.surname, author.name, author.misc_name))
- fullname = u' '.join(names)
- print fullname.encode(default_encoding),
+ print author.fullname.encode(default_encoding),
print
print " ", _("Genre(s)").encode(default_encoding), ":",
for genre in book.genres:
print book.series.encode(default_encoding), \
"(%d)" % book.ser_no
- if args.details >= 2:
+ if args.verbose >= 2:
print " ", _("Date").encode(default_encoding), ":", book.date
print " ", _("Language").encode(default_encoding), ":", \
book.language.name.encode(default_encoding)
- if args.details >= 3:
+ if args.verbose >= 3:
print " ", _("Archive").encode(default_encoding), ":", book.archive
print " ", _("File").encode(default_encoding), ":", book.file
print " ", _("Extension").encode(default_encoding), ":", \
book.size, _("bytes").encode(default_encoding)
print " ", _("Deleted").encode(default_encoding), ":", \
_(str(book.deleted)).encode(default_encoding)
+ count += 1
+ print_count(count)
def _search_extensions(case_sensitive, search_type, args):
extensions = search_extensions(search_type, case_sensitive, values,
orderBy='name')
if args.count:
- print extensions.count()
+ print_count(extensions.count())
return
+ count = 0
for ext in extensions:
print ext.name.encode(default_encoding), \
(u"(%s: %d)" % (_('books'), ext.count)).encode(default_encoding),
- if args.details >= 1:
+ if args.verbose >= 1:
print "(id=%d)" % ext.id,
print
+ count += 1
+ print_count(count)
def _search_genres(case_sensitive, search_type, args):
case_sensitive = _guess_case_sensitivity(values)
genres = search_genres(search_type, case_sensitive, values, orderBy='name')
if args.count:
- print genres.count()
+ print_count(genres.count())
return
+ count = 0
for genre in genres:
names = filter(None, (genre.name, genre.title))
fullname = u' '.join(names)
print fullname.encode(default_encoding), \
(u"(%s: %d)" % (_('books'), genre.count)).encode(default_encoding),
- if args.details >= 1:
+ if args.verbose >= 1:
print "(id=%d)" % genre.id,
print
+ count += 1
+ print_count(count)
def _search_languages(case_sensitive, search_type, args):
case_sensitive = _guess_case_sensitivity(values)
else:
values = {}
- for lang in search_languages(search_type, case_sensitive, values,
- orderBy='name'):
+ languages = search_languages(search_type, case_sensitive, values,
+ orderBy='name')
+ if args.count:
+ print_count(languages.count())
+ return
+ count = 0
+ for lang in languages:
print lang.name.encode(default_encoding), \
(u"(%s: %d)" % (_('books'), lang.count)).encode(default_encoding),
- if args.details >= 1:
+ if args.verbose >= 1:
print "(id=%d)" % lang.id,
print
+ count += 1
+ print_count(count)
if __name__ == '__main__':
main_parser = argparse.ArgumentParser(description='Search')
+ main_parser.add_argument('-d', '--database', help='database URI')
main_parser.add_argument('-i', '--ignore-case', action='store_true',
help='ignore case '
'(default is to guess)')
help='search substring anywhere')
main_parser.add_argument('-f', '--full', action='store_true',
help='match the entire string')
- main_parser.add_argument('-d', '--details', action='count',
- help='output more details about books; '
- 'repeat for even more details')
main_parser.add_argument('-c', '--count', action='store_true',
help='count instead of output')
+ main_parser.add_argument('-v', '--verbose', action='count',
+ help='output more details about books; '
+ 'repeat for even more details')
subparsers = main_parser.add_subparsers(help='Commands')
parser = subparsers.add_parser('authors', help='Search authors')
parser.add_argument('-s', '--series', help='search by series')
parser.add_argument('-a', '--archive', help='search by archive (zip file)')
parser.add_argument('-f', '--file', help='search by file name')
+ parser.add_argument('-p', '--path', help='path to the library archives')
+ parser.add_argument('--get', action='store_true',
+ help='download exactly one book')
parser.add_argument('--id', help='search by database id')
parser.add_argument('--surname', help='search by author\'s surname')
parser.add_argument('--name', help='search by author\'s name')
else:
search_type = 'start'
- open_db()
+ open_db(args.database)
args.func(case_sensitive, search_type, args)