[IMP] base : Improved the typos.
[odoo/odoo.git] / openerp / pychart / pychart_util.py
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 math
17 import types
18 import traceback
19 from types import *
20
21 def inch_to_point(inch):
22     return inch * 72.0
23 def point_to_inch(pt):
24     return float(pt) / 72.0
25
26 def rotate(x, y, degree):
27     """Rotate a coordinate around point (0,0).
28     - x and y specify the coordinate.
29     - degree is a number from 0 to 360.
30     Returns a new coordinate.
31     """
32     radian = float(degree) * 2 * math.pi / 360.0
33     newx = math.cos(radian) * x - math.sin(radian) * y
34     newy = math.sin(radian) * x + math.cos(radian) * y
35     return (newx, newy)
36
37 debug_level = 1
38
39 def warn(*strs):
40     for s in strs:
41         sys.stderr.write(str(s))
42         sys.stderr.write(" ")
43     sys.stderr.write("\n")
44
45 def info(*strs):
46     if debug_level < 100:
47         return
48     for s in strs:
49         sys.stderr.write(str(s))
50     sys.stderr.write("\n")
51
52 def get_sample_val(l, col):
53     if len(l) <= col:
54         return None
55     return l[col]
56
57 def get_data_list(data, col):
58     # data = [ elem[col] for elem in data if elem[col] != None ]
59     r = []
60     for item in data:
61         val = get_sample_val(item, col)
62         if val != None:
63             r.append(val)
64     return r        
65     
66 def get_data_range(data, col):
67     data = get_data_list(data, col)
68     for item in data:
69         if type(item) not in (types.IntType, types.LongType, types.FloatType):
70             raise TypeError, "Non-number passed to data: %s" % (data)
71     return (min(data), max(data))
72
73 def round_down(val, bound):
74     return int(val/float(bound)) * bound
75
76 def round_up(val, bound):
77     return (int((val-1)/float(bound))+1) * bound
78
79     
80 #
81 # Attribute type checking stuff
82 #
83
84 def new_list():
85     return []
86
87 def union_dict(dict1, dict2):
88     dict = dict1.copy()
89     dict.update(dict2)
90     return dict
91
92 def TextVAlignType(val):
93     if val in ('T', 'B', 'M', None):
94         return None
95     return "Text vertical alignment must be one of T(op), B(ottom), or M(iddle).\n"
96
97 def TextAlignType(val):
98     if val in ('C', 'R', 'L', None):
99         return None
100     return "Text horizontal alignment must be one of C(enter), R(ight), or L(eft)."
101
102 def apply_format(format, val, defaultidx):
103     if format == None:
104         return None
105     elif type(format) == StringType:
106         return format % val[defaultidx]
107     else:
108         return apply(format, val)
109
110     
111 data_desc = "Specifies the data points. <<chart_data>>"
112 label_desc = "The label to be displayed in the legend. <<legend>>, <<font>>"
113 xcol_desc = """The column, within attribute "data", from which the X values of sample points are extracted. <<chart_data>>"""
114 ycol_desc = """The column, within attribute "data", from which the Y values of sample points are extracted. <<chart_data>>"""
115 tick_mark_desc = "Tick marks to be displayed at each sample point. <<tick_mark>>"
116 line_desc="The style of the line. "
117
118 def interval_desc(w):
119     return "When the value is a number, it specifies the interval at which %s are drawn. Otherwise, the value must be a function that takes no argument and returns the list of numbers. The return value specifies the X or Y points at which %s are drawn." % (w,w)
120
121 shadow_desc = """The value is either None or a tuple. When non-None,
122 a drop-shadow is drawn beneath the object. X-off, and y-off specifies the
123 offset of the shadow relative to the object, and fill specifies the
124 style of the shadow (@pxref{module-fill-style})."""
125
126 string_desc = """The appearance of the string produced here can be
127 controlled using escape sequences. <<font>>"""
128
129 #
130 #
131
132 class symbol_lookup_table:
133     def __init__(self, dict, objs):
134         self.names = {}
135         for name, val in dict.items():
136             for obj in objs.list():
137                 if val == obj:
138                     self.names[val] = name
139                     break
140     def lookup(self, obj):
141         if self.names.has_key(obj):
142             return self.names[obj]
143         return None