I decided to give it a try because I need more experience with the new calibre db layer.
Here is a standalone python program built on calibre's db layer that does what I think you want. It fills in a custom text column with the proper format for a series, computed by looking at all the books in the series.
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 = 'd:/cbh_data/calibre.git/library.test_small')
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)