Part 1 of issue 4276. I principle same patch as for branch, but adapeted to trunk.

svn: r15959
This commit is contained in:
Peter Landgren 2010-10-09 12:33:47 +00:00
parent 5ba9d48f86
commit 1d8a776cd3
27 changed files with 118 additions and 45 deletions

View File

@ -478,7 +478,7 @@ class ExportAssistant(gtk.Assistant, ManagedWindow.ManagedWindow) :
#Allow for exotic error: file is still not correct
self.check_fileselect(self.chooser, show=False)
if self.get_page_complete(self.chooser) :
filename = Utils.get_unicode_path(self.chooser.get_filename())
filename = Utils.get_unicode_path_from_file_chooser(self.chooser.get_filename())
name = os.path.split(filename)[1]
folder = os.path.split(filename)[0]
confirm_text = _(
@ -612,7 +612,7 @@ class ExportAssistant(gtk.Assistant, ManagedWindow.ManagedWindow) :
hasattr(self.option_box_instance, "no_fileselect")):
filename = ""
else:
filename = Utils.get_unicode_path(self.chooser.get_filename())
filename = Utils.get_unicode_path_from_file_chooser(self.chooser.get_filename())
config.set('paths.recent-export-dir', os.path.split(filename)[0])
ix = self.get_selected_format_index()
config.set('behavior.recent-export-type', ix)

View File

@ -27,6 +27,7 @@
#-------------------------------------------------------------------------
from xml.sax import make_parser, SAXParseException
import os
import sys
#-------------------------------------------------------------------------
#
@ -114,8 +115,7 @@ class FilterList(object):
return l.replace('"', '"')
def save(self):
f = open(self.file.encode('utf-8'), 'w')
f = open(self.file.encode(sys.getfilesystemencoding()), 'w')
f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
f.write('<filters>\n')
for namespace in self.filter_namespaces:

View File

@ -38,6 +38,13 @@ import tempfile
#
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#
# Gramps modules
#
#-------------------------------------------------------------------------
import Utils
#-------------------------------------------------------------------------
#
# resize_to_jpeg
@ -111,6 +118,7 @@ def resize_to_jpeg_buffer(source, width, height):
img = gtk.gdk.pixbuf_new_from_file(source)
scaled = img.scale_simple(int(width), int(height), gtk.gdk.INTERP_BILINEAR)
os.close(filed)
dest = Utils.get_unicode_path_from_env_var(dest)
scaled.save(dest, 'jpeg')
ofile = open(dest, mode='rb')
data = ofile.read()

View File

@ -234,6 +234,11 @@ def get_addon_translator(filename=None, domain="addon", languages=None):
gramps_translator = gettext.translation(LOCALEDOMAIN, LOCALEDIR,
fallback=True)
path = os.path.dirname(os.path.abspath(filename))
# Check if path is of type str. Do import and conversion if so.
# The import cannot be done at the top as that will conflict with the translation system.
if type(path) == str:
from Utils import get_unicode_path_from_env_var
path = get_unicode_path_from_env_var(path)
if languages:
addon_translator = gettext.translation(domain, os.path.join(path,"locale"),
languages=languages,

View File

@ -326,14 +326,14 @@ def find_folder( filename):
# not found
return ''
def get_unicode_path(path):
def get_unicode_path_from_file_chooser(path):
"""
Return the Unicode version of a path string.
:type path: str
:param path: The path to be converted to Unicode
:rtype: unicode
:returns: The Unicode version of path.
:returns: The Unicode version of path.
"""
# Don't make unicode of unicode
if isinstance(path, unicode):
@ -353,6 +353,33 @@ def get_unicode_path(path):
LOG.warn("Problem encountered converting string: %s." % path)
return unicode(path, sys.getfilesystemencoding(), errors='replace')
def get_unicode_path_from_env_var(path):
"""
Return the Unicode version of a path string.
:type path: str
:param path: The path to be converted to Unicode
:rtype: unicode
:returns: The Unicode version of path.
"""
if isinstance(path, unicode):
return path
if constfunc.win():
# In Windows path/filename returned from a emvironment variable is in filesystemencoding
try:
new_path = unicode(path, sys.getfilesystemencoding())
return new_path
except:
LOG.warn("Problem encountered converting string: %s." % path)
return unicode(path, sys.getfilesystemencoding(), errors='replace')
else:
try:
return unicode(path)
except:
LOG.warn("Problem encountered converting string: %s." % path)
return unicode(path, sys.getfilesystemencoding(), errors='replace')
#-------------------------------------------------------------------------
@ -962,6 +989,7 @@ def get_empty_tempdir(dirname):
if os.path.isdir(dirpath):
shutil.rmtree(dirpath)
os.makedirs(dirpath)
dirpath = get_unicode_path_from_env_var(dirpath)
return dirpath
def rm_tempdir(path):

View File

@ -167,6 +167,7 @@ class CLIDbManager(object):
"""
# make the default directory if it does not exist
dbdir = os.path.expanduser(config.get('behavior.database-path'))
dbdir = dbdir.encode(sys.getfilesystemencoding())
make_dbdir(dbdir)
self.current_names = []
@ -354,6 +355,7 @@ def find_next_db_dir():
while True:
base = "%x" % int(time.time())
dbdir = os.path.expanduser(config.get('behavior.database-path'))
dbdir = dbdir.encode(sys.getfilesystemencoding())
new_path = os.path.join(dbdir, base)
if not os.path.isdir(new_path):
break

View File

@ -117,10 +117,7 @@ else:
# Conversion of USER_HOME to unicode was needed to have better
# support for non ASCII path names in Windows for the Gramps database.
USER_HOME = unicode(USER_HOME, sys.getfilesystemencoding())
# Tried also coversion of HOME_DIR, but that caused a lot of problems
# in Windows. Leave it unconverted for now.
#HOME_DIR = unicode(HOME_DIR, sys.getfilesystemencoding())
HOME_DIR = unicode(HOME_DIR, sys.getfilesystemencoding())
#-------------------------------------------------------------------------
#

View File

@ -1052,8 +1052,6 @@ class PluginRegister(object):
lenpd = len(self.__plugindata)
full_filename = os.path.join(dir, filename)
local_gettext = get_addon_translator(full_filename).gettext
if type(full_filename) == str:
full_filename = unicode(full_filename, sys.getfilesystemencoding())
try:
#execfile(full_filename,
execfile(full_filename.encode(sys.getfilesystemencoding()),

View File

@ -40,6 +40,7 @@ import os
#-------------------------------------------------------------------------
from gen.plug._pluginreg import make_environment
import const
import Utils
#-------------------------------------------------------------------------
#
@ -278,7 +279,7 @@ def load_addon_file(path, callback=None):
gpr_files = set([os.path.split(os.path.join(const.USER_PLUGINS, name))[0]
for name in good_gpr])
for gpr_file in gpr_files:
u_gpr_file = unicode(gpr_file, sys.getfilesystemencoding())
u_gpr_file = Utils.get_unicode_path_from_file_chooser(gpr_file)
if callback:
callback(" " + (_("Registered '%s'") % u_gpr_file) + "\n")
file_obj.close()

View File

@ -1138,7 +1138,7 @@ class GrampsPreferences(ConfigureDialog):
status = f.run()
if status == gtk.RESPONSE_OK:
val = Utils.get_unicode_path(f.get_filename())
val = Utils.get_unicode_path_from_file_chooser(f.get_filename())
if val:
self.path_entry.set_text(val)
f.destroy()

View File

@ -86,7 +86,14 @@ class DbLoader(CLIDbLoader):
return 1
def _dberrordialog(self, msg):
DBErrorDialog(str(msg.value))
import traceback
exc = traceback.format_exc()
try:
DBErrorDialog(str(msg.value))
_LOG.error(str(msg.value))
except:
DBErrorDialog(str(msg))
_LOG.error(str(msg) +"\n" + exc)
def _begin_progress(self):
self.uistate.set_busy_cursor(1)
@ -154,7 +161,7 @@ class DbLoader(CLIDbLoader):
if response == gtk.RESPONSE_CANCEL:
break
elif response == gtk.RESPONSE_OK:
filename = Utils.get_unicode_path(import_dialog.get_filename())
filename = Utils.get_unicode_path_from_file_chooser(import_dialog.get_filename())
if self.check_errors(filename):
# displays errors if any
continue
@ -312,8 +319,9 @@ class DbLoader(CLIDbLoader):
except Errors.DbError, msg:
self.dbstate.no_database()
self._dberrordialog(msg)
except Exception:
except Exception as newerror:
self.dbstate.no_database()
self._dberrordialog(str(newerror))
self._end_progress()
return True

View File

@ -77,6 +77,7 @@ import RecentFiles
from glade import Glade
from gen.db.backup import restore
from gen.db.exceptions import DbException
from Utils import get_unicode_path_from_env_var
_RETURN = gtk.gdk.keyval_from_name("Return")
@ -233,7 +234,7 @@ class DbManager(CLIDbManager):
self.rcs.set_sensitive(False)
if store.get_value(node, STOCK_COL) == gtk.STOCK_DIALOG_ERROR:
path = store.get_value(node, PATH_COL)
path = get_unicode_path_from_env_var(store.get_value(node, PATH_COL))
backup = os.path.join(path, "person.gbkp")
self.repair.set_sensitive(os.path.isfile(backup))
else:
@ -346,8 +347,8 @@ class DbManager(CLIDbManager):
self.top.destroy()
del self.selection
del self.name_renderer
return (store.get_value(node, PATH_COL),
store.get_value(node, NAME_COL))
path = get_unicode_path_from_env_var(store.get_value(node, PATH_COL))
return (path, store.get_value(node, NAME_COL))
else:
self.top.destroy()
del self.selection
@ -381,7 +382,7 @@ class DbManager(CLIDbManager):
try:
self.break_lock(self.lock_file)
store, node = self.selection.get_selected()
dbpath = store.get_value(node, PATH_COL)
dbpath = get_unicode_path_from_env_var(store.get_value(node, PATH_COL))
(tval, last) = time_val(dbpath)
store.set_value(node, OPEN_COL, 0)
store.set_value(node, STOCK_COL, "")

View File

@ -149,7 +149,7 @@ class AddMediaObject(ManagedWindow.ManagedWindow):
ErrorDialog(msgstr, msgstr2)
return
filename = Utils.get_unicode_path(self.file_text.get_filename())
filename = Utils.get_unicode_path_from_file_chooser(self.file_text.get_filename())
full_file = filename
if self.relpath.get_active():
@ -186,7 +186,7 @@ class AddMediaObject(ManagedWindow.ManagedWindow):
fname = self.file_text.get_filename()
if not fname:
return
filename = Utils.get_unicode_path(fname)
filename = Utils.get_unicode_path_from_file_chooser(fname)
basename = os.path.basename(filename)
(root, ext) = os.path.splitext(basename)
old_title = unicode(self.description.get_text())

View File

@ -231,7 +231,7 @@ class EditMedia(EditPrimary):
def select_file(self, val):
self.determine_mime()
path = self.file_path.get_text()
self.obj.set_path(Utils.get_unicode_path(path))
self.obj.set_path(Utils.get_unicode_path_from_file_chooser(path))
AddMediaObject(self.dbstate, self.uistate, self.track, self.obj,
self._update_addmedia)
@ -275,7 +275,7 @@ class EditMedia(EditPrimary):
path = self.file_path.get_text()
self.determine_mime()
self.obj.set_path(Utils.get_unicode_path(path))
self.obj.set_path(Utils.get_unicode_path_from_file_chooser(path))
trans = self.db.transaction_begin()
if not self.obj.get_handle():

View File

@ -1276,7 +1276,7 @@ class GuiDestinationOption(gtk.HBox):
status = fcd.run()
if status == gtk.RESPONSE_OK:
path = Utils.get_unicode_path(fcd.get_filename())
path = Utils.get_unicode_path_from_file_chooser(fcd.get_filename())
if path:
if not self.__option.get_directory_entry() and \
not path.endswith(self.__option.get_extension()):

View File

@ -430,7 +430,7 @@ class PluginStatus(ManagedWindow.ManagedWindow):
status = fcd.run()
if status == gtk.RESPONSE_OK:
path = Utils.get_unicode_path(fcd.get_filename())
path = Utils.get_unicode_path_from_file_chooser(fcd.get_filename())
if path:
self.install_addon_path.set_text(path)
fcd.destroy()

View File

@ -74,7 +74,7 @@ class FileEntry(gtk.HBox):
dialog.present()
status = dialog.run()
if status == gtk.RESPONSE_OK:
self.set_filename(Utils.get_unicode_path(dialog.get_filename()))
self.set_filename(Utils.get_unicode_path_from_file_chooser(dialog.get_filename()))
dialog.destroy()
def set_filename(self, path):

View File

@ -36,6 +36,7 @@ import time
from types import ClassType, InstanceType
from gen.ggettext import gettext as _
from subprocess import Popen, PIPE
import sys
#-------------------------------------------------------------------------------
#
@ -448,8 +449,10 @@ class GVPsDoc(GVDocBase):
# but the output is clipped, some margins have disappeared. I used 1 inch margins always.
# See bug tracker issue 2815
# :cairo does not work with Graphviz 2.26.3 See issue 4164
# Covert filename to str using file system encoding.
fname = self._filename.encode(sys.getfilesystemencoding())
command = 'dot -Tps:cairo -o"%s" "%s"' % (self._filename, tmp_dot)
command = 'dot -Tps:cairo -o"%s" "%s"' % (fname, tmp_dot)
dotversion = Popen(['dot', '-V'], stderr=PIPE).communicate(input=None)[1]
# Problem with dot 2.26.3 and multiple pages, which gives "cairo: out of memory"
# If the :cairo is skipped for these cases it gives acceptable result.
@ -498,9 +501,11 @@ class GVSvgDoc(GVDocBase):
dotfile = os.fdopen(handle,"w")
dotfile.write(self._dot.getvalue())
dotfile.close()
# Covert filename to str using file system encoding.
fname = self._filename.encode(sys.getfilesystemencoding())
# Generate the SVG file.
os.system( 'dot -Tsvg -o"%s" "%s"' % (self._filename, tmp_dot) )
os.system( 'dot -Tsvg -o"%s" "%s"' % (fname, tmp_dot) )
# Delete the temporary dot file
os.remove(tmp_dot)
@ -544,9 +549,11 @@ class GVSvgzDoc(GVDocBase):
dotfile = os.fdopen(handle,"w")
dotfile.write(self._dot.getvalue())
dotfile.close()
# Covert filename to str using file system encoding.
fname = self._filename.encode(sys.getfilesystemencoding())
# Generate the SVGZ file.
os.system( 'dot -Tsvgz -o"%s" "%s"' % (self._filename, tmp_dot) )
os.system( 'dot -Tsvgz -o"%s" "%s"' % (fname, tmp_dot) )
# Delete the temporary dot file
os.remove(tmp_dot)
@ -590,9 +597,11 @@ class GVPngDoc(GVDocBase):
dotfile = os.fdopen(handle,"w")
dotfile.write(self._dot.getvalue())
dotfile.close()
# Covert filename to str using file system encoding.
fname = self._filename.encode(sys.getfilesystemencoding())
# Generate the PNG file.
os.system( 'dot -Tpng -o"%s" "%s"' % (self._filename, tmp_dot) )
os.system( 'dot -Tpng -o"%s" "%s"' % (fname, tmp_dot) )
# Delete the temporary dot file
os.remove(tmp_dot)
@ -636,9 +645,11 @@ class GVJpegDoc(GVDocBase):
dotfile = os.fdopen(handle,"w")
dotfile.write(self._dot.getvalue())
dotfile.close()
# Covert filename to str using file system encoding.
fname = self._filename.encode(sys.getfilesystemencoding())
# Generate the JPEG file.
os.system( 'dot -Tjpg -o"%s" "%s"' % (self._filename, tmp_dot) )
os.system( 'dot -Tjpg -o"%s" "%s"' % (fname, tmp_dot) )
# Delete the temporary dot file
os.remove(tmp_dot)
@ -682,9 +693,11 @@ class GVGifDoc(GVDocBase):
dotfile = os.fdopen(handle,"w")
dotfile.write(self._dot.getvalue())
dotfile.close()
# Covert filename to str using file system encoding.
fname = self._filename.encode(sys.getfilesystemencoding())
# Generate the GIF file.
os.system( 'dot -Tgif -o"%s" "%s"' % (self._filename, tmp_dot) )
os.system( 'dot -Tgif -o"%s" "%s"' % (fname, tmp_dot) )
# Delete the temporary dot file
os.remove(tmp_dot)
@ -731,9 +744,11 @@ class GVPdfGvDoc(GVDocBase):
dotfile = os.fdopen(handle,"w")
dotfile.write(self._dot.getvalue())
dotfile.close()
# Covert filename to str using file system encoding.
fname = self._filename.encode(sys.getfilesystemencoding())
# Generate the PDF file.
os.system( 'dot -Tpdf -o"%s" "%s"' % (self._filename, tmp_dot) )
os.system( 'dot -Tpdf -o"%s" "%s"' % (fname, tmp_dot) )
# Delete the temporary dot file
os.remove(tmp_dot)
@ -799,9 +814,10 @@ class GVPdfGsDoc(GVDocBase):
height_pt = int( (paper_size.get_height_inches() * 72) + 0.5 )
# Convert to PDF using ghostscript
fname = self._filename.encode(sys.getfilesystemencoding())
command = '%s -q -sDEVICE=pdfwrite -dNOPAUSE -dDEVICEWIDTHPOINTS=%d' \
' -dDEVICEHEIGHTPOINTS=%d -sOutputFile="%s" "%s" -c quit' \
% ( _GS_CMD, width_pt, height_pt, self._filename, tmp_ps )
% ( _GS_CMD, width_pt, height_pt, fname, tmp_ps )
os.system(command)
os.remove(tmp_ps)

View File

@ -58,7 +58,7 @@ from _stylecombobox import StyleComboBox
from _styleeditor import StyleListDisplay
from _fileentry import FileEntry
from const import URL_MANUAL_PAGE
import Utils
#-------------------------------------------------------------------------
#
# Private Constants
@ -467,7 +467,7 @@ class ReportDialog(ManagedWindow.ManagedWindow):
to tell the calling routine to give up. This function also
saves the current directory so that any future reports will
default to the most recently used directory."""
self.target_path = self.target_fileentry.get_full_path(0)
self.target_path = Utils.get_unicode_path_from_file_chooser(self.target_fileentry.get_full_path(0))
if not self.target_path:
return None

View File

@ -41,6 +41,7 @@ from gen.ggettext import gettext as _
from gen.ggettext import ngettext
from cStringIO import StringIO
from collections import defaultdict
import sys
#-------------------------------------------------------------------------
#
@ -1206,6 +1207,7 @@ class ViewManager(CLIManager):
value = dialog.run()
if value:
(filename, title) = value
filename = filename.encode(sys.getfilesystemencoding())
self.db_loader.read_file(filename)
self._post_load_newdb(filename, 'x-directory/normal', title)
@ -1393,6 +1395,7 @@ class ViewManager(CLIManager):
basefile = file_entry.get_text()
basefile = basefile.replace("/", r"-")
filename = os.path.join(path_entry.get_text(), basefile)
filename = filename.encode(sys.getfilesystemencoding())
if include.get_active():
from ExportPkg import PackageWriter
writer = PackageWriter(self.dbstate.db, filename,
@ -1408,6 +1411,7 @@ class ViewManager(CLIManager):
writer.write(filename)
self.uistate.set_busy_cursor(0)
self.uistate.progress.hide()
filename = Utils.get_unicode_path_from_env_var(filename)
self.uistate.push_message(self.dbstate, _("Backup saved to '%s'" % filename))
config.set('paths.quick-backup-directory', path_entry.get_text())
else:
@ -1444,7 +1448,7 @@ class ViewManager(CLIManager):
if status == gtk.RESPONSE_OK:
filename = f.get_filename()
if filename:
val = Utils.get_unicode_path(filename)
val = Utils.get_unicode_path_from_file_chooser(filename)
if val:
path_entry.set_text(val)
f.destroy()

View File

@ -945,6 +945,7 @@ class ListView(NavigationView):
while True:
value = chooser.run()
fn = chooser.get_filename()
fn = Utils.get_unicode_path_from_file_chooser(fn)
fl = combobox.get_active()
if value == gtk.RESPONSE_OK:
if fn:

View File

@ -30,6 +30,7 @@
#
#------------------------------------------------------------------------
from gen.ggettext import gettext as _
import sys
#------------------------------------------------------------------------
#
@ -85,7 +86,8 @@ class PdfDoc(libcairodoc.CairoDoc):
top_margin = self.paper.get_top_margin() * DPI / 2.54
# create cairo context and pango layout
surface = cairo.PDFSurface(self._backend.filename, paper_width, paper_height)
filename = self._backend.filename.encode(sys.getfilesystemencoding())
surface = cairo.PDFSurface(filename, paper_width, paper_height)
surface.set_fallback_resolution(300, 300)
cr = pangocairo.CairoContext(cairo.Context(surface))

View File

@ -159,8 +159,7 @@ class PackageWriter(object):
pass
def fs_ok_clicked(obj):
name = unicode(fs_top.get_filename(),
sys.getfilesystemencoding())
name = Utils.get_unicode_path_from_file_chooser(fs_top.get_filename())
if os.path.isfile(name):
archive.add(name)

View File

@ -526,7 +526,7 @@ class CheckIntegrity(object):
self.bad_photo.append(ObjectId)
def fs_ok_clicked(obj):
name = Utils.get_unicode_path(fs_top.get_filename())
name = Utils.get_unicode_path_from_file_chooser(fs_top.get_filename())
if os.path.isfile(name):
obj = self.db.get_object_from_handle(ObjectId)
obj.set_path(name)

View File

@ -396,7 +396,7 @@ class DisplayChart(ManagedWindow.ManagedWindow):
f.hide()
if status == gtk.RESPONSE_OK:
name = Utils.get_unicode_path(f.get_filename())
name = Utils.get_unicode_path_from_file_chooser(f.get_filename())
doc = ODSTab(len(self.row_data))
doc.creator(self.db.get_researcher().get_name())

View File

@ -118,7 +118,7 @@ from htmlrenderer import HtmlView
#
#-------------------------------------------------------------------------
#covert to unicode for better hadnling of path in Windows
GEOVIEW_SUBPATH = Utils.get_unicode_path(Utils.get_empty_tempdir('geoview'))
GEOVIEW_SUBPATH = Utils.get_empty_tempdir('geoview')
DISABLED = -1
MRU_SIZE = 10

View File

@ -33,6 +33,7 @@ from gen.ggettext import sgettext as _
from gen.ggettext import ngettext
from cgi import escape
import math
import sys
#-------------------------------------------------------------------------
#
@ -200,6 +201,8 @@ class PersonBoxWidgetCairo(_PersonWidgetBase):
self.img_surf = None
if image:
image_path = self.get_image(dbstate, person)
if isinstance(image_path, unicode):
image_path = image_path.encode(sys.getfilesystemencoding())
if image_path:
self.img_surf = cairo.ImageSurface.create_from_png(image_path)