Python Scripting

     

ThumbsPlus uses Python as its internal scripting language, and you can use it too. Python is not a difficult language to learn, and several of the existing features in ThumbsPlus are written mostly or all in Python.

ThumbsPlus version 8 uses Python version 2.7.1. It is likely that version 9 will use Python 3, but we haven't decided for sure. For information, tutorials, editors and more, please visit the official Python site at http://www.python.org.

Here is an example script, which moves thumbnails from the database to separate files. This option is designed for Access (.tpdb8) and Sqlite3 (.tpdb8s). Client/server databases should not have any trouble storing many terabytes within the database. The script is stored as:

 

 ThumbsPlus\Python\Lib\Thumb\thumbstofiles.py

import sys

import os

import time

import _tpc

import _tp

import _tpd

import tpdb

 

def move_thumbs():

    _tp.doing_start(u'Moving Thumbnails to External Files'3)

    _tpd.begin()

    cnames = _tpd.colnames()

    print cnames

    rowcnt = tpdb.get_count(u'Thumbnail')

    basedir = tpdb.get_db_thumb_path()

    if not os.path.isdir(basedir):

        os.makedirs(basedir)

    sqlcmd = u'select idThumb, thumbnail from Thumbnail where Thumbnail.thumbnail is not null'

    _tpd.execute(sqlcmd)

    while (True):

        rowres = _tpd.fetch()

        if (not rowres):

            break

        id = rowres[0]

        thumb = rowres[1]

        if (thumb == None):

            continue               # Already moved I hope!

        d1 = id / (1627 * 1627);

        d2 = (id - (d1 * (1627 * 1627))) / 1627;

        d3 = id % 1627;

        filename = u'%s\\%03x\\%03x\\%03x.tn' % (basedir, d1, d2, d3)

        if not os.path.isdir(filename[:7]):

            try:

                os.makedirs(filename[:7])

            except:

                print u'Unable to create folder <%s>' % filename[:7]

                continue;

        try:

            f = open(filename, 'wb')

        except:

            print u'Unable to create thumbnail file <%s>' % filename

            continue

        f.write(thumb)

        f.close()

        done += 1

        _tp.doing_percent(done, rowcnt)

        if _tp.doing_abort():

            break

        tpdb.execute(u'update Thumbnail set Thumbnail.thumbnail = NULL')

    _tp.doing_done()

    return True