1
# -*- coding: utf-8 -*-
2
# Copyright 2011 Takeshi KOMIYA
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
8
# http://www.apache.org/licenses/LICENSE-2.0
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.
19
from ConfigParser import SafeConfigParser
20
from optparse import OptionParser
24
from blockdiag.command import create_fontmap, detectfont
25
from builder import ScreenNodeBuilder
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()
54
options.type = options.type.upper()
55
if not options.type in ('SVG', 'PNG', 'PDF'):
56
msg = "ERROR: unknown format: %s\n" % options.type
60
if options.type == 'PDF':
62
import reportlab.pdfgen.canvas
64
msg = "ERROR: could not output PDF format; Install reportlab\n"
68
if options.separate and options.type != 'SVG':
69
msg = "ERROR: --separate option work in SVG images.\n"
73
if options.config and not os.path.isfile(options.config):
74
msg = "ERROR: config file is not found: %s\n" % options.config
78
configpath = options.config or "%s/.blockdiagrc" % os.environ.get('HOME')
79
if os.path.isfile(configpath):
80
config = SafeConfigParser()
81
config.read(configpath)
83
if config.has_option('blockdiag', 'fontpath'):
84
fontpath = config.get('blockdiag', 'fontpath')
85
options.font.append(fontpath)
91
options, args = parse_option()
95
outfile = options.filename
97
outfile = 'output.' + options.type.lower()
99
outfile = re.sub('\..*', '', infile) + '.' + options.type.lower()
104
stream = codecs.getreader('utf-8')(sys.stdin)
105
tree = diagparser.parse_string(stream.read())
107
tree = diagparser.parse_file(infile)
109
fontmap = create_fontmap(options)
110
diagram = ScreenNodeBuilder.build(tree, separate=options.separate)
111
draw = DiagramDraw.DiagramDraw(options.type, diagram, outfile,
113
antialias=options.antialias,
114
nodoctype=options.nodoctype)
117
except UnicodeEncodeError, e:
118
msg = "ERROR: UnicodeEncodeError caught (check your font settings)\n"
119
sys.stderr.write(msg)
121
sys.stderr.write("ERROR: %s\n" % e)