ebk 0.3.1__py3-none-any.whl → 0.3.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ebk might be problematic. Click here for more details.
- ebk/ai/__init__.py +23 -0
- ebk/ai/knowledge_graph.py +443 -0
- ebk/ai/llm_providers/__init__.py +21 -0
- ebk/ai/llm_providers/base.py +230 -0
- ebk/ai/llm_providers/ollama.py +362 -0
- ebk/ai/metadata_enrichment.py +396 -0
- ebk/ai/question_generator.py +328 -0
- ebk/ai/reading_companion.py +224 -0
- ebk/ai/semantic_search.py +434 -0
- ebk/ai/text_extractor.py +394 -0
- ebk/cli.py +1097 -9
- ebk/db/__init__.py +37 -0
- ebk/db/migrations.py +180 -0
- ebk/db/models.py +526 -0
- ebk/db/session.py +144 -0
- ebk/exports/__init__.py +0 -0
- ebk/exports/base_exporter.py +218 -0
- ebk/exports/html_library.py +1390 -0
- ebk/exports/html_utils.py +117 -0
- ebk/exports/hugo.py +59 -0
- ebk/exports/jinja_export.py +287 -0
- ebk/exports/multi_facet_export.py +164 -0
- ebk/exports/symlink_dag.py +479 -0
- ebk/exports/zip.py +25 -0
- ebk/library_db.py +155 -0
- ebk/repl/__init__.py +9 -0
- ebk/repl/find.py +126 -0
- ebk/repl/grep.py +174 -0
- ebk/repl/shell.py +1677 -0
- ebk/repl/text_utils.py +320 -0
- ebk/services/__init__.py +11 -0
- ebk/services/import_service.py +442 -0
- ebk/services/tag_service.py +282 -0
- ebk/services/text_extraction.py +317 -0
- ebk/similarity/__init__.py +77 -0
- ebk/similarity/base.py +154 -0
- ebk/similarity/core.py +445 -0
- ebk/similarity/extractors.py +168 -0
- ebk/similarity/metrics.py +376 -0
- ebk/vfs/__init__.py +101 -0
- ebk/vfs/base.py +301 -0
- ebk/vfs/library_vfs.py +124 -0
- ebk/vfs/nodes/__init__.py +54 -0
- ebk/vfs/nodes/authors.py +196 -0
- ebk/vfs/nodes/books.py +480 -0
- ebk/vfs/nodes/files.py +155 -0
- ebk/vfs/nodes/metadata.py +385 -0
- ebk/vfs/nodes/root.py +100 -0
- ebk/vfs/nodes/similar.py +165 -0
- ebk/vfs/nodes/subjects.py +184 -0
- ebk/vfs/nodes/tags.py +371 -0
- ebk/vfs/resolver.py +228 -0
- {ebk-0.3.1.dist-info → ebk-0.3.2.dist-info}/METADATA +1 -1
- ebk-0.3.2.dist-info/RECORD +69 -0
- ebk-0.3.2.dist-info/entry_points.txt +2 -0
- ebk-0.3.2.dist-info/top_level.txt +1 -0
- ebk-0.3.1.dist-info/RECORD +0 -19
- ebk-0.3.1.dist-info/entry_points.txt +0 -6
- ebk-0.3.1.dist-info/top_level.txt +0 -2
- {ebk-0.3.1.dist-info → ebk-0.3.2.dist-info}/WHEEL +0 -0
- {ebk-0.3.1.dist-info → ebk-0.3.2.dist-info}/licenses/LICENSE +0 -0
ebk/db/__init__.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Database module for ebk.
|
|
3
|
+
|
|
4
|
+
Provides SQLAlchemy session management and initialization.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from .models import (
|
|
8
|
+
Base, Book, Author, Subject, Identifier, File, ExtractedText,
|
|
9
|
+
TextChunk, Cover, Concept, BookConcept, ConceptRelation,
|
|
10
|
+
ReadingSession, Annotation, PersonalMetadata, Tag
|
|
11
|
+
)
|
|
12
|
+
from .session import get_session, init_db, close_db
|
|
13
|
+
from .migrations import run_all_migrations, check_migrations
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
'Base',
|
|
17
|
+
'Book',
|
|
18
|
+
'Author',
|
|
19
|
+
'Subject',
|
|
20
|
+
'Identifier',
|
|
21
|
+
'File',
|
|
22
|
+
'ExtractedText',
|
|
23
|
+
'TextChunk',
|
|
24
|
+
'Cover',
|
|
25
|
+
'Concept',
|
|
26
|
+
'BookConcept',
|
|
27
|
+
'ConceptRelation',
|
|
28
|
+
'ReadingSession',
|
|
29
|
+
'Annotation',
|
|
30
|
+
'PersonalMetadata',
|
|
31
|
+
'Tag',
|
|
32
|
+
'get_session',
|
|
33
|
+
'init_db',
|
|
34
|
+
'close_db',
|
|
35
|
+
'run_all_migrations',
|
|
36
|
+
'check_migrations'
|
|
37
|
+
]
|
ebk/db/migrations.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Database migration utilities for ebk.
|
|
3
|
+
|
|
4
|
+
Since this project uses SQLAlchemy's create_all() approach rather than Alembic,
|
|
5
|
+
this module provides simple migration functions for schema changes.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from sqlalchemy import create_engine, text, inspect
|
|
10
|
+
from sqlalchemy.engine import Engine
|
|
11
|
+
from typing import Optional
|
|
12
|
+
|
|
13
|
+
import logging
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def get_engine(library_path: Path) -> Engine:
|
|
19
|
+
"""Get database engine for a library."""
|
|
20
|
+
db_path = library_path / 'library.db'
|
|
21
|
+
if not db_path.exists():
|
|
22
|
+
raise FileNotFoundError(f"Database not found at {db_path}")
|
|
23
|
+
|
|
24
|
+
db_url = f'sqlite:///{db_path}'
|
|
25
|
+
return create_engine(db_url, echo=False)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def table_exists(engine: Engine, table_name: str) -> bool:
|
|
29
|
+
"""Check if a table exists in the database."""
|
|
30
|
+
inspector = inspect(engine)
|
|
31
|
+
return table_name in inspector.get_table_names()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def migrate_add_tags(library_path: Path, dry_run: bool = False) -> bool:
|
|
35
|
+
"""
|
|
36
|
+
Add tags table and book_tags association table to existing database.
|
|
37
|
+
|
|
38
|
+
This migration adds support for hierarchical user-defined tags,
|
|
39
|
+
separate from bibliographic subjects.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
library_path: Path to library directory
|
|
43
|
+
dry_run: If True, only check if migration is needed
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
True if migration was applied (or would be applied in dry_run),
|
|
47
|
+
False if already up-to-date
|
|
48
|
+
"""
|
|
49
|
+
engine = get_engine(library_path)
|
|
50
|
+
|
|
51
|
+
# Check if migration is needed
|
|
52
|
+
if table_exists(engine, 'tags'):
|
|
53
|
+
logger.info("Tags table already exists, skipping migration")
|
|
54
|
+
return False
|
|
55
|
+
|
|
56
|
+
if dry_run:
|
|
57
|
+
logger.info("Migration needed: tags table does not exist")
|
|
58
|
+
return True
|
|
59
|
+
|
|
60
|
+
logger.info("Applying migration: Adding tags table and book_tags association")
|
|
61
|
+
|
|
62
|
+
with engine.begin() as conn:
|
|
63
|
+
# Create tags table
|
|
64
|
+
conn.execute(text("""
|
|
65
|
+
CREATE TABLE tags (
|
|
66
|
+
id INTEGER NOT NULL PRIMARY KEY,
|
|
67
|
+
name VARCHAR(200) NOT NULL,
|
|
68
|
+
path VARCHAR(500) NOT NULL UNIQUE,
|
|
69
|
+
parent_id INTEGER,
|
|
70
|
+
description TEXT,
|
|
71
|
+
color VARCHAR(7),
|
|
72
|
+
created_at DATETIME NOT NULL,
|
|
73
|
+
FOREIGN KEY(parent_id) REFERENCES tags (id) ON DELETE CASCADE
|
|
74
|
+
)
|
|
75
|
+
"""))
|
|
76
|
+
|
|
77
|
+
# Create indexes on tags table
|
|
78
|
+
conn.execute(text("CREATE INDEX idx_tag_path ON tags (path)"))
|
|
79
|
+
conn.execute(text("CREATE INDEX idx_tag_parent ON tags (parent_id)"))
|
|
80
|
+
conn.execute(text("CREATE INDEX ix_tags_name ON tags (name)"))
|
|
81
|
+
|
|
82
|
+
# Create book_tags association table
|
|
83
|
+
conn.execute(text("""
|
|
84
|
+
CREATE TABLE book_tags (
|
|
85
|
+
book_id INTEGER NOT NULL,
|
|
86
|
+
tag_id INTEGER NOT NULL,
|
|
87
|
+
created_at DATETIME,
|
|
88
|
+
PRIMARY KEY (book_id, tag_id),
|
|
89
|
+
FOREIGN KEY(book_id) REFERENCES books (id) ON DELETE CASCADE,
|
|
90
|
+
FOREIGN KEY(tag_id) REFERENCES tags (id) ON DELETE CASCADE
|
|
91
|
+
)
|
|
92
|
+
"""))
|
|
93
|
+
|
|
94
|
+
logger.info("Migration completed successfully")
|
|
95
|
+
|
|
96
|
+
return True
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def migrate_add_book_color(library_path: Path, dry_run: bool = False) -> bool:
|
|
100
|
+
"""
|
|
101
|
+
Add color column to books table.
|
|
102
|
+
|
|
103
|
+
This migration adds a color field to books for user customization.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
library_path: Path to library directory
|
|
107
|
+
dry_run: If True, only check if migration is needed
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
True if migration was applied (or would be applied in dry_run),
|
|
111
|
+
False if already up-to-date
|
|
112
|
+
"""
|
|
113
|
+
engine = get_engine(library_path)
|
|
114
|
+
inspector = inspect(engine)
|
|
115
|
+
|
|
116
|
+
# Check if migration is needed
|
|
117
|
+
if 'books' not in inspector.get_table_names():
|
|
118
|
+
logger.error("Books table does not exist")
|
|
119
|
+
return False
|
|
120
|
+
|
|
121
|
+
columns = [col['name'] for col in inspector.get_columns('books')]
|
|
122
|
+
if 'color' in columns:
|
|
123
|
+
logger.info("Books.color column already exists, skipping migration")
|
|
124
|
+
return False
|
|
125
|
+
|
|
126
|
+
if dry_run:
|
|
127
|
+
logger.info("Migration needed: books.color column does not exist")
|
|
128
|
+
return True
|
|
129
|
+
|
|
130
|
+
logger.info("Applying migration: Adding color column to books table")
|
|
131
|
+
|
|
132
|
+
with engine.begin() as conn:
|
|
133
|
+
conn.execute(text("ALTER TABLE books ADD COLUMN color VARCHAR(7)"))
|
|
134
|
+
logger.info("Migration completed successfully")
|
|
135
|
+
|
|
136
|
+
return True
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def run_all_migrations(library_path: Path, dry_run: bool = False) -> dict:
|
|
140
|
+
"""
|
|
141
|
+
Run all pending migrations on a library database.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
library_path: Path to library directory
|
|
145
|
+
dry_run: If True, only check which migrations are needed
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
Dict mapping migration name to whether it was applied
|
|
149
|
+
"""
|
|
150
|
+
results = {}
|
|
151
|
+
|
|
152
|
+
# Add future migrations here
|
|
153
|
+
migrations = [
|
|
154
|
+
('add_tags', migrate_add_tags),
|
|
155
|
+
('add_book_color', migrate_add_book_color),
|
|
156
|
+
]
|
|
157
|
+
|
|
158
|
+
for name, migration_func in migrations:
|
|
159
|
+
try:
|
|
160
|
+
applied = migration_func(library_path, dry_run=dry_run)
|
|
161
|
+
results[name] = applied
|
|
162
|
+
except Exception as e:
|
|
163
|
+
logger.error(f"Migration '{name}' failed: {e}")
|
|
164
|
+
results[name] = False
|
|
165
|
+
raise
|
|
166
|
+
|
|
167
|
+
return results
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def check_migrations(library_path: Path) -> dict:
|
|
171
|
+
"""
|
|
172
|
+
Check which migrations need to be applied.
|
|
173
|
+
|
|
174
|
+
Args:
|
|
175
|
+
library_path: Path to library directory
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
Dict mapping migration name to whether it's needed
|
|
179
|
+
"""
|
|
180
|
+
return run_all_migrations(library_path, dry_run=True)
|