[MERGE] Remove the embedded pychart library, and use the online version
[odoo/odoo.git] /
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
4
5 # Jockey is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2, or (at your option) any
8 # later version.
9 #
10 # Jockey is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 # for more details.
14 #
15 import sys
16 import os
17 import re
18 import getopt
19 import pychart_util
20
21 __doc__ = """This module is defines variables for changing the looks
22 of charts. All the variables can be changed either via environment
23 variable PYCHART_OPTIONS or via the command-line options.
24
25 The value of PYCHART_OPTIONS should be a sequence of var=val separated
26 by space.  Below is an example, which tells Pychart to write to file
27 foo.pdf and use Times-Roman as the default font.
28
29 PYCHART_OPTIONS="output=foo.pdf font-family=Times"
30
31 The summary of attributes that can be set via PYCHART_OPTIONS follows:
32
33 output=FILENAME (default: stdout)
34
35     Set the output file name.
36     
37 format=[ps|pdf|pdf-uncompressed|png|x11|svg] (default: ps)
38
39     Set the output file format.
40     
41 font-family=NAME (default: Helvetica)
42
43     Set the default font to be used by texts.
44     
45 font-size=N (default: 9)
46
47     Set the default font to be used by texts.
48 line-width=X (default: 0.4)
49
50     Set the default line width, in points.  See also
51     pychart.line_style.
52
53 scale=X (default: 1.0)
54
55     Set the scaling factor.  The default is 1.0. 
56
57 color=[yes|no] (default: no)
58
59     If yes, Pychart colorizes default object attributes.
60
61 You can also set these variables by calling theme.get_options.
62 """
63
64 use_color = 0
65 scale_factor = 1
66 output_format = None   # "ps", "pdf", "png", "x11", or "svg"
67 compress_output = 1
68 output_file = ""
69
70 default_font_family = "Helvetica"
71 default_font_size = 9
72 default_line_height = None
73 default_font_halign = "L"
74 default_font_valign = "B"
75 default_font_angle = 0
76 default_line_width = 0.4
77
78 debug_level = 1
79 delta_bounding_box = [-3, -3, 3, 3]
80 bounding_box = {}
81          
82 def parse_yesno(str):
83     if str in ("yes", "true", "1"):
84         return 1
85     else:
86         return 0
87
88 def parse_bounding_box(arg):
89     global delta_bounding_box, bounding_box
90     
91     l = arg.split(",")
92     if len(l) != 4:
93         raise ValueError, "Need to specify margin=LEFT,BOTTOM,RIGHT,TOP"
94     for i in range(0, 4):
95         val = l[i].strip()
96         if val[0] == '+':
97             delta_bounding_box[i] = int(val[1:])
98         elif val[0] == '-':
99             delta_bounding_box[i] = int(val[1:])
100         else:
101             bounding_box[i] = int(val)
102
103 def adjust_bounding_box(bbox):
104     """Adjust the bounding box as specified by user.
105     Returns the adjusted bounding box.
106
107     - bbox: Bounding box computed from the canvas drawings.
108     It must be a four-tuple of numbers.
109     """
110     for i in range(0, 4):
111         if bounding_box.has_key(i):
112             bbox[i] = bounding_box[i]
113         else:
114             bbox[i] += delta_bounding_box[i]
115     return bbox
116
117 def parse_option(opt, arg):
118     global use_color, scale_factor, margin
119     global output_format, output_file, compress_output
120     global default_font_family, default_font_size
121     global default_line_height
122     global default_line_width, debug_level
123     if opt == "format":
124         if arg in ("ps", "eps"):
125             output_format = "ps"
126         elif arg == "png":
127             output_format = "png"
128         elif arg == "svg":
129             output_format = "svg"
130         elif arg == "x11":
131             output_format = "x11"
132         elif arg == "pdf-uncompressed":
133             output_format = "pdf"
134             compress_output = 0
135         elif arg in ("pdf-compressed", "pdf"):
136             output_format = "pdf"
137             compress_output = 1
138         else:
139             raise ValueError, "Unknown output option: " + str(arg)
140     elif opt == "output":
141         output_file = arg
142     elif opt == "color":
143         use_color = 1
144     elif opt == "scale":
145         scale_factor = float(arg)
146     elif opt == "bbox":
147         parse_bounding_box(arg)
148     elif opt == "font-family":
149         default_font_family = arg
150     elif opt == "font-size":
151         default_font_size = float(arg)
152         default_line_height = float(arg)            
153     elif opt == "line-width":
154         default_line_width = float(arg)
155     elif opt == "debug-level":
156         debug_level = int(arg)
157     else:
158         raise getopt.GetoptError, "Unknown option: " + opt + " " + arg
159     
160 if os.environ.has_key("PYCHART_OPTIONS"):
161     for opt in os.environ["PYCHART_OPTIONS"].split():
162         opt, arg = opt.split("=")
163         parse_option(opt, arg)
164
165 hooks = []        
166 def add_reinitialization_hook(proc):
167     global hooks
168     hooks.append(proc)
169     proc()
170     
171 def usage():
172     print "Usage: %s [options..]" % sys.argv[0]
173     print """
174     --scale=X: Set the scaling factor to X (default: 1.0).
175     --format=[ps|png|pdf|x11|svg]: Set the output format (default: ps).
176     --font-family=NAME: Set the default font family (default: Helvetica).
177     --font-size=NAME: Set the default font size (default: 9pts).
178     --line-width=NAME: Set the default line width (default: 0.4).
179     --debug-level=N: Set the messaging verbosity (default: 0).
180     --bbox=LEFT,BOTTOM,RIGHT,TOP: Specifies the amount of space (in PS points) to be left in the edges of the picture (default: -1,-1,+1,+1).
181     """
182
183 def reinitialize():
184     """This procedure must be called after setting variables in
185     the |theme| module. This procedure propagates the new values of
186     the theme variables to other modules that depend on their values."""
187     for proc in hooks:
188         proc()
189     
190 def get_options(argv = None):
191     """This procedure takes a list of command line arguments in <argv>
192     and parses
193     options. It returns the non-parsed portion of <argv>. Parameter
194     <argv> can be
195     omitted, in which case its value defaults to |sys.argv[1:]|.
196     The options supported are: "|--format=[ps,png,pdf,x11,svg]|",
197     "|--output=|<file>", "|--color=[yes,no]|"
198     "|--scale=|<X>", "|--font-family=|<name>", "|--font-size=|<X>",
199     "|--line-width=|<X>",
200     "|--debug-level=|<N>", "|bbox=|<left,bottom,right,top>".
201     The below code shows an example.
202
203 #!/usr/bin/python
204 from pychart import *
205 args = theme.get_options()
206 ar = area.T(...)
207 ...
208     """
209     if argv == None:
210         argv = sys.argv[1:]
211     try:
212         opts, args = getopt.getopt(argv, "d:co:f:",
213                                    ["format=", "output=", "color=",
214                                     "scale=", "font-family=", "font-size=",
215                                     "line-width=", "debug-level=",
216                                     "bbox="])
217     except getopt.GetoptError, foo:
218         print foo
219         usage()
220         raise getopt.GetoptError
221     for opt, arg in opts:
222         if opt == "-d":
223             parse_option("debug-level", arg)
224         elif opt == "-c":
225             parse_option("color", None)
226         elif opt == "-o":
227             parse_option("output", arg)
228         elif opt == "-f":
229             parse_option("format", arg)
230         else:
231             parse_option(opt[2:], arg)
232     reinitialize()
233     return args
234
235