From f78f88d7a376bb7ec49ae8c3277f18020a85d198 Mon Sep 17 00:00:00 2001 From: never-eat-yellow-snow Date: Sat, 6 Jun 2015 11:07:45 +0200 Subject: [PATCH 1/3] Remove explicit open() calls on .css and .svg map files ... and therefore allow py2exe to do a better job in packaging. --- pygal/config.py | 6 +-- pygal/css/__init__.py | 0 pygal/css/{base.css => base_css.py} | 2 + pygal/css/{graph.css => graph_css.py} | 2 + pygal/css/{style.css => style_css.py} | 2 + pygal/graph/frenchmap.py | 14 +----- pygal/graph/maps/__init__.py | 0 .../{ch.cantons.svg => ch_cantons_svg.py} | 3 +- ....departments.svg => fr_departments_svg.py} | 2 + .../{fr.regions.svg => fr_regions_svg.py} | 2 + .../maps/{worldmap.svg => worldmap_svg.py} | 2 + pygal/graph/swissmap.py | 7 +-- pygal/graph/worldmap.py | 7 +-- pygal/svg.py | 48 ++++++++++--------- setup.py | 2 +- 15 files changed, 48 insertions(+), 51 deletions(-) create mode 100644 pygal/css/__init__.py rename pygal/css/{base.css => base_css.py} (99%) rename pygal/css/{graph.css => graph_css.py} (99%) rename pygal/css/{style.css => style_css.py} (99%) create mode 100644 pygal/graph/maps/__init__.py rename pygal/graph/maps/{ch.cantons.svg => ch_cantons_svg.py} (99%) rename pygal/graph/maps/{fr.departments.svg => fr_departments_svg.py} (99%) rename pygal/graph/maps/{fr.regions.svg => fr_regions_svg.py} (99%) rename pygal/graph/maps/{worldmap.svg => worldmap_svg.py} (99%) diff --git a/pygal/config.py b/pygal/config.py index a89239cb..af2ef1e9 100644 --- a/pygal/config.py +++ b/pygal/config.py @@ -23,7 +23,7 @@ from copy import deepcopy from pygal.style import Style, DefaultStyle from pygal.interpolate import INTERPOLATIONS - +from pygal.css import style_css, graph_css CONFIG_ITEMS = [] @@ -170,9 +170,9 @@ class Config(CommonConfig): DefaultStyle, Style, "Style", "Style holding values injected in css") css = Key( - ('style.css', 'graph.css'), list, "Style", + (style_css, graph_css), list, "Style", "List of css file", - "It can be an absolute file path or an external link", + "It can be an absolute file path, an external link or a python module exporting the string variable data containing the .css style", str) # Look # 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/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..e98ebb84 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -32,6 +32,7 @@ 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,36 +84,39 @@ 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: + if not os.path.exists(css): + css = os.path.join( + os.path.dirname(__file__), 'css', css) - class FontSizes(object): - """Container for font sizes""" - fs = FontSizes() - for name in dir(self.graph.state): - if name.endswith('_font_size'): - setattr( - fs, - name.replace('_font_size', ''), - ('%dpx' % getattr(self.graph, name))) + class FontSizes(object): + """Container for font sizes""" + fs = FontSizes() + for name in dir(self.graph.state): + if name.endswith('_font_size'): + setattr( + fs, + 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) + 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) + else: + css_text = css.data if not self.graph.pretty_print: css_text = minify_css(css_text) all_css.append(css_text) 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'] From e9ddb87df67f1cd279551180c0637ea83b4e5921 Mon Sep 17 00:00:00 2001 From: "never-eat-yellow-snow@gmx.net" Date: Sat, 6 Jun 2015 19:12:34 +0200 Subject: [PATCH 2/3] fix testcases (run on linux, python3.4 with equivalent results to master branch) --- pygal/config.py | 5 ++--- pygal/svg.py | 5 ++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pygal/config.py b/pygal/config.py index af2ef1e9..f818ab26 100644 --- a/pygal/config.py +++ b/pygal/config.py @@ -23,7 +23,6 @@ from copy import deepcopy from pygal.style import Style, DefaultStyle from pygal.interpolate import INTERPOLATIONS -from pygal.css import style_css, graph_css CONFIG_ITEMS = [] @@ -170,9 +169,9 @@ 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, an external link or a python module exporting the string variable data containing the .css style", + "It can be an absolute file path or an external link", str) # Look # diff --git a/pygal/svg.py b/pygal/svg.py index e98ebb84..586525c3 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -27,6 +27,7 @@ import io import os import json +import importlib from datetime import date, datetime from numbers import Number from math import cos, sin, pi @@ -93,7 +94,9 @@ def add_styles(self): if type(css) == str and css.startswith('inline:'): css_text = css[len('inline:'):] else: - if type(css) == str: + if type(css) == str and css.startswith("!"): + css_text = 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) From 30dd263826d8e146b0a6bbb97a531d2a423cc02a Mon Sep 17 00:00:00 2001 From: never-eat-yellow-snow Date: Sun, 7 Jun 2015 11:59:22 +0200 Subject: [PATCH 3/3] ups - default style did not work! --- pygal/graph/pie.py | 13 +++++++++++-- pygal/svg.py | 43 +++++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 22 deletions(-) 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/svg.py b/pygal/svg.py index 586525c3..0a219001 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -95,31 +95,32 @@ def add_styles(self): css_text = css[len('inline:'):] else: if type(css) == str and css.startswith("!"): - css_text = importlib.import_module(css[1:]).data + 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) - - class FontSizes(object): - """Container for font sizes""" - fs = FontSizes() - for name in dir(self.graph.state): - if name.endswith('_font_size'): - setattr( - fs, - 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_raw = f.read() else: - css_text = css.data + css_raw = css.data + + class FontSizes(object): + """Container for font sizes""" + fs = FontSizes() + for name in dir(self.graph.state): + if name.endswith('_font_size'): + setattr( + fs, + name.replace('_font_size', ''), + ('%dpx' % getattr(self.graph, name))) + + 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) @@ -134,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)):