Example #1
0
def make_images(image_dir, default_image, make_thumbnails=True):
    """Use nodejs to make images and (optionally) thumbnails"""

    can_save = savechart_available()
    if not can_save:
        warnings.warn('Node is not correctly configured: cannot save images.')

    if not os.path.exists(image_dir):
        os.makedirs(image_dir)

    # store hashes so that we know whether images need to be generated
    hash_file = os.path.join(image_dir, '_image_hashes.json')

    if os.path.exists(hash_file):
        with open(hash_file) as f:
            hashes = json.load(f)
    else:
        hashes = {}

    for example in iter_examples_with_metadata():
        filename = example['name'] + '.png'
        image_file = os.path.join(image_dir, filename)

        # check whether image already exists
        spec = example['spec']
        spec_hash = dict_hash(spec)
        if hashes.get(filename, '') == spec_hash:
            continue

        if can_save:
            chart = Chart.from_dict(spec)
            try:
                print('-> saving {0}'.format(image_file))
                savechart(chart, image_file)
            except NodeExecError:
                warnings.warn(
                    'Node is not correctly configured: cannot save images.')
                can_save = False
                if not os.path.exists(image_file):
                    shutil.copyfile(default_image, image_file)
            else:
                hashes[filename] = spec_hash
        elif not os.path.exists(image_file):
            shutil.copyfile(default_image, image_file)

        if make_thumbnails:
            convert_pct = lambda x: 0.01 * int(x.strip('%'))
            params = example.get('galleryParameters', {})

            zoom = params.get('backgroundSize', '100%')
            if zoom == 'contains': zoom = '100%'
            zoom = convert_pct(zoom)

            #position = params.get('backgroundPosition', '0% 0%')
            #if position == 'left': position = '0% 0%'
            #xoffset, yoffset = map(convert_pct, position.split())

            thumb_file = os.path.join(image_dir,
                                      example['name'] + '-thumb.png')
            create_thumbnail(image_file, thumb_file, zoom=zoom)

    # Save hashes so we know whether we need to re-generate plots
    if hashes:
        with open(hash_file, 'w') as f:
            json.dump(hashes, f)
Example #2
0
def test_savechart_svg():
    chart = make_chart()

    with tempfile.NamedTemporaryFile(suffix='.svg') as f:
        savechart(chart, f.name)
        assert consistent_with_svg(f.name)
Example #3
0
def make_images(image_dir, default_image, make_thumbnails=True):
    """Use nodejs to make images and (optionally) thumbnails"""

    can_save = savechart_available()
    if not can_save:
        warnings.warn('Node is not correctly configured: cannot save images.')

    if not os.path.exists(image_dir):
        os.makedirs(image_dir)

    # store hashes so that we know whether images need to be generated
    hash_file = os.path.join(image_dir, '_image_hashes.json')

    if os.path.exists(hash_file):
        with open(hash_file) as f:
            hashes = json.load(f)
    else:
        hashes = {}

    for example in iter_examples_with_metadata():
        filename = example['name'] + '.png'
        image_file = os.path.join(image_dir, filename)

        # check whether image already exists
        spec = example['spec']
        spec_hash = dict_hash(spec)
        if hashes.get(filename, '') == spec_hash:
            continue

        if can_save:
            chart = Chart.from_dict(spec)
            try:
                print('-> saving {0}'.format(image_file))
                savechart(chart, image_file)
            except NodeExecError:
                warnings.warn('Node is not correctly configured: cannot save images.')
                can_save = False
                if not os.path.exists(image_file):
                    shutil.copyfile(default_image, image_file)
            else:
                hashes[filename] = spec_hash
        elif not os.path.exists(image_file):
            shutil.copyfile(default_image, image_file)

        if make_thumbnails:
            convert_pct = lambda x: 0.01 * int(x.strip('%'))
            params = example.get('galleryParameters', {})

            zoom = params.get('backgroundSize', '100%')
            if zoom == 'contains': zoom = '100%'
            zoom = convert_pct(zoom)

            #position = params.get('backgroundPosition', '0% 0%')
            #if position == 'left': position = '0% 0%'
            #xoffset, yoffset = map(convert_pct, position.split())

            thumb_file = os.path.join(image_dir, example['name'] + '-thumb.png')
            create_thumbnail(image_file, thumb_file, zoom=zoom)

    # Save hashes so we know whether we need to re-generate plots
    if hashes:
        with open(hash_file, 'w') as f:
            json.dump(hashes, f)
def test_savechart_svg():
    chart = make_chart()

    with tempfile.NamedTemporaryFile(suffix='.svg') as f:
        savechart(chart, f.name)
        assert consistent_with_svg(f.name)