3 from zipfile import ZipFile
4 from sqlobject import sqlhub
5 from sqlobject.sqlbuilder import Select
6 from .db import Author, Book, Extension, Genre, Language, \
7 insert_name, insert_author
9 __all__ = ['import_inpx']
12 EOT = chr(4) # INP field separator
16 parts = line.strip().split(EOT)
19 raise ValueError('Unknown INP structure: "%s"' % line)
21 if _l == 11: # Standard structure
22 parts.append(None) # Emulate lang
23 elif _l == 15: # New structure
25 elif _l == 17: # Very new structure
28 parts = parts[:11] + [language]
30 raise ValueError('Unknown INP structure: "%s"' % line)
34 def import_inp_line(archive, parts):
35 authors, genres, title, series, ser_no, file, size, lib_id, deleted, \
36 extension, date, language = parts
42 deleted = deleted == '1'
43 extension_row = insert_name(Extension, extension)
44 language_row = insert_name(Language, language)
45 book = Book(title=title, series=series, ser_no=ser_no,
46 archive=archive, file=file, size=size,
47 lib_id=lib_id, deleted=deleted,
48 extension=extension_row, date=date,
49 language=language_row)
50 authors = authors.split(':')
52 for author in authors:
54 if author in seen_authors:
56 seen_authors.add(author)
57 alist = author.split(',', 2)
67 author_row = insert_author(surname, name, misc_name)
68 book.addAuthor(author_row)
69 for genre in genres.split(':'):
71 genre_row = insert_name(Genre, genre, title=genre)
72 book.addGenre(genre_row)
76 if isinstance(s, bytes):
77 return s.decode('utf-8')
82 def import_inp(archive, inp):
85 connection = sqlhub.processConnection
86 for file, in connection.queryAll(connection.sqlrepr(
87 Select(Book.q.file, Book.q.archive == archive))):
88 files.add((archive, tounicode(file)))
90 line = line.decode('utf-8')
91 _archive, parts = split_line(line)
92 if _archive and (_archive not in archives):
93 archives.add(_archive)
94 for file, in connection.queryAll(connection.sqlrepr(
95 Select(Book.q.file, Book.q.archive == _archive))):
96 files.add((_archive, tounicode(file)))
98 if (_archive or archive, file) not in files:
99 files.add((_archive or archive, file))
100 import_inp_line(_archive or archive, parts)
103 def import_inpx(path, pbar_cb=None):
107 for name in inpx.namelist():
108 ext = os.path.splitext(name)[1]
111 pbar_cb.set_max(inp_count)
113 for name in inpx.namelist():
114 archive, ext = os.path.splitext(name)
119 pbar_cb.display(inp_count)
120 inp = inpx.open(name)
121 sqlhub.doInTransaction(import_inp, archive + '.zip', inp)
123 connection = sqlhub.processConnection
124 if connection.dbName == 'postgres':
125 for table in Author, Book, Extension, Genre, Language:
126 connection.query("VACUUM %s" % table.sqlmeta.table)
127 elif connection.dbName == 'sqlite':
128 connection.query("VACUUM")