From 6bd9bb41bc381bb2b30290f0135e96c5ff0911c4 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 29 May 2016 21:13:47 +0300 Subject: [PATCH] Output count of found objects --- docs-ru/command_line.rst | 4 +++- docs/command_line.rst | 3 ++- scripts/ml-search.py | 35 +++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/docs-ru/command_line.rst b/docs-ru/command_line.rst index a74b45b..8a07d15 100644 --- a/docs-ru/command_line.rst +++ b/docs-ru/command_line.rst @@ -32,7 +32,7 @@ ml-search.py Использование:: - ml-search.py [-i] [-I] [-t] [-s] [-f] [-d] ... + ml-search.py [-i] [-I] [-t] [-s] [-f] [-d] [-c] ... Программа выполняет поиск по базе данных и показывает список результатов. В настоящее время может искать только в списке авторов. @@ -48,6 +48,8 @@ ml-search.py -d, --details Выводить больше информации о найденных объектах; повторите эту опцию несколько раз, чтобы получить ещё больше деталей + -c, --count Выводить число найденных объектов, а не сами + объекты Опции ``-i/-I`` не могут использованы одновременно, поскольку означают противоположные команды. В случае, если ни одна из них не использована, diff --git a/docs/command_line.rst b/docs/command_line.rst index 0fab06b..53fe926 100644 --- a/docs/command_line.rst +++ b/docs/command_line.rst @@ -31,7 +31,7 @@ ml-search.py Usage:: - ml-search.py [-i] [-I] [-t] [-s] [-f] [-d] ... + ml-search.py [-i] [-I] [-t] [-s] [-f] [-d] [-c] ... Search through the database and display results. Currently can only search authors by name. @@ -46,6 +46,7 @@ Global options:: -f, --full search type: match the full string -d, --details Output more details about found objects; repeat for even more details + -c, --count Output count of found objects Options ``-i/-I`` cannot be used together as they are the opposite. In case none of them are used the program guesses case-sensitivity by diff --git a/scripts/ml-search.py b/scripts/ml-search.py index 8c389a6..861edbc 100755 --- a/scripts/ml-search.py +++ b/scripts/ml-search.py @@ -48,9 +48,12 @@ def _search_authors(case_sensitive, search_type, args): )) if case_sensitive is None: case_sensitive = _guess_case_sensitivity(values) - for author in search_authors(search_type, case_sensitive, values, - expressions, - orderBy=('surname', 'name', 'misc_name')): + authors = search_authors(search_type, case_sensitive, values, expressions, + orderBy=('surname', 'name', 'misc_name')) + if args.count: + print authors.count() + return + for author in authors: names = filter(None, (author.surname, author.name, author.misc_name)) fullname = u' '.join(names) print fullname.encode(default_encoding), \ @@ -111,9 +114,12 @@ def _search_books(case_sensitive, search_type, args): Language, search_type, case_sensitive, {'name': args.lang}) join_expressions.extend(conditions) - for book in search_books(search_type, case_sensitive, values, - join_expressions, - orderBy=('series', 'ser_no', 'title')): + books = search_books(search_type, case_sensitive, values, join_expressions, + orderBy=('series', 'ser_no', 'title')) + if args.count: + print books.count() + return + for book in books: print book.title.encode(default_encoding), if args.details >= 1: print "(id=%d)" % book.id, @@ -158,8 +164,12 @@ def _search_extensions(case_sensitive, search_type, args): case_sensitive = _guess_case_sensitivity(values) else: values = {} - for ext in search_extensions(search_type, case_sensitive, values, - orderBy='name'): + extensions = search_extensions(search_type, case_sensitive, values, + orderBy='name') + if args.count: + print extensions.count() + return + for ext in extensions: print ext.name.encode(default_encoding), \ (u"(%s: %d)" % (_('books'), ext.count)).encode(default_encoding), if args.details >= 1: @@ -171,8 +181,11 @@ def _search_genres(case_sensitive, search_type, args): values = _get_values(args, 'name', 'title') if case_sensitive is None: case_sensitive = _guess_case_sensitivity(values) - for genre in search_genres(search_type, case_sensitive, values, - orderBy='name'): + genres = search_genres(search_type, case_sensitive, values, orderBy='name') + if args.count: + print genres.count() + return + for genre in genres: names = filter(None, (genre.name, genre.title)) fullname = u' '.join(names) print fullname.encode(default_encoding), \ @@ -215,6 +228,8 @@ if __name__ == '__main__': 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') subparsers = main_parser.add_subparsers(help='Commands') parser = subparsers.add_parser('authors', help='Search authors') -- 2.39.2