OSDN Git Service

revise unicode variables and this app can run under unicode path.
authorkei9 <kei9@users.sourceforge.jp>
Sat, 14 Dec 2013 08:10:27 +0000 (17:10 +0900)
committerkei9 <kei9@users.sourceforge.jp>
Sat, 14 Dec 2013 08:10:27 +0000 (17:10 +0900)
However, after bundled by pyinstaller, the app cannot work under unicode path.
Maybe because pyinstaller 2.1's bug (http://www.pyinstaller.org/ticket/824)

AmuletToolsMH4.spec
amulettool.py
convertcoding.py [new file with mode: 0644]
make.py
model/db_supports.py

index 0d7b852..16cb532 100644 (file)
@@ -1,11 +1,11 @@
 # -*- mode: python -*-
 a = Analysis(['amulettool.py'],
-             pathex=['D:\\Git\\AmuletToolsMH4'],
+             pathex=['D:\\Git\\Publish\\AmuletToolsMH4'],
              hiddenimports=[],
              hookspath=None,
              runtime_hooks=None)
-a.datas += [("view/mainframe.xrc", "d:\\Git\\AmuletToolsMH4\\view\\mainframe.xrc", "DATA")]
-a.datas += [("view/gaugedialog.xrc", "d:\\Git\\AmuletToolsMH4\\view\\gaugedialog.xrc", "DATA")]
+a.datas += [("view/mainframe.xrc", r"D:\Git\Publish\AmuletToolsMH4\view\mainframe.xrc", "DATA")]
+a.datas += [("view/gaugedialog.xrc", r"D:\Git\Publish\AmuletToolsMH4\view\gaugedialog.xrc", "DATA")]
 pyz = PYZ(a.pure)
 exe = EXE(pyz,
           a.scripts,
@@ -13,7 +13,7 @@ exe = EXE(pyz,
           a.zipfiles,
           a.datas,
           name='AmuletToolsMH4.exe',
-          debug=False,
+          debug=True,
           strip=None,
           upx=True,
-          console=False )
+          console=True )
index e52b0e1..8821308 100644 (file)
@@ -1,3 +1,4 @@
+#!/usr/bin/python
 # -*- coding: utf-8 -*-
 
 # お守りスキルのSeed特定ツールのコントロールクラス
@@ -14,6 +15,7 @@ import wx
 
 import view
 import model
+import convertcoding
 
 SETTING_FILE = u"settings"
 SETTING_THRESHOLD1 = u"threshold1"
@@ -26,14 +28,16 @@ def _get_base_dir():
         basedir = sys._MEIPASS
     else:
         # we are running in a normal Python environment
-        basedir = os.path.dirname(__file__)
-    return basedir
+        #1basedir = os.path.dirname(__file__)
+        basedir = os.path.dirname(os.path.abspath(__file__))
+    return convertcoding.convert_unicode(basedir)
+
 
 class AmuletToolController(wx.App):
     u""" アプリケーションの制御クラス """
     def OnInit(self):
         self._read_settings()
-        self.frame_view = view.MainFrameView(os.path.join(_get_base_dir(), "view", view.XRC_MAIN_FRAME))
+        self.frame_view = view.MainFrameView(os.path.join(_get_base_dir(), u"view", view.XRC_MAIN_FRAME))
 
         self._init_events()
 
@@ -87,7 +91,7 @@ class AmuletToolController(wx.App):
                 generator = model.DataBaseGenerator(model.DB_FILE_NAME)
                 #generator = model.DataBaseGenerator()
 
-                dlg_view = view.GaugeDialogView(os.path.join(_get_base_dir(), "view", view.XRC_GAUGE_DIALOG))
+                dlg_view = view.GaugeDialogView(os.path.join(_get_base_dir(), u"view", view.XRC_GAUGE_DIALOG))
                 def _loop():
                     while t1.is_alive():
                         dlg_view.gauge.Pulse()
diff --git a/convertcoding.py b/convertcoding.py
new file mode 100644 (file)
index 0000000..3127bd2
--- /dev/null
@@ -0,0 +1,39 @@
+# -*- coding: utf-8 -*-
+
+u"""
+    Conversion Function str to unicode
+    2013/12/14 written by kei9
+"""
+
+codecs = [u'utf-8',u'cp932',u'shift_jis',u'euc_jp',
+        u'euc_jis_2004',u'euc_jisx0213',u'iso2022_jp',u'iso2022_jp_1',
+        u'iso2022_jp_2',u'iso2022_jp_2004',u'iso2022_jp_3',u'iso2022_jp_ext',
+        u'shift_jis_2004',u'shift_jisx0213',u'utf_16',u'utf_16_be',
+        u'utf_16_le',u'utf_7',u'utf_8_sig'];
+
+def convert_unicode(data):
+    u""" str型の引数をunicodeへの変換を試みる。
+    変換が可能であれば変換後のunicode文字列と、引数のcodecを返す。
+    変換ができない場合はNoneを返す
+    return:
+        when data is unicode
+            data
+        when data can decode to unicode
+            uni_str
+        cannot decode
+            None
+    """
+    if isinstance(data, basestring):
+        if isinstance(data, unicode):
+            # when data isunicode
+            return data
+
+        f = lambda d, enc: d.decode(enc);
+        for codec in codecs:
+            try:
+                return f(data, codec);
+            except: continue;
+        return None;
+    else:
+        # data isn't string
+        return None
diff --git a/make.py b/make.py
index f44acd7..1a4782c 100644 (file)
--- a/make.py
+++ b/make.py
@@ -9,31 +9,41 @@ import os
 import os.path
 import shutil
 
+import convertcoding
+func = lambda path_list: os.path.join(convertcoding.convert_unicode(
+    os.path.dirname(os.path.abspath(__file__))), *path_list)
+"""
 BUNDLE_RESOURCES = [(r"view/mainframe.xrc", r"d:\\Git\\AmuletToolsMH4\\view\\mainframe.xrc"),
        (r"view/gaugedialog.xrc", r"d:\\Git\\AmuletToolsMH4\\view\\gaugedialog.xrc")]
-MOVE_FILES = ["data.zip", "readme.txt", "license.txt"]
+"""
+BUNDLE_RESOURCES = [(ur"view/mainframe.xrc", func([u"view", u"mainframe.xrc"])),
+       (ur"view/gaugedialog.xrc", func([u"view", u"gaugedialog.xrc"]))]
+MOVE_FILES = [u"data.zip", u"readme.txt", u"license.txt"]
 
 
-SCRIPTFILE = "amulettool.py"
-APPNAME = "AmuletToolsMH4"
+SCRIPTFILE = u"amulettool.py"
+APPNAME = u"AmuletToolsMH4"
 
-MAKESPEC = "pyi-makespec"
-PYINSTALLER = "pyinstaller"
+MAKESPEC = u"pyi-makespec"
+PYINSTALLER = u"pyinstaller"
 
-SPECOPTIONS = ["--onefile", "--windowed", "--name=" + APPNAME]
+SPECOPTIONS = [u"--onefile", u"--windowed", u"--name=" + APPNAME]
 
 def get_insert_lines():
     result = []
     for item in BUNDLE_RESOURCES:
-        result.append(r'a.datas += [("{0}", "{1}", "DATA")]'.format(item[0], item[1]) + "\n")
+        result.append(ur'a.datas += [("{0}", r"{1}", "DATA")]'.format(item[0], item[1]) + "\n")
     return result
 
 if __name__ == "__main__":
+    print u"file path: ", os.path.dirname(os.path.abspath(__file__))
+    
     cmd = [MAKESPEC] + SPECOPTIONS + [SCRIPTFILE]
     print " ".join(cmd)
     out = subprocess.check_output(cmd)
     print out
 
+
     # insert bundle data line to .spec
     print "insert bundle resources to .spec file"
     specfile = APPNAME + ".spec"
@@ -49,6 +59,7 @@ if __name__ == "__main__":
     os.remove(specfile)
     os.rename(tmpname, specfile)
 
+
     # conduct pyinstall
     cmd = [PYINSTALLER, specfile]
     print " ".join(cmd)
index ffb37ce..e37ea17 100644 (file)
@@ -2,19 +2,19 @@
 
 # constant numbers of database
 # データベース関連の定数値など
-# 2013/12/09 written by kei9 
+# 2013/12/14 written by kei9 
 
 # import modules
 
 # --- filenames ---
-ZIP_FILE = "data.zip"
-DB_FILE_NAME = "OmamoriMH4.sqlite3"
-SKILL_FILE_NAME = "skill.csv"
-AMULET_FILE_NAME = "amulet.csv"
-MIN_MAX_FILE_NAME = "minmax.csv"
-SUFFICIENT_FILE_NAME = "sufficient.csv"
-SEED2_SKILL2_FILE_NAME = "seed2_skill2.csv"
-SEED2_THRESHOLD1_FILE_NAME = "seed2_threshold1.csv"
+ZIP_FILE = u"data.zip"
+DB_FILE_NAME = u"OmamoriMH4.sqlite3"
+SKILL_FILE_NAME = u"skill.csv"
+AMULET_FILE_NAME = u"amulet.csv"
+MIN_MAX_FILE_NAME = u"minmax.csv"
+SUFFICIENT_FILE_NAME = u"sufficient.csv"
+SEED2_SKILL2_FILE_NAME = u"seed2_skill2.csv"
+SEED2_THRESHOLD1_FILE_NAME = u"seed2_threshold1.csv"
 
 # --- sql sentences of database ---
 # for skill table