From 1c8112d265e0df9badf0c4d9edf11a439f41b4a1 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 15 May 2016 20:26:31 +0300 Subject: [PATCH] Search and print a list of books by title, series, archive or file name --- docs-ru/command_line.rst | 16 ++++++++++++++++ docs/command_line.rst | 16 ++++++++++++++++ scripts/ml-search.py | 30 ++++++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/docs-ru/command_line.rst b/docs-ru/command_line.rst index 9a383c4..77480b4 100644 --- a/docs-ru/command_line.rst +++ b/docs-ru/command_line.rst @@ -93,6 +93,22 @@ ml-search.py Искать и печатать список авторов, чья фамилия начинается на "друг" и имя начинается на "в", без учёта регистра. +Поиск книг +^^^^^^^^^^ + +Использование:: + + ml-search.py [-i] [-I] [-t ...] books [-t title] [-s series] [-a archive] [-f file] + +Искать и печатать список книг по заголовку, серии, архиву, имени файла. + +Опции:: + + -t, --title заголовок Искать по заголовку + -s, --series серия Искать по серии + -a, --archive архив Искать по имени архива (zip-файла) + -f, --file файл Искать по имени файла (без расширения) + Поиск расширений ^^^^^^^^^^^^^^^^ diff --git a/docs/command_line.rst b/docs/command_line.rst index 4b30eb6..1298111 100644 --- a/docs/command_line.rst +++ b/docs/command_line.rst @@ -91,6 +91,22 @@ Example:: Search and print a list of authors whose surname starts with "duck", and name starts with "mack", case insensitive. +Book search +^^^^^^^^^^^ + +Usage:: + + ml-search.py [-i] [-I] [-t ...] books [-t title] [-s series] [-a archive] [-f file] + +Search and print a list of books by title, series, archive or file name. + +Options:: + + -t, --title title Search by title + -s, --series series Search by series + -a, --archive archive Search by archive (zip file) + -f, --file file Search by file name (without extension) + Extension search ^^^^^^^^^^^^^^^^ diff --git a/scripts/ml-search.py b/scripts/ml-search.py index ec907e8..6f71d0a 100755 --- a/scripts/ml-search.py +++ b/scripts/ml-search.py @@ -6,8 +6,8 @@ from sqlobject.sqlbuilder import CONCAT from m_lib.defenc import default_encoding from m_librarian.db import Author, open_db -from m_librarian.search import search_authors, search_extensions, \ - search_genres, search_languages +from m_librarian.search import search_authors, search_books, \ + search_extensions, search_genres, search_languages from m_librarian.translations import translations _ = translations.ugettext @@ -51,6 +51,25 @@ def _search_authors(case_sensitive, args): (u"(%s: %d)" % (_('books'), author.count)).encode(default_encoding) +def _search_books(case_sensitive, args): + values = {} + for column in 'title', 'series', 'archive', 'file': + value = getattr(args, column) + if value: + values[column] = unicode(value, default_encoding) + if case_sensitive is None: + case_sensitive = _guess_case_sensitivity(values) + for book in search_books(args.search_type, case_sensitive, values, + orderBy='title'): + print book.title.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 + + def _search_extensions(case_sensitive, args): if args.name: values = {'name': args.name} @@ -117,6 +136,13 @@ if __name__ == '__main__': parser.add_argument('fullname', nargs='?', help='search by full name') parser.set_defaults(func=_search_authors) + parser = subparsers.add_parser('books', help='Search books') + parser.add_argument('-t', '--title', help='search by title') + 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.set_defaults(func=_search_books) + parser = subparsers.add_parser('ext', help='Search extensions') parser.add_argument('name', nargs='?', help='search by name') parser.set_defaults(func=_search_extensions) -- 2.39.2