]> git.phdru.name Git - m_librarian.git/blob - m_librarian/inp.py
d4e6cc1ff730f09f3dc403bdb213e928da4dbd54
[m_librarian.git] / m_librarian / inp.py
1
2 __all__ = ['import_inpx']
3
4 import os
5 from zipfile import ZipFile
6 from sqlobject import sqlhub, SQLObjectNotFound
7 from .db import Author, Book, Extension, Genre, Language, insert_name
8
9
10 EOT = chr(4)  # INP field separator
11
12
13 def split_line(line):
14     parts = line.strip().split(EOT)
15     l = len(parts)
16     if l < 11:
17         raise ValueError('Unknown INP structure: "%s"' % line)
18     if l == 11:  # Standard structure
19         parts.append(None)  # Emulate lang
20     else:  # New structure
21         parts = parts[:12]
22     return parts
23
24
25 def import_inp_line(archive, parts):
26     authors, genres, title, series, ser_no, file, size, lib_id, deleted, \
27         extension, date, language = parts
28     try:
29         Book.archive_file_idx.get(archive, file)
30     except SQLObjectNotFound:
31         pass
32     else:
33         return
34     try:
35         ser_no = int(ser_no)
36     except ValueError:
37         ser_no = None
38     size = int(size)
39     deleted = deleted == '1'
40     extension_row = insert_name(Extension, extension)
41     language_row = insert_name(Language, language)
42     book = Book(title=title, series=series, ser_no=ser_no,
43                 archive=archive, file=file, size=size,
44                 lib_id=lib_id, deleted=deleted,
45                 extension=extension_row, date=date,
46                 language=language_row)
47     for author in authors.split(':'):
48         if author:
49             author_row = insert_name(Author, author)
50             book.addAuthor(author_row)
51     for genre in genres.split(':'):
52         if genre:
53             genre_row = insert_name(Genre, genre, title=genre)
54             book.addGenre(genre_row)
55
56
57 def import_inp(archive, inp):
58     for line in inp:
59         import_inp_line(archive, split_line(line))
60
61
62 def import_inpx(path):
63     inpx = ZipFile(path)
64     for name in inpx.namelist():
65         archive, ext = os.path.splitext(name)
66         if ext != '.inp':
67             continue
68         inp = inpx.open(name)
69         sqlhub.doInTransaction(import_inp, archive + '.zip', inp)
70         inp.close()
71     connection = sqlhub.processConnection
72     if connection.dbName in ('postgres', 'sqlite'):
73         for table in Author, Book, Extension, Genre, Language:
74             connection.query("VACUUM %s" % table.sqlmeta.table)