Использование::
- 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]
Искать и печатать список книг по заголовку, серии, архиву, имени файла.
-a, --archive архив Искать по имени архива (zip-файла)
-f, --file файл Искать по имени файла (без расширения)
-p, --path path Путь к директории с архивами библиотеки
+ --format format Формат имени сохраняемого файла
--get Загрузить ровно один файл
--get-many N Загрузить не больше указанного числа файлов
--id id Искать по id книги
в конец с точкой в качестве разделителя. Т.о. формат `%f` эквивалентен
формату `%f.%e`.
+Опция `--format format` позволяет указать формат в командной строке.
+
Опция `--get-many N` позволяет загрузить указанное число книг (не больше
чем N, где N — целое число.) Опции `--get-many N` и `--get` взаимно
исключают друг друга и не должны использоваться одновременно.
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.
-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
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.
__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
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
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:
def test():
- _do_compile_format()
- print _compiled_format
+ _compile_format()
+ print compiled_format
if __name__ == '__main__':
test()
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)
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,
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')