~ubuntu-branches/ubuntu/quantal/actdiag/quantal

« back to all changes in this revision

Viewing changes to src/actdiag/command.py

  • Committer: Package Import Robot
  • Author(s): Kouhei Maeda
  • Date: 2012-02-11 23:18:49 UTC
  • Revision ID: package-import@ubuntu.com-20120211231849-9c5auxs6dyhxecju
Tags: upstream-0.3.0
ImportĀ upstreamĀ versionĀ 0.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#  Copyright 2011 Takeshi KOMIYA
 
3
#
 
4
#  Licensed under the Apache License, Version 2.0 (the "License");
 
5
#  you may not use this file except in compliance with the License.
 
6
#  You may obtain a copy of the License at
 
7
#
 
8
#      http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
10
#  Unless required by applicable law or agreed to in writing, software
 
11
#  distributed under the License is distributed on an "AS IS" BASIS,
 
12
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
#  See the License for the specific language governing permissions and
 
14
#  limitations under the License.
 
15
 
 
16
import os
 
17
import re
 
18
import sys
 
19
from ConfigParser import SafeConfigParser
 
20
from optparse import OptionParser
 
21
import actdiag
 
22
import DiagramDraw
 
23
import diagparser
 
24
from blockdiag.command import create_fontmap, detectfont
 
25
from builder import ScreenNodeBuilder
 
26
 
 
27
 
 
28
def parse_option():
 
29
    version = "%%prog %s" % actdiag.__version__
 
30
    usage = "usage: %prog [options] infile"
 
31
    p = OptionParser(usage=usage, version=version)
 
32
    p.add_option('-a', '--antialias', action='store_true',
 
33
                 help='Pass diagram image to anti-alias filter')
 
34
    p.add_option('-c', '--config',
 
35
                 help='read configurations from FILE', metavar='FILE')
 
36
    p.add_option('-o', dest='filename',
 
37
                 help='write diagram to FILE', metavar='FILE')
 
38
    p.add_option('-f', '--font', default=[], action='append',
 
39
                 help='use FONT to draw diagram', metavar='FONT')
 
40
    p.add_option('--fontmap',
 
41
                 help='use FONTMAP file to draw diagram', metavar='FONT')
 
42
    p.add_option('-s', '--separate', action='store_true',
 
43
                 help='Separate diagram images for each group (SVG only)')
 
44
    p.add_option('-T', dest='type', default='PNG',
 
45
                 help='Output diagram as TYPE format')
 
46
    p.add_option('--nodoctype', action='store_true',
 
47
                 help='Do not output doctype definition tags (SVG only)')
 
48
    options, args = p.parse_args()
 
49
 
 
50
    if len(args) == 0:
 
51
        p.print_help()
 
52
        sys.exit(0)
 
53
 
 
54
    options.type = options.type.upper()
 
55
    if not options.type in ('SVG', 'PNG', 'PDF'):
 
56
        msg = "ERROR: unknown format: %s\n" % options.type
 
57
        sys.stderr.write(msg)
 
58
        sys.exit(0)
 
59
 
 
60
    if options.type == 'PDF':
 
61
        try:
 
62
            import reportlab.pdfgen.canvas
 
63
        except ImportError:
 
64
            msg = "ERROR: could not output PDF format; Install reportlab\n"
 
65
            sys.stderr.write(msg)
 
66
            sys.exit(0)
 
67
 
 
68
    if options.separate and options.type != 'SVG':
 
69
        msg = "ERROR: --separate option work in SVG images.\n"
 
70
        sys.stderr.write(msg)
 
71
        sys.exit(0)
 
72
 
 
73
    if options.config and not os.path.isfile(options.config):
 
74
        msg = "ERROR: config file is not found: %s\n" % options.config
 
75
        sys.stderr.write(msg)
 
76
        sys.exit(0)
 
77
 
 
78
    configpath = options.config or "%s/.blockdiagrc" % os.environ.get('HOME')
 
79
    if os.path.isfile(configpath):
 
80
        config = SafeConfigParser()
 
81
        config.read(configpath)
 
82
 
 
83
        if config.has_option('blockdiag', 'fontpath'):
 
84
            fontpath = config.get('blockdiag', 'fontpath')
 
85
            options.font.append(fontpath)
 
86
 
 
87
    return options, args
 
88
 
 
89
 
 
90
def main():
 
91
    options, args = parse_option()
 
92
 
 
93
    infile = args[0]
 
94
    if options.filename:
 
95
        outfile = options.filename
 
96
    elif infile == '-':
 
97
        outfile = 'output.' + options.type.lower()
 
98
    else:
 
99
        outfile = re.sub('\..*', '', infile) + '.' + options.type.lower()
 
100
 
 
101
    try:
 
102
        if infile == '-':
 
103
            import codecs
 
104
            stream = codecs.getreader('utf-8')(sys.stdin)
 
105
            tree = diagparser.parse_string(stream.read())
 
106
        else:
 
107
            tree = diagparser.parse_file(infile)
 
108
 
 
109
        fontmap = create_fontmap(options)
 
110
        diagram = ScreenNodeBuilder.build(tree, separate=options.separate)
 
111
        draw = DiagramDraw.DiagramDraw(options.type, diagram, outfile,
 
112
                                       fontmap=fontmap,
 
113
                                       antialias=options.antialias,
 
114
                                       nodoctype=options.nodoctype)
 
115
        draw.draw()
 
116
        draw.save()
 
117
    except UnicodeEncodeError, e:
 
118
        msg = "ERROR: UnicodeEncodeError caught (check your font settings)\n"
 
119
        sys.stderr.write(msg)
 
120
    except Exception, e:
 
121
        sys.stderr.write("ERROR: %s\n" % e)