This URL has Read-Only access.

Statistics
| Branch: | Tag: | Revision:

root / generate_from_json.py @ bb59f925

History | View | Annotate | Download (2.2 kB)

1
#!/usr/bin/env python
2
import json
3
import subprocess
4
from optparse import OptionParser
5

    
6
__version__ = "0.1.3"
7

    
8
DEFAULT_JSON_FILE = "data.json"
9
DEFAULT_TEXT_RECT = "./text-rect"
10
#COLOR = [0.6666, 0.6666, 0.6666, 1.0]
11
COLOR = [0, 0, 0, 1.0]
12
DESCRIPTION = "parse a JSON file and outputs images calling text-rect"
13
FONT = "Gill sans 48" # also change ARBITRARY_SPACE_FACTOR if you change the font
14
ARBITRARY_SPACE_FACTOR = 48
15
ARBITRARY_HEIGHT = 800
16

    
17
def _load(file_name):
18
    f = None
19
    try:
20
        f = open(file_name, "r")
21
    except IOError, e:
22
        raise RuntimeError(e.message)
23
    else:
24
        try:
25
            data = json.load(f, "UTF-8")
26
        except ValueError, e:
27
            raise RuntimeError("Error in JSON formatting: %s" % (e.message))
28
    if f is not None:
29
        f.close()
30
    return data
31

    
32
def do_it(text_rect, file_path, verbose):
33
    ret = True
34
    data = _load(file_path)
35
    if verbose:
36
        print(data)
37
    for k, v in data.iteritems():
38
        w = len(v) * ARBITRARY_SPACE_FACTOR
39
        h = ARBITRARY_HEIGHT
40
        command = "%s -o %s.png -t \"%s\" -f \"%s\" -r %f -g %f -b %f -a %f -W %d -H %d" % (text_rect, k, v, FONT, COLOR[0], COLOR[1], COLOR[2], COLOR[3], w, h)
41
        returncode = subprocess.call(command, shell=True)
42
        if returncode == 0:
43
            if verbose:
44
                print("WIN  %s" % (command))
45
        else:
46
            if verbose:
47
                print("FAIL %s" % (command))
48
            ret = False
49
    return ret
50

    
51
if __name__ == "__main__":
52
    parser = OptionParser(usage="%prog [config file] [options]", version="%prog " + __version__, description=DESCRIPTION)
53
    parser.add_option("-j", "--json-file", type="string",
54
                        help="Specifies the JSON config file.")
55
    parser.add_option("-t", "--text-rect", type="string",
56
                        help="Specifies the path to the text-rect to call.")
57
    parser.add_option("-v", "--verbose", action="store_true", help="Makes the logging output verbose.")
58
    (options, args) = parser.parse_args()
59

    
60
    json_file = DEFAULT_JSON_FILE
61
    text_rect = DEFAULT_TEXT_RECT
62
    verbose = options.verbose
63
    if options.text_rect:
64
        text_rect = options.text_rect
65
    if options.json_file:
66
        json_file = options.json_file
67

    
68
    do_it(text_rect, json_file, verbose)
69