3 from zipfile import ZipFile
5 from sqlobject import sqlhub
6 from sqlobject.sqlbuilder import Select
8 from .db import Author, Book, Extension, Genre, Language, \
9 insert_name, insert_author
11 __all__ = ['import_inpx']
14 EOT = chr(4) # INP field separator
18 parts = line.strip().split(EOT)
21 raise ValueError('Unknown INP structure: "%s"' % line)
23 if _l == 11: # Standard structure
24 parts.append(None) # Emulate lang
25 elif _l == 15: # New structure
27 elif _l == 17: # Very new structure
30 parts = parts[:11] + [language]
32 raise ValueError('Unknown INP structure: "%s"' % line)
36 def import_inp_line(archive, parts):
37 authors, genres, title, series, ser_no, file, size, lib_id, deleted, \
38 extension, date, language = parts
44 deleted = deleted == '1'
45 extension_row = insert_name(Extension, extension)
46 language_row = insert_name(Language, language)
47 book = Book(title=title, series=series, ser_no=ser_no,
48 archive=archive, file=file, size=size,
49 lib_id=lib_id, deleted=deleted,
50 extension=extension_row, date=date,
51 language=language_row)
52 authors = authors.split(':')
54 for author in authors:
56 if author in seen_authors:
58 seen_authors.add(author)
59 alist = author.split(',', 2)
69 author_row = insert_author(surname, name, misc_name)
70 book.addAuthor(author_row)
71 for genre in genres.split(':'):
73 genre_row = insert_name(Genre, genre, title=genre)
74 book.addGenre(genre_row)
78 if isinstance(s, bytes):
79 return s.decode('utf-8')
84 def import_inp(archive, inp):
87 connection = sqlhub.processConnection
88 for file, in connection.queryAll(connection.sqlrepr(
89 Select(Book.q.file, Book.q.archive == archive))):
90 files.add((archive, tounicode(file)))
92 line = line.decode('utf-8')
93 _archive, parts = split_line(line)
94 if _archive and (_archive not in archives):
95 archives.add(_archive)
96 for file, in connection.queryAll(connection.sqlrepr(
97 Select(Book.q.file, Book.q.archive == _archive))):
98 files.add((_archive, tounicode(file)))
100 if (_archive or archive, file) not in files:
101 files.add((_archive or archive, file))
102 import_inp_line(_archive or archive, parts)
105 def import_inpx(path, pbar_cb=None):
109 for name in inpx.namelist():
110 ext = os.path.splitext(name)[1]
113 pbar_cb.set_max(inp_count)
115 for name in inpx.namelist():
116 archive, ext = os.path.splitext(name)
121 pbar_cb.display(inp_count)
122 inp = inpx.open(name)
123 sqlhub.doInTransaction(import_inp, archive + '.zip', inp)
125 connection = sqlhub.processConnection
126 if connection.dbName == 'postgres':
127 for table in Author, Book, Extension, Genre, Language:
128 connection.query("VACUUM %s" % table.sqlmeta.table)
129 elif connection.dbName == 'sqlite':
130 connection.query("VACUUM")