diff --git a/pygal/config.py b/pygal/config.py index a89239cb..f818ab26 100644 --- a/pygal/config.py +++ b/pygal/config.py @@ -24,7 +24,6 @@ from pygal.style import Style, DefaultStyle from pygal.interpolate import INTERPOLATIONS - CONFIG_ITEMS = [] @@ -170,7 +169,7 @@ class Config(CommonConfig): DefaultStyle, Style, "Style", "Style holding values injected in css") css = Key( - ('style.css', 'graph.css'), list, "Style", + ("!pygal.css.style_css", "!pygal.css.graph_css"), list, "Style", "List of css file", "It can be an absolute file path or an external link", str) diff --git a/pygal/css/__init__.py b/pygal/css/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pygal/css/base.css b/pygal/css/base_css.py similarity index 99% rename from pygal/css/base.css rename to pygal/css/base_css.py index 8837ca46..f1339907 100644 --- a/pygal/css/base.css +++ b/pygal/css/base_css.py @@ -1,3 +1,4 @@ +data = """\ /* * This file is part of pygal * @@ -55,3 +56,4 @@ {{ id }}text.no_data { font-size: {{ font_sizes.no_data }}; } +""" \ No newline at end of file diff --git a/pygal/css/graph.css b/pygal/css/graph_css.py similarity index 99% rename from pygal/css/graph.css rename to pygal/css/graph_css.py index 095e7bc8..a8dc0be9 100644 --- a/pygal/css/graph.css +++ b/pygal/css/graph_css.py @@ -1,3 +1,4 @@ +data="""\ /* * This file is part of pygal * @@ -126,3 +127,4 @@ {{ id }}.tooltip text tspan.label { fill-opacity: .8; } +""" \ No newline at end of file diff --git a/pygal/css/style.css b/pygal/css/style_css.py similarity index 99% rename from pygal/css/style.css rename to pygal/css/style_css.py index 2c3f7086..8c152b37 100644 --- a/pygal/css/style.css +++ b/pygal/css/style_css.py @@ -1,3 +1,4 @@ +data="""\ /* * This file is part of pygal * @@ -139,3 +140,4 @@ {{ colors }} +""" \ No newline at end of file diff --git a/pygal/graph/frenchmap.py b/pygal/graph/frenchmap.py index 78125bb3..e4479f40 100644 --- a/pygal/graph/frenchmap.py +++ b/pygal/graph/frenchmap.py @@ -169,12 +169,7 @@ '06': u("Mayotte") } - -with open(os.path.join( - os.path.dirname(__file__), 'maps', - 'fr.departments.svg')) as file: - DPT_MAP = file.read() - +from .maps.fr_departments_svg import data as DPT_MAP class IntCodeMixin(object): def adapt_code(self, area_code): @@ -194,12 +189,7 @@ class FrenchMapDepartments(IntCodeMixin, BaseMap): kind = 'departement' svg_map = DPT_MAP - -with open(os.path.join( - os.path.dirname(__file__), 'maps', - 'fr.regions.svg')) as file: - REG_MAP = file.read() - +from .maps.fr_regions_svg import data as REG_MAP class FrenchMapRegions(IntCodeMixin, BaseMap): """French regions map""" diff --git a/pygal/graph/maps/__init__.py b/pygal/graph/maps/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pygal/graph/maps/ch.cantons.svg b/pygal/graph/maps/ch_cantons_svg.py similarity index 99% rename from pygal/graph/maps/ch.cantons.svg rename to pygal/graph/maps/ch_cantons_svg.py index 7f016ade..ee0836e2 100644 --- a/pygal/graph/maps/ch.cantons.svg +++ b/pygal/graph/maps/ch_cantons_svg.py @@ -1,3 +1,4 @@ +data = """\ @@ -88,4 +89,4 @@ - +""" diff --git a/pygal/graph/maps/fr.departments.svg b/pygal/graph/maps/fr_departments_svg.py similarity index 99% rename from pygal/graph/maps/fr.departments.svg rename to pygal/graph/maps/fr_departments_svg.py index 0d02fbdc..c2b33849 100644 --- a/pygal/graph/maps/fr.departments.svg +++ b/pygal/graph/maps/fr_departments_svg.py @@ -1,3 +1,4 @@ +data="""\ @@ -326,3 +327,4 @@ +""" \ No newline at end of file diff --git a/pygal/graph/maps/fr.regions.svg b/pygal/graph/maps/fr_regions_svg.py similarity index 99% rename from pygal/graph/maps/fr.regions.svg rename to pygal/graph/maps/fr_regions_svg.py index 046c62d7..4121d274 100644 --- a/pygal/graph/maps/fr.regions.svg +++ b/pygal/graph/maps/fr_regions_svg.py @@ -1,3 +1,4 @@ +data="""\ @@ -89,3 +90,4 @@ +""" \ No newline at end of file diff --git a/pygal/graph/maps/worldmap.svg b/pygal/graph/maps/worldmap_svg.py similarity index 99% rename from pygal/graph/maps/worldmap.svg rename to pygal/graph/maps/worldmap_svg.py index b024ce1c..4546b284 100644 --- a/pygal/graph/maps/worldmap.svg +++ b/pygal/graph/maps/worldmap_svg.py @@ -1,3 +1,4 @@ +data="""\ @@ -2408,3 +2409,4 @@ +""" \ No newline at end of file diff --git a/pygal/graph/pie.py b/pygal/graph/pie.py index 844c36ad..28505f9d 100644 --- a/pygal/graph/pie.py +++ b/pygal/graph/pie.py @@ -32,6 +32,15 @@ class Pie(Graph): """Pie graph""" _adapters = [positive, none_to_zero] + + @property + def _format(self): + """Return the value formatter for this graph""" + def percentage_formatter(y, self=self): + total = sum(map(sum, map(lambda x: x.values, self.series))) + perc = y/total + return '{0:.2%}'.format(perc) + return self.value_formatter or percentage_formatter def slice(self, serie, start_angle, total): """Make a serie slice""" @@ -57,7 +66,7 @@ def slice(self, serie, start_angle, total): else: angle = 2 * pi * perc serie_angle += angle - val = '{0:.2%}'.format(perc) + val = self._format(val) metadata = serie.metadata.get(i) slice_ = decorate( self.svg, @@ -77,7 +86,7 @@ def slice(self, serie, start_angle, total): total_perc += perc if dual: - val = '{0:.2%}'.format(total_perc) + val = self._format(total_perc*total) self.svg.slice(serie_node, self.svg.node(slices, class_="big_slice"), radius * .9, 0, serie_angle, diff --git a/pygal/graph/swissmap.py b/pygal/graph/swissmap.py index 384cf054..19cc4576 100644 --- a/pygal/graph/swissmap.py +++ b/pygal/graph/swissmap.py @@ -56,12 +56,7 @@ 'kt-ge': u("Genf"), } - -with open(os.path.join( - os.path.dirname(__file__), 'maps', - 'ch.cantons.svg')) as file: - CNT_MAP = file.read() - +from .maps.ch_cantons_svg import data as CNT_MAP class SwissMapCantons(BaseMap): """Swiss Cantons map""" diff --git a/pygal/graph/worldmap.py b/pygal/graph/worldmap.py index a98a2f73..9141bb95 100644 --- a/pygal/graph/worldmap.py +++ b/pygal/graph/worldmap.py @@ -27,12 +27,7 @@ from pygal.i18n import COUNTRIES, SUPRANATIONAL import os - -with open(os.path.join( - os.path.dirname(__file__), 'maps', - 'worldmap.svg')) as file: - WORLD_MAP = file.read() - +from .maps.worldmap_svg import data as WORLD_MAP class Worldmap(BaseMap): """Worldmap graph""" diff --git a/pygal/svg.py b/pygal/svg.py index 169f33dd..0a219001 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -27,11 +27,13 @@ import io import os import json +import importlib from datetime import date, datetime from numbers import Number from math import cos, sin, pi from pygal.util import template, coord_format, minify_css from pygal import __version__ +from pygal.css import base_css class Svg(object): @@ -83,18 +85,25 @@ def add_styles(self): """Add the css to the svg""" colors = self.graph.style.get_colors(self.id) all_css = [] - for css in ['base.css'] + list(self.graph.css): - if '://' in css: + for css in [base_css] + list(self.graph.css): + if type(css) == str and '://' in css: self.processing_instructions.append( etree.PI( u('xml-stylesheet'), u('href="%s"' % css))) else: - if css.startswith('inline:'): + if type(css) == str and css.startswith('inline:'): css_text = css[len('inline:'):] else: - if not os.path.exists(css): - css = os.path.join( - os.path.dirname(__file__), 'css', css) + if type(css) == str and css.startswith("!"): + css_raw = importlib.import_module(css[1:]).data + elif type(css) == str: + if not os.path.exists(css): + css = os.path.join( + os.path.dirname(__file__), 'css', css) + with io.open(css, encoding='utf-8') as f: + css_raw = f.read() + else: + css_raw = css.data class FontSizes(object): """Container for font sizes""" @@ -106,13 +115,12 @@ class FontSizes(object): name.replace('_font_size', ''), ('%dpx' % getattr(self.graph, name))) - with io.open(css, encoding='utf-8') as f: - css_text = template( - f.read(), - style=self.graph.style, - colors=colors, - font_sizes=fs, - id=self.id) + css_text = template( + css_raw, + style=self.graph.style, + colors=colors, + font_sizes=fs, + id=self.id) if not self.graph.pretty_print: css_text = minify_css(css_text) all_css.append(css_text) @@ -127,7 +135,9 @@ def get_js_dict(): return dict((k, getattr(self.graph.state, k)) for k in dir(self.graph.config) if not k.startswith('_') and not hasattr( - getattr(self.graph.config, k), '__call__')) + getattr(self.graph.config, k), '__call__') + and not hasattr(getattr(self.graph.state, k), '__call__') + ) def json_default(o): if isinstance(o, (datetime, date)): diff --git a/setup.py b/setup.py index 6aab2bd6..5b0564c3 100644 --- a/setup.py +++ b/setup.py @@ -64,7 +64,7 @@ def run_tests(self): "svg", "chart", "graph", "diagram", "plot", "histogram", "kiviat"], tests_require=["pytest", "pyquery", "flask", "cairosvg"], cmdclass={'test': PyTest}, - package_data={'pygal': ['css/*', 'graph/maps/*.svg']}, + #package_data={'pygal': ['css/*', 'graph/maps/*.svg']}, extras_require={ 'lxml': ['lxml'], 'png': ['cairosvg']