From e53f480b62166cc8028f1d6e4fda307ad7af2233 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Thu, 24 Dec 2015 18:36:08 +0300 Subject: [PATCH] Import INP(X) --- m_librarian/data/README.rus.txt | 21 ++++++++++ m_librarian/db.py | 14 +++++++ m_librarian/inp.py | 70 +++++++++++++++++++++++++++++++++ scripts/ml-import.py | 14 +++++++ 4 files changed, 119 insertions(+) create mode 100644 m_librarian/inp.py create mode 100755 scripts/ml-import.py diff --git a/m_librarian/data/README.rus.txt b/m_librarian/data/README.rus.txt index 2f60bc1..9f4f650 100644 --- a/m_librarian/data/README.rus.txt +++ b/m_librarian/data/README.rus.txt @@ -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 diff --git a/m_librarian/db.py b/m_librarian/db.py index 98e541e..ef0e3bd 100755 --- a/m_librarian/db.py +++ b/m_librarian/db.py @@ -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 index 0000000..db0a3d8 --- /dev/null +++ b/m_librarian/inp.py @@ -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 index 0000000..0273ff6 --- /dev/null +++ b/scripts/ml-import.py @@ -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() -- 2.39.5