|
7 | 7 | import ctypes, os, sys |
8 | 8 | libcef_so = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'libcef.so') |
9 | 9 | if os.path.exists(libcef_so): |
| 10 | + CEFPYTHON_PACKAGE = False |
10 | 11 | # Import local module |
11 | 12 | ctypes.CDLL(libcef_so, ctypes.RTLD_GLOBAL) |
12 | 13 | if 0x02070000 <= sys.hexversion < 0x03000000: |
13 | 14 | import cefpython_py27 as cefpython |
14 | 15 | else: |
15 | 16 | raise Exception("Unsupported python version: %s" % sys.version) |
| 17 | + # These dirs will be set after GetApplicationPath() is defined. |
| 18 | + cefpython.locales_dir = None |
| 19 | + cefpython.resources_dir = None |
16 | 20 | else: |
| 21 | + CEFPYTHON_PACKAGE = True |
17 | 22 | # Import from package |
18 | 23 | from cefpython1 import cefpython |
| 24 | + # cefpython.locales_dir and cefpython.resources_dir are already |
| 25 | + # defined when importing cefpython from a package. |
19 | 26 |
|
20 | 27 | import pygtk |
21 | 28 | pygtk.require('2.0') |
@@ -44,13 +51,24 @@ def GetApplicationPath(file=None): |
44 | 51 | return path |
45 | 52 | return str(file) |
46 | 53 |
|
| 54 | +if not CEFPYTHON_PACKAGE: |
| 55 | + # Set these only when importing a local module. |
| 56 | + cefpython.locales_dir = GetApplicationPath("locales") |
| 57 | + cefpython.resources_dir = GetApplicationPath() |
| 58 | + |
47 | 59 | def ExceptHook(type, value, traceObject): |
48 | 60 | import traceback, os, time |
49 | 61 | # This hook does the following: in case of exception display it, |
50 | 62 | # write to error.log, shutdown CEF and exit application. |
51 | 63 | error = "\n".join(traceback.format_exception(type, value, traceObject)) |
52 | | - with open(GetApplicationPath("error.log"), "a") as file: |
53 | | - file.write("\n[%s] %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), error)) |
| 64 | + try: |
| 65 | + with open(GetApplicationPath("error.log"), "a") as file: |
| 66 | + file.write("\n[%s] %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), error)) |
| 67 | + except: |
| 68 | + # If this is an example run from |
| 69 | + # /usr/local/lib/python2.7/dist-packages/cefpython1/examples/ |
| 70 | + # then we do not have permission to write to that directory. |
| 71 | + pass |
54 | 72 | print("\n"+error+"\n") |
55 | 73 | cefpython.QuitMessageLoop() |
56 | 74 | cefpython.Shutdown() |
@@ -159,15 +177,20 @@ def OnExit(self, widget, data=None): |
159 | 177 |
|
160 | 178 | sys.excepthook = ExceptHook |
161 | 179 | cefpython.g_debug = True |
162 | | - cefpython.g_debugFile = GetApplicationPath("debug.log") |
| 180 | + if not CEFPYTHON_PACKAGE: |
| 181 | + # No permission to write files when running examples/ from a package. |
| 182 | + cefpython.g_debugFile = GetApplicationPath("debug.log") |
163 | 183 | settings = { |
164 | 184 | "log_severity": cefpython.LOGSEVERITY_INFO, |
165 | 185 | "log_file": GetApplicationPath("debug.log"), |
166 | 186 | "release_dcheck_enabled": True, # Enable only when debugging. |
167 | 187 | # This directories must be set on Linux |
168 | | - "locales_dir_path": GetApplicationPath("locales"), |
169 | | - "resources_dir_path": GetApplicationPath() |
| 188 | + "locales_dir_path": cefpython.locales_dir, |
| 189 | + "resources_dir_path": cefpython.resources_dir |
170 | 190 | } |
| 191 | + if CEFPYTHON_PACKAGE: |
| 192 | + # No permission to write files when running examples/ from a package. |
| 193 | + settings["log_file"] = "" |
171 | 194 | cefpython.Initialize(settings) |
172 | 195 |
|
173 | 196 | gobject.threads_init() # timer for messageloop |
|
0 commit comments