From b612118432b0f1eb7e61c2b220396bb30355cbd3 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?David=20MENTR=C3=89?= Date: Sat, 19 Dec 2009 17:31:55 +0100 Subject: [PATCH 1/5] Move locale-depend code into a new module i18n --- ocitysmap/i18n.py | 65 +++++++++++++++++++++++++++++++++++++++++++++ ocitysmap/street_index.py | 44 +++--------------------------- 2 files changed, 69 insertions(+), 40 deletions(-) create mode 100644 ocitysmap/i18n.py diff --git a/ocitysmap/i18n.py b/ocitysmap/i18n.py new file mode 100644 index 0000000..7e95514 --- /dev/null +++ b/ocitysmap/i18n.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8; mode: Python -*- + +# ocitysmap, city map and street index generator from OpenStreetMap data +# Copyright (C) 2009 David Decotigny +# Copyright (C) 2009 Frédéric Lehobey +# Copyright (C) 2009 David Mentré +# Copyright (C) 2009 Maxime Petazzoni +# Copyright (C) 2009 Thomas Petazzoni +# Copyright (C) 2009 Gaël Utard + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or any later version. + +# This program 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 Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import re + +APPELLATIONS = [ u"Allée", u"Avenue", u"Boulevard", u"Carrefour", u"Chaussée", + u"Chemin", u"Cité", u"Clos", u"Côte", u"Cour", u"Cours", + u"Degré", + u"Esplanade", u"Impasse", u"Liaison", u"Mail", u"Montée", + u"Passage", u"Place", u"Placette", u"Pont", u"Promenade", + u"Quai", + u"Résidence", u"Rond-Point", u"Rang", u"Route", u"Rue", + u"Ruelle", + u"Square", u"Traboule", u"Traverse", u"Venelle", u"Villa", + u"Voie", u"Rond-point" ] +DETERMINANTS = [ u" des", u" du", u" de la", u" de l'", u" de", u" d'", u"" ] + +SPACE_REDUCE = re.compile(r"\s+") +PREFIX_REGEXP = re.compile(r"^(?P(%s)(%s)?)\s?\b(?P.*)" % + ("|".join(APPELLATIONS), + "|".join(DETERMINANTS)), re.IGNORECASE | re.UNICODE) + +# for IndexPageGenerator._upper_unaccent_string +E_ACCENT = re.compile(ur"[éèêëẽ]", re.IGNORECASE | re.UNICODE) +I_ACCENT = re.compile(ur"[íìîïĩ]", re.IGNORECASE | re.UNICODE) +A_ACCENT = re.compile(ur"[áàâäã]", re.IGNORECASE | re.UNICODE) +O_ACCENT = re.compile(ur"[óòôöõ]", re.IGNORECASE | re.UNICODE) +U_ACCENT = re.compile(ur"[úùûüũ]", re.IGNORECASE | re.UNICODE) + +def user_readable_street(name): + name = name.strip() + name = SPACE_REDUCE.sub(" ", name) + name = PREFIX_REGEXP.sub(r"\g (\g)", name) + return name + +def _upper_unaccent_string(s): + s = E_ACCENT.sub("e", s) + s = I_ACCENT.sub("i", s) + s = A_ACCENT.sub("a", s) + s = O_ACCENT.sub("o", s) + s = U_ACCENT.sub("u", s) + return s.upper() + +def first_letter_equal(a, b): + return _upper_unaccent_string(a) == _upper_unaccent_string(b) diff --git a/ocitysmap/street_index.py b/ocitysmap/street_index.py index 75586bc..ba0f691 100644 --- a/ocitysmap/street_index.py +++ b/ocitysmap/street_index.py @@ -24,6 +24,7 @@ import logging, traceback import sys, os, tempfile, pgdb, re, math, cairo, locale, gzip, csv import ConfigParser +import i18n from coords import BoundingBox import map_canvas, grid, utils @@ -32,26 +33,6 @@ from draw_utils import enclose_in_frame l = logging.getLogger('ocitysmap') -APPELLATIONS = [ u"Allée", u"Avenue", u"Boulevard", u"Carrefour", u"Chaussée", - u"Chemin", u"Cité", u"Clos", u"Côte", u"Cour", u"Cours", u"Degré", - u"Esplanade", u"Impasse", u"Liaison", u"Mail", u"Montée", - u"Passage", u"Place", u"Placette", u"Pont", u"Promenade", u"Quai", - u"Résidence", u"Rond-Point", u"Rang", u"Route", u"Rue", u"Ruelle", - u"Square", u"Traboule", u"Traverse", u"Venelle", u"Villa", - u"Voie", u"Rond-point" ] -DETERMINANTS = [ u" des", u" du", u" de la", u" de l'", u" de", u" d'", u"" ] - -SPACE_REDUCE = re.compile(r"\s+") -PREFIX_REGEXP = re.compile(r"^(?P(%s)(%s)?)\s?\b(?P.*)" % - ("|".join(APPELLATIONS), - "|".join(DETERMINANTS)), re.IGNORECASE | re.UNICODE) -# for IndexPageGenerator._upper_unaccent_string -E_ACCENT = re.compile(ur"[éèêëẽ]", re.IGNORECASE | re.UNICODE) -I_ACCENT = re.compile(ur"[íìîïĩ]", re.IGNORECASE | re.UNICODE) -A_ACCENT = re.compile(ur"[áàâäã]", re.IGNORECASE | re.UNICODE) -O_ACCENT = re.compile(ur"[óòôöõ]", re.IGNORECASE | re.UNICODE) -U_ACCENT = re.compile(ur"[úùûüũ]", re.IGNORECASE | re.UNICODE) - class BaseOCitySMapError(Exception): """Base class for exceptions thrown by OCitySMap.""" @@ -62,12 +43,6 @@ def _humanize_street_label(street): """Creates a street label usable in the street list adjacent to the map (like 'Bréhat (Allée des)' from the street definition tuple.""" - def unprefix_street(name): - name = name.strip() - name = SPACE_REDUCE.sub(" ", name) - name = PREFIX_REGEXP.sub(r"\g (\g)", name) - return name - def couple_compare(x,y): a = y[0] - x[0] if a: @@ -77,7 +52,7 @@ def _humanize_street_label(street): def distance(a,b): return (b[0]-a[0])**2 + (b[1]-a[1])**2 - name = unprefix_street(street[0]) + name = i18n.user_readable_street(street[0]) squares = street[1] minx = min([x[0] for x in squares]) maxx = max([x[0] for x in squares]) @@ -143,24 +118,13 @@ class IndexPageGenerator: 'em' : em, } - def _upper_unaccent_string(self, s): - s = E_ACCENT.sub("e", s) - s = I_ACCENT.sub("i", s) - s = A_ACCENT.sub("a", s) - s = O_ACCENT.sub("o", s) - s = U_ACCENT.sub("u", s) - return s.upper() - - def _equal_without_accent(self, a, b): - return self._upper_unaccent_string(a) == self._upper_unaccent_string(b) - def _fits_in_page(self, cr, paperwidth, paperheight, fontsize): fp = self._get_font_parameters(cr, fontsize) prevletter = u'' heading_letter_count = 0 for street in self.streets: - if not self._equal_without_accent(street[0][0], prevletter): + if not i18n.first_letter_equal(street[0][0], prevletter): heading_letter_count += 1 prevletter = street[0][0] @@ -214,7 +178,7 @@ class IndexPageGenerator: for street in self.streets: # Letter label firstletter = street[0][0] - if not self._equal_without_accent(firstletter, prevletter): + if not i18n.first_letter_equal(firstletter, prevletter): # Make sure we have no orphelin heading letter label at the # end of a column if y + heading_fheight + fheight > paperheight: -- 1.6.3.3