]> git.phdru.name Git - m_librarian.git/blob - m_librarian/download.py
Allow to set download format
[m_librarian.git] / m_librarian / download.py
1 #! /usr/bin/env python
2
3 import os
4 from time import mktime
5 from shutil import copyfileobj
6 from zipfile import ZipFile
7 from .config import get_config
8
9 __all__ = ['download']
10
11
12 _format = '%f'
13 _compile_format = True
14 _compiled_format = '%(file)s'
15
16
17 def _do_compile_format():
18     global _format, _compile_format, _compiled_format
19     if _compile_format:
20         _compile_format = False
21         try:
22             _format = get_config().get('download', 'format')
23         except:
24             return
25     got_percent = False
26     compiled = []
27     for c in _format:
28         if c == '%':
29             if got_percent:
30                 got_percent = False
31                 compiled.append('%')
32             else:
33                 got_percent = True
34         else:
35             if got_percent:
36                 got_percent = False
37                 if c == 'a':
38                     new_format = u'%(author)s'
39                 elif c == 'e':
40                     new_format = u'%(extension)s'
41                 elif c == 'f':
42                     new_format = u'%(file)s'
43                 elif c == 'G':
44                     new_format = u'%(gname)s'
45                 elif c == 'g':
46                     new_format = u'%(gtitle)s'
47                 elif c == 'l':
48                     new_format = u'%(language)s'
49                 elif c == 'n':
50                     new_format = u'%(ser_no)d'
51                 elif c == 's':
52                     new_format = u'%(series)s'
53                 elif c == 't':
54                     new_format = u'%(title)s'
55                 else:
56                     raise ValueError('Bad format specifier "%%%c"' % c)
57                 compiled.append(new_format)
58             else:
59                 compiled.append(c)
60     _compiled_format = ''.join(compiled)
61
62
63 _library_path = None
64
65
66 def download(book, path=None):
67     if path is None:
68         global _library_path
69         if _library_path is None:
70             _library_path = get_config().get('library', 'path')
71         path = _library_path
72
73     global _compiled_format
74     _do_compile_format()
75     if _compiled_format[-1] in ('\0', '\\', '/'):
76         raise ValueError('Bad format: "%s"' % _compiled_format)
77     bdict = {}
78     bdict['author'] = book.authors[0].fullname
79     bdict['extension'] = book.extension.name
80     bdict['file'] = book.file
81     genre = book.genres[0]
82     bdict['gname'] = genre.name
83     bdict['gtitle'] = genre.title
84     bdict['language'] = book.language.name
85     bdict['ser_no'] = book.ser_no or 0
86     bdict['series'] = book.series
87     bdict['title'] = book.title
88     if '%(extension)s' not in _compiled_format:
89         _compiled_format += '.%(extension)s'
90     filename = _compiled_format % bdict
91     try:
92         os.makedirs(os.path.dirname(filename))
93     except OSError:
94         pass  # Already exists
95     zf = ZipFile(os.path.join(path, book.archive),  'r')
96     infile = zf.open('%s.%s' % (book.file, book.extension.name))
97     outfile = open(filename, 'wb')
98     copyfileobj(infile, outfile)
99     outfile.close()
100     infile.close()
101     zf.close()
102     dt = mktime(book.date.timetuple())
103     os.utime(filename, (dt, dt))
104
105
106 def test():
107     _do_compile_format()
108     print _compiled_format
109
110 if __name__ == '__main__':
111     test()