From: Oleg Broytman Date: Thu, 9 Jun 2016 18:10:04 +0000 (+0300) Subject: Pass format of the downloaded file name in the command line X-Git-Tag: 0.0.12~1 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=8d0cd0faf04cf7a988b77d6fe2a4285444a2f3ea;p=m_librarian.git Pass format of the downloaded file name in the command line --- diff --git a/docs-ru/command_line.rst b/docs-ru/command_line.rst index a86687e..8bf74c8 100644 --- a/docs-ru/command_line.rst +++ b/docs-ru/command_line.rst @@ -121,7 +121,7 @@ ml-search.py Использование:: - ml-search.py books [-t title] [-s series] [-a archive] [-f file] [-p path] [--get] [--get-many N] [--id id] [--surname name] [--name name] [--misc-name name] [--fullname name] [--aid aid] [-e ext] [--eid eid] [--gname name] [--gtitle title] [--gid gid] [-l lang] [--lid lid] + ml-search.py books [-t title] [-s series] [-a archive] [-f file] [-p path] [--format f] [--get] [--get-many N] [--id id] [--surname name] [--name name] [--misc-name name] [--fullname name] [--aid aid] [-e ext] [--eid eid] [--gname name] [--gtitle title] [--gid gid] [-l lang] [--lid lid] Искать и печатать список книг по заголовку, серии, архиву, имени файла. @@ -132,6 +132,7 @@ ml-search.py -a, --archive архив Искать по имени архива (zip-файла) -f, --file файл Искать по имени файла (без расширения) -p, --path path Путь к директории с архивами библиотеки + --format format Формат имени сохраняемого файла --get Загрузить ровно один файл --get-many N Загрузить не больше указанного числа файлов --id id Искать по id книги @@ -195,6 +196,8 @@ ml-search.py в конец с точкой в качестве разделителя. Т.о. формат `%f` эквивалентен формату `%f.%e`. +Опция `--format format` позволяет указать формат в командной строке. + Опция `--get-many N` позволяет загрузить указанное число книг (не больше чем N, где N — целое число.) Опции `--get-many N` и `--get` взаимно исключают друг друга и не должны использоваться одновременно. diff --git a/docs/command_line.rst b/docs/command_line.rst index 5125d0e..2196791 100644 --- a/docs/command_line.rst +++ b/docs/command_line.rst @@ -118,7 +118,7 @@ Book searching and downloading Usage:: - ml-search.py books [-t title] [-s series] [-a archive] [-f file] [-p path] [--get] [--get-many N] [--id id] [--surname name] [--name name] [--misc-name name] [--fullname name] [--aid aid] [-e ext] [--eid eid] [--gname name] [--gtitle title] [--gid gid] [-l lang] [--lid lid] + ml-search.py books [-t title] [-s series] [-a archive] [-f file] [-p path] [--format f] [--get] [--get-many N] [--id id] [--surname name] [--name name] [--misc-name name] [--fullname name] [--aid aid] [-e ext] [--eid eid] [--gname name] [--gtitle title] [--gid gid] [-l lang] [--lid lid] Search and print a list of books by title, series, archive or file name. @@ -130,6 +130,7 @@ Options:: -f, --file file Search by file name (without extension) -p, --path path Path to the directory with the library archives + --format format Format of the downloaded file name --get Download exactly one book --get-many N Download at most this many books --id id Search by database id @@ -191,6 +192,8 @@ Format must not end in directory separator (`/` or `\\`). If specifier unconditionally with a dot. That is, format `%f` is equivalent to `%f.%e`. +Option `--format format` allows to overwrite this configuration value. + Option `--get-many N` allows to download many books (at most N, where N is an integer). Options `--get-many N` and `--get` are, of course, mutually incompatible. diff --git a/m_librarian/download.py b/m_librarian/download.py index a66e692..8e309cb 100755 --- a/m_librarian/download.py +++ b/m_librarian/download.py @@ -9,22 +9,22 @@ from .config import get_config __all__ = ['download'] -_format = '%f' -_compile_format = True -_compiled_format = '%(file)s' +format = '%f' +compile_format = True +compiled_format = '%(file)s' -def _do_compile_format(): - global _format, _compile_format, _compiled_format - if _compile_format: - _compile_format = False +def _compile_format(): + global format, compile_format, compiled_format + if compile_format: + compile_format = False try: - _format = get_config().get('download', 'format') + format = get_config().get('download', 'format') except: return got_percent = False compiled = [] - for c in _format: + for c in format: if c == '%': if got_percent: got_percent = False @@ -57,23 +57,26 @@ def _do_compile_format(): compiled.append(new_format) else: compiled.append(c) - _compiled_format = ''.join(compiled) + compiled_format = ''.join(compiled) _library_path = None -def download(book, path=None): +def download(book, path=None, a_format=None): if path is None: global _library_path if _library_path is None: _library_path = get_config().get('library', 'path') path = _library_path - global _compiled_format - _do_compile_format() - if _compiled_format[-1] in ('\0', '\\', '/'): - raise ValueError('Bad format: "%s"' % _compiled_format) + global format, compile_format, compiled_format + if a_format: + format = a_format + compile_format = True + _compile_format() + if compiled_format[-1] in ('\0', '\\', '/'): + raise ValueError('Bad format: "%s"' % compiled_format) bdict = {} bdict['author'] = book.authors[0].fullname bdict['extension'] = book.extension.name @@ -85,9 +88,9 @@ def download(book, path=None): bdict['ser_no'] = book.ser_no or 0 bdict['series'] = book.series bdict['title'] = book.title - if '%(extension)s' not in _compiled_format: - _compiled_format += '.%(extension)s' - filename = _compiled_format % bdict + if '%(extension)s' not in compiled_format: + compiled_format += '.%(extension)s' + filename = compiled_format % bdict try: os.makedirs(os.path.dirname(filename)) except OSError: @@ -104,8 +107,8 @@ def download(book, path=None): def test(): - _do_compile_format() - print _compiled_format + _compile_format() + print compiled_format if __name__ == '__main__': test() diff --git a/scripts/ml-search.py b/scripts/ml-search.py index 9f06657..a6fe3e5 100755 --- a/scripts/ml-search.py +++ b/scripts/ml-search.py @@ -189,7 +189,7 @@ def _search_books(case_sensitive, search_type, args): print " ", _("Deleted").encode(default_encoding), ":", \ _(str(book.deleted)).encode(default_encoding) if args.get or args.get_many: - download(book, args.path) + download(book, args.path, args.format) count += 1 print_count(count) @@ -295,6 +295,7 @@ if __name__ == '__main__': 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('--format', help='download format, default is %f') parser.add_argument('--get', action='store_true', help='download exactly one book') parser.add_argument('--get-many', type=int, diff --git a/tests/test_format.py b/tests/test_format.py index 9d52fb9..8dc1a22 100755 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -9,8 +9,8 @@ from m_librarian import config, download class TestFormat(unittest.TestCase): def test_compile_format(self): config.get_config().set('download', 'format', '%a/%s/%n %t') - download._do_compile_format() - self.assertEqual(download._compiled_format, + download._compile_format() + self.assertEqual(download.compiled_format, u'%(author)s/%(series)s/%(ser_no)d %(title)s')