GrampsLocale: Replace gen.plug.utils gfloat()

With GrampsLocale.float().
Also remove gen.plug.utils gformat(), which was written to work
around string formatting with %f localizing the decimal point,
which it doesn't do. locale.format() does, but it wasn't being
used anyway.

svn: r22028
This commit is contained in:
John Ralls 2013-04-20 23:09:16 +00:00
parent 769203162c
commit 467a9b1c43
5 changed files with 77 additions and 90 deletions

View File

@ -43,11 +43,11 @@ def escxml(string):
# GRAMPS modules # GRAMPS modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ..utils import gformat, gfloat
from .paragraphstyle import ParagraphStyle from .paragraphstyle import ParagraphStyle
from .fontstyle import FontStyle from .fontstyle import FontStyle
from .tablestyle import TableStyle, TableCellStyle from .tablestyle import TableStyle, TableCellStyle
from .graphicstyle import GraphicsStyle from .graphicstyle import GraphicsStyle
from gramps.gen.const import GRAMPS_LOCALE as glocale
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -182,12 +182,12 @@ class StyleSheetList(object):
'<para ' + '<para ' +
'description="%s" ' % 'description="%s" ' %
escxml(para.get_description()) + escxml(para.get_description()) +
'rmargin="%s" ' % gformat(rmargin) + 'rmargin="%.3f" ' % rmargin +
'lmargin="%s" ' % gformat(lmargin) + 'lmargin="%.3f" ' % lmargin +
'first="%s" ' % gformat(findent) + 'first="%.3f" ' % findent +
'tmargin="%s" ' % gformat(tmargin) + 'tmargin="%.3f" ' % tmargin +
'bmargin="%s" ' % gformat(bmargin) + 'bmargin="%.3f" ' % bmargin +
'pad="%s" ' % gformat(padding) + 'pad="%.3f" ' % padding +
'bgcolor="#%02x%02x%02x" ' % bg_color + 'bgcolor="#%02x%02x%02x" ' % bg_color +
'level="%d" ' % para.get_header_level() + 'level="%d" ' % para.get_header_level() +
@ -408,18 +408,18 @@ class SheetParser(handler.ContentHandler):
elif tag == "para": elif tag == "para":
if 'description' in attrs: if 'description' in attrs:
self.p.set_description(attrs['description']) self.p.set_description(attrs['description'])
self.p.set_right_margin(gfloat(attrs['rmargin'])) self.p.set_right_margin(glocale.float(attrs['rmargin']))
self.p.set_right_margin(gfloat(attrs['rmargin'])) self.p.set_right_margin(glocale.float(attrs['rmargin']))
self.p.set_left_margin(gfloat(attrs['lmargin'])) self.p.set_left_margin(glocale.float(attrs['lmargin']))
self.p.set_first_indent(gfloat(attrs['first'])) self.p.set_first_indent(glocale.float(attrs['first']))
try: try:
# This is needed to read older style files # This is needed to read older style files
# lacking tmargin and bmargin # lacking tmargin and bmargin
self.p.set_top_margin(gfloat(attrs['tmargin'])) self.p.set_top_margin(glocale.float(attrs['tmargin']))
self.p.set_bottom_margin(gfloat(attrs['bmargin'])) self.p.set_bottom_margin(glocale.float(attrs['bmargin']))
except KeyError: except KeyError:
pass pass
self.p.set_padding(gfloat(attrs['pad'])) self.p.set_padding(glocale.float(attrs['pad']))
self.p.set_alignment(int(attrs['align'])) self.p.set_alignment(int(attrs['align']))
self.p.set_right_border(int(attrs['rborder'])) self.p.set_right_border(int(attrs['rborder']))
self.p.set_header_level(int(attrs['level'])) self.p.set_header_level(int(attrs['level']))

View File

@ -26,15 +26,14 @@
# Python modules # Python modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ...const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# GRAMPS modules # GRAMPS modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ..utils import gfloat from ...const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
from ..docgen import PaperSize from ..docgen import PaperSize
from ...const import PAPERSIZE from ...const import PAPERSIZE
@ -74,8 +73,8 @@ class PageSizeParser(handler.ContentHandler):
def startElement(self, tag, attrs): def startElement(self, tag, attrs):
if tag == "page": if tag == "page":
name = attrs['name'] name = attrs['name']
height = gfloat(attrs['height']) height = glocale.float(attrs['height'])
width = gfloat(attrs['width']) width = glocale.float(attrs['width'])
self.paper_list.append(PaperSize(name, height, width)) self.paper_list.append(PaperSize(name, height, width))
#------------------------------------------------------------------------- #-------------------------------------------------------------------------

View File

@ -29,7 +29,6 @@ General utility functions useful for the generic plugin system
# Standard Python modules # Standard Python modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
import locale
import sys import sys
import os import os
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
@ -54,33 +53,6 @@ _ = glocale.translation.gettext
# Local utility functions for gen.plug # Local utility functions for gen.plug
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
def gfloat(val):
"""Convert to floating number, taking care of possible locale differences.
Useful for reading float values from text entry fields
while under non-English locale.
"""
try:
return float(val)
except:
try:
return float(val.replace('.', ', '))
except:
return float(val.replace(', ', '.'))
return 0.0
def gformat(val):
"""Performs ('%.3f' % val) formatting with the resulting string always
using dot ('.') as a decimal point.
Useful for writing float values into XML when under non-English locale.
"""
decimal_point = locale.localeconv()['decimal_point']
return_val = "%.3f" % val
return return_val.replace(decimal_point, '.')
def version_str_to_tup(sversion, positions): def version_str_to_tup(sversion, positions):
""" """
Given a string version and positions count, returns a tuple of Given a string version and positions count, returns a tuple of

View File

@ -932,6 +932,26 @@ class GrampsLocale(object):
""" """
return locale.format_string(format, val, grouping) return locale.format_string(format, val, grouping)
def float(self, val):
"""
Parse a string to a floating point number. Uses locale.atof(),
in future with ICU present will use icu.NumberFormat.parse().
"""
try:
return locale.atof(val)
except ValueError:
point = locale.localeconv()['decimal_point']
sep = locale.localeconv()['thousands_sep']
try:
if point == ',':
return locale.atof(val.replace(' ', sep).replace('.', sep))
elif point == '.':
return locale.atof(val.replace(' ', sep).replace(',', sep))
else:
return None
except ValueError:
return None
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# Translations Classes # Translations Classes

View File

@ -40,16 +40,14 @@ from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext _ = glocale.translation.gettext
from gramps.gen.plug.report import utils as ReportUtils from gramps.gen.plug.report import utils as ReportUtils
from gramps.gen.plug.docgen import BaseDoc, DrawDoc, FONT_SERIF, PAPER_PORTRAIT, SOLID from gramps.gen.plug.docgen import BaseDoc, DrawDoc, FONT_SERIF, PAPER_PORTRAIT, SOLID
from gramps.gen.plug.utils import gformat
from gramps.gen.errors import ReportError from gramps.gen.errors import ReportError
from gramps.gen.constfunc import cuni from gramps.gen.constfunc import cuni
def lrgb(grp): def lrgb(grp):
grp = ReportUtils.rgb_color(grp) return ['%.3f' % x for x in ReportUtils.rgb_color(grp)]
return (gformat(grp[0]), gformat(grp[1]), gformat(grp[2]))
def coords(grp): def coords(grp):
return (gformat(grp[0]), gformat(grp[1])) return ['%.3f' % x for x in grp]
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -160,8 +158,8 @@ class PSDrawDoc(BaseDoc, DrawDoc):
"%d %d\n" % (self.page, self.page) "%d %d\n" % (self.page, self.page)
) )
if self.paper.get_orientation() != PAPER_PORTRAIT: if self.paper.get_orientation() != PAPER_PORTRAIT:
self.file.write('90 rotate %s cm %s cm translate\n' % ( self.file.write('90 rotate %.3f cm %.3f cm translate\n' %
gformat(0),gformat(-1*self.paper.get_size().get_height()))) (0, -1*self.paper.get_size().get_height()))
def end_page(self): def end_page(self):
self.file.write( self.file.write(
@ -196,10 +194,10 @@ class PSDrawDoc(BaseDoc, DrawDoc):
self.file.write( self.file.write(
'gsave\n' 'gsave\n'
'%s %s %s setrgbcolor\n' % lrgb(stype.get_color()) + '%.3f %.3f %.3f setrgbcolor\n' % stype.get_color() +
fdef + fdef +
'(%s) dup stringwidth pop -2 div ' % text + '(%s) dup stringwidth pop -2 div ' % text +
'%s cm add %s cm moveto ' % coords(self.translate(x, y)) + '%.3f cm add %.3f cm moveto ' % self.translate(x, y) +
'show\n' 'show\n'
'grestore\n' 'grestore\n'
) )
@ -218,7 +216,7 @@ class PSDrawDoc(BaseDoc, DrawDoc):
self.file.write( self.file.write(
'gsave\n' 'gsave\n'
'%s cm %s cm moveto\n' % coords(self.translate(x1, y1)) + '%.3f cm %.3f cm moveto\n' % self.translate(x1, y1) +
fdef + fdef +
'(%s) show grestore\n' % text '(%s) show grestore\n' % text
) )
@ -239,14 +237,12 @@ class PSDrawDoc(BaseDoc, DrawDoc):
(new_text, fdef) = self.encode_text(p, text[0]) (new_text, fdef) = self.encode_text(p, text[0])
coords = self.translate(x, y)
self.file.write( self.file.write(
'gsave\n' + 'gsave\n' +
fdef + fdef +
'%s cm %s cm translate\n' % ( '%.3f cm %.3f cm translate\n' % self.translate(x, y) +
gformat(coords[0]), gformat(coords[1])) + '%.3f rotate\n' % -angle +
'%s rotate\n' % gformat(-angle) + '%.3f %.3f %.3f setrgbcolor\n' % stype.get_color()
'%s %s %s setrgbcolor\n' % lrgb(stype.get_color())
) )
y = ((size * len(text)) / 2.0) - size y = ((size * len(text)) / 2.0) - size
@ -255,7 +251,7 @@ class PSDrawDoc(BaseDoc, DrawDoc):
self.file.write( self.file.write(
'(%s) dup stringwidth pop -2 div ' '(%s) dup stringwidth pop -2 div '
% self.encode(line) + % self.encode(line) +
"%s moveto show\n" % gformat(y) "%.3f moveto show\n" % y
) )
y -= size y -= size
@ -267,7 +263,7 @@ class PSDrawDoc(BaseDoc, DrawDoc):
self.file.write( self.file.write(
'gsave\n' 'gsave\n'
'newpath\n' 'newpath\n'
'%s setlinewidth\n' % gformat(stype.get_line_width()) '%.3f setlinewidth\n' % stype.get_line_width()
) )
if stype.get_line_style() == SOLID: if stype.get_line_style() == SOLID:
self.file.write('[] 0 setdash\n') self.file.write('[] 0 setdash\n')
@ -281,21 +277,21 @@ class PSDrawDoc(BaseDoc, DrawDoc):
x1 = point[0] + self.paper.get_left_margin() x1 = point[0] + self.paper.get_left_margin()
y1 = point[1] + self.paper.get_top_margin() y1 = point[1] + self.paper.get_top_margin()
self.file.write( self.file.write(
'%s cm %s cm moveto\n' % coords(self.translate(x1, y1)) '%.3f cm %.3f cm moveto\n' % self.translate(x1, y1)
) )
for point in path[1:]: for point in path[1:]:
x1 = point[0] + self.paper.get_left_margin() x1 = point[0] + self.paper.get_left_margin()
y1 = point[1] + self.paper.get_top_margin() y1 = point[1] + self.paper.get_top_margin()
self.file.write( self.file.write(
'%s cm %s cm lineto\n' % coords(self.translate(x1, y1)) '%.3f cm %.3f cm lineto\n' % self.translate(x1, y1)
) )
self.file.write('closepath\n') self.file.write('closepath\n')
color = stype.get_fill_color() color = stype.get_fill_color()
self.file.write( self.file.write(
'gsave %s %s %s setrgbcolor fill grestore\n' % lrgb(color) + 'gsave %.3f %.3f %.3f setrgbcolor fill grestore\n' % color +
'%s %s %s setrgbcolor stroke\n' % lrgb(stype.get_color()) + '%.3f %.3f %.3f setrgbcolor stroke\n' % stype.get_color() +
'grestore\n' 'grestore\n'
) )
@ -308,9 +304,9 @@ class PSDrawDoc(BaseDoc, DrawDoc):
stype = style_sheet.get_draw_style(style) stype = style_sheet.get_draw_style(style)
self.file.write( self.file.write(
'gsave newpath\n' 'gsave newpath\n'
'%s cm %s cm moveto\n' % coords(self.translate(x1, y1)) + '%.3f cm %.3f cm moveto\n' % self.translate(x1, y1) +
'%s cm %s cm lineto\n' % coords(self.translate(x2, y2)) + '%.3f cm %.3f cm lineto\n' % self.translate(x2, y2) +
'%s setlinewidth\n' % gformat(stype.get_line_width()) '%.3f setlinewidth\n' % stype.get_line_width()
) )
if stype.get_line_style() == SOLID: if stype.get_line_style() == SOLID:
self.file.write('[] 0 setdash\n') self.file.write('[] 0 setdash\n')
@ -322,7 +318,7 @@ class PSDrawDoc(BaseDoc, DrawDoc):
self.file.write( self.file.write(
'2 setlinecap\n' + '2 setlinecap\n' +
'%s %s %s setrgbcolor stroke\n' % lrgb(stype.get_color()) + '%.3f %.3f %.3f setrgbcolor stroke\n' % stype.get_color() +
'grestore\n' 'grestore\n'
) )
@ -340,41 +336,41 @@ class PSDrawDoc(BaseDoc, DrawDoc):
if box_style.get_shadow(): if box_style.get_shadow():
self.file.write( self.file.write(
'newpath\n' 'newpath\n'
'%s cm %s cm moveto\n' '%.3f cm %.3f cm moveto\n'
% coords(self.translate(x+shadsize, y+shadsize)) + % self.translate(x+shadsize, y+shadsize) +
'0 -%s cm rlineto\n' % gformat(h) + '0 -%.3f cm rlineto\n' % h +
'%s cm 0 rlineto\n' % gformat(w) + '%.3f cm 0 rlineto\n' % w +
'0 %s cm rlineto\n' % gformat(h) + '0 %.3f cm rlineto\n' % h +
'closepath\n' 'closepath\n'
'.5 setgray\n' '.5 setgray\n'
'fill\n' 'fill\n'
) )
self.file.write( self.file.write(
'newpath\n' 'newpath\n'
'%s cm %s cm moveto\n' % coords(self.translate(x, y)) + '%.3f cm %.3f cm moveto\n' % self.translate(x, y) +
'0 -%s cm rlineto\n' % gformat(h) + '0 -%.3f cm rlineto\n' % h +
'%s cm 0 rlineto\n' % gformat(w) + '%.3f cm 0 rlineto\n' % w +
'0 %s cm rlineto\n' % gformat(h) + '0 %.3f cm rlineto\n' % h +
'closepath\n' 'closepath\n'
) )
fill_color = box_style.get_fill_color() fill_color = box_style.get_fill_color()
color = box_style.get_color() color = box_style.get_color()
self.file.write( self.file.write(
'gsave %s %s %s setrgbcolor fill grestore\n' % lrgb(fill_color) + 'gsave %.3f %.3f %.3f setrgbcolor fill grestore\n' % fill_color +
'%s %s %s setrgbcolor stroke\n' % lrgb(color) '%.3f %.3f %.3f setrgbcolor stroke\n' % color
) )
self.file.write('newpath\n') self.file.write('newpath\n')
if box_style.get_line_width(): if box_style.get_line_width():
self.file.write( self.file.write(
'%s cm %s cm moveto\n' % coords(self.translate(x, y)) + '%.3f cm %.3f cm moveto\n' % self.translate(x, y) +
'0 -%s cm rlineto\n' % gformat(h) + '0 -%.3f cm rlineto\n' % h +
'%s cm 0 rlineto\n' % gformat(w) + '%.3f cm 0 rlineto\n' % w +
'0 %s cm rlineto\n' % gformat(h) + '0 %.3f cm rlineto\n' % h +
'closepath\n' + 'closepath\n' +
'%s setlinewidth\n' % gformat(box_style.get_line_width()) + '%.3f setlinewidth\n' % box_style.get_line_width() +
'%s %s %s setrgbcolor stroke\n' % lrgb(box_style.get_color()) '%.3f %.3f %.3f setrgbcolor stroke\n' % box_style.get_color()
) )
if text: if text:
para_name = box_style.get_paragraph_style() para_name = box_style.get_paragraph_style()
@ -392,8 +388,8 @@ class PSDrawDoc(BaseDoc, DrawDoc):
for i, line in enumerate(lines): for i, line in enumerate(lines):
ypos = ystart + (i * fs) ypos = ystart + (i * fs)
self.file.write( self.file.write(
'%s cm %s cm moveto\n' '%.3f cm %.3f cm moveto\n'
% coords(self.translate(x+mar, ypos)) + % self.translate(x+mar, ypos) +
"(%s) show\n" % lines[i] "(%s) show\n" % lines[i]
) )
self.file.write('grestore\n') self.file.write('grestore\n')