Projet

Général

Profil

0002-actesweb-work-around-not-latin15-encodable-character.patch

Benjamin Dauvergne, 01 mars 2019 08:41

Télécharger (3,36 ko)

Voir les différences:

Subject: [PATCH 2/2] actesweb: work-around not latin15 encodable characters
 (fixes #30995)

 debian/control                     |  3 ++-
 passerelle/apps/actesweb/models.py |  4 ++++
 passerelle/utils/conversion.py     | 25 +++++++++++++++++++++++++
 setup.py                           |  1 +
 4 files changed, 32 insertions(+), 1 deletion(-)
debian/control
29 29
    python-pyproj,
30 30
    python-pil,
31 31
    python-zeep,
32
    python-jsonschema
32
    python-jsonschema,
33
    python-unidecode
33 34
Recommends: python-soappy, python-phpserialize
34 35
Description: Uniform access to multiple data sources and services (Python module)
35 36

  
passerelle/apps/actesweb/models.py
31 31
from passerelle.base.models import BaseResource
32 32
from passerelle.utils.api import endpoint
33 33
from passerelle.utils.jsonresponse import APIError
34
from passerelle.utils.conversion import ensure_encoding
34 35

  
35 36

  
36 37
@contextlib.contextmanager
......
80 81
            else:
81 82
                os.makedirs(tmp_dir)
82 83

  
84
        # ensure demand_content can be encoded to latin15
85
        demand_content = ensure_encoding(demand_content, 'iso-8859-15')
86

  
83 87
        filename = '%s.DEM' % now().strftime('%Y-%m-%d_%H-%M-%S_%f')
84 88
        filepath = os.path.join(self.basepath, filename)
85 89
        with named_tempfile(dir=tmp_dir, suffix='.DEM', delete=False) as tpf:
passerelle/utils/conversion.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17
import re
18
import unicodedata
17 19
import warnings
18 20
from StringIO import StringIO
19 21

  
22
import unidecode
20 23
from PIL import Image
21 24

  
22 25

  
......
37 40
    out = StringIO()
38 41
    image.save(out, format='PDF')
39 42
    return out.getvalue()
43

  
44

  
45
# copied from
46
# https://stackoverflow.com/questions/10294032/python-replace-typographical-quotes-dashes-etc-with-their-ascii-counterparts
47
def char_filter(string):
48
    '''Fallback to ASCII char if found'''
49
    latin = re.compile('[a-zA-Z]+')
50
    for char in unicodedata.normalize('NFC', string):
51
        decoded = unidecode.unidecode(char)
52
        if latin.match(decoded):
53
            yield char
54
        else:
55
            yield decoded
56

  
57

  
58
def clean_string(string):
59
    return "".join(char_filter(string))
60

  
61

  
62
def ensure_encoding(s, encoding):
63
    s = clean_string(s)
64
    return s.encode(encoding, 'replace').decode(encoding)
setup.py
107 107
            'jsonschema',
108 108
            'zeep < 3.0',
109 109
            'pycrypto',
110
            'unidecode',
110 111
        ],
111 112
        cmdclass={
112 113
            'build': build,
113
-