]> git.phdru.name Git - m_librarian.git/commitdiff
Import INP(X)
authorOleg Broytman <phd@phdru.name>
Thu, 24 Dec 2015 15:36:08 +0000 (18:36 +0300)
committerOleg Broytman <phd@phdru.name>
Thu, 24 Dec 2015 15:36:08 +0000 (18:36 +0300)
m_librarian/data/README.rus.txt
m_librarian/db.py
m_librarian/inp.py [new file with mode: 0644]
scripts/ml-import.py [new file with mode: 0755]

index 2f60bc1459ea740812d0aa60e4573542eaf9c7b1..9f4f65005b00901f6293b7c76b6e4c6476448f82 100644 (file)
@@ -2,3 +2,24 @@
 Программа является свободной, распространяется под лицензией типа BSD. Точные
 условия лицензии см. в файле License.txt. Домашняя страница программы:
 http://home-lib.net/
+
+Структура inp: http://forum.home-lib.net/index.php?showtopic=16
+Список колонок либо в файле structure.info, либо
+
+Стандартная структура (librusec.inpx и librusec_usr.inpx):
+
+AUTHOR;GENRE;TITLE;SERIES;SERNO;FILE;SIZE;LIBID;DEL;EXT;DATE;
+
+Структура для пользовательских коллекций:
+
+AUTHOR;GENRE;TITLE;SERIES;SERNO;FILE;SIZE;LIBID;DEL;EXT;DATE;INSNO;FOLDER;
+
+Новый librusec.inpx:
+
+AUTHOR;GENRE;TITLE;SERIES;SERNO;FILE;SIZE;LIBID;DEL;EXT;DATE;LANG;LIBRATE;KEYWORDS;
+
+Скачать:
+
+http://home-lib.net/download/inpx/librusec_local_fb2.inpx
+http://home-lib.net/download/inpx/librusec_local_usr.inpx
+http://home-lib.net/download/inpx/librusec_local_all.inpx
index 98e541e95732378344f2e6f47b6b5f2cb1af2f3c..ef0e3bd350d52b0b96b8fdae3b5f3832d72708d2 100755 (executable)
@@ -114,6 +114,20 @@ def insert_name(table, name, **kw):
         return table(name=name, count=0, **kw)
 
 
+def update_counters():
+    for author in Author.select():
+        author.count = len(author.books)
+
+    for ext in Extension.select():
+        ext.count = Book.select(Book.q.extension == ext.name).count()
+
+    for genre in Genre.select():
+        genre.count = len(genre.books)
+
+    for language in Language.select():
+        language.count = Book.select(Book.q.language == language.name).count()
+
+
 def test():
     print "DB dirs:", db_dirs
     if db_uri:
diff --git a/m_librarian/inp.py b/m_librarian/inp.py
new file mode 100644 (file)
index 0000000..db0a3d8
--- /dev/null
@@ -0,0 +1,70 @@
+
+__all__ = ['import_inpx']
+
+import os
+from zipfile import ZipFile
+from sqlobject import sqlhub, SQLObjectNotFound
+from .db import Author, Book, Extension, Genre, Language, insert_name
+
+
+EOT = chr(4)  # INP field separator
+
+
+def split_line(line):
+    parts = line.strip().split(EOT)
+    l = len(parts)
+    if l < 11:
+        raise ValueError('Unknown INP structure: "%s"' % line)
+    if l == 11:  # Standard structure
+        parts.append(None)  # Emulate lang
+    else:  # New structure
+        parts = parts[:12]
+    return parts
+
+
+def import_inp_line(archive, parts):
+    authors, genres, title, series, ser_no, file, size, lib_id, deleted, \
+        extension, date, language = parts
+    try:
+        Book.archive_file_idx.get(archive, file)
+    except SQLObjectNotFound:
+        pass
+    else:
+        return
+    try:
+        ser_no = int(ser_no)
+    except ValueError:
+        ser_no = None
+    size = int(size)
+    deleted = deleted == '1'
+    extension_row = insert_name(Extension, extension)
+    language_row = insert_name(Language, language)
+    book = Book(title=title, series=series, ser_no=ser_no,
+                archive=archive, file=file, size=size,
+                lib_id=lib_id, deleted=deleted,
+                extension=extension_row, date=date,
+                language=language_row)
+    for author in authors.split(':'):
+        if author:
+            author_row = insert_name(Author, author)
+            book.addAuthor(author_row)
+    for genre in genres.split(':'):
+        if genre:
+            genre_row = insert_name(Genre, genre, title=genre)
+            book.addGenre(genre_row)
+
+
+def import_inp(archive, inp):
+    for line in inp:
+        import_inp_line(archive, split_line(line))
+
+
+def import_inpx(path):
+    inpx = ZipFile(path)
+    for name in inpx.namelist():
+        archive, ext = os.path.splitext(name)
+        if ext != 'inp':
+            continue
+        inp = inpx.open(name)
+        sqlhub.doInTransaction(import_inp, archive + '.zip', inp)
+        inp.close()
diff --git a/scripts/ml-import.py b/scripts/ml-import.py
new file mode 100755 (executable)
index 0000000..0273ff6
--- /dev/null
@@ -0,0 +1,14 @@
+#! /usr/bin/env python
+
+import sys
+from m_librarian.db import init_db, update_counters
+from m_librarian.glst import import_data
+from m_librarian.inp import import_inpx
+
+if __name__ == '__main__':
+    if len(sys.argv) != 2:
+        sys.exit("Usage: %s file.inpx" % sys.argv[0])
+    init_db()
+    import_data()
+    import_inpx(sys.argv[1])
+    update_counters()