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

View File

@ -26,15 +26,14 @@
# Python modules
#
#-------------------------------------------------------------------------
from ...const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
#-------------------------------------------------------------------------
#
# GRAMPS modules
#
#-------------------------------------------------------------------------
from ..utils import gfloat
from ...const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
from ..docgen import PaperSize
from ...const import PAPERSIZE
@ -74,8 +73,8 @@ class PageSizeParser(handler.ContentHandler):
def startElement(self, tag, attrs):
if tag == "page":
name = attrs['name']
height = gfloat(attrs['height'])
width = gfloat(attrs['width'])
height = glocale.float(attrs['height'])
width = glocale.float(attrs['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
#
#-------------------------------------------------------------------------
import locale
import sys
import os
if sys.version_info[0] < 3:
@ -54,33 +53,6 @@ _ = glocale.translation.gettext
# 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):
"""
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)
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

View File

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