def putContents(cls, content, path, append=False, raiseErrors=False, gzipped=False): if not StringUtils.isStringType(content): content = '' content = StringUtils.toStr2(content) try: mode = 'a+' if append and os.path.exists(path) else 'w+' if gzipped: f = open(path, mode) else: f = open(path, mode) f.write(content) f.close() except Exception as err: if raiseErrors: raise print(err) return False return True
def _createSetupFile(self, binPath): path = FileUtils.createPath(binPath, 'setup.py', isFile=True) scriptPath = inspect.getabsfile(self.applicationClass) try: sourcePath = PyGlassEnvironment.getPyGlassResourcePath( '..', 'setupSource.txt', isFile=True) f = open(sourcePath, 'r+') source = f.read() f.close() except Exception as err: print(err) return None try: f = open(path, 'w+') f.write(source.replace( '##SCRIPT_PATH##', StringUtils.escapeBackSlashes(scriptPath) ).replace( '##RESOURCES##', StringUtils.escapeBackSlashes(JSON.asString(self.resources)) ).replace( '##INCLUDES##', StringUtils.escapeBackSlashes(JSON.asString(self.siteLibraries)) ).replace( '##ICON_PATH##', StringUtils.escapeBackSlashes(self._createIcon(binPath)) ).replace( '##APP_NAME##', self.appDisplayName ).replace( '##SAFE_APP_NAME##', self.appDisplayName.replace(' ', '_') )) f.close() except Exception as err: print(err) return None return path
def receiveInChunks(cls, socket, chunkSize =None, echo =False): if chunkSize is None: chunkSize = NimbleEnvironment.SOCKET_CHUNK_SIZE chunkSize *= 2 terminator = StringUtils.unicodeToStr(NimbleEnvironment.TERMINATION_IDENTIFIER) message = [] while True: try: result = socket.recv(chunkSize) if result.endswith(terminator): result = result[:-len(terminator)] message.append(result) break message.append(result) except Exception as err: if not message: print(err) raise if message[-1].endswith(terminator): message[-1] = message[-1][:-len(terminator)] break if not message: return None return StringUtils.unicodeToStr('').join(message)
def insertColumn(self, sheetname, columnname, columnnumber): """Inserts a new empty column into the current doc. @param sheetname: The name of the sheet to be added to. @type sheetname: string @param columnname: The name of the new column to be added @type columnname: string @param columnnumber: Where to insert the new column (= how many come before it?) @type columnnumber: int """ sheets = self._doc.spreadsheet.getElementsByType(Table) for sheet in sheets: if sheet.getAttribute('name') == sheetname: rownum = 0 rows = sheet.getElementsByType(TableRow) for row in rows: colNum = 0 cells = row.getElementsByType(TableCell) for cell in cells: if colNum == columnnumber: newCell = TableCell() if rownum == 0: p = P() p.addText(StringUtils.toUnicode(columnname)) newCell.addElement(p) else: p = P() p.addText(StringUtils.toUnicode('')) newCell.addElement(p) row.insertBefore(newCell, cell) colNum += 1 rownum += 1
def receiveInChunks(cls, socket, chunkSize=None, echo=False): if chunkSize is None: chunkSize = NimbleEnvironment.SOCKET_CHUNK_SIZE chunkSize *= 2 terminator = StringUtils.unicodeToStr( NimbleEnvironment.TERMINATION_IDENTIFIER) message = [] while True: try: result = socket.recv(chunkSize) if result.endswith(terminator): result = result[:-len(terminator)] message.append(result) break message.append(result) except Exception as err: if not message: print(err) raise if message[-1].endswith(terminator): message[-1] = message[-1][:-len(terminator)] break if not message: return None return StringUtils.unicodeToStr('').join(message)
def _handleResponseReady(self, request, response): """Event handler for the response object being ready for use.""" if self._cacheControlPublic: response.cache_control = "public" # ------------------------------------------------------------------------------------------- # Cache Expiration: Set the caching values according to the _expires property rep = self._explicitResponse if rep is None or (isinstance(rep, ViewResponse) and rep.allowCaching): response.cache_control.max_age = self.expires if not self.expires is None else 0 else: response.cache_control.max_age = 0 # ------------------------------------------------------------------------------------------- # Cache Validators if self._etag is not None: response.etag = StringUtils.toUnicode(self._etag) if self._lastModified is not None: response.last_modified = self._lastModified # If required encode the response headers as strings to prevent unicode errors. This is # necessary for certain WSGI server applications, e.g. flup. if self.ziggurat.strEncodeEnviron: for n, v in DictUtils.iter(response.headers): if StringUtils.isStringType(v): response.headers[n] = StringUtils.toStr2(v) # Clean up per-thread sessions. ConcreteModelsMeta.cleanupSessions()
def prettyPrint(target, indentLevel=1): indent = '\n' + (indentLevel * ' ') s = '\n' if isinstance(target, list): index = 0 for t in target: try: v = StringUtils.toUnicode(t) except Exception: v = '<UNPRINTABLE>' s += '%s[%s]: %s' % (indent, index, v) return s if isinstance(target, dict): for n, v in target.items(): try: v = StringUtils.toUnicode(v) except Exception: v = '<UNPRINTABLE>' s += '%s%s: %s' % (indent, n, v) return s items = dir(target) for n in items: v = getattr(target, n) try: v = StringUtils.toUnicode(v) except Exception: v = '<UNPRINTABLE>' s += '%s%s: %s' % (indent, n, v) return s
def formatAsString(cls, src, indentLevel=0): indents = ' ' * indentLevel if isinstance(src, (list, tuple)): out = [StringUtils.toUnicode('%s%s' % (indents, src[0]))] indents += ' ' lines = [] maxIndex = 0 for item in src[1:]: item = StringUtils.toUnicode(item) index = item.find(':') index = index if index != -1 and index < 12 else 0 maxIndex = max(index, maxIndex) lines.append([index, item]) for item in lines: if item[0] > 0: out.append(indents + (' ' * max(0, maxIndex - item[0])) + item[1]) else: out.append(indents + item[1]) return StringUtils.toUnicode('\n'.join(out)) else: return StringUtils.toUnicode(indents + src)
def asWebRgbOpacity(self, opacity =None): c = self.asRgb(output=tuple) return 'rgba(%s, %s, %s, %s)' % ( StringUtils.toUnicode(c[0]), StringUtils.toUnicode(c[1]), StringUtils.toUnicode(c[2]), StringUtils.toUnicode(100.0*(self._opacity if opacity is None else opacity)) + '%' )
def cleanFilename(cls, filename): if not filename: return StringUtils.getRandomString(12) out = StringUtils.slugify(filename) if not out: return StringUtils.getRandomString(12) return out
def __init__(self): """Creates a new instance of UniqueObject.""" self._INSTANCE_INDEX += 1 self._instanceUid = TimeUtils.getUidTimecode( prefix=self.__class__.__name__, suffix=StringUtils.toUnicode(self._INSTANCE_INDEX) + '-' + StringUtils.getRandomString(8))
def __init__(self, capType, addExp=None, addReplace="", removeExp=None, removeReplace=""): """Creates a new instance of ClassTemplate.""" self._addPattern = re.compile(addExp) if StringUtils.isStringType(addExp) else addExp self._removePattern = re.compile(removeExp) if StringUtils.isStringType(removeExp) else removeExp self._addReplace = addReplace self._removeReplace = removeReplace self._capType = capType
def prettyPrint(target, indentLevel =1): indent = '\n' + (indentLevel*' ') s = '\n' if isinstance(target, list): index = 0 for t in target: try: v = StringUtils.toUnicode(t) except Exception: v = '<UNPRINTABLE>' s += '%s[%s]: %s' % (indent, index, v) return s if isinstance(target, dict): for n,v in target.items(): try: v = StringUtils.toUnicode(v) except Exception: v = '<UNPRINTABLE>' s += '%s%s: %s' % (indent, n, v) return s items = dir(target) for n in items: v = getattr(target, n) try: v = StringUtils.toUnicode(v) except Exception: v = '<UNPRINTABLE>' s += '%s%s: %s' % (indent, n, v) return s
def __init__(self, capType, addExp =None, addReplace ='', removeExp =None, removeReplace =''): """Creates a new instance of ClassTemplate.""" self._addPattern = re.compile(addExp) if StringUtils.isStringType(addExp) else addExp self._removePattern = re.compile(removeExp) if StringUtils.isStringType(removeExp) else removeExp self._addReplace = addReplace self._removeReplace = removeReplace self._capType = capType
def addKeysFromLists(self, **kwargs): self._clearCache() x = ArgsUtils.get('times', None, kwargs) if not x: return y = ArgsUtils.get('values', None, kwargs) if not y: return tans = ArgsUtils.get('tangents', None, kwargs) inTans = ArgsUtils.get('inTangents', tans, kwargs) outTans = ArgsUtils.get('outTangents', tans, kwargs) if not inTans: inTans = 'lin' if not outTans: outTans = 'lin' for i in range(0,len(x)): self._keys.append(DataChannelKey( time=x[i], value=y[i], inTangent=inTans if StringUtils.isStringType(inTans) else inTans[i], outTangent=outTans if StringUtils.isStringType(outTans) else outTans[i] ))
def autoDescription(self): if self._autoDescription: return self._autoDescription if not self._result: return u'' source = self._result p = re.compile('<script.*(?!</script>)', re.DOTALL) source = p.sub(u'', source) p = re.compile('<pre.*(?!</pre>)', re.DOTALL) source = p.sub(u'', source) p = re.compile('<[/A-Za-z]+[^>]+>') source = p.sub(u'\n', source) res = re.compile('[A-Za-z0-9.,?!\'" -:;\(\)&%$#@]{64,}').search(source) if not res or not res.group(): return u'' r = res.group() r = r[:2000].replace(u'\n',u' ') r = MarkupProcessor._WHITESPACE_KILLER_REGEX.sub(u' ', r) return StringUtils.htmlEscape(StringUtils.getCompleteFragment(r, 255, True))
def formatAsString(cls, src, indentLevel =0): indents = ' '*indentLevel if isinstance(src, (list, tuple)): out = [StringUtils.toUnicode('%s%s' % (indents, src[0]))] indents += ' ' lines = [] maxIndex = 0 for item in src[1:]: item = StringUtils.toUnicode(item) index = item.find(':') index = index if index != -1 and index < 12 else 0 maxIndex = max(index, maxIndex) lines.append([index, item]) for item in lines: if item[0] > 0: out.append(indents + (' '*max(0, maxIndex - item[0])) + item[1]) else: out.append(indents + item[1]) return StringUtils.toUnicode('\n'.join(out)) else: return StringUtils.toUnicode(indents + src)
def getUidTimecode(cls, prefix=None, suffix=None): """ Creates a timecode down to the microsecond for use in creating unique UIDs. """ out = Base64.to64(cls.getNowSeconds()) + '-' + Base64.to64( datetime.microsecond) return ((StringUtils.toUnicode(prefix) + '-') if prefix else '') + out \ + (('-' + StringUtils.toUnicode(suffix)) if suffix else '')
def remove(self, key): if not key: return False if not StringUtils.isStringType(key) and len(key) == 1: key = key[0] if StringUtils.isStringType(key): k, value = self._getFrom(self._data, key, includeKey=True) if value == self.null: return False hierarchy = [(k, self._data)] else: value, hierarchy = self.get(key, defaultValue=self.null, includeHierarchy=True) if value == self.null or not hierarchy: return False while len(hierarchy) > 0: item = hierarchy.pop() del item[1][item[0]] if item[1]: return True return True
def set(self, key, value, defaultValue=None): if not key: return False if value is None or value == defaultValue: self.remove(key) return True if not StringUtils.isStringType(key) and len(key) == 1: key = key[0] if StringUtils.isStringType(key): addKey = self._formatKey(key) source = self._data else: addKey = self._formatKey(key[-1]) source = self._data temp = self._data for k in key[:-1]: temp = self._getFrom(temp, k) if temp == self.null: temp = dict() source[self._formatKey(k)] = temp source = temp source[addKey] = value return True
def _handleAddApp(self): defaultPath = self.appConfig.get('LAST_APP_PATH', OsUtils.getDocumentsPath()) path = PyGlassBasicDialogManager.browseForDirectory( parent=self, caption=StringUtils.dedent(""" Specify the root path to a PyGlass application, in which a resource folder resides"""), defaultPath=defaultPath) if not path: return label = PyGlassBasicDialogManager.openTextQuery( parent=self, header='Enter Application Name', message='Specify the name of this application for display within Alembic Migrator', defaultText=os.path.basename(path.rstrip(os.sep)) ) apps = self.appConfig.get('APPLICATIONS', dict()) appData = { 'label':label, 'path':path, 'databases':dict(), 'id':TimeUtils.getUidTimecode('App', StringUtils.slugify(label))} apps[appData['id']] = appData self.appConfig.set('APPLICATIONS', apps) self.refresh() resultItem = self.appsListWidget.findItems(appData['id'], QtCore.Qt.MatchExactly) if resultItem: resultItem[0].setSelected(True)
def __init__(self): """Creates a new instance of UniqueObject.""" self._INSTANCE_INDEX += 1 self._instanceUid = TimeUtils.getUidTimecode( prefix=self.__class__.__name__, suffix=StringUtils.toUnicode( self._INSTANCE_INDEX) + '-' + StringUtils.getRandomString(8) )
def set(self, key, value, defaultValue =None): if not key: return False if value is None or value == defaultValue: self.remove(key) return True if not StringUtils.isStringType(key) and len(key) == 1: key = key[0] if StringUtils.isStringType(key): addKey = self._formatKey(key) source = self._data else: addKey = self._formatKey(key[-1]) source = self._data temp = self._data for k in key[:-1]: temp = self._getFrom(temp, k) if temp == self.null: temp = dict() source[self._formatKey(k)] = temp source = temp source[addKey] = value return True
def createErrorMessage(cls, message, error): try: errorType = StringUtils.toUnicode(sys.exc_info()[0]) except Exception: try: errorType = str(sys.exc_info()[0]) except Exception: errorType = '[[UNABLE TO PARSE]]' try: errorValue = StringUtils.toUnicode(sys.exc_info()[1]) except Exception: try: errorValue = str(sys.exc_info()[1]) except Exception: errorValue = '[[UNABLE TO PARSE]]' try: error = StringUtils.toUnicode(error) except Exception as err: try: error = str(err) except Exception: error = '[[UNABLE TO PARSE]]' try: es = '%s\n TYPE: %s\n VALUE: %s\nERROR: %s\n' % ( cls.formatAsString(message), errorType, errorValue, error) except Exception: try: es = '%s\n [[ERROR ATTRIBUTE PARSING FAILURE]]' % cls.formatAsString(message) except Exception: es = 'FAILED TO PARSE EXCEPTION' return es
def getPrefix(self, *args, **kwargs): if self._locationPrefix: item = self.getStackData()[-1] loc = ' -> %s #%s]' % (item['file'], StringUtils.toUnicode(item['line'])) else: loc = ']' if self._app and self._app.pyramidApp: wsgi = self._app.environ initials = self._INITIALS_RX.sub('', ArgsUtils.get('REMOTE_USER', '', wsgi)) if initials: initials += ' | ' domainName = ArgsUtils.get('SERVER_NAME', '', wsgi) uriPath = ArgsUtils.get( 'REQUEST_URI', ArgsUtils.get('HTTP_REQUEST_URI', '', wsgi), wsgi) info = ' <' + initials + domainName + uriPath + '>' else: info = '' threadID = ThreadUtils.getCurrentID() return StringUtils.toUnicode( TimeUtils.toFormat('[%a %H:%M <%S.%f>') + '<' + threadID + '>' + info + loc)
def _listPath(cls, rootPath, recursive, **kwargs): listDirs = ArgsUtils.get('listDirs', False, kwargs) skipSVN = ArgsUtils.get('skipSVN', True, kwargs) skips = ArgsUtils.get('skips', None, kwargs) allowExtensions = ArgsUtils.getAsList('allowExtensions', kwargs) skipExtensions = ArgsUtils.getAsList('skipExtensions', kwargs) out = [] for item in os.listdir(rootPath): if (skipSVN and item == '.svn') or (skips and item in skips): continue absItem = os.path.join(rootPath, item) if os.path.isdir(absItem): path = (absItem + os.sep) if listDirs: out.append(path) absItem = None if recursive: out += cls._listPath(path, recursive, **kwargs) elif os.path.isfile(absItem): if skipExtensions and StringUtils.ends(item, skipExtensions): continue if allowExtensions and not StringUtils.ends(item, allowExtensions): continue if absItem: out.append(absItem) return out
def createUploadPolicy(self, key, durationSeconds, maxSizeBytes): """Returns a S3 upload policy and signature for this bucket with the specified key. """ return self._conn.build_post_form_args( bucket_name=StringUtils.toUnicode(self.bucketName), key=StringUtils.toUnicode(key), expires_in=durationSeconds, acl=StringUtils.toUnicode('private'), max_content_length=maxSizeBytes)
def __str__(self): modelInfo = self._getReprString() return '<%s[%s] cts[%s] upts[%s]%s>' % ( self.__class__.__name__, StringUtils.toUnicode(self.i), StringUtils.toUnicode(self.cts.strftime('%m-%d-%y %H:%M:%S') if self.cts else 'None'), StringUtils.toUnicode(self.upts.strftime('%m-%d-%y %H:%M:%S') if self.upts else 'None'), (' %s' % modelInfo) if modelInfo else '')
def getPrefix(self, *args, **kwargs): if self._locationPrefix: item = self.getStackData()[-1] loc = ' -> %s #%s]' % (item['file'], StringUtils.toUnicode(item['line'])) else: loc = ']' return StringUtils.toUnicode( self.getTime(self.timezone).strftime('[%a %H:%M <%S.%f>') + loc)
def getUidTimecode(cls, prefix=None, suffix=None): """ Creates a timecode down to the microsecond for use in creating unique UIDs. """ out = Base64.to64(cls.getNowSeconds()) + "-" + Base64.to64(datetime.microsecond) return ( ((StringUtils.toUnicode(prefix) + "-") if prefix else "") + out + (("-" + StringUtils.toUnicode(suffix)) if suffix else "") )
def _compileUiFile(self, path, filename): """Doc...""" source = FileUtils.createPath(path, filename, isFile=True) if self._verbose: self._log.write('COMPILING: ' + source) if PyGlassEnvironment.isWindows: uicCommand = FileUtils.createPath(self._pythonPath, 'Scripts', 'pyside-uic.exe') else: uicCommand = 'pyside-uic' cmd = '%s %s' % (uicCommand, source) pipe = subprocess.Popen( cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, error = pipe.communicate() if pipe.returncode or error: self._log.write('ERROR: Failed to compile %s widget: %s' % (str(source), str(error))) return False out = StringUtils.toUnicode(out) res = WidgetUiCompiler._CLASS_NAME_RE.search(out) if not res: self._log.write('ERROR: Failed to find widget class name for ' + str(source)) return False out = WidgetUiCompiler._CLASS_NAME_RE.sub('PySideUiFileSetup', out, 1) res = WidgetUiCompiler._SETUP_UI_RE.search(out) if not res: self._log.write('ERROR: Failed to find widget setupUi method for ' + str(source)) return False targetName = res.groupdict().get('parentName') out = WidgetUiCompiler._SETUP_UI_RE.sub('\g<parentName>', out, 1) res = WidgetUiCompiler._RETRANSLATE_RE.search(out) if not res: self._log.write('ERROR: Failed to find widget retranslateUi method for ' + str(source)) return False out = WidgetUiCompiler._RETRANSLATE_RE.sub('\g<parentName>', out, 1) out = StringUtils.toUnicode(out) out = WidgetUiCompiler._SELF_RE.sub(targetName + '.', out) dest = FileUtils.createPath(path, filename[:-3] + '.py', isFile=True) if os.path.exists(dest): os.remove(dest) f = open(dest, 'w+') f.write(out) f.close() py_compile.compile(dest) return True
def _executeCommand(cls, payload): cmd = payload['command'] if cmd is None or (StringUtils.isStringType(cmd) and not cmd in globals()): return NimbleResponseData( kind=DataKindEnum.COMMAND, response=NimbleResponseData.FAILED_RESPONSE, error=DataErrorEnum.INVALID_COMMAND) if StringUtils.isStringType(cmd): targetObject = globals().get(cmd) else: if isinstance(cmd, dict): module = str(cmd['module']) target = str(cmd['target']) method = str(cmd['method']) if 'method' in cmd else None else: target = str(cmd[0]) module = str(cmd[1]) if len(cmd) > 0 else None method = str(cmd[2]) if len(cmd) > 1 else None try: res = __import__(module, globals(), locals(), [target]) Target = getattr(res, target) if method: m = getattr(Target, method) if m is None: raise Exception( '%s not found on %s. Unable to execute command.' % \ (str(method), str(target) )) except Exception as err: return NimbleResponseData( kind=DataKindEnum.COMMAND, response=NimbleResponseData.FAILED_RESPONSE, error=cls._getDetailedError( 'Failed to import remote command module', err)) if method: targetObject = getattr(Target, method) if inspect.ismethod( targetObject) and targetObject.__self__ is None: targetObject = getattr(cls._instantiateClass(Target, cmd), method) elif inspect.isclass(Target): targetObject = cls._instantiateClass(Target, cmd) else: targetObject = Target try: result = targetObject(*payload['args'], **DictUtils.cleanDictKeys(payload['kwargs'])) return cls.createReply(DataKindEnum.COMMAND, result) except Exception as err: return NimbleResponseData( kind=DataKindEnum.COMMAND, response=NimbleResponseData.FAILED_RESPONSE, error=cls._getDetailedError('Failed to execute command', err))
def openSpreadsheet(self): """(Re)Loads the spreadsheet.""" self._doc = load(self._filepath) rows = self._doc.spreadsheet.getElementsByType(TableRow) dataWidth = 1 # Determine data-width (as opposed to trailing blank cells) cells = rows[0].getElementsByType(TableCell) for cell in cells[1:]: pl = cell.getElementsByType(P) if len(pl) > 0 and (pl[0].firstChild) and len( StringUtils.toUnicode(pl[0].firstChild)) > 0: dataWidth += 1 else: break # Expand out / decompress repeated cells (e.g. number-columns-repeated="2") for row in rows: cells = row.getElementsByType(TableCell) colNum = 0 for cell in cells: if colNum < dataWidth: repeated = int( cell.getAttribute('numbercolumnsrepeated') or 0) pl = cell.getElementsByType(P) if repeated > 1: if len(pl) > 0 and pl[0].firstChild and len( StringUtils.toUnicode(pl[0].firstChild)) > 0: for i in range(repeated): c = TableCell() p = P() p.addText( StringUtils.toUnicode(pl[0].firstChild)) c.addElement(p) row.insertBefore(c, cell) row.removeChild(cell) else: for i in range(min(repeated, dataWidth - colNum)): c = TableCell() p = P() p.addText(StringUtils.toUnicode('')) c.addElement(p) row.insertBefore(c, cell) row.removeChild(cell) else: row.removeChild(cell) colNum += 1 # Add a constant 3 trailing columns for i in range(3): c = TableCell() p = P() p.addText(StringUtils.toUnicode('')) c.addElement(p) row.addElement(c)
def _executeCommand(cls, payload): cmd = payload['command'] if cmd is None or (StringUtils.isStringType(cmd) and not cmd in globals()): return NimbleResponseData( kind=DataKindEnum.COMMAND, response=NimbleResponseData.FAILED_RESPONSE, error=DataErrorEnum.INVALID_COMMAND ) if StringUtils.isStringType(cmd): targetObject = globals().get(cmd) else: if isinstance(cmd, dict): module = str(cmd['module']) target = str(cmd['target']) method = str(cmd['method']) if 'method' in cmd else None else: target = str(cmd[0]) module = str(cmd[1]) if len(cmd) > 0 else None method = str(cmd[2]) if len(cmd) > 1 else None try: res = __import__(module, globals(), locals(), [target]) Target = getattr(res, target) if method: m = getattr(Target, method) if m is None: raise Exception( '%s not found on %s. Unable to execute command.' % \ (str(method), str(target) )) except Exception as err: return NimbleResponseData( kind=DataKindEnum.COMMAND, response=NimbleResponseData.FAILED_RESPONSE, error=cls._getDetailedError( 'Failed to import remote command module', err) ) if method: targetObject = getattr(Target, method) if inspect.ismethod(targetObject) and targetObject.__self__ is None: targetObject = getattr(cls._instantiateClass(Target, cmd), method) elif inspect.isclass(Target): targetObject = cls._instantiateClass(Target, cmd) else: targetObject = Target try: result = targetObject( *payload['args'], **DictUtils.cleanDictKeys(payload['kwargs']) ) return cls.createReply(DataKindEnum.COMMAND, result) except Exception as err: return NimbleResponseData( kind=DataKindEnum.COMMAND, response=NimbleResponseData.FAILED_RESPONSE, error=cls._getDetailedError('Failed to execute command', err) )
def _reformatValue(cls, value): if isinstance(value, dict): value = cls._reformat(value) elif StringUtils.isStringType(value): value = StringUtils.toUnicode(value) elif isinstance(value, (list, tuple)): vout = [] for item in value: vout.append(cls._reformatValue(item)) value = vout return value
def secondsToDurationTimecode(cls, seconds): """ Turns the specified number of seconds (including fractional seconds) into a durational timecode of the format HH:MM:SS.000 """ time = cls.explodeElapsedTime(seconds) secs = int(time['seconds']) millis = int(round(1000.0 * (time['seconds'] - float(secs)))) return StringUtils.toUnicode(time['hours']).zfill(2) + ':' \ + StringUtils.toUnicode(time['minutes']).zfill(2) + ':' \ + StringUtils.toUnicode(secs).zfill(2) + '.' \ + StringUtils.toUnicode(millis).zfill(3)
def openSpreadsheet(self): """(Re)Loads the spreadsheet.""" self._doc = load(self._filepath) rows = self._doc.spreadsheet.getElementsByType(TableRow) dataWidth = 1 # Determine data-width (as opposed to trailing blank cells) cells = rows[0].getElementsByType(TableCell) for cell in cells[1:]: pl = cell.getElementsByType(P) if len(pl) > 0 and (pl[0].firstChild) and len(StringUtils.toUnicode(pl[0].firstChild)) > 0: dataWidth += 1 else: break # Expand out / decompress repeated cells (e.g. number-columns-repeated="2") for row in rows: cells = row.getElementsByType(TableCell) colNum = 0 for cell in cells: if colNum < dataWidth: repeated = int(cell.getAttribute('numbercolumnsrepeated') or 0) pl = cell.getElementsByType(P) if repeated > 1: if len(pl) > 0 and pl[0].firstChild and len(StringUtils.toUnicode(pl[0].firstChild)) > 0: for i in range(repeated): c = TableCell() p = P() p.addText(StringUtils.toUnicode(pl[0].firstChild)) c.addElement(p) row.insertBefore(c, cell) row.removeChild(cell) else: for i in range(min(repeated, dataWidth-colNum)): c = TableCell() p = P() p.addText(StringUtils.toUnicode('')) c.addElement(p) row.insertBefore(c, cell) row.removeChild(cell) else: row.removeChild(cell) colNum += 1 # Add a constant 3 trailing columns for i in range(3): c = TableCell() p = P() p.addText(StringUtils.toUnicode('')) c.addElement(p) row.addElement(c)
def unpack(self, dataType, length): data = StringUtils.unicodeToStr(self.read(length)) assert len(data) == length, \ u"[UNPACK ERROR]: Unexpected end of stream [%s | %s]" % ( StringUtils.toUnicode(len(data)), StringUtils.toUnicode(length)) try: return struct.unpack(StringUtils.unicodeToStr(self.endianess + dataType), data)[0] except struct.error: print(len(data)) print(u"Unable to unpack '%r'" % data) raise
def logMessageToString( cls, logMessage, includePrefix =True, includeStack =True, prefixSeparator ='\n ', stackSeparator ='\n' ): out = [] if includePrefix and 'prefix' in logMessage: out.append(StringUtils.toUnicode(logMessage['prefix']) + prefixSeparator) out.append(StringUtils.toUnicode(logMessage['log'])) if includeStack and 'stack' in logMessage: out.append(stackSeparator + StringUtils.toUnicode(logMessage['stack'])) return ''.join(out)
def asAscii(cls, string): if sys.version > '3': return StringUtils.toUnicode(string) if StringUtils.isStringType(string): try: return string.encode('utf8', 'ignore') except Exception: try: return unicodedata.normalize('NFKD', string).encode('ascii', 'ignore') except Exception: return '[[UNABLE TO DISPLAY LOG ENTRY IN ASCII CHARS]]' else: return string
def unpack(self, dataType, length): data = StringUtils.unicodeToStr(self.read(length)) assert len(data) == length, \ u"[UNPACK ERROR]: Unexpected end of stream [%s | %s]" % ( StringUtils.toUnicode(len(data)), StringUtils.toUnicode(length)) try: return struct.unpack( StringUtils.unicodeToStr(self.endianess + dataType), data)[0] except struct.error: print(len(data)) print(u"Unable to unpack '%r'" % data) raise
def asAscii(cls, string): if sys.version > '3': return StringUtils.toUnicode(string) if StringUtils.isStringType(string): try: return string.encode('utf8', 'ignore') except Exception: try: return unicodedata.normalize('NFKD', string).encode( 'ascii', 'ignore') except Exception: return '[[UNABLE TO DISPLAY LOG ENTRY IN ASCII CHARS]]' else: return string
def _buildHttpsReply(self, parent, request, url, operation, data, page): headers = dict() for header in request.rawHeaderList(): headers[StringUtils.toUnicode(header)] = StringUtils.toUnicode(request.rawHeader(header)) if data: data = data.readAll() thread = HttpsRemoteExecutionThread( parent=self, operation=operation, data=data, headers=headers, url=url.toString()) thread.completeSignal.signal.connect(self._handleHttpsResult) thread.start()
def executeCommand(cls, cmd, remote=False, shell=True, wait=False, background=False, resultObj=False): if shell and not StringUtils.isStringType(cmd): from pyaid.list.ListUtils import ListUtils cmd = ' '.join(ListUtils.itemsToString(cmd)) # Background nohup processes shouldn't PIPE and once run should immediately return if background: subprocess.Popen(cmd, shell=shell) return {'error': '', 'out': '', 'code': 0, 'command': cmd} if remote: pipe = subprocess.Popen(cmd, shell=shell, stdout=None, stderr=None, stdin=None, close_fds=False) if wait: pipe.wait() return {'error': '', 'out': '', 'code': 0, 'command': cmd} pipe = subprocess.Popen(cmd, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if wait: pipe.wait() out, error = pipe.communicate() if resultObj: return cls.CMD_RESULT_NT(error=StringUtils.toUnicode(error), output=StringUtils.toUnicode(out), code=pipe.returncode, success=pipe.returncode == 0, command=cmd) return { 'error': StringUtils.toUnicode(error), 'out': StringUtils.toUnicode(out), 'code': pipe.returncode, 'command': cmd }
def getColorNameAndValue(self): """ Finds the nearest named color by comparing all named colors """ if self._rawColor == 0: return { 'name':'Black', 'value':0, 'key':'black', 'residual':0.0 } maxRange = 560.0 nearestValue = None nearestName = None range = 360 myColor = self.asHsl(output=list) poolColor = self.__class__(0) for name, value in DictUtils.iter(ColorNames.NAMES): poolColor.load(value) color = poolColor.asHsl(output=list) test = (myColor[0] - color[0])*(myColor[0] - color[0]) \ + (myColor[1] - color[1])*(myColor[1] - color[1]) \ + (myColor[2] - color[2])*(myColor[2] - color[2]) if test < range: nearestValue = value nearestName = name range = test if range < 1: break return { 'name':StringUtils.capitalizeWords(nearestName.replace('_', ' ')), 'value':nearestValue, 'key':nearestName, 'residual':100.0*range/maxRange }
def __init__(self, parent, path, **kwargs): """Creates a new instance of RecentPathElement.""" self._path = path super(RecentPathElement, self).__init__(parent, **kwargs) mainLayout = self._getLayout(self, QtGui.QVBoxLayout) line = LineSeparatorWidget(self) mainLayout.addWidget(line) mainLayout.addSpacing(6) widget, layout = self._createWidget(self, QtGui.QHBoxLayout, True) layout.setContentsMargins(6, 0, 12, 0) label = QtGui.QLabel(widget) label.setText(StringUtils.abbreviateCenter(path, 72)) layout.addWidget(label) self._label = label layout.addStretch() icon = IconElement(widget, icon=IconSheetMap.FORWARD, size=SizeEnum.SMALL) layout.addWidget(icon) self._icon = icon mainLayout.addSpacing(6)
def open(self): if self._source: return self._source f = open(self.path, 'r') raw = f.read() f.close() res = self._cls.FLAGS_PATTERN.finditer(raw) if res: for r in res: for f in r.group('flags').strip().split(','): f = f.strip().lower().replace(' ', '').replace('-', '').replace( '_', '') if f and not f in self._flags: self._flags.append(f) #------------------------------------------------------------------------------------------- # REPLACE COMPILE VARIABLES reps = {} # NONE AT THE MOMENT for n, v in reps.items(): raw = raw.replace(n, StringUtils.toUnicode(v)) self._source = raw + '\n' return raw
def __init__(self, name=None, **kwargs): """Initializes settings.""" self._buffer = [] self._meta = dict() self._fileCount = kwargs.get('fileCount', 10) self._zeroTime = kwargs.get('zeroTime', self._ZERO_TIME) self._reportPath = kwargs.get('path', self._REPORT_PATH) self._time = datetime.datetime.utcnow() self._timeCode = Reporter.getTimecodeFromDatetime(self._time, self._zeroTime) if StringUtils.isStringType(name) and len(name) > 0: self._name = name elif hasattr(name, '__class__'): try: self._name = name.__class__.__name__ except Exception: self._name = 'UnknownObject' elif hasattr(name, '__name__'): try: self._name = name.__name__ except Exception: self._name = 'UnknownClass' else: self._name = 'General' self._meta['_vw'] = self._name
def cleanBytesToText(cls, source, inPlace =True): """cleanBytesToText doc...""" out = source if inPlace else dict() from pyaid.list.ListUtils import ListUtils for n, v in cls.iter(source): if isinstance(v, (tuple, list)): v = ListUtils.cleanBytesToText(v, inPlace=inPlace) elif isinstance(v, dict): v = cls.cleanBytesToText(v) else: v = StringUtils.strToUnicode(v, force=False) out[StringUtils.toStrStr(n)] = v return out
def get(name, defaultValue=None, kwargs=None, args=None, index=None, allowNone=True): if args and not index is None and (index < 0 or index < len(args)): out = args[index] if not allowNone and args[index] is None: return defaultValue return out try: if StringUtils.isStringType(name): if name in kwargs: out = kwargs[name] if not allowNone and out is None: return defaultValue return out else: for n in name: if n in kwargs: out = kwargs[n] if not allowNone and out is None: return defaultValue return out except Exception as err: pass return defaultValue
def moveColumn(self, sheetname, oldcolumn, newcolumn): """Replaces the column oldcolumn with newcolumn and deletes newcolumn. This function assumes: oldcolumn > newcolumn. @param sheetname: The name of the sheet to be operated on. @type sheetname: string @param oldcolumn: The column to move data from. @type oldcolumn: int @param newcolumn: The column to move data to. @type newcolumn: int """ sheets = self._doc.spreadsheet.getElementsByType(Table) for sheet in sheets: if sheet.getAttribute('name') == sheetname: rows = sheet.getElementsByType(TableRow) for row in rows: colNum = 0 cells = row.getElementsByType(TableCell) for cell in cells: if colNum == newcolumn: newcolumncell = cell pl = cell.getElementsByType(P) for p in pl: cell.removeChild(p) elif colNum == oldcolumn: pl = cell.getElementsByType(P) if len(pl) > 0: p = P() if pl[0].firstChild: p.addText( StringUtils.toUnicode( pl[0].firstChild)) newcolumncell.addElement(p) colNum += 1
def addFields(self, *args): """ Tuples containing key, name pairs""" for arg in args: if StringUtils.isStringType(arg): self.addField(arg, arg) else: self.addField(arg[0], arg[1])