branch: externals/calibre
commit 330471a75f138bb747efb0e311a761a208ea9c48
Author: Kjartan Óli Ágústsson <[email protected]>
Commit: Kjartan Óli Ágústsson <[email protected]>
Fix circular dependency and byte compiler warnings
* calibre-book.el: Don't require calibre-db.
(calibre-make-book): Move to calibre-db.el.
* calibre-db.el: Replace declare-function calls with require.
* calibre-library.el: Require calibre-db and calibre-book instead of
calibre-util.
* calibre-util.el: Move contents into calibre-db.el.
* calibre.el: Remove now unnecessary require statements.
---
calibre-book.el | 21 --------
calibre-db.el | 140 ++++++++++++++++++++++++++++++++++++++++++-----------
calibre-library.el | 3 +-
calibre-util.el | 99 -------------------------------------
calibre.el | 2 -
5 files changed, 115 insertions(+), 150 deletions(-)
diff --git a/calibre-book.el b/calibre-book.el
index 718661c9eb..b3a4edaa44 100644
--- a/calibre-book.el
+++ b/calibre-book.el
@@ -28,8 +28,6 @@
(require 'eieio)
(require 'parse-time)
-(require 'calibre-db)
-
(defun calibre-parse-timestamp (timestamp)
"Return a Lisp timestamp from TIMESTAMP.
TIMESTAMP is a string of the form YYYY-MM-DD HH:MM:SS.xxxxxx+00:00."
@@ -94,25 +92,6 @@ for private functions."
(calibre-book--slot path t)
(calibre-book--slot file-name)
-(defun calibre-make-book (entry)
- "Create a `calibre-book' from ENTRY.
-ENTRY is a list of the form:
-\(ID TITLE SERIES SERIES-INDEX TIMESTAMP PUBDATE LAST-MODIFIED)."
- (seq-let [id title series series-index timestamp pubdate last-modified path]
entry
- (calibre-book :id id
- :title title
- :authors (calibre-db--get-book-authors id)
- :publishers (calibre-db--get-book-publishers id)
- :series series
- :series-index series-index
- :timestamp (calibre-parse-timestamp timestamp)
- :pubdate (calibre-parse-timestamp pubdate)
- :last-modified (calibre-parse-timestamp last-modified)
- :tags (calibre-db--get-book-tags id)
- :formats (calibre-db--get-book-formats id)
- :path path
- :file-name (calibre-db--get-book-file-name id))))
-
(defcustom calibre-format-preferences '(pdf epub)
"The preference order of file formats."
:type '(repeat symbol :tag "Format")
diff --git a/calibre-db.el b/calibre-db.el
index b772e1e53a..aa2fede8c3 100644
--- a/calibre-db.el
+++ b/calibre-db.el
@@ -24,33 +24,27 @@
;; Fetch data from the library database.
;;; Code:
-(declare-function calibre--library "calibre.el")
-(declare-function calibre-make-book "calibre-book.el")
-
-(defvar calibre--db nil)
-(defun calibre--db ()
- "Return the metadata database."
- (unless calibre--db
- (let ((file-name (file-name-concat (calibre--library) "metadata.db")))
- (if (not (file-exists-p file-name))
- (progn
- (display-warning 'calibre (format "Metedata database %s does not
exist. Add some books to the library to create it." file-name))
- (setf calibre--db nil))
- (setf calibre--db
- (sqlite-open
- file-name)))))
- calibre--db)
-
-(defun calibre-db--get-books ()
- "Return all books in the Calibre library `calibre-library-dir'."
- (if (not (calibre--db))
- nil
- (mapcar #'calibre-make-book
- (sqlite-select (calibre--db)
- "SELECT books.id, title, series.name, series_index,
timestamp, pubdate, last_modified, path
-FROM books
-LEFT JOIN books_series_link sl ON books.id = sl.book
-LEFT JOIN series ON sl.series = series.id;"))))
+(require 'calibre)
+(require 'calibre-book)
+
+(defun calibre-make-book (entry)
+ "Create a `calibre-book' from ENTRY.
+ENTRY is a list of the form:
+\(ID TITLE SERIES SERIES-INDEX TIMESTAMP PUBDATE LAST-MODIFIED)."
+ (seq-let [id title series series-index timestamp pubdate last-modified path]
entry
+ (calibre-book :id id
+ :title title
+ :authors (calibre-db--get-book-authors id)
+ :publishers (calibre-db--get-book-publishers id)
+ :series series
+ :series-index series-index
+ :timestamp (calibre-parse-timestamp timestamp)
+ :pubdate (calibre-parse-timestamp pubdate)
+ :last-modified (calibre-parse-timestamp last-modified)
+ :tags (calibre-db--get-book-tags id)
+ :formats (calibre-db--get-book-formats id)
+ :path path
+ :file-name (calibre-db--get-book-file-name id))))
(defun calibre-db--get-book-authors (id)
"Return a list of authors for the book identified by ID."
@@ -93,5 +87,97 @@ WHERE books.id = ?"
"SELECT format FROM data WHERE book = ?"
`[,id]))))
+(defvar calibre--db nil)
+(defun calibre--db ()
+ "Return the metadata database."
+ (unless calibre--db
+ (let ((file-name (file-name-concat (calibre--library) "metadata.db")))
+ (if (not (file-exists-p file-name))
+ (progn
+ (display-warning 'calibre (format "Metedata database %s does not
exist. Add some books to the library to create it." file-name))
+ (setf calibre--db nil))
+ (setf calibre--db
+ (sqlite-open
+ file-name)))))
+ calibre--db)
+
+(defun calibre-db--get-books ()
+ "Return all books in the Calibre library `calibre-library-dir'."
+ (if (not (calibre--db))
+ nil
+ (mapcar #'calibre-make-book
+ (sqlite-select (calibre--db)
+ "SELECT books.id, title, series.name, series_index,
timestamp, pubdate, last_modified, path
+FROM books
+LEFT JOIN books_series_link sl ON books.id = sl.book
+LEFT JOIN series ON sl.series = series.id;"))))
+
+(defvar calibre--books nil)
+(defun calibre--books (&optional force)
+ "Return the in memory list of books.
+If FORCE is non-nil the list is refreshed from the database."
+ (when (or force (not calibre--books))
+ (setf calibre--books (calibre-db--get-books)))
+ calibre--books)
+
+(defvar calibre--library nil
+ "The active library.")
+
+(defun calibre-select-library (&optional library)
+ "Prompt the user to select a library from `calibre-libraries'.
+If LIBRARY is non-nil, select that instead."
+ (interactive)
+ (setf calibre--library (if library
+ library
+ (completing-read "Library: "
(calibre--library-names) nil t))
+ calibre--db nil
+ calibre--books nil)
+ (calibre-library--refresh t))
+
+(defun calibre--library ()
+ "Return the active library.
+If no library is active, prompt the user to select one."
+ (unless calibre--library
+ (calibre-select-library))
+ (alist-get calibre--library calibre-libraries nil nil #'string=))
+
+(defconst calibre-library-buffer "*Library*")
+(defun calibre-library--refresh (&optional force)
+ "Refresh the contents of the library buffer.
+If FORCE is non-nil fetch book data from the database."
+ (let* ((buffer (get-buffer calibre-library-buffer)))
+ (with-current-buffer buffer
+ (setf tabulated-list-entries
+ (mapcar #'calibre-book--print-info
+ (calibre--books force)))
+ (tabulated-list-print))))
+
+(defun calibre-book--print-info (book)
+ "Return list suitable as a value of `tabulated-list-entries'.
+BOOK is a `calibre-book'."
+ (list book
+ (with-slots (id title authors publishers series series-index tags
formats) book
+ (vconcat (mapcar (lambda (x)
+ (let ((column (car x))
+ (width (cdr x)))
+ (cl-case column
+ (id (format (format "%%%ds" width) id))
+ (title title)
+ (authors (string-join authors ", "))
+ (publishers (string-join publishers ", "))
+ (series (if (not series) "" series))
+ (series-index (if series (format "%.1f"
series-index) ""))
+ (tags (string-join tags ", "))
+ (formats (string-join (mapcar (lambda (f)
(upcase (symbol-name f))) formats) ", ")))))
+ calibre-library-columns)))))
+
+(defun calibre-book--file (book format)
+ "Return the path to BOOK in FORMAT."
+ (with-slots (path file-name) book
+ (message "%S" file-name)
+ (file-name-concat (calibre--library)
+ path
+ (message "%s.%s" file-name format))))
+
(provide 'calibre-db)
;;; calibre-db.el ends here
diff --git a/calibre-library.el b/calibre-library.el
index da4dc7d194..5c4e9f4077 100644
--- a/calibre-library.el
+++ b/calibre-library.el
@@ -24,7 +24,8 @@
;;; Code:
(require 'dired)
-(require 'calibre-util)
+(require 'calibre-db)
+(require 'calibre-book)
;;;###autoload
(defun calibre-library-add-book (file)
diff --git a/calibre-util.el b/calibre-util.el
deleted file mode 100644
index 20955ad919..0000000000
--- a/calibre-util.el
+++ /dev/null
@@ -1,99 +0,0 @@
-;;; calibre-util.el --- Break circular dependencies in other parts -*-
lexical-binding:t -*-
-
-;; Copyright (C) 2023 Kjartan Oli Agustsson
-
-;; Author: Kjartan Oli Agustsson <[email protected]>
-;; Maintainer: Kjartan Oli Agustsson <[email protected]>
-
-;; This file is part of calibre.el.
-
-;; calibre.el is free software: you can redistribute it and/or modify
-;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation, either version 3 of the License, or
-;; (at your option) any later version.
-
-;; calibre.el is distributed in the hope that it will be useful,
-;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-;; GNU General Public License for more details.
-
-;; You should have received a copy of the GNU General Public License
-;; along with calibre.el. If not, see <http://www.gnu.org/licenses/>.
-
-;;; Commentary:
-;; This file contains functions and variables that might conceptually
-;; belong in other files, whose presence in those files would create a
-;; circular dependency.
-
-;;; Code:
-(require 'calibre)
-
-(defvar calibre--books nil)
-(defun calibre--books (&optional force)
- "Return the in memory list of books.
-If FORCE is non-nil the list is refreshed from the database."
- (when (or force (not calibre--books))
- (setf calibre--books (calibre-db--get-books)))
- calibre--books)
-
-(defvar calibre--library nil
- "The active library.")
-
-(defun calibre-select-library (&optional library)
- "Prompt the user to select a library from `calibre-libraries'.
-If LIBRARY is non-nil, select that instead."
- (interactive)
- (setf calibre--library (if library
- library
- (completing-read "Library: "
(calibre--library-names) nil t))
- calibre--db nil
- calibre--books nil)
- (calibre-library--refresh t))
-
-(defun calibre--library ()
- "Return the active library.
-If no library is active, prompt the user to select one."
- (unless calibre--library
- (calibre-select-library))
- (alist-get calibre--library calibre-libraries nil nil #'string=))
-
-(defconst calibre-library-buffer "*Library*")
-(defun calibre-library--refresh (&optional force)
- "Refresh the contents of the library buffer.
-If FORCE is non-nil fetch book data from the database."
- (let* ((buffer (get-buffer calibre-library-buffer)))
- (with-current-buffer buffer
- (setf tabulated-list-entries
- (mapcar #'calibre-book--print-info
- (calibre--books force)))
- (tabulated-list-print))))
-
-(defun calibre-book--print-info (book)
- "Return list suitable as a value of `tabulated-list-entries'.
-BOOK is a `calibre-book'."
- (list book
- (with-slots (id title authors publishers series series-index tags
formats) book
- (vconcat (mapcar (lambda (x)
- (let ((column (car x))
- (width (cdr x)))
- (cl-case column
- (id (format (format "%%%ds" width) id))
- (title title)
- (authors (string-join authors ", "))
- (publishers (string-join publishers ", "))
- (series (if (not series) "" series))
- (series-index (if series (format "%.1f"
series-index) ""))
- (tags (string-join tags ", "))
- (formats (string-join (mapcar (lambda (f)
(upcase (symbol-name f))) formats) ", ")))))
- calibre-library-columns)))))
-
-(defun calibre-book--file (book format)
- "Return the path to BOOK in FORMAT."
- (with-slots (path file-name) book
- (message "%S" file-name)
- (file-name-concat (calibre--library)
- path
- (message "%s.%s" file-name format))))
-
-(provide 'calibre-util)
-;;; calibre-util.el ends here
diff --git a/calibre.el b/calibre.el
index 9318547e87..b33531dbfb 100644
--- a/calibre.el
+++ b/calibre.el
@@ -29,8 +29,6 @@
;; associated with each book, and read them, all from within Emacs.
;;; Code:
-(require 'calibre-db)
-(require 'calibre-book)
(defgroup calibre nil
"Interact with a Calibre library."