View Single Post
Old 09-06-2014, 03:14 PM   #552
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,476
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by DMee View Post
Chaley, if that checks on a per-item basis it won't trigger for all the books in the series, just those with fractions?
You are right. I answered what I thought was the question, but clearly I was confused.

I have a python "script" that puts the correct format for a series into a custom column. It looks at all books in the series, figures out whether it needs decimals and how many leading zeros (could be underscores) are required, and then writes the correct format for "format_number" to the custom column for each book in the series. The script is run from the command line with calibre-debug -e. I mention this mostly to agree that I was being dense, especially as I had already solved this problem in a different way, but if you are comfortable with this sort of thing you are welcome to use the script appropriately modified.

The script is:
Spoiler:
Code:
def init_cache(library_path):
	from calibre.db.backend import DB
	from calibre.db.cache import Cache
	backend = DB(library_path)
	cache = Cache(backend)
	cache.init()
	return cache

import math
from collections import defaultdict
	
cache = init_cache(library_path = 'path-to-your-library')
series_info = defaultdict(dict)

for id_ in cache.all_book_ids():
	series = cache.field_for('series', id_)
	if series:
		sidx = cache.field_for('series_index', id_)
		str_sidx = str(sidx)
		components = str_sidx.split('.')
		if components[1] == '0':
			fract_part = 0
		else:
			fract_part = len(components[1])
		val_part = len(components[0])
		series_info[series]['val_part'] = max(series_info[series].get('val_part', 0), val_part)
		series_info[series]['fract_part'] = max(series_info[series].get('fract_part', 0), fract_part)
		
for series,sinfo in series_info.iteritems():
	fract_part = sinfo['fract_part']
	format_str = '{0:0%d.%df}'%(sinfo['val_part'] + fract_part + (1 if fract_part > 0 else 0), fract_part)
	sids = cache.search('series:"=' + series + '"')
	dct = {book_id:format_str for book_id in sids}
	cache.set_field('#text', dct)
chaley is offline   Reply With Quote