handle correctly non ascii chars in path under win. fixes #2812

This commit is contained in:
Yann Leboulanger 2007-01-02 17:56:26 +00:00
parent f5108c745f
commit 558a57802c
3 changed files with 48 additions and 32 deletions

View file

@ -20,11 +20,10 @@ import tempfile
# happily pass to functions that operate on files and directories, so we can # happily pass to functions that operate on files and directories, so we can
# just leave it as is. Since these paths are meant to be internal to Gajim and # just leave it as is. Since these paths are meant to be internal to Gajim and
# not displayed to the user, Unicode is not really necessary here. # not displayed to the user, Unicode is not really necessary here.
#
# Update: Python stdlib seems broken, and uses sys.getdefaultencoding() instead def fse(s):
# of sys.getfilesystemencoding() for converting unicode paths in file operations. '''Convert from filesystem encoding if not already Unicode'''
# Additionally, PyGTK overrides defaultencoding to utf-8, overriding site.py. return unicode(s, sys.getfilesystemencoding())
# Therefore, we now use bytestrings and never unicode. (See #2812.)
class ConfigPaths: class ConfigPaths:
def __init__(self, root=None): def __init__(self, root=None):
@ -39,12 +38,13 @@ class ConfigPaths:
# How are we supposed to know what encoding the environment # How are we supposed to know what encoding the environment
# variable 'appdata' is in? Assuming it to be in filesystem # variable 'appdata' is in? Assuming it to be in filesystem
# encoding. # encoding.
self.root = os.path.join(os.environ['appdata'], 'Gajim') self.root = os.path.join(fse(os.environ[u'appdata']), u'Gajim')
except KeyError: except KeyError:
# win9x, in cwd # win9x, in cwd
self.root = '' self.root = u''
else: # Unices else: # Unices
self.root = os.path.expanduser('~/.gajim') # Pass in an Unicode string, and hopefully get one back.
self.root = os.path.expanduser(u'~/.gajim')
def add_from_root(self, name, path): def add_from_root(self, name, path):
self.paths[name] = (True, path) self.paths[name] = (True, path)
@ -77,8 +77,8 @@ def init():
paths = ConfigPaths() paths = ConfigPaths()
# LOG is deprecated # LOG is deprecated
k = ('LOG', 'LOG_DB', 'VCARD', 'AVATAR', 'MY_EMOTS' ) k = ( 'LOG', 'LOG_DB', 'VCARD', 'AVATAR', 'MY_EMOTS' )
v = ('logs', 'logs.db', 'vcards', 'avatars', 'emoticons') v = (u'logs', u'logs.db', u'vcards', u'avatars', u'emoticons')
if os.name == 'nt': if os.name == 'nt':
v = map(lambda x: x.capitalize(), v) v = map(lambda x: x.capitalize(), v)
@ -86,9 +86,9 @@ def init():
for n, p in zip(k, v): for n, p in zip(k, v):
paths.add_from_root(n, p) paths.add_from_root(n, p)
paths.add('DATA', os.path.join('..', windowsify('data'))) paths.add('DATA', os.path.join(u'..', windowsify(u'data')))
paths.add('HOME', os.path.expanduser('~')) paths.add('HOME', fse(os.path.expanduser('~')))
paths.add('TMP', tempfile.gettempdir()) paths.add('TMP', fse(tempfile.gettempdir()))
try: try:
import svn_config import svn_config
@ -104,17 +104,13 @@ def init():
gajimpaths = init() gajimpaths = init()
def init_profile(profile, paths=gajimpaths): def init_profile(profile, paths=gajimpaths):
# no unicode conffile = windowsify(u'config')
if isinstance(profile, unicode): pidfile = windowsify(u'gajim')
profile = profile.encode(sys.getfilesystemencoding())
conffile = windowsify('config')
pidfile = windowsify('gajim')
if len(profile) > 0: if len(profile) > 0:
conffile += '.' + profile conffile += u'.' + profile
pidfile += '.' + profile pidfile += u'.' + profile
pidfile += '.pid' pidfile += u'.pid'
paths.add_from_root('CONFIG_FILE', conffile) paths.add_from_root('CONFIG_FILE', conffile)
paths.add_from_root('PID_FILE', pidfile) paths.add_from_root('PID_FILE', pidfile)

View file

@ -31,6 +31,7 @@ except ImportError:
import configpaths import configpaths
LOG_DB_PATH = configpaths.gajimpaths['LOG_DB'] LOG_DB_PATH = configpaths.gajimpaths['LOG_DB']
LOG_DB_FOLDER, LOG_DB_FILE = os.path.split(LOG_DB_PATH)
class Constants: class Constants:
def __init__(self): def __init__(self):
@ -97,10 +98,18 @@ class Logger:
def open_db(self): def open_db(self):
self.close_db() self.close_db()
# FIXME: sqlite3_open wants UTF8 strings. So a path with
# non-ascii chars doesn't work. See #2812 and
# http://lists.initd.org/pipermail/pysqlite/2005-August/000134.html
back = os.getcwd()
os.chdir(LOG_DB_FOLDER)
# if locked, wait up to 20 sec to unlock # if locked, wait up to 20 sec to unlock
# before raise (hopefully should be enough) # before raise (hopefully should be enough)
self.con = sqlite.connect(LOG_DB_PATH, timeout = 20.0,
self.con = sqlite.connect(LOG_DB_FILE, timeout = 20.0,
isolation_level = 'IMMEDIATE') isolation_level = 'IMMEDIATE')
os.chdir(back)
self.cur = self.con.cursor() self.cur = self.con.cursor()
self.set_synchronous(False) self.set_synchronous(False)

View file

@ -98,11 +98,6 @@ class OptionsParser:
def write(self): def write(self):
(base_dir, filename) = os.path.split(self.__filename) (base_dir, filename) = os.path.split(self.__filename)
try:
base_dir = base_dir.decode(sys.getfilesystemencoding())
filename = filename.decode(sys.getfilesystemencoding())
except:
pass
self.__tempfile = os.path.join(base_dir, '.' + filename) self.__tempfile = os.path.join(base_dir, '.' + filename)
try: try:
f = open(self.__tempfile, 'w') f = open(self.__tempfile, 'w')
@ -211,7 +206,11 @@ class OptionsParser:
def assert_unread_msgs_table_exists(self): def assert_unread_msgs_table_exists(self):
'''create table unread_messages if there is no such table''' '''create table unread_messages if there is no such table'''
con = sqlite.connect(logger.LOG_DB_PATH) #FIXME see #2812
back = os.getcwd()
os.chdir(logger.LOG_DB_FOLDER)
con = sqlite.connect(logger.LOG_DB_FILE)
os.chdir(back)
cur = con.cursor() cur = con.cursor()
try: try:
cur.executescript( cur.executescript(
@ -280,7 +279,11 @@ class OptionsParser:
def update_config_to_01013(self): def update_config_to_01013(self):
'''create table transports_cache if there is no such table''' '''create table transports_cache if there is no such table'''
con = sqlite.connect(logger.LOG_DB_PATH) #FIXME see #2812
back = os.getcwd()
os.chdir(logger.LOG_DB_FOLDER)
con = sqlite.connect(logger.LOG_DB_FILE)
os.chdir(back)
cur = con.cursor() cur = con.cursor()
try: try:
cur.executescript( cur.executescript(
@ -300,7 +303,11 @@ class OptionsParser:
def update_config_to_01014(self): def update_config_to_01014(self):
'''apply indeces to the logs database''' '''apply indeces to the logs database'''
print _('migrating logs database to indices') print _('migrating logs database to indices')
con = sqlite.connect(logger.LOG_DB_PATH) #FIXME see #2812
back = os.getcwd()
os.chdir(logger.LOG_DB_FOLDER)
con = sqlite.connect(logger.LOG_DB_FILE)
os.chdir(back)
cur = con.cursor() cur = con.cursor()
# apply indeces # apply indeces
try: try:
@ -319,7 +326,11 @@ class OptionsParser:
def update_config_to_01015(self): def update_config_to_01015(self):
'''clean show values in logs database''' '''clean show values in logs database'''
con = sqlite.connect(logger.LOG_DB_PATH) #FIXME see #2812
back = os.getcwd()
os.chdir(logger.LOG_DB_FOLDER)
con = sqlite.connect(logger.LOG_DB_FILE)
os.chdir(back)
cur = con.cursor() cur = con.cursor()
status = dict((i[5:].lower(), logger.constants.__dict__[i]) for i in \ status = dict((i[5:].lower(), logger.constants.__dict__[i]) for i in \
logger.constants.__dict__.keys() if i.startswith('SHOW_')) logger.constants.__dict__.keys() if i.startswith('SHOW_'))