View Single Post
Old 09-29-2025, 10:13 AM   #1
mfsav2
Member
mfsav2 began at the beginning.
 
Posts: 23
Karma: 10
Join Date: Jan 2013
Device: Kindle
Question New file type plugin - Issue with metadata

Team,
sorry to bother but is driving me nuts
I want to use Calibre for my physical library together with my electronic library so I avoid to purchase multiple times the same book

I've followed a suggestion I've found in internet that I found super cool to create a file format .pbook (https://www.davideaversa.it/blog/how...sical-library/) with some elements using TOML syntax .

one of my pbook is:
Code:
[metadata]
title=I vostri nomi sono scritti nei cieli. Nel mondo di Rose Busingye
author=Davide Perillo
ISBN=9788817163835
source=GoogleBooks
publication_date=
language=it
tag=Physical
I wrote a plugin to read the file, and it works, but it does not read the Metadata

I tried it as FileType or Input but never works.

__init__.py
Code:
from calibre.customize import Plugin

class PBookWrapperPlugin(Plugin):
    name = 'PBook Metadata Reader'
    description = 'Reads metadata from .pbook files (title, authors, tags, ISBN, language)'
    supported_platforms = ['windows', 'osx', 'linux']
    author = 'Your Name'
    version = (1, 0, 0)
    file_types = ['pbook']
    type = 'metadata'

    # Actual plugin implementation
    actual_plugin = 'pbook_main:PBookMetadataReaderPlugin'
pbook_main.py
Code:
import os
from calibre.customize import MetadataReaderPlugin
from calibre.ebooks.metadata.book.base import Metadata

class PBookMetadataReaderPlugin(MetadataReaderPlugin):
    name = "PBook Metadata Reader"
    description = "Reads metadata (title, authors, tags, ISBN, language) from .pbook files"
    supported_platforms = ["windows", "osx", "linux"]
    author = "Your Name"
    version = (1, 0, 0)
    file_types = {"pbook"}

    def get_metadata(self, path_to_ebook, log, abort, *args, **kwargs):
        """
        This is called by Calibre during Add Books.
        path_to_ebook: full path to the .pbook file
        log: logging object
        abort: function to check for user cancellation
        """
        log.info(f"[PBook] get_metadata called for {path_to_ebook}")

        try:
            with open(path_to_ebook, "r", encoding="utf-8") as f:
                raw = f.read()
            log.info(f"[PBook] raw contents:\n{raw}")
        except Exception as e:
            log.error(f"[PBook] Could not read file: {e}")
            return None

        data = self._parse_pbook(raw, log)

        title = data.get("title") or os.path.splitext(os.path.basename(path_to_ebook))[0]
        mi = Metadata(title)

        authors = data.get("author") or data.get("authors")
        if authors:
            mi.authors = [a.strip() for a in authors.split(",")]

        if "isbn" in data:
            mi.isbn = data["isbn"]

        if "tag" in data or "tags" in data:
            mi.tags = [t.strip() for t in (data.get("tag") or data.get("tags")).split(",")]

        if "language" in data:
            mi.language = data["language"]

        log.info(f"[PBook] Metadata extracted: {mi.__dict__}")
        return mi

    def _parse_pbook(self, raw_data, log):
        data = {}
        for line in raw_data.splitlines():
            if "=" not in line:
                continue
            key, val = line.split("=", 1)
            key = key.strip().lower()
            val = val.strip()
            data[key] = val
            log.info(f"[PBook] parsed {key}={val}")
        log.info(f"[PBook] parsed dict: {data}")
        return data
plugin.xml
Code:
<plugin>
    <name>PBook Metadata Reader</name>
    <description>Reads metadata from .pbook files</description>
    <author>Your Name</author>
    <version>1.0.0</version>
    <type>metadata</type>
    <file_types>pbook</file_types>
</plugin>
Can you help me?
what am I doing wrong?

I want the name, author to be read: working
ISBN, Tags, Language to be added to the file: not working

I've tried to debug and the get_metadata() get invoked as the logs (calibre-debug -g) does not show anything

thanks
M
mfsav2 is offline   Reply With Quote