X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=m_librarian%2Fdownload.py;h=744e1a0273c35f06c5d1489b53796cba9556cf43;hb=12e046208dda7a46efc4b02dde9a1cb507a940ba;hp=a66e6925a6e160d4d4913ee89832bc030eaf6b42;hpb=76fad1db9bf3d812333ca9f65b1ed87c8549cab7;p=m_librarian.git diff --git a/m_librarian/download.py b/m_librarian/download.py index a66e692..744e1a0 100755 --- a/m_librarian/download.py +++ b/m_librarian/download.py @@ -1,5 +1,6 @@ #! /usr/bin/env python +from __future__ import print_function import os from time import mktime from shutil import copyfileobj @@ -9,22 +10,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 - try: - _format = get_config().get('download', 'format') - except: - return +def _compile_format(): + global format, compile_format, compiled_format + if not compile_format: + return + compile_format = False + format = get_config().get('download', 'format') + if not format: + return got_percent = False compiled = [] - for c in _format: + for c in format: if c == '%': if got_percent: got_percent = False @@ -35,7 +36,7 @@ def _do_compile_format(): if got_percent: got_percent = False if c == 'a': - new_format = u'%(author)s' + new_format = u'%(author1)s' elif c == 'e': new_format = u'%(extension)s' elif c == 'f': @@ -57,25 +58,28 @@ 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): - if path is None: +def download(book, dest_path=None, lib_path=None, a_format=None): + if lib_path is None: global _library_path if _library_path is None: - _library_path = get_config().get('library', 'path') - path = _library_path + _library_path = get_config().getpath('library', 'path') + lib_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: + compile_format = True + format = a_format + _compile_format() + if compiled_format[-1] in ('\0', '\\', '/'): + raise ValueError('Bad format: "%s"' % compiled_format) bdict = {} - bdict['author'] = book.authors[0].fullname + bdict['author1'] = book.author1 bdict['extension'] = book.extension.name bdict['file'] = book.file genre = book.genres[0] @@ -85,27 +89,29 @@ 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 + full_path = os.path.join(dest_path, filename) try: - os.makedirs(os.path.dirname(filename)) + os.makedirs(os.path.dirname(full_path)) except OSError: pass # Already exists - zf = ZipFile(os.path.join(path, book.archive), 'r') + zf = ZipFile(os.path.join(lib_path, book.archive), 'r') infile = zf.open('%s.%s' % (book.file, book.extension.name)) - outfile = open(filename, 'wb') + outfile = open(full_path, 'wb') copyfileobj(infile, outfile) outfile.close() infile.close() zf.close() dt = mktime(book.date.timetuple()) - os.utime(filename, (dt, dt)) + os.utime(full_path, (dt, dt)) def test(): - _do_compile_format() - print _compiled_format + _compile_format() + print(compiled_format) + if __name__ == '__main__': test()