def testIsRawBytes(self): """Test reading a file that can't be properly encoded and was read as raw bytes. """ txt = self.file.Read() self.assertTrue(ebmlib.IsUnicode(txt)) self.assertFalse(self.file.IsRawBytes()) txt = self.rfile.Read() self.assertFalse(ebmlib.IsUnicode(txt)) self.assertTrue(self.rfile.IsRawBytes()) bytes = self.img.Read() self.assertTrue(self.rfile.IsRawBytes())
def run(self): """Start the server. The server runs in blocking mode, this shouldn't be an issue as it should rarely need to respond to anything. """ while not self._exit: try: client, addr = self.socket.accept() if self._exit: break # Block for up to 2 seconds while reading start = time.time() recieved = '' while time.time() < start + 2: recieved += client.recv(4096) if recieved.endswith(MSGEND): break # If message key is correct and the message is ended, process # the input and dispatch to the app. if recieved.startswith( self.__key) and recieved.endswith(MSGEND): # Strip the key recieved = recieved.replace(self.__key, '', 1) # Strip the end token xmlstr = recieved.rstrip(MSGEND).strip(";") # Parse the xml exml = IPCCommand() try: # Well formed xml must be utf-8 string not Unicode if not ebmlib.IsUnicode(xmlstr): xmlstr = unicode(xmlstr, sys.getfilesystemencoding()) xmlstr = xmlstr.encode('utf-8') exml = IPCCommand.parse(xmlstr) except Exception, msg: # Log and ignore parsing errors logmsg = "[ed_ipc][err] Parsing failed: %s\n" % msg xmlstr = xmlstr.replace('\n', '').strip() logmsg += "Bad xml was: %s" % repr(xmlstr) util.Log(logmsg) continue evt = IpcServerEvent(edEVT_COMMAND_RECV, wx.ID_ANY, exml) wx.CallAfter(wx.PostEvent, self.app, evt) except socket.error: # TODO: Better error handling self._exit = True # Shutdown Server try: self.socket.shutdown(socket.SHUT_RDWR) except: pass self.socket.close()
def NavigateToTaskSource(self, itemIndex): """ Navigates to the file and position saved in this item @param itemIndex: a int """ if itemIndex < 0 or itemIndex > len(self.itemDataMap): self._log("[err] itemIndex out of range!") return key = self.itemIndexMap[itemIndex] source = self.itemDataMap[key][-1] if not ebmlib.IsUnicode(source): source = ed_txt.DecodeString(source) line = self.itemDataMap[key][-2] try: nbook = self.GetParent().GetMainWindow().GetNotebook() ctrls = nbook.GetTextControls() for ctrl in ctrls: if source == ctrl.GetFileName(): nbook.SetSelection(nbook.GetPageIndex(ctrl)) nbook.GoCurrentPage() ctrl.GotoLine(line-1) break except Exception, excp: self._log("[err] %s" % excp)
def LoadFile(self, path): """Load the file at the given path into the buffer. Returns True if no errors and False otherwise. To retrieve the errors check the last error that was set in the file object returned by L{GetDocument}. @param path: path to file """ # Post notification that a file load is starting ed_msg.PostMessage(ed_msg.EDMSG_FILE_OPENING, path) self.file.SetPath(path) txt = self.file.Read() if txt is not None: if self.file.IsRawBytes() and not ebmlib.IsUnicode(txt): self.AddStyledText(txt) self.SetReadOnly(True) # Don't allow editing of raw bytes else: self.SetText(txt) else: self.file.SetPath('') return False if self.file.GetLastError() != 'None': # Return false if there was an encoding error and a fallback # was used. So the caller knows to check the error status return False else: return True
def get_unicodevalue(_value): if not isinstance(_value, basestring): _value = repr(_value) _value = ed_txt.DecodeString(_value) if not ebmlib.IsUnicode(_value): # Give up and do what we can _value = unicode(_value, 'latin1', errors='replace') return _value
def testIsRawBytes(self): """Test reading a file that can't be properly encoded and was read as raw bytes. """ txt = self.file.Read() self.assertTrue(ebmlib.IsUnicode(txt)) self.assertFalse(self.file.IsRawBytes()) rpath = common.GetDataFilePath(u'embedded_nulls.txt') rfile = ed_txt.EdFile(rpath) txt = rfile.Read() self.assertTrue(ebmlib.IsUnicode(txt)) self.assertFalse(rfile.IsRawBytes()) bytes_value = self.img.Read() self.assertTrue(self.img.IsRawBytes())
def testUnicode(self): """Test that attributes are properly converted to Unicode""" msg1 = dev_tool.LogMsg("Error Message", "ed_main", "err") self.assertTrue(ebmlib.IsUnicode(msg1.Value)) self.assertTrue(ebmlib.IsUnicode(msg1.Origin)) self.assertTrue(ebmlib.IsUnicode(msg1.Type)) self.assertTrue(ebmlib.IsUnicode(msg1.ClockTime)) self.assertTrue(ebmlib.IsUnicode(unicode(msg1))) # Test with some non ascii values for conversion msg2 = dev_tool.LogMsg('\\u0259','\\u0259','\\u0259') self.assertTrue(ebmlib.IsUnicode(msg2.Value)) self.assertTrue(ebmlib.IsUnicode(msg2.Origin)) self.assertTrue(ebmlib.IsUnicode(msg2.Type)) self.assertTrue(ebmlib.IsUnicode(unicode(msg2)))
def GetSubElements(self): """Get the objects subelements""" xmlstr = u"" tmp = u"<%s %s=\"" % (EDXML_FILE, syntax.EXML_VALUE) tmp += u"%s\"/>" for fname in self._files: if not ebmlib.IsUnicode(fname): fname = fname.decode(sys.getfilesystemencoding()) xmlstr += tmp % fname return xmlstr
def FormatResult(self, fname, lnum, match): """Format the search result string for find all action that is performed on a selection. @return: string @todo: better unicode handling """ fname = ed_txt.DecodeString(fname, sys.getfilesystemencoding()) if not ebmlib.IsUnicode(fname): fname = _("DECODING ERROR") match = ed_txt.DecodeString(match) if not ebmlib.IsUnicode(match): match = _("DECODING ERROR") else: match = u" " + match.lstrip() rstring = u"%(fname)s (%(lnum)d): %(match)s" lnum = lnum + self._offset + 1 return rstring % dict(fname=fname, lnum=lnum, match=match)
def testGetLastError(self): """Test fetching last file op error""" self.assertEquals(self.file.GetLastError(), u"None") # Test that errors come back as Unicode self.file.SetLastError("OS CALL FAILED") errmsg = self.file.GetLastError() self.assertTrue(ebmlib.IsUnicode(errmsg), "Error not decoded properly!") # Tests path for error message that is already Unicode self.file.SetLastError(u"FAIL!") errmsg = self.file.GetLastError() self.assertEquals(errmsg, u"FAIL!")
def DoProcessError(self, code, excdata=None): """Handle notifications of when an error occurs in the process @param code: an OBP error code @keyword excdata: Exception string @return: None """ if code == eclib.OPB_ERROR_INVALID_COMMAND: self.AppendUpdate(_("The requested command could not be executed.") + u"\n") # Log the raw exception data to the log as well if excdata is not None: try: excstr = str(excdata) if not ebmlib.IsUnicode(excstr): excstr = ed_txt.DecodeString(excstr) util.Log(u"[launch][err] %s" % excdata) except UnicodeDecodeError: util.Log(u"[launch][err] error decoding log message string")
def LoadSessionFile(self, session): """Load files from saved session data in profile @param session: session filename @return: tuple (error desc, error msg), or None if no error """ self._ses_load = True if os.path.exists(session): try: f_handle = open(session, 'rb') except IOError: f_handle = None else: f_handle = None # Invalid file if f_handle is None: self._ses_load = False return (_("Invalid File"), _("Session file doesn't exist.")) # Load and validate file try: try: flist = cPickle.load(f_handle) # TODO: Extend in future to support loading sessions # for multiple windows. flist = flist.get('win1', list()) for item in flist: if type(item) not in (unicode, str): raise TypeError("Invalid item in unpickled sequence") except (cPickle.UnpicklingError, TypeError, EOFError), e: self._ses_load = False return (_("Invalid file"), _("Selected file is not a valid session file")) finally: f_handle.close() if not len(flist): self._ses_load = False return (_("Empty File"), _("Session file is empty.")) # Close current files self.CloseAllPages() missingfns = [] for loadfn in flist: if os.path.exists(loadfn) and os.access(loadfn, os.R_OK): if not ebmlib.IsUnicode(loadfn): try: loadfn = loadfn.decode(sys.getfilesystemencoding()) except UnicodeDecodeError: self.LOG( "[ed_pages][err] LoadSessionFile: Failed to decode file name" ) self.OpenPage(os.path.dirname(loadfn), os.path.basename(loadfn)) else: missingfns.append(loadfn) if missingfns: rmsg = ( _("Missing session files"), _("Some files in saved session could not be found on disk:\n") + u'\n'.join(missingfns)) self._ses_load = False return rmsg self._ses_load = False if self.GetPageCount() == 0: self.NewPage() self.Refresh() return None
def DoCheck(self): """Run pylint""" # Figure out what Python to use # 1) First check configuration # 2) Second check for it on the path localpythonpath = ToolConfig.GetConfigValue(ToolConfig.TLC_PYTHON_PATH) if not localpythonpath: localpythonpath = PyToolsUtils.GetDefaultPython() # No configured Python if not localpythonpath: return [ (u"No Python", self.nopythonerror, u"NA"), ] util.Log("[PyLint][info] Using Python: %s" % localpythonpath) childPath, parentPath = PyToolsUtils.get_packageroot(self.filename) # Start pylint modpath = PyToolsUtils.get_modulepath(childPath) if ebmlib.IsUnicode(modpath): # Convert to string modpath = modpath.encode(sys.getfilesystemencoding()) allargs = self.pylintargs + [ modpath, ] pythoncode = "from pylint import lint;lint.Run(%s)" % repr(allargs) plint_cmd = [localpythonpath, "-c", pythoncode] util.Log("[PyLint][info] Starting command: %s" % repr(plint_cmd)) util.Log("[Pylint][info] Using CWD: %s" % parentPath) processrunner = ProcessRunner(self.pythonpath) processrunner.runprocess(plint_cmd, parentPath) stdoutdata, stderrdata = processrunner.getalloutput() processrunner.restorepath() util.Log("[Pylint][info] stdout %s" % stdoutdata) util.Log("[Pylint][info] stderr %s" % stderrdata) stderrlower = stderrdata.lower() ind = stderrlower.find("importerror") if ind != -1: if stderrlower.find("pylint", ind) != -1: return [ (u"No Pylint", self.nopylinterror, u"NA"), ] # The parseable line format is '%(path)s:%(line)s: [%(sigle)s%(obj)s] %(msg)s' # NOTE: This would be cleaner if we added an Emacs reporter to pylint.reporters.text .. regex = re.compile(r"(.*):(.*): \[([A-Z])[, ]*(.*)\] (.*)%s" % os.linesep) rows = [] if self.pythonpath: rows.append((u"***", u"Using PYTHONPATH + %s"\ % u", ".join(self.pythonpath), u"NA")) rows.append( (u"***", u"Pylint command line: %s" % " ".join(plint_cmd), u"NA")) rows.append( (u"***", u"Directory Variables file: %s" % self.dirvarfile, u"NA")) rowsdict = {} for matcher in regex.finditer(stdoutdata): if matcher is None: continue mtypeabr = matcher.group(3) linenostr = matcher.group(2) classmethod = matcher.group(4) mtext = matcher.group(5) if mtypeabr == u"E" or mtypeabr == u"F": mtype = u"Error" else: mtype = u"Warning" outtext = mtext if classmethod: outtext = u"[%s] %s" % (classmethod, outtext) try: lineno = int(linenostr) mtyperows = rowsdict.get(mtype) if not mtyperows: mtyperows = {} rowsdict[mtype] = mtyperows linenorows = mtyperows.get(lineno) if not linenorows: linenorows = set() mtyperows[lineno] = linenorows linenorows.add(outtext) except: rows.append((mtype, outtext, linenostr)) for mtype in sorted(rowsdict): mtyperows = rowsdict[mtype] for lineno in sorted(mtyperows): linenorows = mtyperows[lineno] for outtext in sorted(linenorows): rows.append((mtype, outtext, lineno)) util.Log("[PyLint][info] Pylint command finished running") return rows
def testIsUnicode(self): """Test checking if a string is unicode or not""" self.assertTrue(ebmlib.IsUnicode(u"HELLO")) self.assertFalse(ebmlib.IsUnicode("Hello"))
return ( _("Session Load Error"), _("Failed to load the session: %(sessionname)s\n\nError: %(error)s" ) % errdict) if not len(flist): self._ses_load = False return (_("Empty File"), _("Session file is empty.")) # Close current files self.CloseAllPages() missingfns = [] for loadfn in flist: if os.path.exists(loadfn) and os.access(loadfn, os.R_OK): if not ebmlib.IsUnicode(loadfn): try: loadfn = loadfn.decode(sys.getfilesystemencoding()) except UnicodeDecodeError: self.LOG( "[ed_pages][err] LoadSessionFile: Failed to decode file name" ) self.OpenPage(os.path.dirname(loadfn), os.path.basename(loadfn)) else: missingfns.append(loadfn) if missingfns: rmsg = ( _("Missing session files"), _("Some files in saved session could not be found on disk:\n")
def SetQuery(self, query): """Set the query string""" if ebmlib.IsUnicode(query): # Encode to UTF-8 as used internally by the stc query = query.encode('utf-8') super(EdSearchEngine, self).SetQuery(query)
def testString(self): """Test conversion to bytestring""" msg1 = dev_tool.LogMsg("Error Message", "ed_main", "err") self.assertTrue(not ebmlib.IsUnicode(str(msg1)))
def ResolvConfigDir(config_dir, sys_only=False): """Checks for a user config directory and if it is not found it then resolves the absolute path of the executables directory from the relative execution path. This is then used to find the location of the specified directory as it relates to the executable directory, and returns that path as a string. @param config_dir: name of config directory to resolve @keyword sys_only: only get paths of system config directory or user one @note: This method is probably much more complex than it needs to be but the code has proven itself. """ # Try to get a User config directory if not sys_only: user_config = GetUserConfigBase() user_config = os.path.join(user_config, config_dir) if os.path.exists(user_config): return user_config + os.sep # Check if the system install path has already been resolved once before if ed_glob.CONFIG['INSTALL_DIR'] != u"": tmp = os.path.join(ed_glob.CONFIG['INSTALL_DIR'], config_dir) tmp = os.path.normpath(tmp) + os.sep if os.path.exists(tmp): return tmp else: del tmp # The following lines are used only when Editra is being run as a # source package. If the found path does not exist then Editra is # running as as a built package. if not hasattr(sys, 'frozen'): path = __file__ path = os.sep.join(path.split(os.sep)[:-2]) path = path + os.sep + config_dir + os.sep if os.path.exists(path): if not ebmlib.IsUnicode(path): path = unicode(path, sys.getfilesystemencoding()) return path # If we get here we need to do some platform dependant lookup # to find everything. path = sys.argv[0] if not ebmlib.IsUnicode(path): path = unicode(path, sys.getfilesystemencoding()) # If it is a link get the real path if os.path.islink(path): path = os.path.realpath(path) # Tokenize path pieces = path.split(os.sep) if wx.Platform == u'__WXMSW__': # On Windows the exe is in same dir as config directories pro_path = os.sep.join(pieces[:-1]) if os.path.isabs(pro_path): pass elif pro_path == u"": pro_path = os.getcwd() pieces = pro_path.split(os.sep) pro_path = os.sep.join(pieces[:-1]) else: pro_path = os.path.abspath(pro_path) elif wx.Platform == u'__WXMAC__': # On OS X the config directories are in the applet under Resources stdpath = wx.StandardPaths_Get() pro_path = stdpath.GetResourcesDir() pro_path = os.path.join(pro_path, config_dir) else: pro_path = os.sep.join(pieces[:-2]) if pro_path.startswith(os.sep): pass elif pro_path == u"": pro_path = os.getcwd() pieces = pro_path.split(os.sep) if pieces[-1] not in [ ed_glob.PROG_NAME.lower(), ed_glob.PROG_NAME ]: pro_path = os.sep.join(pieces[:-1]) else: pro_path = os.path.abspath(pro_path) if wx.Platform != u'__WXMAC__': pro_path = pro_path + os.sep + config_dir + os.sep path = os.path.normpath(pro_path) + os.sep # Make sure path is unicode if not ebmlib.IsUnicode(path): path = unicode(path, sys.getdefaultencoding()) return path