def __init__(self): self.running = True sys.excepthook = exceptionHook applicationsettings = dict() if self.CEF_DEBUG: window.g_debug = True applicationsettings['debug'] = True applicationsettings['release_dcheck_enabled'] = True applicationsettings['log_file'] = getApplicationPath('debug.log') applicationsettings['log_severity'] = cefpython.LOGSEVERITY_INFO applicationsettings['browser_subprocess_path'] = '%s/%s' % (cefpython.GetModuleDirectory(), 'subprocess') cefpython.Initialize(applicationsettings) browsersettings = dict() browsersettings['file_access_from_file_urls_allowed'] = True browsersettings['universal_access_from_file_urls_allowed'] = True windowhandles = { win32con.WM_CLOSE: self.close_window, win32con.WM_DESTROY: self.quit, win32con.WM_SIZE: cefpython.WindowUtils.OnSize, win32con.WM_SETFOCUS: cefpython.WindowUtils.OnSetFocus, win32con.WM_ERASEBKGND: cefpython.WindowUtils.OnEraseBackground } windowhandle = window.createWindow(title='Cloudburst', className='Cloudburst', width=800, height=700, icon=getApplicationPath('res/images/cloudburst.ico'), windowHandle=windowhandles) windowinfo = cefpython.WindowInfo() windowinfo.SetAsChild(windowhandle) browser = cefpython.CreateBrowserSync(windowinfo, browsersettings, navigateUrl=getApplicationPath("res/views/vlc-test.html")) jsbindings = cefpython.JavascriptBindings(bindToFrames=False, bindToPopups=True) # jsBindings.SetProperty("pyProperty", "This was set in Python") # TODO figure out how to set these properties in js # self.jsBindings.SetProperty("pyConfig", ["This was set in Python", # {"name": "Nested dictionary", "isNested": True}, # [1,"2", None]]) self.vlc = VLC.instance() self.vlc.set_browser(browser) jsbindings.SetObject("python", self.vlc) browser.SetJavascriptBindings(jsbindings) browser.SetClientCallback("OnLoadEnd", self.on_load_end) media_manager = MediaManager.instance() # blocking loop cefpython.MessageLoop() cefpython.Shutdown() # Shuts down threads and cancels running timers (these would otherwise block) media_manager.shutdown() print 'Shutdown complete'
def exceptionHook(exceptionType, exceptionValue, traceObject): errorMessage = os.linesep.join(traceback.format_exception(exceptionType, exceptionValue, traceObject)) errorFile = getApplicationPath("error.log") try: applicationEncoding = cefpython.g_applicationSettings["string_encoding"] except: applicationEncoding = "utf-8" if type(errorMessage) == bytes: errorMessage = errorMessage.decode(encoding=applicationEncoding, errors="replace") try: with codecs.open(errorFile, mode='a', encoding=applicationEncoding) as fp: fp.write((os.linesep + '[%s] %s' + os.linesep) % (time.strftime("%d-%m-%Y %H:%M:%S"), errorMessage)) except: print("cloudburst: WARNING: failed writing to error file: %s" % errorFile) # Convert error message to ascii before printing to prevent errors like this: # UnicodeEncodeError: 'charmap' codec can't encode characters errorMessage = errorMessage.encode('ascii', errors='replace') errorMessage = errorMessage.decode('ascii', errors='replace') print os.linesep + errorMessage + os.linesep cefpython.QuitMessageLoop() cefpython.Shutdown() os._exit(1)
def debug(message): if not g_debug: return message = str("Cloudburst > Window: " + message) print(message) with open(getApplicationPath("debug.log"), "a") as debugFile: debugFile.write(message + "\n")
def createWindow(title, className, width, height, xPosition=None, yPosition=None, icon=None, windowHandle=None): if not windowHandle: windowHandle = {win32con.WM_CLOSE: WM_CLOSE} windowClass = win32gui.WNDCLASS() windowClass.hInstance = win32api.GetModuleHandle(None) windowClass.lpszClassName = className windowClass.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW windowClass.hbrBackground = win32con.COLOR_WINDOW windowClass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) windowClass.lpfnWndProc = windowHandle global g_registeredClasses if not className in g_registeredClasses: g_registeredClasses[className] = True win32gui.RegisterClass(windowClass) debug('win32gui..RegisterClass(%s)' % className) if xPosition is None or yPosition is None: debug('Centering window on screen.') screenX = win32api.GetSystemMetrics(win32con.SM_CXSCREEN) screenY = win32api.GetSystemMetrics(win32con.SM_CYSCREEN) xPosition = int(math.floor((screenX - width) / 2)) yPosition = int(math.floor((screenY - height) / 2)) if xPosition < 0: xPosition = 0 if yPosition < 0: yPosition = 0 windowId = win32gui.CreateWindow(className, title, win32con.WS_OVERLAPPEDWINDOW | win32con.WS_CLIPCHILDREN | win32con.WS_VISIBLE, xPosition, yPosition, width, height, 0, 0, windowClass.hInstance, None) g_windows[windowId] = className debug("windowId = %s" % windowId) if icon: icon = getApplicationPath(icon) bigIcon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, win32api.GetSystemMetrics(win32con.SM_CXICON), win32api.GetSystemMetrics(win32con.SM_CYICON), win32con.LR_LOADFROMFILE) smallIcon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, win32api.GetSystemMetrics(win32con.SM_CXSMICON), win32api.GetSystemMetrics(win32con.SM_CYSMICON), win32con.LR_LOADFROMFILE) win32api.SendMessage(windowId, win32con.WM_SETICON, win32con.ICON_BIG, bigIcon) win32api.SendMessage(windowId, win32con.WM_SETICON, win32con.ICON_SMALL, smallIcon) return windowId
def createWindow(title, className, width, height, xPosition=None, yPosition=None, icon=None, windowHandle=None): if not windowHandle: windowHandle = {win32con.WM_CLOSE: WM_CLOSE} windowClass = win32gui.WNDCLASS() windowClass.hInstance = win32api.GetModuleHandle(None) windowClass.lpszClassName = className windowClass.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW windowClass.hbrBackground = win32con.COLOR_WINDOW windowClass.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) windowClass.lpfnWndProc = windowHandle global g_registeredClasses if not className in g_registeredClasses: g_registeredClasses[className] = True win32gui.RegisterClass(windowClass) debug('win32gui..RegisterClass(%s)' % className) if xPosition is None or yPosition is None: debug('Centering window on screen.') screenX = win32api.GetSystemMetrics(win32con.SM_CXSCREEN) screenY = win32api.GetSystemMetrics(win32con.SM_CYSCREEN) xPosition = int(math.floor((screenX - width) / 2)) yPosition = int(math.floor((screenY - height) / 2)) if xPosition < 0: xPosition = 0 if yPosition < 0: yPosition = 0 windowId = win32gui.CreateWindow( className, title, win32con.WS_OVERLAPPEDWINDOW | win32con.WS_CLIPCHILDREN | win32con.WS_VISIBLE, xPosition, yPosition, width, height, 0, 0, windowClass.hInstance, None) g_windows[windowId] = className debug("windowId = %s" % windowId) if icon: icon = getApplicationPath(icon) bigIcon = win32gui.LoadImage( 0, icon, win32con.IMAGE_ICON, win32api.GetSystemMetrics(win32con.SM_CXICON), win32api.GetSystemMetrics(win32con.SM_CYICON), win32con.LR_LOADFROMFILE) smallIcon = win32gui.LoadImage( 0, icon, win32con.IMAGE_ICON, win32api.GetSystemMetrics(win32con.SM_CXSMICON), win32api.GetSystemMetrics(win32con.SM_CYSMICON), win32con.LR_LOADFROMFILE) win32api.SendMessage(windowId, win32con.WM_SETICON, win32con.ICON_BIG, bigIcon) win32api.SendMessage(windowId, win32con.WM_SETICON, win32con.ICON_SMALL, smallIcon) return windowId
def testApplicationPath(self): self.assertEqual( 'D:\\Dropbox\\Workspaces\\Python\\Cloudburst\\debug.log', getApplicationPath("debug.log"))
def testApplicationPath(self): self.assertEqual('D:\\Dropbox\\Workspaces\\Python\\Cloudburst\\debug.log', getApplicationPath("debug.log"))