OSDN Git Service

Merge branch 'master' of git://gitorious.org/mypaint/mypaint
[mypaint-anime/master.git] / mypaint.py
index 49b3fec..9ed8906 100644 (file)
@@ -12,15 +12,54 @@ to figure out where the python modules are.
 """
 import sys, os
 
+def win32_unicode_argv():
+    # fix for https://gna.org/bugs/?17739
+    # code mostly comes from http://code.activestate.com/recipes/572200/
+    """Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode
+    strings.
+
+    Versions 2.x of Python don't support Unicode in sys.argv on
+    Windows, with the underlying Windows API instead replacing multi-byte
+    characters with '?'.
+    """
+    try:
+        from ctypes import POINTER, byref, cdll, c_int, windll
+        from ctypes.wintypes import LPCWSTR, LPWSTR
+
+        GetCommandLineW = cdll.kernel32.GetCommandLineW
+        GetCommandLineW.argtypes = []
+        GetCommandLineW.restype = LPCWSTR
+        CommandLineToArgvW = windll.shell32.CommandLineToArgvW
+        CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
+
+        CommandLineToArgvW.restype = POINTER(LPWSTR)
+        cmd = GetCommandLineW()
+        argc = c_int(0)
+        argv = CommandLineToArgvW(cmd, byref(argc))
+        if argc.value > 0:
+            # Remove Python executable if present
+            if argc.value - len(sys.argv) == 1:
+                start = 1
+            else:
+                start = 0
+            return [argv[i] for i in xrange(start, argc.value)]
+    except Exception:
+        return [s.decode(sys.getfilesystemencoding()) for s in args]
+
 def get_paths():
     join = os.path.join
 
     lib_shared='share/mypaint/'
     # note: some distros use lib64 instead, they have to edit this...
     lib_compiled='lib/mypaint/'
-    
-    arg0 = sys.argv[0].decode(sys.getfilesystemencoding())
-    scriptdir=os.path.dirname(arg0)
+
+    # convert sys.argv to a list of unicode objects
+    # (actually convertig sys.argv confuses gtk, thus we add a new variable)
+    if sys.platform == 'win32':
+        sys.argv_unicode = win32_unicode_argv()
+    else:
+        sys.argv_unicode = [s.decode(sys.getfilesystemencoding()) for s in sys.argv]
+    scriptdir=os.path.dirname(sys.argv_unicode[0])
 
     # this script is installed as $prefix/bin. We just need $prefix to continue.
     #pwd=os.getcwd() # why????
@@ -35,17 +74,20 @@ def get_paths():
         sys.path.insert(0, libpath)
         sys.path.insert(0, libpath_compiled)
         localepath = join(prefix, 'share/locale')
+        extradata = join(prefix, 'share')
     elif sys.platform == 'win32':
         prefix=None
         # this is py2exe point of view, all executables in root of installdir
         # all path must be normalized to absolute path
-        libpath = os.path.abspath(os.path.dirname(os.path.realpath(arg0)))
+        libpath = os.path.abspath(os.path.dirname(os.path.realpath(sys.argv_unicode[0])))
         sys.path.insert(0, libpath)
-        localepath = join(libpath,'share/locale')
+        localepath = join(libpath, 'share/locale')
+        extradata = join(libpath, 'share')
     else:
         # we are not installed
         prefix = None
         libpath = u'.'
+        extradata = u'desktop'
         localepath = 'po'
 
     assert isinstance(libpath, unicode)
@@ -55,7 +97,7 @@ def get_paths():
     except ImportError:
         print
         print "We are not correctly installed or compiled!"
-        print 'script: "%s"' % arg0
+        print 'script: "%s"' % sys.argv[0]
         if prefix:
             print 'deduced prefix: "%s"' % prefix
             print 'lib_shared: "%s"' % libpath
@@ -71,22 +113,25 @@ def get_paths():
 
     from lib import helpers
     homepath =  helpers.expanduser_unicode(u'~')
-    if homepath == '~':
+    if sys.platform == 'win32':
+        # using patched win32 glib using correct CSIDL_LOCAL_APPDATA
+        import glib
+        confpath = os.path.join(glib.get_user_config_dir().decode('utf-8'),'mypaint')
+    elif homepath == '~':
         confpath = join(prefix, 'UserData')
     else:
-        confpath = join(homepath, '.mypaint')
+        confpath = join(homepath, '.mypaint/')
 
     assert isinstance(datapath, unicode)
     assert isinstance(confpath, unicode)
-    return datapath, confpath, localepath
+    assert isinstance(extradata, unicode)
+    return datapath, extradata, confpath, localepath
 
 def psyco_opt():
     # This helps on slow PCs where the python overhead dominates.
     # (30% higher framerate measured on 533MHz CPU; startup slowdown below 20%)
     # Note: python -O -O does not help.
-    if os.name in ('nt', 'ce'):
-        # reported to be broken on Windows
-        return
+
     try:
         import psyco
         if sys.platform == 'win32':
@@ -104,7 +149,7 @@ def psyco_opt():
 if __name__ == '__main__':
     psyco_opt()
 
-    datapath, confpath, localepath = get_paths()
+    datapath, extradata, confpath, localepath = get_paths()
 
     # must be done before importing any translated python modules
     # (to get global strings translated, especially brushsettings.py)
@@ -116,4 +161,4 @@ if __name__ == '__main__':
     gettext.textdomain("mypaint")
 
     from gui import main
-    main.main(datapath, confpath)
+    main.main(datapath, extradata, confpath)