From: Oleg Broytman Date: Mon, 23 May 2016 20:25:00 +0000 (+0300) Subject: Search books by extension X-Git-Tag: 0.0.7~6 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=5126c1afb98575a2430d188ad20a6cc6d5627acb;p=m_librarian.git Search books by extension --- diff --git a/docs-ru/command_line.rst b/docs-ru/command_line.rst index a6f226b..4cb958f 100644 --- a/docs-ru/command_line.rst +++ b/docs-ru/command_line.rst @@ -100,7 +100,7 @@ ml-search.py Использование:: - ml-search.py [-i/-I] [-t/-s/-f] books [-t title] [-s series] [-a archive] [-f file] [-d] + ml-search.py [-i/-I] [-t/-s/-f] books [-t title] [-s series] [-a archive] [-f file] [-d] [-e ext] Искать и печатать список книг по заголовку, серии, архиву, имени файла. @@ -113,6 +113,7 @@ ml-search.py -d, --details Выводить больше информации о найденных книгах; повторите эту опцию несколько раз, чтобы получить ещё больше деталей + -e, --ext ext Искать по расширению имени файла По умолчанию программы выводит список заголовков найденных книг. При использовании опции `-d` также выводится список авторов и жанров, а diff --git a/docs/command_line.rst b/docs/command_line.rst index 97c0648..99fb72e 100644 --- a/docs/command_line.rst +++ b/docs/command_line.rst @@ -99,7 +99,7 @@ Book search Usage:: - ml-search.py [-i/-I] [-t/-s/-f] books [-t title] [-s series] [-a archive] [-f file] [-d] + ml-search.py [-i/-I] [-t/-s/-f] books [-t title] [-s series] [-a archive] [-f file] [-d] [-e ext] Search and print a list of books by title, series, archive or file name. @@ -111,6 +111,7 @@ Options:: -f, --file file Search by file name (without extension) -d, --details Output more details about books; repeat for even more details + -e, --ext ext Search by file extension By default the program prints only titles of the found book. With one option `-d` it also prints the list of authors and genres, and also diff --git a/m_librarian/search.py b/m_librarian/search.py index 5b289f0..7572287 100644 --- a/m_librarian/search.py +++ b/m_librarian/search.py @@ -40,16 +40,19 @@ _search_conditions_dict = { def mk_search_conditions(table, search_type, case_sensitive, values, - expressions=None): + expressions=None, join_expressions=None): + if join_expressions is None: + join_expressions = [] return _mk_search_conditions_with_operator( table, case_sensitive, _search_conditions_dict[search_type], - values, expressions) + values, expressions) + join_expressions def _search(table, search_type, case_sensitive, values, - expressions=None, orderBy=None): + expressions=None, join_expressions=None, orderBy=None): conditions = mk_search_conditions( - table, search_type, case_sensitive, values, expressions=expressions) + table, search_type, case_sensitive, values, expressions=expressions, + join_expressions=join_expressions) return table.select(AND(*conditions), orderBy=orderBy) @@ -59,9 +62,10 @@ def search_authors(search_type, case_sensitive, values, expressions=None, orderBy=orderBy) -def search_books(search_type, case_sensitive, values, orderBy=None): +def search_books(search_type, case_sensitive, values, join_expressions=None, + orderBy=None): return _search(Book, search_type, case_sensitive, values, - orderBy=orderBy) + join_expressions=join_expressions, orderBy=orderBy) def search_extensions(search_type, case_sensitive, values, orderBy=None): diff --git a/scripts/ml-search.py b/scripts/ml-search.py index b4c01e2..b5b34c4 100755 --- a/scripts/ml-search.py +++ b/scripts/ml-search.py @@ -5,8 +5,9 @@ import sys 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_books, \ +from m_librarian.db import Author, Book, Extension, open_db +from m_librarian.search import mk_search_conditions, \ + search_authors, search_books, \ search_extensions, search_genres, search_languages from m_librarian.translations import translations @@ -53,13 +54,26 @@ def _search_authors(case_sensitive, search_type, args): def _search_books(case_sensitive, search_type, args): values = {} + join_expressions = [] 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) + test_values = values.copy() + for column in 'ext', : + value = getattr(args, column) + if value: + test_values[column] = unicode(value, default_encoding) + case_sensitive = _guess_case_sensitivity(test_values) + if args.ext: + join_expressions.append(Book.j.extension) + conditions = mk_search_conditions( + Extension, search_type, case_sensitive, + {'name': args.ext}) + join_expressions.extend(conditions) for book in search_books(search_type, case_sensitive, values, + join_expressions, orderBy=('series', 'ser_no', 'title')): print book.title.encode(default_encoding) if args.details >= 1: @@ -164,6 +178,7 @@ if __name__ == '__main__': parser.add_argument('-d', '--details', action='count', help='output more details about books; ' 'repeat for even more details') + parser.add_argument('-e', '--ext', help='search by file extension') parser.set_defaults(func=_search_books) parser = subparsers.add_parser('ext', help='Search extensions')