From 991627dd1d4fcdecc04a091ec9c0c8b9e8e8b27c Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 12 Feb 2016 00:37:27 +0300 Subject: [PATCH] Process author names --- m_librarian/db.py | 10 +++++++++- m_librarian/inp.py | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/m_librarian/db.py b/m_librarian/db.py index 6e08204..ba091f3 100755 --- a/m_librarian/db.py +++ b/m_librarian/db.py @@ -2,7 +2,7 @@ __all__ = ['Author', 'Book', 'Extension', 'Genre', 'Language', 'AuthorBook', 'BookGenre', - 'init_db', 'insert_name', 'update_counters', + 'init_db', 'insert_name', 'insert_author', 'update_counters', ] import os @@ -157,6 +157,14 @@ def insert_name(table, name, **kw): return table(name=name, count=0, **kw) +def insert_author(surname, name, misc_name): + try: + return Author.full_name_idx.get( + surname=surname, name=name, misc_name=misc_name) + except SQLObjectNotFound: + return Author(surname=surname, name=name, misc_name=misc_name, count=0) + + def update_counters(): for author in Author.select(): author.count = len(author.books) diff --git a/m_librarian/inp.py b/m_librarian/inp.py index d4e6cc1..56f85e4 100644 --- a/m_librarian/inp.py +++ b/m_librarian/inp.py @@ -4,7 +4,8 @@ __all__ = ['import_inpx'] import os from zipfile import ZipFile from sqlobject import sqlhub, SQLObjectNotFound -from .db import Author, Book, Extension, Genre, Language, insert_name +from .db import Author, Book, Extension, Genre, Language, \ + insert_name, insert_author EOT = chr(4) # INP field separator @@ -46,7 +47,17 @@ def import_inp_line(archive, parts): language=language_row) for author in authors.split(':'): if author: - author_row = insert_name(Author, author) + alist = author.split(',', 2) + surname = alist[0] + if len(alist) > 1: + name = alist[1] + if len(alist) == 3: + misc_name = alist[2] + else: + misc_name = '' + else: + name = misc_name = '' + author_row = insert_author(surname, name, misc_name) book.addAuthor(author_row) for genre in genres.split(':'): if genre: -- 2.39.2