From 7dfb2e016f2591dc9938b49433e7915d31354429 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Thu, 3 Apr 2014 14:16:16 -0700 Subject: [PATCH] Test for ability to open a DB in an arbitrary Unicode path with a Unicode name. And fix a couple of bugs that made the test fail. --- gramps/cli/clidbman.py | 11 ++++++----- gramps/cli/test/cli_test.py | 35 +++++++++++++++++++++++++++++++++++ gramps/gen/db/write.py | 6 +++++- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/gramps/cli/clidbman.py b/gramps/cli/clidbman.py index c72274b55..96d608416 100644 --- a/gramps/cli/clidbman.py +++ b/gramps/cli/clidbman.py @@ -37,6 +37,7 @@ from __future__ import print_function import os import sys import time +import io if sys.version_info[0] < 3: from urlparse import urlparse from urllib2 import urlopen, url2pathname @@ -244,7 +245,7 @@ class CLIDbManager(object): dirpath = os.path.join(dbdir, dpath) path_name = os.path.join(dirpath, NAME_FILE) if os.path.isfile(path_name): - file = open(path_name) + file = io.open(path_name, 'r', encoding='utf8') name = file.readline().strip() file.close() @@ -302,8 +303,8 @@ class CLIDbManager(object): if title is None: name_list = [ name[0] for name in self.current_names ] title = find_next_db_name(name_list) - - name_file = open(path_name, "w") + + name_file = io.open(path_name, "w", encoding='utf8') name_file.write(title) name_file.close() @@ -412,10 +413,10 @@ class CLIDbManager(object): """ try: filepath = conv_to_unicode(filepath, 'utf8') - name_file = open(filepath, "r") + name_file = io.open(filepath, "r", encoding='utf8') old_text=name_file.read() name_file.close() - name_file = open(filepath, "w") + name_file = io.open(filepath, "w", encoding='utf8') name_file.write(new_text) name_file.close() except (OSError, IOError) as msg: diff --git a/gramps/cli/test/cli_test.py b/gramps/cli/test/cli_test.py index 4e83ccbc2..2a88d1cad 100644 --- a/gramps/cli/test/cli_test.py +++ b/gramps/cli/test/cli_test.py @@ -25,6 +25,7 @@ import os import unittest import re +import io test_ged = """0 HEAD 1 SOUR min1r.ged min 1-rec @@ -102,6 +103,40 @@ class Test(unittest.TestCase): for fn in bogofiles: self.assertFalse(os.path.exists(fn)) +class UnicodeTest(unittest.TestCase): + + @unittest.skipIf(sys.version_info[0] < 3 and sys.platform == 'win32') + + def setUp(self): + from gramps.cli.clidbman import CLIDbManager + from gramps.gen.config import set as setconfig + from gramps.gen.dbstate import DbState + self.newpath = os.path.join(os.path.dirname(__file__), + u'\u0393\u03c1\u03b1\u03bc\u03c3\u03c0') + self.newtitle = u'Gr\u00e4mps T\u00e9st' + os.makedirs(self.newpath) + setconfig('behavior.database-path', self.newpath) + self.cli = CLIDbManager(DbState()) + + def tearDown(self): + for (dirpath, dirnames, filenames) in os.walk(self.newpath, False): + for afile in filenames: + os.remove(os.path.join(dirpath, afile)) + for adir in dirnames: + os.rmdir(os.path.join(dirpath, adir)) + os.rmdir(self.newpath) + + # Test that clidbman will open files in a path containing + # arbitrary Unicode characters. + def test4_arbitrary_uncode_path(self): + (dbpath, title) = self.cli.create_new_db_cli(self.newtitle) + + self.assertEquals(self.newpath, os.path.dirname(dbpath), + "Compare paths %s and %s" % (repr(self.newpath), + repr(dbpath))) + self.assertEquals(self.newtitle, title, "Compare titles %s and %s" % + (repr(self.newtitle), repr(title))) + if __name__ == "__main__": unittest.main() diff --git a/gramps/gen/db/write.py b/gramps/gen/db/write.py index 880518f71..70eb2fcda 100644 --- a/gramps/gen/db/write.py +++ b/gramps/gen/db/write.py @@ -95,6 +95,10 @@ _ = glocale.translation.gettext _LOG = logging.getLogger(DBLOGNAME) LOG = logging.getLogger(".citation") +#_LOG.setLevel(logging.DEBUG) +#_hdlr = logging.StreamHandler() +#_hdlr.setFormatter(logging.Formatter(fmt="%(name)s.%(levelname)s: %(message)s")) +#_LOG.addHandler(_hdlr) _MINVERSION = 9 _DBVERSION = 17 @@ -221,7 +225,7 @@ def _encode(path): """ Conditionally return the unicode string encoded to sys.filesystem.encoding """ - if not (isinstance(path, UNITYPE) and win() and sys.version_info[0] < 3): + if not (isinstance(path, UNITYPE) and sys.version_info[0] < 3): _LOG.debug("Didn't Encode %s", repr(path)) return path _LOG.debug("Encoding %s", repr(path))