mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Enhancement #7892: New tweak options for series number increment
This commit is contained in:
parent
95fcf6d3af
commit
b12e505f5c
@ -12,13 +12,24 @@ defaults.
|
|||||||
|
|
||||||
|
|
||||||
# The algorithm used to assign a new book in an existing series a series number.
|
# The algorithm used to assign a new book in an existing series a series number.
|
||||||
|
# New series numbers assigned using this tweak are always integer values, except
|
||||||
|
# if a constant non-integer is specified.
|
||||||
# Possible values are:
|
# Possible values are:
|
||||||
# next - Next available number
|
# next - First available integer larger than the largest existing number
|
||||||
|
# first_free - First available integer larger than 0
|
||||||
|
# next_free - First available integer larger than the smallest existing number
|
||||||
|
# last_free - First available integer smaller than the largest existing number
|
||||||
|
# Return largest existing + 1 if no free number is found
|
||||||
# const - Assign the number 1 always
|
# const - Assign the number 1 always
|
||||||
|
# a number - Assign that number always. The number is not in quotes. Note that
|
||||||
|
# 0.0 can be used here.
|
||||||
|
# Examples:
|
||||||
|
# series_index_auto_increment = 'next'
|
||||||
|
# series_index_auto_increment = 'next_free'
|
||||||
|
# series_index_auto_increment = 16.5
|
||||||
series_index_auto_increment = 'next'
|
series_index_auto_increment = 'next'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# The algorithm used to copy author to author_sort
|
# The algorithm used to copy author to author_sort
|
||||||
# Possible values are:
|
# Possible values are:
|
||||||
# invert: use "fn ln" -> "ln, fn" (the original algorithm)
|
# invert: use "fn ln" -> "ln, fn" (the original algorithm)
|
||||||
|
@ -303,7 +303,7 @@ class Series(Base):
|
|||||||
if val == '':
|
if val == '':
|
||||||
val = s_index = None
|
val = s_index = None
|
||||||
elif s_index == 0.0:
|
elif s_index == 0.0:
|
||||||
if tweaks['series_index_auto_increment'] == 'next':
|
if tweaks['series_index_auto_increment'] != 'const':
|
||||||
s_index = self.db.get_next_cc_series_num_for(val,
|
s_index = self.db.get_next_cc_series_num_for(val,
|
||||||
num=self.col_id)
|
num=self.col_id)
|
||||||
else:
|
else:
|
||||||
@ -572,7 +572,6 @@ class BulkSeries(BulkBase):
|
|||||||
val = None if clear else self.normalize_ui_val(val)
|
val = None if clear else self.normalize_ui_val(val)
|
||||||
if clear or val != '':
|
if clear or val != '':
|
||||||
extras = []
|
extras = []
|
||||||
next_index = self.db.get_next_cc_series_num_for(val, num=self.col_id)
|
|
||||||
for book_id in book_ids:
|
for book_id in book_ids:
|
||||||
if clear:
|
if clear:
|
||||||
extras.append(None)
|
extras.append(None)
|
||||||
@ -581,9 +580,8 @@ class BulkSeries(BulkBase):
|
|||||||
if force_start:
|
if force_start:
|
||||||
s_index = at_value
|
s_index = at_value
|
||||||
at_value += 1
|
at_value += 1
|
||||||
elif tweaks['series_index_auto_increment'] == 'next':
|
elif tweaks['series_index_auto_increment'] != 'const':
|
||||||
s_index = next_index
|
s_index = self.db.get_next_cc_series_num_for(val, num=self.col_id)
|
||||||
next_index += 1
|
|
||||||
else:
|
else:
|
||||||
s_index = 1.0
|
s_index = 1.0
|
||||||
else:
|
else:
|
||||||
|
@ -839,7 +839,7 @@ class MetadataSingleDialog(ResizableDialog, Ui_MetadataSingleDialog):
|
|||||||
series = unicode(self.series.text()).strip()
|
series = unicode(self.series.text()).strip()
|
||||||
if series and series != self.original_series_name:
|
if series and series != self.original_series_name:
|
||||||
ns = 1
|
ns = 1
|
||||||
if tweaks['series_index_auto_increment'] == 'next':
|
if tweaks['series_index_auto_increment'] != 'const':
|
||||||
ns = self.db.get_next_series_num_for(series)
|
ns = self.db.get_next_series_num_for(series)
|
||||||
self.series_index.setValue(ns)
|
self.series_index.setValue(ns)
|
||||||
self.original_series_name = series
|
self.original_series_name = series
|
||||||
|
@ -772,7 +772,7 @@ class BooksModel(QAbstractTableModel): # {{{
|
|||||||
self.db.set_series_index(id, float(match.group(1)))
|
self.db.set_series_index(id, float(match.group(1)))
|
||||||
val = pat.sub('', val).strip()
|
val = pat.sub('', val).strip()
|
||||||
elif val:
|
elif val:
|
||||||
if tweaks['series_index_auto_increment'] == 'next':
|
if tweaks['series_index_auto_increment'] != 'const':
|
||||||
ni = self.db.get_next_series_num_for(val)
|
ni = self.db.get_next_series_num_for(val)
|
||||||
if ni != 1:
|
if ni != 1:
|
||||||
self.db.set_series_index(id, ni)
|
self.db.set_series_index(id, ni)
|
||||||
|
@ -707,7 +707,7 @@ def parse_series_string(db, label, value):
|
|||||||
val = pat.sub('', val).strip()
|
val = pat.sub('', val).strip()
|
||||||
s_index = float(match.group(1))
|
s_index = float(match.group(1))
|
||||||
elif val:
|
elif val:
|
||||||
if tweaks['series_index_auto_increment'] == 'next':
|
if tweaks['series_index_auto_increment'] != 'const':
|
||||||
s_index = db.get_next_cc_series_num_for(val, label=label)
|
s_index = db.get_next_cc_series_num_for(val, label=label)
|
||||||
else:
|
else:
|
||||||
s_index = 1.0
|
s_index = 1.0
|
||||||
|
@ -8,12 +8,12 @@ __docformat__ = 'restructuredtext en'
|
|||||||
|
|
||||||
import json, re
|
import json, re
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from math import floor
|
|
||||||
|
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
from calibre.constants import preferred_encoding
|
from calibre.constants import preferred_encoding
|
||||||
from calibre.library.field_metadata import FieldMetadata
|
from calibre.library.field_metadata import FieldMetadata
|
||||||
from calibre.utils.date import parse_date
|
from calibre.utils.date import parse_date
|
||||||
|
from calibre.utils.config import tweaks
|
||||||
|
|
||||||
class CustomColumns(object):
|
class CustomColumns(object):
|
||||||
|
|
||||||
@ -261,15 +261,15 @@ class CustomColumns(object):
|
|||||||
series_id = self.conn.get('SELECT id from %s WHERE value=?'%table,
|
series_id = self.conn.get('SELECT id from %s WHERE value=?'%table,
|
||||||
(series,), all=False)
|
(series,), all=False)
|
||||||
if series_id is None:
|
if series_id is None:
|
||||||
|
if isinstance(tweaks['series_index_auto_increment'], (int, float)):
|
||||||
|
return float(tweaks['series_index_auto_increment'])
|
||||||
return 1.0
|
return 1.0
|
||||||
# get the label of the associated series number table
|
series_indices = self.conn.get('''
|
||||||
series_num = self.conn.get('''
|
SELECT {lt}.extra FROM {lt}
|
||||||
SELECT MAX({lt}.extra) FROM {lt}
|
|
||||||
WHERE {lt}.book IN (SELECT book FROM {lt} where value=?)
|
WHERE {lt}.book IN (SELECT book FROM {lt} where value=?)
|
||||||
'''.format(lt=lt), (series_id,), all=False)
|
ORDER BY {lt}.extra
|
||||||
if series_num is None:
|
'''.format(lt=lt), (series_id,))
|
||||||
return 1.0
|
return self._get_next_series_num_for_list(series_indices)
|
||||||
return floor(series_num+1)
|
|
||||||
|
|
||||||
def all_custom(self, label=None, num=None):
|
def all_custom(self, label=None, num=None):
|
||||||
if label is not None:
|
if label is not None:
|
||||||
|
@ -8,7 +8,7 @@ The database used to store ebook metadata
|
|||||||
'''
|
'''
|
||||||
import os, sys, shutil, cStringIO, glob, time, functools, traceback, re
|
import os, sys, shutil, cStringIO, glob, time, functools, traceback, re
|
||||||
from itertools import repeat
|
from itertools import repeat
|
||||||
from math import floor
|
from math import ceil
|
||||||
from Queue import Queue
|
from Queue import Queue
|
||||||
|
|
||||||
from PyQt4.QtGui import QImage
|
from PyQt4.QtGui import QImage
|
||||||
@ -1365,14 +1365,43 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
|||||||
series_id = self.conn.get('SELECT id from series WHERE name=?',
|
series_id = self.conn.get('SELECT id from series WHERE name=?',
|
||||||
(series,), all=False)
|
(series,), all=False)
|
||||||
if series_id is None:
|
if series_id is None:
|
||||||
|
if isinstance(tweaks['series_index_auto_increment'], (int, float)):
|
||||||
|
return float(tweaks['series_index_auto_increment'])
|
||||||
return 1.0
|
return 1.0
|
||||||
series_num = self.conn.get(
|
series_indices = self.conn.get(
|
||||||
('SELECT MAX(series_index) FROM books WHERE id IN '
|
('SELECT series_index FROM books WHERE id IN '
|
||||||
'(SELECT book FROM books_series_link where series=?)'),
|
'(SELECT book FROM books_series_link where series=?) '
|
||||||
(series_id,), all=False)
|
'ORDER BY series_index'),
|
||||||
if series_num is None:
|
(series_id,))
|
||||||
|
return self._get_next_series_num_for_list(series_indices)
|
||||||
|
|
||||||
|
def _get_next_series_num_for_list(self, series_indices):
|
||||||
|
if not series_indices:
|
||||||
|
if isinstance(tweaks['series_index_auto_increment'], (int, float)):
|
||||||
|
return float(tweaks['series_index_auto_increment'])
|
||||||
|
return 1.0
|
||||||
|
series_indices = [x[0] for x in series_indices]
|
||||||
|
print series_indices
|
||||||
|
if tweaks['series_index_auto_increment'] == 'next':
|
||||||
|
return series_indices[-1] + 1
|
||||||
|
if tweaks['series_index_auto_increment'] == 'first_free':
|
||||||
|
for i in range(1, 10000):
|
||||||
|
if i not in series_indices:
|
||||||
|
return i
|
||||||
|
# really shouldn't get here.
|
||||||
|
if tweaks['series_index_auto_increment'] == 'next_free':
|
||||||
|
for i in range(int(ceil(series_indices[0])), 10000):
|
||||||
|
if i not in series_indices:
|
||||||
|
return i
|
||||||
|
# really shouldn't get here.
|
||||||
|
if tweaks['series_index_auto_increment'] == 'last_free':
|
||||||
|
for i in range(int(ceil(series_indices[-1])), 0, -1):
|
||||||
|
if i not in series_indices:
|
||||||
|
return i
|
||||||
|
return series_indices[-1] + 1
|
||||||
|
if isinstance(tweaks['series_index_auto_increment'], (int, float)):
|
||||||
|
return float(tweaks['series_index_auto_increment'])
|
||||||
return 1.0
|
return 1.0
|
||||||
return floor(series_num+1)
|
|
||||||
|
|
||||||
def set(self, row, column, val):
|
def set(self, row, column, val):
|
||||||
'''
|
'''
|
||||||
@ -1760,18 +1789,17 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
|
|||||||
FROM books, books_series_link as lt
|
FROM books, books_series_link as lt
|
||||||
WHERE books.id = lt.book AND lt.series=?
|
WHERE books.id = lt.book AND lt.series=?
|
||||||
ORDER BY books.series_index''', (old_id,))
|
ORDER BY books.series_index''', (old_id,))
|
||||||
# Get the next series index
|
|
||||||
index = self.get_next_series_num_for(new_name)
|
|
||||||
# Now update the link table
|
# Now update the link table
|
||||||
self.conn.execute('''UPDATE books_series_link
|
self.conn.execute('''UPDATE books_series_link
|
||||||
SET series=?
|
SET series=?
|
||||||
WHERE series=?''',(new_id, old_id,))
|
WHERE series=?''',(new_id, old_id,))
|
||||||
# Now set the indices
|
# Now set the indices
|
||||||
for (book_id,) in books:
|
for (book_id,) in books:
|
||||||
|
# Get the next series index
|
||||||
|
index = self.get_next_series_num_for(new_name)
|
||||||
self.conn.execute('''UPDATE books
|
self.conn.execute('''UPDATE books
|
||||||
SET series_index=?
|
SET series_index=?
|
||||||
WHERE id=?''',(index, book_id,))
|
WHERE id=?''',(index, book_id,))
|
||||||
index = index + 1
|
|
||||||
self.dirty_books_referencing('series', new_id, commit=False)
|
self.dirty_books_referencing('series', new_id, commit=False)
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user