# copyright 2007 ETH Zurich, DMATL
# author lorenz textor

OBJECTS = ['Silva Image']
CONTAINERS = ['Silva Folder','Silva Publication']

result = {'folders':0, 'image_objects':[]}
missing = []
problems = []

def scan_images(this_container, result):
    for object in this_container.objectValues(OBJECTS):
        try:
            web_format = object.getWebFormat()
            web_scale = object.getWebScale()
            web_crop = object.getWebCrop()
            if web_format=='unknown':
                id = object.getId()
                if id[-3:]=='png':
                    web_format = 'PNG'
                elif id[-3:]=='GIF':
                    web_format = 'GIF'
                else:
                    web_format = 'JPEG'
            if object.get_file_size()!=0:
                tmp_scale = web_scale!='50%' and '50%' or '80%'
                object.set_web_presentation_properties(web_format, tmp_scale, web_crop)
                object.set_web_presentation_properties(web_format, web_scale, web_crop)
                result['image_objects'].append('Object: %s - Scale: %s - Format: %s - Crop: %s' %(object.absolute_url(),web_scale,web_format,web_crop))
            elif object.get_file_size()==0:
                missing.append('Object: %s - Scale: %s - Format: %s - Crop: %s' %(object.absolute_url(),web_scale,web_format,web_crop))
        except:
            problems.append('Object: %s' %(object.absolute_url()))
    return result

def scan_images_in(this_container, result):
    result['folders'] = result['folders'] + 1
    result = scan_images(this_container, result)
    for folder in this_container.objectValues(CONTAINERS):
        result = scan_images_in(folder, result)
    return result

result = scan_images_in(container, result)
out_html = '<p>Number of folders checked: %i</p><p>Number of image objects adjusted: %i</p><ul><li>%s</li></ul>'
print out_html %(int(result['folders']), len(result['image_objects']), '</li><li>'.join(result['image_objects']))
if missing:
    out_html = '<p><p>Number of image objects missing on the filesystem: %i</p><ul><li>%s</li></ul>'
    print out_html %(len(missing), '</li><li>'.join(missing))
if problems:
    out_html = '<p><p>Number of "completely" broken images: %i</p><ul><li>%s</li></ul>'
    print out_html %(len(problems), '</li><li>'.join(problems))
return printed

