From 99d3b41088f9d98e2e53912b19d8765fc94ce91e Mon Sep 17 00:00:00 2001 From: jaraco Date: Sun, 22 Aug 2010 17:34:34 +0000 Subject: [PATCH] Restuctured the way stylesheets are loaded so they can be more easily customized by subclasses and so they're less dependent on the class names --- svg/charts/bar.py | 2 +- svg/charts/graph.py | 34 ++++++++++++++++++---------------- svg/charts/line.py | 2 +- svg/charts/plot.py | 2 ++ svg/charts/schedule.py | 2 +- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/svg/charts/bar.py b/svg/charts/bar.py index 26fee37..a6e04b1 100644 --- a/svg/charts/bar.py +++ b/svg/charts/bar.py @@ -18,7 +18,7 @@ class Bar(Graph): scale_divisions = None - css_file = 'bar.css' + stylesheet_names = Graph.stylesheet_names + ['bar.css'] def __init__(self, fields, *args, **kargs): self.fields = fields diff --git a/svg/charts/graph.py b/svg/charts/graph.py index 08d512f..47cef4b 100644 --- a/svg/charts/graph.py +++ b/svg/charts/graph.py @@ -3,12 +3,13 @@ from operator import itemgetter from itertools import islice -import cssutils import pkg_resources +import functools +import cssutils from lxml import etree -from svg.charts import css # causes the profile to be loaded +from svg.charts import css # causes the SVG profile to be loaded try: import zlib @@ -84,6 +85,8 @@ class Graph(object): top_align = top_font = right_align = right_font = 0 compress = False + + stylesheet_names = ['graph.css'] def __init__(self, config = {}): """Initialize the graph object with the graph settings.""" @@ -660,22 +663,21 @@ class Graph(object): sheet = cssutils.parseString(css_string) return sheet - def get_stylesheet(self): - cssutils.log.setLevel(30) # disable INFO log messages - # allow css to include class variables: + def get_stylesheet_resources(self): + "Get the stylesheets for this instance" + # allow css to include class variables class_vars = class_dict(self) - sheet = self.load_resource_stylesheet('graph.css', class_vars) - child_sheet = self.load_resource_stylesheet(self.css_file, class_vars) - map(sheet.add, child_sheet) - return sheet - - #deprecated - def get_style(self): - return self.get_stylesheet().cssText + loader = functools.partial(self.load_resource_stylesheet, + subs=class_vars) + sheets = map(loader, self.stylesheet_names) + return sheets - @property - def css_file(self): - return self.__class__.__name__.lower() + '.css' + def get_stylesheet(self): + cssutils.log.setLevel(30) # disable INFO log messages + def merge_sheets(s1, s2): + map(s1.add, s2) + return s1 + return reduce(merge_sheets, self.get_stylesheet_resources()) class class_dict(object): "Emulates a dictionary, but retrieves class attributes" diff --git a/svg/charts/line.py b/svg/charts/line.py index 12c3000..f6a4dc9 100644 --- a/svg/charts/line.py +++ b/svg/charts/line.py @@ -90,7 +90,7 @@ class Line(Graph): #override some defaults top_align = top_font = right_align = right_font = True - css_file = 'plot.css' + stylesheet_names = Graph.stylesheet_names + ['plot.css'] def max_value(self): data = map(itemgetter('data'), self.data) diff --git a/svg/charts/plot.py b/svg/charts/plot.py index 8254795..d5d395d 100644 --- a/svg/charts/plot.py +++ b/svg/charts/plot.py @@ -126,6 +126,8 @@ class Plot(Graph): stacked = False + stylesheet_names = Graph.stylesheet_names + ['plot.css'] + @apply def scale_x_divisions(): doc = """Determines the scaling for the X axis divisions. diff --git a/svg/charts/schedule.py b/svg/charts/schedule.py index fdbc7b2..6e040eb 100644 --- a/svg/charts/schedule.py +++ b/svg/charts/schedule.py @@ -116,7 +116,7 @@ class Schedule(Graph): scale_x_integers = False bar_gap = True - css_file = 'bar.css' + stylesheet_names = Graph.stylesheet_names + ['bar.css'] def add_data(self, data): """