Browse Source

Protect the config

pull/8/head
Florian Mounier 12 years ago
parent
commit
1dc6708a92
  1. 12
      pygal/config.py
  2. 12
      pygal/ghost.py
  3. 112
      pygal/test/test_config.py

12
pygal/config.py

@ -20,7 +20,7 @@
"""
Config module with all options
"""
from copy import deepcopy
from pygal.style import DefaultStyle
@ -117,6 +117,13 @@ class Config(object):
def __init__(self, **kwargs):
"""Can be instanciated with config kwargs"""
for k in dir(self):
v = getattr(self, k)
if (k not in self.__dict__ and not
k.startswith('_') and not
hasattr(v, '__call__')):
setattr(self, k, v)
self.css = list(self.css)
self.js = list(self.js)
self._update(kwargs)
@ -152,3 +159,6 @@ class Config(object):
elif not hasattr(value, '__call__'):
config[attr] = value
return config
def copy(self):
return deepcopy(self)

12
pygal/ghost.py

@ -40,8 +40,16 @@ class Ghost(object):
def __init__(self, config=None, **kwargs):
"""Init config"""
self.config = config or Config()
self.config(**kwargs)
if config and type(config) == type:
config = config()
if config:
config = config.copy()
else:
config = Config()
config(**kwargs)
self.config = config
self.series = []
def add(self, title, values):

112
pygal/test/test_config.py

@ -16,11 +16,121 @@
#
# You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>.
from pygal import Line, Dot, Pie, Radar
from pygal import Line, Dot, Pie, Radar, Config
from pygal.test.utils import texts
from pygal.test import pytest_generate_tests, make_data
def test_config_behaviours():
line1 = Line()
line1.show_legend = False
line1.fill = True
line1.pretty_print = True
line1.x_labels = ['a', 'b', 'c']
line1.add('_', [1, 2, 3])
l1 = line1.render()
line2 = Line(
show_legend=False,
fill=True,
pretty_print=True,
x_labels=['a', 'b', 'c'])
line2.add('_', [1, 2, 3])
l2 = line2.render()
assert l1 == l2
class LineConfig(Config):
show_legend = False
fill = True
pretty_print = True
x_labels = ['a', 'b', 'c']
line3 = Line(LineConfig)
line3.add('_', [1, 2, 3])
l3 = line3.render()
assert l1 == l3
line4 = Line(LineConfig())
line4.add('_', [1, 2, 3])
l4 = line4.render()
assert l1 == l4
def test_config_alterations_class():
class LineConfig(Config):
show_legend = False
fill = True
pretty_print = True
x_labels = ['a', 'b', 'c']
line1 = Line(LineConfig)
line1.add('_', [1, 2, 3])
l1 = line1.render()
LineConfig.stroke = False
line2 = Line(LineConfig)
line2.add('_', [1, 2, 3])
l2 = line2.render()
assert l1 != l2
l1bis = line1.render()
assert l1 == l1bis
def test_config_alterations_instance():
class LineConfig(Config):
show_legend = False
fill = True
pretty_print = True
x_labels = ['a', 'b', 'c']
config = LineConfig()
line1 = Line(config)
line1.add('_', [1, 2, 3])
l1 = line1.render()
config.stroke = False
line2 = Line(config)
line2.add('_', [1, 2, 3])
l2 = line2.render()
assert l1 != l2
l1bis = line1.render()
assert l1 == l1bis
def test_config_alterations_kwargs():
class LineConfig(Config):
show_legend = False
fill = True
pretty_print = True
x_labels = ['a', 'b', 'c']
config = LineConfig()
line1 = Line(config)
line1.add('_', [1, 2, 3])
l1 = line1.render()
line1.stroke = False
l1bis = line1.render()
assert l1 != l1bis
line2 = Line(config)
line2.add('_', [1, 2, 3])
l2 = line2.render()
assert l1 == l2
assert l1bis != l2
line3 = Line(config, title='Title')
line3.add('_', [1, 2, 3])
l3 = line3.render()
assert l3 != l2
l2bis = line2.render()
assert l2 == l2bis
def test_logarithmic():
line = Line(logarithmic=True)
line.add('_', [1, 10 ** 10, 1])

Loading…
Cancel
Save