Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Integrate support for GIF where appropriate (*not* in Blorb or TADS)
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:0e64a95c3be629c578e4b5c6383fc2985303c43f
User & Date: brandon 2018-11-08 23:16:33
Context
2018-11-09
23:43
Add unit tests for the image functions Leaf check-in: 684ae7cbb9 user: brandon tags: trunk
2018-11-08
23:16
Integrate support for GIF where appropriate (*not* in Blorb or TADS) check-in: 0e64a95c3b user: brandon tags: trunk
23:09
Make the various image functions a bit more robust via exceptions rather than returning None. check-in: fcec9a25cf user: brandon tags: trunk
Changes

Changes to treatyofbabel/ifiction.py.

578
579
580
581
582
583
584
585
586
587
588
589
590
591



592
593
594
595
596
597
598
599
600
601
602
603
def add_cover(ifiction_dom, story_node, img_format, height, width,
              description):
    """Add cover art information to a story Node.

    Args:
        ifiction_dom: an xml.dom Document
        story_node: a story Node
        img_format: the file format of the image ('jpg' or 'png')
        height: the height in pixels of the image
        width: the width in pixels of the image
        description: a description of the contents of the image
    Raises:
        IFictionError if any of the elements do not conform to the IFiction
        standard




    """
    if img_format is None or img_format == "":
        raise IFictionError("cover: image format required")
    if img_format not in ["jpg", "png"]:
        raise IFictionError("cover: invalid image format")
    if height is None or height == "":
        raise IFictionError("cover: image height required")
    try:
        if int(height) <= 0:
            raise IFictionError("cover: height must be a positive integer")
    except:







|






>
>
>




|







578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
def add_cover(ifiction_dom, story_node, img_format, height, width,
              description):
    """Add cover art information to a story Node.

    Args:
        ifiction_dom: an xml.dom Document
        story_node: a story Node
        img_format: the file format of the image ('jpg', 'png', or 'gif')
        height: the height in pixels of the image
        width: the width in pixels of the image
        description: a description of the contents of the image
    Raises:
        IFictionError if any of the elements do not conform to the IFiction
        standard
    Note:
        Support for GIF images is a pyifbabel extension to the original Treaty 
        of Babel

    """
    if img_format is None or img_format == "":
        raise IFictionError("cover: image format required")
    if img_format not in ["jpg", "png", "gif"]:
        raise IFictionError("cover: invalid image format")
    if height is None or height == "":
        raise IFictionError("cover: image height required")
    try:
        if int(height) <= 0:
            raise IFictionError("cover: height must be a positive integer")
    except:

Changes to treatyofbabel/ifstory.py.

1
2
3
4
5
6
7
8
9
10
11
12
..
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
...
360
361
362
363
364
365
366




367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# -*- coding: utf-8 -*-
#
#       ifstory.py
#
#       Copyright © 2012, 2013, 2014, 2015 Brandon Invergo <brandon@invergo.net>
#
#       This file is part of pyifbabel.
#
#       pyifbabel is free software: you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation, either version 3 of the License, or
#       (at your option) any later version.
................................................................................

import os.path
from urllib2 import urlopen
import time

import ifiction
import treatyofbabel
from utils._imgfuncs import CoverImage, get_png_dim, get_jpeg_dim
from babelerrors import IFictionError, BabelError
from treatyofbabel import PYIFBABEL_VERSION


class StrictDict(dict):
    """A derivative of the dict class whose legal keys must be declared at
    initialization.
................................................................................
        cover_url = cover.get("url")
        if cover_url is None:
            return
        cover_file = urlopen(cover_url)
        cover_data = cover_file.read()
        if cover_data is None:
            return




        width, height = get_jpeg_dim(cover_data)
        if width is not None and height is not None:
            self.cover.data = cover_data
            self.cover.img_format = "jpg"
            self.cover.width = width
            self.cover.height = height
            self.cover.description = None
        else:
            width, height = get_png_dim(cover_data)
            if width is not None and height is not None:
                self.cover.data = cover_data
                self.cover.img_format = "png"
                self.cover.width = width
                self.cover.height = height
                self.cover.description = None




|







 







|







 







>
>
>
>
|
<
<
<
<
|
|

|
<
|
|
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
..
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
...
360
361
362
363
364
365
366
367
368
369
370
371




372
373
374
375

376
377
378
379
380
# -*- coding: utf-8 -*-
#
#       ifstory.py
#
#       Copyright © 2012, 2013, 2014, 2015, 2018 Brandon Invergo <brandon@invergo.net>
#
#       This file is part of pyifbabel.
#
#       pyifbabel is free software: you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation, either version 3 of the License, or
#       (at your option) any later version.
................................................................................

import os.path
from urllib2 import urlopen
import time

import ifiction
import treatyofbabel
from utils._imgfuncs import CoverImage, get_png_dim, get_jpeg_dim, get_gif_dim, deduce_img_format
from babelerrors import IFictionError, BabelError
from treatyofbabel import PYIFBABEL_VERSION


class StrictDict(dict):
    """A derivative of the dict class whose legal keys must be declared at
    initialization.
................................................................................
        cover_url = cover.get("url")
        if cover_url is None:
            return
        cover_file = urlopen(cover_url)
        cover_data = cover_file.read()
        if cover_data is None:
            return
        fmt = deduce_img_format(cover_data)
        if fmt is None:
            return
        if fmt == "jpeg":
            width, height = get_jpeg_dim(cover_data)




        elif fmt == "png":
            width, height = get_png_dim(cover_data)
        else:
            width, height = get_gif_dim(cover_data)

        self.cover.data = cover_data
        self.cover.img_format = fmt
        self.cover.width = width
        self.cover.height = height
        self.cover.description = None

Changes to treatyofbabel/wrappers/blorb.py.

1
2
3
4
5
6
7
8
9
10
11
12
..
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- coding: utf-8 -*-
#
#       blorb.py
#
#       Copyright © 2011, 2012, 2013, 2014, 2015 Brandon Invergo <brandon@invergo.net>
#       Copyright © 2009, 2010 Per Liedman <per@liedman.net>
#
#       This file is part of Grotesque.
#
#       Grotesque is free software: you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation, either version 3 of the License, or
................................................................................
        ext = "png"
        (width, height) = get_png_dim(cover_data)
    elif cover_format == "JPEG":
        ext = "jpg"
        (width, height) = get_jpeg_dim(cover_data)
    else:
        raise BabelError("Unsupported image format")
    if width is None or height is None:
        raise BabelError("Image corrupted: cannot determine dimensions")
    cover = CoverImage(cover_data, ext, width, height, description)
    return cover


def get_story_file_ifid(file_buffer):
    meta = get_story_file_meta(file_buffer)
    if meta is None:




|







 







<
<







1
2
3
4
5
6
7
8
9
10
11
12
..
89
90
91
92
93
94
95


96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
#
#       blorb.py
#
#       Copyright © 2011, 2012, 2013, 2014, 2015, 2018 Brandon Invergo <brandon@invergo.net>
#       Copyright © 2009, 2010 Per Liedman <per@liedman.net>
#
#       This file is part of Grotesque.
#
#       Grotesque is free software: you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation, either version 3 of the License, or
................................................................................
        ext = "png"
        (width, height) = get_png_dim(cover_data)
    elif cover_format == "JPEG":
        ext = "jpg"
        (width, height) = get_jpeg_dim(cover_data)
    else:
        raise BabelError("Unsupported image format")


    cover = CoverImage(cover_data, ext, width, height, description)
    return cover


def get_story_file_ifid(file_buffer):
    meta = get_story_file_meta(file_buffer)
    if meta is None: