gallerifrey.py
def readCategories(images):
    """Reads the category files and returns a pair of tables listing all
relationships, the first by (category -> file set) and the second by
(file -> category set)
"""
    categoryFiles = os.listdir(CATEGORYDIR)
    categoryFiles = filter(lambda f: not os.path.isdir(f), categoryFiles)
    categoryFiles = filter(lambda f: not fileHasExt(f, '.bak'), categoryFiles)
    categoryFiles = filter(lambda f: not fileHasExt(f, '.tmp'), categoryFiles)
    imagesByCategory = {}
    categoriesByImage = {}
    # the 'all' category contains all images
    imagesByCategory['all'] = set(images)

    for i in images:
        categoriesByImage[i] = {'all'}
    # We also always have an 'uncategorized' category
    imagesByCategory['uncategorized'] = set()
    # Now we slurp up the category file and add all images
    # listed in it to that category
    for cf in categoryFiles:
        # We skip the auto-generated categories; they don't
        # read their contents from the files!
        if cf in ['all', 'uncategorized']:
            continue
        category = set()
        with open(os.path.join(CATEGORYDIR, cf)) as f:
            for imgname in f.xreadlines():
                imgname = imgname.strip()
                category.add(imgname)

                # While we are at it, we add that category to the
                # set of categories that image is in.
                if not categoriesByImage.has_key(imgname):
                    categoriesByImage[imgname] = set()
                categoriesByImage[imgname].add(cf)
        imagesByCategory[cf] = category
    
    # The 'uncategorized' category contains all images
    # without other defined categories.
    for (image,cats) in categoriesByImage.iteritems():
        num = len(cats)
        if num == 1:
            # Only the 'all' category, add 'uncategorized'
            categoriesByImage[image].add('uncategorized')
            imagesByCategory['uncategorized'].add(image)
        elif num > 2 and 'uncategorized' in cats:
            # We have 'all', 'uncategorized' and something else;
            # remove uncategorized.
            categoriesByImage[image].remove('uncategorized')
            imagesByCategory['uncategorized'].remove(image)

    # Though it is inelegant, we turn the sets into sorted lists here
    # since it is inevitable that we do so anyway for output, so we might
    # as well do it once all in the same place.
    ibyc = {}
    cbyi = {}
    for (c,i) in imagesByCategory.iteritems():
        l = list(i)
        l.sort()
        ibyc[c] = l
    for (i,c) in categoriesByImage.iteritems():
        l = list(c)
        l.sort()
        cbyi[i] = l
    return (ibyc, cbyi)
Reads the category files and returns a pair of tables listing all
relationships, the first by (category -> file set) and the second by
(file -> category set)

Reads the category files and returns a pair of tables listing all
relationships, the first by (category -> file set) and the second by
(file -> category set)
Reads the category files and returns a pair of tables listing all relationships the first by ( category - > file set ) and the second by ( file - > category set )
def readCategories(images):
    categoryFiles = os.listdir(CATEGORYDIR)
    categoryFiles = filter(lambda f: not os.path.isdir(f), categoryFiles)
    categoryFiles = filter(lambda f: not fileHasExt(f, '.bak'), categoryFiles)
    categoryFiles = filter(lambda f: not fileHasExt(f, '.tmp'), categoryFiles)
    imagesByCategory = {}
    categoriesByImage = {}
    imagesByCategory['all'] = set(images)
    for i in images:
        categoriesByImage[i] = {'all'}
    imagesByCategory['uncategorized'] = set()
    for cf in categoryFiles:
        if cf in ['all', 'uncategorized']:
            continue
        category = set()
        with open(os.path.join(CATEGORYDIR, cf)) as f:
            for imgname in f.xreadlines():
                imgname = imgname.strip()
                category.add(imgname)
                if not categoriesByImage.has_key(imgname):
                    categoriesByImage[imgname] = set()
                categoriesByImage[imgname].add(cf)
        imagesByCategory[cf] = category
    for (image,cats) in categoriesByImage.iteritems():
        num = len(cats)
        if num == 1:
            categoriesByImage[image].add('uncategorized')
            imagesByCategory['uncategorized'].add(image)
        elif num > 2 and 'uncategorized' in cats:
            categoriesByImage[image].remove('uncategorized')
            imagesByCategory['uncategorized'].remove(image)
    ibyc = {}
    cbyi = {}
    for (c,i) in imagesByCategory.iteritems():
        l = list(i)
        l.sort()
        ibyc[c] = l
    for (i,c) in categoriesByImage.iteritems():
        l = list(c)
        l.sort()
        cbyi[i] = l
    return (ibyc, cbyi)
def readCategories ( images ) : categoryFiles = os . listdir ( CATEGORYDIR ) categoryFiles = filter ( lambda f : not os . path . isdir ( f ) , categoryFiles ) categoryFiles = filter ( lambda f : not fileHasExt ( f , '.bak' ) , categoryFiles ) categoryFiles = filter ( lambda f : not fileHasExt ( f , '.tmp' ) , categoryFiles ) imagesByCategory = { } categoriesByImage = { } imagesByCategory [ 'all' ] = set ( images ) for i in images : categoriesByImage [ i ] = { 'all' } imagesByCategory [ 'uncategorized' ] = set ( ) for cf in categoryFiles : if cf in [ 'all' , 'uncategorized' ] : continue category = set ( ) with open ( os . path . join ( CATEGORYDIR , cf ) ) as f : for imgname in f . xreadlines ( ) : imgname = imgname . strip ( ) category . add ( imgname ) if not categoriesByImage . has_key ( imgname ) : categoriesByImage [ imgname ] = set ( ) categoriesByImage [ imgname ] . add ( cf ) imagesByCategory [ cf ] = category for ( image , cats ) in categoriesByImage . iteritems ( ) : num = len ( cats ) if num == 1 : categoriesByImage [ image ] . add ( 'uncategorized' ) imagesByCategory [ 'uncategorized' ] . add ( image ) elif num > 2 and 'uncategorized' in cats : categoriesByImage [ image ] . remove ( 'uncategorized' ) imagesByCategory [ 'uncategorized' ] . remove ( image ) ibyc = { } cbyi = { } for ( c , i ) in imagesByCategory . iteritems ( ) : l = list ( i ) l . sort ( ) ibyc [ c ] = l for ( i , c ) in categoriesByImage . iteritems ( ) : l = list ( c ) l . sort ( ) cbyi [ i ] = l return ( ibyc , cbyi )
Reads the category files and returns a pair of tables listing all
relationships, the first by (category -> file set) and the second by
(file -> category set)
Reads the category files and returns a pair of tables listing all relationships the first by ( category - > file set ) and the second by ( file - > category set )
"""Reads the category files and returns a pair of tables listing all
relationships, the first by (category -> file set) and the second by
(file -> category set)
""" # the 'all' category contains all images # We also always have an 'uncategorized' category # Now we slurp up the category file and add all images # listed in it to that category # We skip the auto-generated categories; they don't # read their contents from the files! # While we are at it, we add that category to the # set of categories that image is in. # The 'uncategorized' category contains all images # without other defined categories. # Only the 'all' category, add 'uncategorized' # We have 'all', 'uncategorized' and something else; # remove uncategorized. # Though it is inelegant, we turn the sets into sorted lists here # since it is inevitable that we do so anyway for output, so we might # as well do it once all in the same place.
images