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 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 __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 __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 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 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 _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 _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 _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 _handleImportSitemaps(self): self.mainWindow.showLoading( self, u'Browsing for Sitemap File', u'Choose the Sitemap CSV file to import into the database') path = PyGlassBasicDialogManager.browseForFileOpen( parent=self, caption=u'Select CSV File to Import', defaultPath=self.mainWindow.appConfig.get(UserConfigEnum.LAST_BROWSE_PATH) ) self.mainWindow.hideLoading(self) if not path or not StringUtils.isStringType(path): self.mainWindow.toggleInteractivity(True) return # Store directory location as the last active directory self.mainWindow.appConfig.set( UserConfigEnum.LAST_BROWSE_PATH, FileUtils.getDirectoryOf(path) ) self.mainWindow.showStatus( self, u'Importing Sitemaps', u'Reading sitemap information into database') SitemapImporterRemoteThread( parent=self, path=path ).execute( callback=self._sitemapImportComplete, logCallback=self._handleImportStatusUpdate)
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 __init__(self, parent =None, **kwargs): """Creates a new instance of PyGlassWidget.""" PyGlassElement.__init__(self, parent, **kwargs) if kwargs.get('verbose', False): print('CREATING: %s | PARENTED TO: %s' % (self, parent)) self.setStyleSheet(self.owner.styleSheetPath) self._displayCount = 0 self._widgetClasses = kwargs.get('widgets', dict()) self._widgetParent = None self._currentWidget = None self._widgets = dict() self._widgetFlags = kwargs.get('widgetFlags') self._widgetID = kwargs.get('widgetID') self._lastChildWidgetID = None self._lastPeerWidgetID = None widgetFile = kwargs.get('widgetFile', True) if widgetFile: parts = self.RESOURCE_WIDGET_FILE if parts: if StringUtils.isStringType(parts): parts = parts.split('/')[-1:] elif parts: parts = parts[-1:] self._widgetData = UiFileLoader.loadWidgetFile(self, names=parts) else: self._widgetData = None name = kwargs.get('containerWidgetName') self._containerWidget = getattr(self, name) if name and hasattr(self, name) else None
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 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 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])
def _setEnvValue(cls, key, value): settings = cls._getEnvValue(None) if cls._ENV_SETTINGS is None else cls._ENV_SETTINGS if settings is None: settings = dict() cls._ENV_SETTINGS = settings if StringUtils.isStringType(key): key = [key] src = settings for k in key[:-1]: src = src[k] src[key[-1]] = value envPath = cls.getRootLocalResourcePath(cls._GLOBAL_SETTINGS_FILE, isFile=True) envDir = os.path.dirname(envPath) if not os.path.exists(envDir): os.makedirs(envDir) f = open(envPath, 'w+') try: f.write(JSON.asString(cls._ENV_SETTINGS)) except Exception: print('ERROR: Unable to write environmental settings file at: ' + envPath) return False finally: f.close() return True
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 _configParserToDict(self, parser): out = dict() for section in parser.sections(): s = dict() for opt in parser.options(section): value = str(parser.get(section, opt)) test = value.lower() if test.startswith('"') and test.endswith('"'): value = value[1:-1] elif value.startswith(ConfigReader._VECTOR_PREFIX): value = Vector3D.fromConfig(value[len(ConfigReader._VECTOR_PREFIX):-1]) elif value.startswith(ConfigReader._JSON_PREFIX): value = json.loads(value[len(ConfigReader._JSON_PREFIX):]) elif StringUtils.isStringType(value) and (value in ['None', 'none', '']): value = None elif test in ['on', 'true', 'yes']: value = True elif test in ['off', 'false', 'no']: value = False elif ConfigReader._NUMERIC_REGEX.match(test): try: value = float(value) if test.find('.') else int(value) except Exception: pass s[opt] = value out[section] = s return out
def __call__(self): super(ZigguratDataView, self).__call__() if isinstance(self._response, Response) or StringUtils.isStringType(self._response): return self._response DictUtils.cleanBytesToText(self._response, inPlace=True) return render_to_response('json', self._response, self._request)
def openLocalWebUrl(self, url): """Doc...""" if StringUtils.isStringType(url): url = url.split('/') url = self.mainWindow.getRootResourcePath('web', *url) self.load(QtCore.QUrl('file:///' + url))
def load(self, data): """ Loads the data into the CadenceData instance, parsing if necessary beforehand. @@@param load:string,dict Either a string or dictionary representation of valid Cadence Interchange Data formatted data to be loaded. @@@return bool True if successful, False if the load process failed because of invalid data. """ if not data: return False if StringUtils.isStringType(data): try: data = json.loads(data) except Exception as err: print("FAILED: Loading Cadence data from JSON string.", err) return False if CadenceData._NAME_KEY in data: self._name = data.get(CadenceData._NAME_KEY) if CadenceData._CONFIGS_KEY in data: self._configs = ConfigReader.fromDict(data.get(CadenceData._CONFIGS_KEY)) if CadenceData._CHANNELS_KEY in data: for c in data.get(CadenceData._CHANNELS_KEY, dict()): self._channels.append(DataChannel.fromDict(c)) return True
def getKey(self, key, createIfMissing=True): if StringUtils.isStringType(key): out = self._bucket.get_key(key_name=key) if createIfMissing and not out: out = Key(self._bucket, key) return out return key
def getKey(self, key, createIfMissing =True): if StringUtils.isStringType(key): out = self._bucket.get_key(key_name=key) if createIfMissing and not out: out = Key(self._bucket, key) return out return key
def _getDataTypeFromValue(cls, value): if StringUtils.isStringType(value): return DataTypeEnum.ENUM elif isinstance(value, Vector3D): return DataTypeEnum.VECTOR elif isinstance(value, dict) or isinstance(value, list): return DataTypeEnum.ARBITRARY return DataTypeEnum.SCALAR
def _updateSetting(self, key, value): if StringUtils.isStringType(key): key = key.split('->') src = self._settings for k in key[:-1]: if k not in src: src[k] = dict() src = src[k] src[key[-1]] = value
def getResourceFolderParts(cls, target): if not inspect.isclass(target): target = target.__class__ out = [] prefix = ClassUtils.getAttrFromClass(target, cls.RESOURCE_FOLDER_PREFIX, None) if prefix: out.extend(prefix.split('/') if StringUtils.isStringType(prefix) else prefix) resourceName = ClassUtils.getAttrFromClass(target, cls.RESOURCE_FOLDER_NAME, None) widget = ClassUtils.getAttrFromClass(target, cls.RESOURCE_WIDGET_FILE, None) if widget: widget = widget.split('/') if StringUtils.isStringType(widget) else widget out.extend(widget[:-1]) if resourceName or not widget: out.append(resourceName if resourceName else target.__name__) return out
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 contains(source, search): from pyaid.string.StringUtils import StringUtils if StringUtils.isStringType(search): return search in source for s in search: if s in source: return True return False
def createLogMessage(cls, logValue, traceStack, shaveStackTrace, htmlEscape, prefix=None, **kwargs): """ Formats log message data into a string for output """ doIndent = kwargs.get('indent', True) if doIndent: logValue = cls.formatAsString(logValue) elif isinstance(logValue, (list, tuple)): logValue = '\n'.join(logValue) if htmlEscape: logValue = StringUtils.htmlEscape(logValue) if doIndent: logValue = logValue.replace('\n', '\n ') logValue = StringUtils.strToUnicode(logValue) if not StringUtils.isStringType(logValue): logValue = 'FAILED TO LOG RESPONSE' out = {'log': logValue} if prefix: logPrefix = StringUtils.strToUnicode(prefix) if not StringUtils.isStringType(logPrefix): logPrefix = 'FAILED TO CREATE PREFIX' out['prefix'] = logPrefix if traceStack: logStack = StringUtils.strToUnicode( 'Stack Trace:\n' + cls.getFormattedStackTrace(shaveStackTrace)) if not StringUtils.isStringType(logStack): logStack = 'FAILED TO CREATE STACK' out['stack'] = logStack return out
def executeForOutput(cls, cmd, critical =None, shell =True, raiseExceptions =True): if shell and not StringUtils.isStringType(cmd): cmd = ' '.join(cmd) try: out = subprocess.check_output(cmd, shell=shell) except Exception as err: if critical or (critical is None and raiseExceptions): raise err return None return out
def addFields(self, *args, **kwargs): """ Adds multiple fields at once. Arguments can be any mix of the following formats addFields( "keyAndValue", # String argument ("key", "value") # Tuple or list argument key="value" # Keyword argument key=("value") """ for arg in args: if StringUtils.isStringType(arg): self.addField(arg) else: self.addField(*arg) for key, value in DictUtils.iter(kwargs): if StringUtils.isStringType(value): self.addField(key, value) else: self.addField(key, *value)
def _findTerminator(self, src, index, pattern, matchReqs=None): index += len(pattern) if index >= len(src): return index stringPattern = StringUtils.isStringType(pattern) if stringPattern: endIndex = src.find(pattern, index) offset = len(pattern) else: try: patternMatch = pattern.search(src, index) if patternMatch is None: endIndex = -1 else: endIndex = patternMatch.start() offset = patternMatch.end() - patternMatch.start() except Exception: return index if endIndex == -1: self._writeDebugLog('TERMINATION ERROR!') self._addError('TERMINATION ERROR') endIndex = src.find('\n', index) return len(src) if endIndex == -1 else endIndex if matchReqs: if not isinstance(matchReqs, list): matchReqs = [matchReqs] for m in matchReqs: # Handles escape characters robustly with an iterative find instead of a # regular expression so that the number of escape characters can be counted # to prevent false positives on complex escape sequences. if m.escapeCharacter and self._isEscaped( src, endIndex, m.escapeCharacter): return self._findTerminator(src, endIndex + 1, pattern, matchReqs) if m.pattern: if m.lookAhead: res = self._lookAhead(src, endIndex, m.pattern) else: res = self._lookBack(src, endIndex, m.pattern) if not res is None and res == m.ignoreIfFound: return self._findTerminator(src, endIndex + 1, pattern, matchReqs) return endIndex + offset
def executeForStatus(cls, cmd, critical =None, shell =True, raiseExceptions =True): """ Returns true if the command executed successfully. """ if shell and not StringUtils.isStringType(cmd): cmd = ' '.join(cmd) stat = subprocess.call(cmd, shell=shell) if stat: if critical or (critical is None and raiseExceptions): raise Exception('Command execution failed') else: return False return True
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 dynamicImport(cls, package, target=None): if StringUtils.isStringType(target): singular = True target = [target] elif not target: target = [package.split('.')[-1]] singular = True else: singular = False result = __import__(package, globals(), locals(), target) if singular: return getattr(result, target[0], None) return result
def dynamicImport(cls, package, target =None): if StringUtils.isStringType(target): singular = True target = [target] elif not target: target = [package.split('.')[-1]] singular = True else: singular = False result = __import__(package, globals(), locals(), target) if singular: return getattr(result, target[0], None) return result
def createFromRootPath(cls, path =None, flags =None): out = FileList() if not flags: flags = [] elif StringUtils.isStringType(flags): flags = [flags] walk = os.path.walk if sys.version < '3' else os.walk walk(path, cls._rootPathWalker, { 'rootPath':path, 'out':out, 'flags':flags }) return out
def createLogMessage( cls, logValue, traceStack, shaveStackTrace, htmlEscape, prefix =None, **kwargs ): """ Formats log message data into a string for output """ doIndent = kwargs.get('indent', True) if doIndent: logValue = cls.formatAsString(logValue) elif isinstance(logValue, (list, tuple)): logValue = '\n'.join(logValue) if htmlEscape: logValue = StringUtils.htmlEscape(logValue) if doIndent: logValue = logValue.replace('\n', '\n ') logValue = StringUtils.strToUnicode(logValue) if not StringUtils.isStringType(logValue): logValue = 'FAILED TO LOG RESPONSE' out = {'log':logValue} if prefix: logPrefix = StringUtils.strToUnicode(prefix) if not StringUtils.isStringType(logPrefix): logPrefix = 'FAILED TO CREATE PREFIX' out['prefix'] = logPrefix if traceStack: logStack = StringUtils.strToUnicode( 'Stack Trace:\n' + cls.getFormattedStackTrace(shaveStackTrace)) if not StringUtils.isStringType(logStack): logStack = 'FAILED TO CREATE STACK' out['stack'] = logStack return out
def createLogName(cls, name): if StringUtils.isStringType(name) and len(name) > 0: return name elif hasattr(name, '__class__'): try: return name.__class__.__name__ except Exception: return 'UnknownObject' elif hasattr(name, '__name__'): try: return name.__name__ except Exception: return 'UnknownClass' else: return 'General'
def get(self, key, defaultValue =None): self._loadSettings() if not key: return self._settings if StringUtils.isStringType(key): key = key.split('->') value = self._settings for k in key: if k in value: value = value[k] else: return defaultValue return value
def createFromRootPath(cls, path=None, flags=None): out = FileList() if not flags: flags = [] elif StringUtils.isStringType(flags): flags = [flags] walk = os.path.walk if sys.version < '3' else os.walk walk(path, cls._rootPathWalker, { 'rootPath': path, 'out': out, 'flags': flags }) return 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 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 executeForOutput(cls, cmd, critical=None, shell=True, raiseExceptions=True): if shell and not StringUtils.isStringType(cmd): cmd = ' '.join(cmd) try: out = subprocess.check_output(cmd, shell=shell) except Exception as err: if critical or (critical is None and raiseExceptions): raise err return None return out
def itemsToUnicode(cls, target, inPlace=False): """ Iterates through the elements of the target list and converts each of them to unicode strings, including decoding byte strings to unicode strings.""" from pyaid.string.StringUtils import StringUtils output = target if inPlace else [] index = 0 while index < len(target): source = target[index] if StringUtils.isStringType(source): output[index] = StringUtils.strToUnicode(source) else: output[index] = StringUtils.toUnicode(source) return output
def executeForStatus(cls, cmd, critical=None, shell=True, raiseExceptions=True): """ Returns true if the command executed successfully. """ if shell and not StringUtils.isStringType(cmd): cmd = ' '.join(cmd) stat = subprocess.call(cmd, shell=shell) if stat: if critical or (critical is None and raiseExceptions): raise Exception('Command execution failed') else: return False return True
def getFilteredByFlags(self, flags, files=True, directories=True): if StringUtils.isStringType(flags): flags = [flags] sources = [] if directories: sources += self._directories.values() if files: sources += self._files.values() sources.sort(key=lambda x: x['index']) out = [] for src in sources: if flags is None or ListUtils.contains(src['flags'], flags): out.insert(0, src['item']) return out
def remove(self, key): nullTest = NullUtils.NULL('REMOVE_TEST_NULL') value = self.get(key, nullTest) if value is nullTest: return True if StringUtils.isStringType(key): key = [key] value = self._settings for k in key[:-1]: if k in value: value = value[k] else: return True del value[key[-1]] self._cleanupSettings() self._saveSettings() return True
def _matches(self, src, index, pattern, matchReqs=None): stringPattern = StringUtils.isStringType(pattern) if stringPattern: offset = len(pattern) if src[index:index + offset] != pattern: return False else: try: patternMatch = pattern.match(src, index) if patternMatch is None: return False offset = patternMatch.end() - patternMatch.start() except Exception: return False # Check match requirements if not matchReqs: return True if not isinstance(matchReqs, list): matchReqs = [matchReqs] for m in matchReqs: # Handles escape characters robustly with an iterative find instead of a # regular expression so that the number of escape characters can be counted # to prevent false positives on complex escape sequences. if m.escapeCharacter and self._isEscaped(src, index, m.escapeCharacter): return False if m.pattern: if m.lookAhead: res = self._lookAhead(src, index + offset - 1, m.pattern) else: res = self._lookBack(src, index, m.pattern) if not res is None and res == m.ignoreIfFound: return False return True
def _generateHeaders(cls, keyName, expires=None, eTag=None, maxAge=-1, gzipped=False): """Doc...""" headers = dict() if expires: if StringUtils.isStringType(expires): headers['Expires'] = StringUtils.toBytes(expires) elif StringUtils.isBinaryType(expires): headers['Expires'] = expires else: headers['Expires'] = StringUtils.toBytes( TimeUtils.dateTimeToWebTimestamp(expires)) elif eTag: headers['ETag'] = StringUtils.toBytes(eTag) if maxAge > -1: headers['Cache-Control'] = StringUtils.toBytes( 'max-age=%s; public' % maxAge) if keyName.endswith('.jpg'): contentType = MIME_TYPES.JPEG_IMAGE elif keyName.endswith('.png'): contentType = MIME_TYPES.PNG_IMAGE elif keyName.endswith('.gif'): contentType = MIME_TYPES.GIF_IMAGE else: contentType = FileUtils.getMimeType(keyName) if StringUtils.begins(contentType, ('text/', 'application/')): contentType = '%s; charset=UTF-8' % contentType headers['Content-Type'] = contentType if gzipped: headers['Content-Encoding'] = 'gzip' return headers
def itemsToString(cls, target, inPlace=False, toUnicode=True): """ Iterates through the elements of the target list and converts each of them to binary strings, including decoding unicode strings to byte strings.""" from pyaid.string.StringUtils import StringUtils output = target if inPlace else (target + []) index = 0 while index < len(target): source = target[index] if StringUtils.isStringType(source): if toUnicode: output[index] = StringUtils.strToUnicode(source) else: output[index] = StringUtils.unicodeToStr(source, force=True) else: output[index] = str(source) index += 1 return output
def get(self, key, defaultValue=None, includeHierarchy=False): if not key: return (defaultValue, None) if includeHierarchy else defaultValue hierarchy = [] if StringUtils.isStringType(key): key, value = self._getFrom(self._data, key, includeKey=True) if value == self.null: return (defaultValue, None) if includeHierarchy else defaultValue hierarchy.append((key, self._data)) return (value, hierarchy) if includeHierarchy else value source = self._data for k in key: sourceKey, value = self._getFrom(source, k, includeKey=True) if value == self.null: return (defaultValue, None) if includeHierarchy else defaultValue hierarchy.append((sourceKey, source)) source = value return (source, hierarchy) if includeHierarchy else source
def render(self, **kwargs): """Doc...""" # ADD KWARGS TO TEMPLATE RENDER PROPERTIES if kwargs: data = DictUtils.merge(self._data, kwargs) else: data = self._data td = [self._rootDir] if StringUtils.isStringType(self._rootDir) else self._rootDir lookup = TemplateLookup( directories=td, input_encoding='utf-8', output_encoding='utf-8', encoding_errors='replace') template = self._template if template: if not template.startswith('/'): template = '/' + template try: target = lookup.get_template(template) except Exception as err: self._result = None self._error = err self._errorMsg = 'Failed to get template (%s):\n%s' % ( template, exceptions.text_error_template().render().replace('%','%%') ) self._log.writeError(self._errorMsg, self._error) return self.dom else: target = Template(self._source if self._source else '', lookup=lookup) mr = MakoDataTransporter(data=data, logger=self._log) try: self._result = target.render_unicode(mr=mr).replace('\r', '') except Exception: d = [] if data: for n,v in data.items(): d.append(StringUtils.toUnicode(n) + ': ' + StringUtils.toUnicode(v)) try: stack = exceptions.text_error_template().render().replace('%','%%') except Exception as err2: stack = '' self._log.writeError('Unable to build mako exception stack', err2) traces = mr.getTraces() self._errorMsg = 'Failed to render (%s):\n%s\n%sDATA:\n\t%s' % ( str(template), str(stack), ('TRACES:\n\t' + '\n\t'.join(traces) if traces else ''), '\n\t'.join(d) if d else '') self._log.write(self._errorMsg) if self._minify: return self.minifyResult() return self.dom