move debug_print into prints module

This commit is contained in:
un-pogaz 2024-06-24 12:10:40 +02:00
parent da197459ec
commit 4b80970b4c
13 changed files with 52 additions and 47 deletions

View File

@ -4,7 +4,7 @@ __docformat__ = 'restructuredtext en'
import os import os
from calibre.devices.usbms.driver import debug_print from calibre.prints import debug_print
class Bookmark(): # {{{ class Bookmark(): # {{{

View File

@ -9,10 +9,10 @@ from calibre import isbytestring
from calibre.constants import DEBUG, preferred_encoding from calibre.constants import DEBUG, preferred_encoding
from calibre.devices.usbms.books import Book as Book_ from calibre.devices.usbms.books import Book as Book_
from calibre.devices.usbms.books import CollectionsBookList from calibre.devices.usbms.books import CollectionsBookList
from calibre.devices.usbms.driver import debug_print
from calibre.ebooks.metadata import author_to_author_sort from calibre.ebooks.metadata import author_to_author_sort
from calibre.ebooks.metadata.book.base import Metadata from calibre.ebooks.metadata.book.base import Metadata
from calibre.ebooks.metadata.book.formatter import SafeFormat from calibre.ebooks.metadata.book.formatter import SafeFormat
from calibre.prints import debug_print
from calibre.utils.config_base import prefs from calibre.utils.config_base import prefs

View File

@ -25,10 +25,11 @@ from calibre.constants import DEBUG
from calibre.devices.kobo.books import Book, ImageWrapper, KTCollectionsBookList from calibre.devices.kobo.books import Book, ImageWrapper, KTCollectionsBookList
from calibre.devices.mime import mime_type_ext from calibre.devices.mime import mime_type_ext
from calibre.devices.usbms.books import BookList, CollectionsBookList from calibre.devices.usbms.books import BookList, CollectionsBookList
from calibre.devices.usbms.driver import USBMS, debug_print from calibre.devices.usbms.driver import USBMS
from calibre.ebooks.metadata import authors_to_string from calibre.ebooks.metadata import authors_to_string
from calibre.ebooks.metadata.book.base import Metadata from calibre.ebooks.metadata.book.base import Metadata
from calibre.ebooks.metadata.utils import normalize_languages from calibre.ebooks.metadata.utils import normalize_languages
from calibre.prints import debug_print
from calibre.ptempfile import PersistentTemporaryFile, better_mktemp from calibre.ptempfile import PersistentTemporaryFile, better_mktemp
from calibre.utils.config_base import prefs from calibre.utils.config_base import prefs
from calibre.utils.date import parse_date from calibre.utils.date import parse_date

View File

@ -8,12 +8,12 @@ import textwrap
from qt.core import QCheckBox, QDialog, QDialogButtonBox, QGridLayout, QLabel, QLineEdit, QPushButton, QVBoxLayout, QWidget from qt.core import QCheckBox, QDialog, QDialogButtonBox, QGridLayout, QLabel, QLineEdit, QPushButton, QVBoxLayout, QWidget
from calibre.devices.usbms.driver import debug_print
from calibre.gui2 import error_dialog from calibre.gui2 import error_dialog
from calibre.gui2.device_drivers.tabbed_device_config import DeviceConfigTab, DeviceOptionsGroupBox, TabbedDeviceConfig from calibre.gui2.device_drivers.tabbed_device_config import DeviceConfigTab, DeviceOptionsGroupBox, TabbedDeviceConfig
from calibre.gui2.dialogs.template_dialog import TemplateDialog from calibre.gui2.dialogs.template_dialog import TemplateDialog
from calibre.gui2.dialogs.template_line_editor import TemplateLineEditor from calibre.gui2.dialogs.template_line_editor import TemplateLineEditor
from calibre.gui2.widgets2 import ColorButton from calibre.gui2.widgets2 import ColorButton
from calibre.prints import debug_print
def wrap_msg(msg): def wrap_msg(msg):

View File

@ -14,7 +14,8 @@ from contextlib import closing
from calibre.devices.errors import DeviceError from calibre.devices.errors import DeviceError
from calibre.devices.mime import mime_type_ext from calibre.devices.mime import mime_type_ext
from calibre.devices.usbms.books import BookList, CollectionsBookList from calibre.devices.usbms.books import BookList, CollectionsBookList
from calibre.devices.usbms.driver import USBMS, debug_print from calibre.devices.usbms.driver import USBMS
from calibre.prints import debug_print
DBPATH = 'paladin/database/books.db' DBPATH = 'paladin/database/books.db'

View File

@ -13,7 +13,8 @@ import time
from calibre import __appname__, fsync, prints from calibre import __appname__, fsync, prints
from calibre.devices.prs505 import CACHE_EXT, CACHE_THUMBNAIL, CACHE_XML, MEDIA_EXT, MEDIA_THUMBNAIL, MEDIA_XML from calibre.devices.prs505 import CACHE_EXT, CACHE_THUMBNAIL, CACHE_XML, MEDIA_EXT, MEDIA_THUMBNAIL, MEDIA_XML
from calibre.devices.usbms.books import CollectionsBookList from calibre.devices.usbms.books import CollectionsBookList
from calibre.devices.usbms.driver import USBMS, debug_print from calibre.devices.usbms.driver import USBMS
from calibre.prints import debug_print
class PRS505(USBMS): class PRS505(USBMS):

View File

@ -12,9 +12,9 @@ from datetime import date
from calibre import fsync, guess_type, isbytestring, prints from calibre import fsync, guess_type, isbytestring, prints
from calibre.constants import DEBUG, preferred_encoding from calibre.constants import DEBUG, preferred_encoding
from calibre.devices.errors import DeviceError from calibre.devices.errors import DeviceError
from calibre.devices.usbms.driver import debug_print
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
from calibre.ebooks.metadata import authors_to_sort_string, authors_to_string, title_sort from calibre.ebooks.metadata import authors_to_sort_string, authors_to_string, title_sort
from calibre.prints import debug_print
from polyglot.binary import from_base64_bytes from polyglot.binary import from_base64_bytes
''' '''

View File

@ -21,8 +21,9 @@ from calibre.devices.errors import DeviceError
from calibre.devices.mime import mime_type_ext from calibre.devices.mime import mime_type_ext
from calibre.devices.usbms.books import BookList, CollectionsBookList from calibre.devices.usbms.books import BookList, CollectionsBookList
from calibre.devices.usbms.device import USBDevice from calibre.devices.usbms.device import USBDevice
from calibre.devices.usbms.driver import USBMS, debug_print from calibre.devices.usbms.driver import USBMS
from calibre.ebooks.metadata import authors_to_sort_string, authors_to_string from calibre.ebooks.metadata import authors_to_sort_string, authors_to_string
from calibre.prints import debug_print
from polyglot.builtins import long_type from polyglot.builtins import long_type
DBPATH = 'Sony_Reader/database/books.db' DBPATH = 'Sony_Reader/database/books.db'

View File

@ -14,6 +14,7 @@ from calibre.devices.interface import BookList as _BookList
from calibre.devices.mime import mime_type_ext from calibre.devices.mime import mime_type_ext
from calibre.ebooks.metadata import title_sort from calibre.ebooks.metadata import title_sort
from calibre.ebooks.metadata.book.base import Metadata from calibre.ebooks.metadata.book.base import Metadata
from calibre.prints import debug_print
from calibre.utils.config_base import tweaks from calibre.utils.config_base import tweaks
from calibre.utils.icu import sort_key from calibre.utils.icu import sort_key
from polyglot.builtins import cmp, iteritems, itervalues, string_or_bytes from polyglot.builtins import cmp, iteritems, itervalues, string_or_bytes
@ -158,7 +159,6 @@ class CollectionsBookList(BookList):
return cat_name.strip() return cat_name.strip()
def get_collections(self, collection_attributes): def get_collections(self, collection_attributes):
from calibre.devices.usbms.driver import debug_print
from calibre.utils.config import device_prefs from calibre.utils.config import device_prefs
debug_print('Starting get_collections:', device_prefs['manage_device_metadata']) debug_print('Starting get_collections:', device_prefs['manage_device_metadata'])
debug_print('Renaming rules:', tweaks['sony_collection_renaming_rules']) debug_print('Renaming rules:', tweaks['sony_collection_renaming_rules'])

View File

@ -11,52 +11,18 @@ for a particular device.
import json import json
import os import os
import shutil import shutil
import time
from itertools import cycle from itertools import cycle
from calibre import fsync, isbytestring, prints from calibre import fsync, isbytestring, prints
from calibre.constants import filesystem_encoding, is_debugging, ismacos, numeric_version from calibre.constants import filesystem_encoding, ismacos, numeric_version
from calibre.devices.usbms.books import Book, BookList from calibre.devices.usbms.books import Book, BookList
from calibre.devices.usbms.cli import CLI from calibre.devices.usbms.cli import CLI
from calibre.devices.usbms.device import Device from calibre.devices.usbms.device import Device
from calibre.ebooks.metadata.book.json_codec import JsonCodec from calibre.ebooks.metadata.book.json_codec import JsonCodec
from calibre.prints import debug_print
from polyglot.builtins import itervalues, string_or_bytes from polyglot.builtins import itervalues, string_or_bytes
def debug_print(*args, **kw):
'''
Prints debug information to the console if debugging is enabled.
This function prints a message prefixed with a timestamp showing the elapsed time
since the first call to this function. The message is printed only if debugging is enabled.
Parameters:
*args : tuple
Variable length argument list to be printed.
**kw : dict
Arbitrary keyword arguments to be passed to the `print` function.
Attributes:
base_time : float
The timestamp of the first call to this function. Stored as an attribute of the function.
Behavior:
- On the first call, initializes `base_time` to the current time using `time.monotonic()`.
- If `is_debugging()` returns True, prints the elapsed time since `base_time` along with the provided arguments.
'''
# Get the base_time attribute, initializing it on the first call
base_time = getattr(debug_print, 'base_time', None)
if base_time is None:
# Set base_time to the current monotonic time if it hasn't been set
debug_print.base_time = base_time = time.monotonic()
# Check if debugging is enabled
if is_debugging():
# Print the elapsed time and the provided arguments if debugging is enabled
prints('DEBUG: %6.1f' % (time.monotonic() - base_time), *args, **kw)
def safe_walk(top, topdown=True, onerror=None, followlinks=False, maxdepth=128): def safe_walk(top, topdown=True, onerror=None, followlinks=False, maxdepth=128):
' A replacement for os.walk that does not die when it encounters undecodeable filenames in a linux filesystem' ' A replacement for os.walk that does not die when it encounters undecodeable filenames in a linux filesystem'
if maxdepth < 0: if maxdepth < 0:

View File

@ -30,7 +30,7 @@ from calibre.devices.errors import (
from calibre.devices.folder_device.driver import FOLDER_DEVICE from calibre.devices.folder_device.driver import FOLDER_DEVICE
from calibre.devices.interface import DevicePlugin, currently_connected_device from calibre.devices.interface import DevicePlugin, currently_connected_device
from calibre.devices.scanner import DeviceScanner from calibre.devices.scanner import DeviceScanner
from calibre.devices.usbms.driver import debug_print from calibre.prints import debug_print
from calibre.ebooks.covers import cprefs, generate_cover, override_prefs, scale_cover from calibre.ebooks.covers import cprefs, generate_cover, override_prefs, scale_cover
from calibre.ebooks.metadata import authors_to_string from calibre.ebooks.metadata import authors_to_string
from calibre.gui2 import ( from calibre.gui2 import (

View File

@ -23,7 +23,7 @@ from qt.core import (
QWidget, QWidget,
) )
from calibre.devices.usbms.driver import debug_print from calibre.prints import debug_print
from calibre.ebooks import BOOK_EXTENSIONS from calibre.ebooks import BOOK_EXTENSIONS
from calibre.gui2.device_drivers.mtp_config import FormatsConfig, TemplateConfig from calibre.gui2.device_drivers.mtp_config import FormatsConfig, TemplateConfig

View File

@ -3,6 +3,7 @@
import io import io
import sys import sys
import time
from polyglot.builtins import as_bytes, as_unicode from polyglot.builtins import as_bytes, as_unicode
@ -44,3 +45,37 @@ def prints(*a, **kw):
stream.flush() stream.flush()
except Exception: except Exception:
pass pass
def debug_print(*args, **kw):
'''
Prints debug information to the console if debugging is enabled.
This function prints a message prefixed with a timestamp showing the elapsed time
since the first call to this function. The message is printed only if debugging is enabled.
Parameters:
*args : tuple
Variable length argument list to be printed.
**kw : dict
Arbitrary keyword arguments to be passed to the `print` function.
Attributes:
base_time : float
The timestamp of the first call to this function. Stored as an attribute of the function.
Behavior:
- On the first call, initializes `base_time` to the current time using `time.monotonic()`.
- If `is_debugging()` returns True, prints the elapsed time since `base_time` along with the provided arguments.
'''
from calibre.constants import is_debugging
# Get the base_time attribute, initializing it on the first call
base_time = getattr(debug_print, 'base_time', None)
if base_time is None:
# Set base_time to the current monotonic time if it hasn't been set
debug_print.base_time = base_time = time.monotonic()
# Check if debugging is enabled
if is_debugging():
# Print the elapsed time and the provided arguments if debugging is enabled
prints('DEBUG: %6.1f' % (time.monotonic() - base_time), *args, **kw)