]> git.phdru.name Git - m_librarian.git/commitdiff
Feat(inpx): Import new (17 fields) INP
authorOleg Broytman <phd@phdru.name>
Wed, 16 May 2018 21:10:06 +0000 (00:10 +0300)
committerOleg Broytman <phd@phdru.name>
Wed, 16 May 2018 21:30:48 +0000 (00:30 +0300)
m_librarian/inp.py
tests/test.inpx
tests/test_inp.py

index 76ab97be9883c4cd240ae725acc3300acf897e98..4b772aff703f050d69117e2ef1725eb575d157c8 100644 (file)
@@ -17,11 +17,18 @@ def split_line(line):
     _l = len(parts)
     if _l < 11:
         raise ValueError('Unknown INP structure: "%s"' % line)
+    archive = None
     if _l == 11:  # Standard structure
         parts.append(None)  # Emulate lang
-    else:  # New structure
+    elif _l == 15:  # New structure
         parts = parts[:12]
-    return parts
+    elif _l == 17:  # Very new structure
+        archive = parts[12]
+        language = parts[13]
+        parts = parts[:11] + [language]
+    else:  # New structure
+        raise ValueError('Unknown INP structure: "%s"' % line)
+    return archive, parts
 
 
 def import_inp_line(archive, parts):
@@ -66,18 +73,24 @@ def import_inp_line(archive, parts):
 
 
 def import_inp(archive, inp):
+    archives = set()
     files = set()
     connection = sqlhub.processConnection
     for file, in connection.queryAll(connection.sqlrepr(
             Select(Book.q.file, Book.q.archive == archive))):
-        files.add(file)
+        files.add((archive, file))
     for line in inp:
         line = line.decode('utf-8')
-        parts = split_line(line)
+        _archive, parts = split_line(line)
+        if _archive and (_archive not in archives):
+            archives.add(_archive)
+            for file, in connection.queryAll(connection.sqlrepr(
+                    Select(Book.q.file, Book.q.archive == _archive))):
+                files.add((_archive, file))
         file = parts[5]
-        if file not in files:
-            files.add(file)
-            import_inp_line(archive, parts)
+        if (_archive or archive, file) not in files:
+            files.add((_archive or archive, file))
+            import_inp_line(_archive or archive, parts)
 
 
 def import_inpx(path, pbar_cb=None):
index f794270360d78167fe59d7b7b6edd261a818324a..e364a6f56d074cbfe9aeee5492cde0a7cf4d542a 100644 (file)
Binary files a/tests/test.inpx and b/tests/test.inpx differ
index 75bcd011b55b4f8a69ccbe35cf3bac1dabae5ef9..f51e730cf7352cfd613d60cc873bba7727818571 100644 (file)
@@ -11,5 +11,5 @@ def test_import_bad_inpx():
 
 def test_import_inpx():
     load_inpx('test.inpx')
-    assert Author.select().count() == 4
-    assert Book.select().count() == 4
+    assert Author.select().count() == 5
+    assert Book.select().count() == 5