From b32bb1c256916b3141ebc5bef3bd61582ce76012 Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Thu, 5 Jun 2014 12:19:41 +0200 Subject: [PATCH] Wrap etree --- pygal/_compat.py | 10 ------ pygal/etree.py | 52 ++++++++++++++++++++++++++++ pygal/graph/frenchmap.py | 3 +- pygal/graph/supranationalworldmap.py | 2 +- pygal/graph/worldmap.py | 2 +- pygal/svg.py | 3 +- pygal/test/__init__.py | 1 - relauncher | 10 ------ 8 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 pygal/etree.py delete mode 100755 relauncher diff --git a/pygal/_compat.py b/pygal/_compat.py index f251f43..d16d706 100644 --- a/pygal/_compat.py +++ b/pygal/_compat.py @@ -16,19 +16,9 @@ # # You should have received a copy of the GNU Lesser General Public License # along with pygal. If not, see . -import os import sys from collections import Iterable -try: - if os.getenv('NO_LXML', None): - raise ImportError('Explicit lxml bypass') - from lxml import etree - etree.lxml = True -except ImportError: - from xml.etree import ElementTree as etree - etree.lxml = False - if sys.version_info[0] == 3: base = (str, bytes) diff --git a/pygal/etree.py b/pygal/etree.py new file mode 100644 index 0000000..41a447a --- /dev/null +++ b/pygal/etree.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# This file is part of pygal +# +# A python svg graph plotting library +# Copyright © 2012-2014 Kozea +# +# This library is free software: you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) any +# later version. +# +# This library is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +# details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with pygal. If not, see . +import os + + +class Etree(object): + def __init__(self): + from xml.etree import ElementTree as _py_etree + self._py_etree = _py_etree + try: + from lxml import etree as _lxml_etree + self._lxml_etree = _lxml_etree + except ImportError: + self._lxml_etree = None + + if os.getenv('NO_LXML', None): + self._etree = self._py_etree + else: + self._etree = self._lxml_etree or self._py_etree + self.lxml = self._etree is self._lxml_etree + + def __getattribute__(self, attr): + if (attr not in object.__getattribute__(self, '__dict__') and + attr not in Etree.__dict__): + return object.__getattribute__(self._etree, attr) + return object.__getattribute__(self, attr) + + def to_lxml(self): + self._etree = self._lxml_etree + self.lxml = True + + def to_etree(self): + self._etree = self._py_etree + self.lxml = False + +etree = Etree() diff --git a/pygal/graph/frenchmap.py b/pygal/graph/frenchmap.py index 91fd1d4..fa60eed 100644 --- a/pygal/graph/frenchmap.py +++ b/pygal/graph/frenchmap.py @@ -26,7 +26,8 @@ from collections import defaultdict from pygal.ghost import ChartCollection from pygal.util import cut, cached_property, decorate from pygal.graph.graph import Graph -from pygal._compat import u, etree +from pygal._compat import u +from pygal.etree import etree from numbers import Number import os diff --git a/pygal/graph/supranationalworldmap.py b/pygal/graph/supranationalworldmap.py index a21be10..b9f7809 100644 --- a/pygal/graph/supranationalworldmap.py +++ b/pygal/graph/supranationalworldmap.py @@ -25,7 +25,7 @@ from __future__ import division from pygal.graph.worldmap import Worldmap from pygal.i18n import SUPRANATIONAL from pygal.util import cut, decorate -from pygal._compat import etree +from pygal.etree import etree import os with open(os.path.join( diff --git a/pygal/graph/worldmap.py b/pygal/graph/worldmap.py index cd9fd73..c509455 100644 --- a/pygal/graph/worldmap.py +++ b/pygal/graph/worldmap.py @@ -25,7 +25,7 @@ from __future__ import division from pygal.util import cut, cached_property, decorate from pygal.graph.graph import Graph from pygal.i18n import COUNTRIES -from pygal._compat import etree +from pygal.etree import etree import os with open(os.path.join( diff --git a/pygal/svg.py b/pygal/svg.py index 7c5f938..f7b9789 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -22,7 +22,8 @@ Svg helper """ from __future__ import division -from pygal._compat import to_str, u, etree +from pygal._compat import to_str, u +from pygal.etree import etree import io import os import json diff --git a/pygal/test/__init__.py b/pygal/test/__init__.py index d3471ae..da94e4a 100644 --- a/pygal/test/__init__.py +++ b/pygal/test/__init__.py @@ -22,7 +22,6 @@ from pygal.util import cut from datetime import datetime from pygal.i18n import COUNTRIES from pygal.graph.frenchmap import DEPARTMENTS, REGIONS -from pygal._compat import etree def get_data(i): diff --git a/relauncher b/relauncher deleted file mode 100755 index 1fc768b..0000000 --- a/relauncher +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/zsh -pkill -f livereload -pkill -f reload -pkill -f SimpleHTTPServer - -livereload& -reload ./demo/simple_test.py **/*.{py,js,css}& -python -m SimpleHTTPServer 1515& -sleep 1 -chromium http://localhost:1515/&