minor refactoring

This commit is contained in:
Miroslav Stampar 2012-06-14 13:38:53 +00:00
parent 1204eb00b2
commit 3a90105fbb
7 changed files with 32 additions and 19 deletions

View File

@ -788,7 +788,7 @@ def readInput(message, default=None, checkBatch=True):
message += " " message += " "
if checkBatch and conf.batch: if checkBatch and conf.batch:
if isinstance(default, (list, tuple, set)): if isListLike(default):
options = ",".join(getUnicode(opt, UNICODE_ENCODING) for opt in default) options = ",".join(getUnicode(opt, UNICODE_ENCODING) for opt in default)
elif default: elif default:
options = getUnicode(default, UNICODE_ENCODING) options = getUnicode(default, UNICODE_ENCODING)
@ -1888,7 +1888,7 @@ def getUnicode(value, encoding=None, system=False, noneToNull=False):
if noneToNull and value is None: if noneToNull and value is None:
return NULL return NULL
if isinstance(value, (list, tuple)): if isListLike(value):
value = list(getUnicode(_, encoding, system, noneToNull) for _ in value) value = list(getUnicode(_, encoding, system, noneToNull) for _ in value)
return value return value
@ -2425,7 +2425,7 @@ def arrayizeValue(value):
Makes a list out of value if it is not already a list or tuple itself Makes a list out of value if it is not already a list or tuple itself
""" """
if not isinstance(value, (list, tuple)): if not isListLike(value):
value = [value] value = [value]
return value return value
@ -2435,7 +2435,7 @@ def unArrayizeValue(value):
Makes a value out of iterable if it is a list or tuple itself Makes a value out of iterable if it is a list or tuple itself
""" """
if isinstance(value, (list, tuple)): if isListLike(value):
value = value[0] if len(value) > 0 else None value = value[0] if len(value) > 0 else None
return value return value
@ -2446,12 +2446,19 @@ def flattenValue(value):
""" """
for i in iter(value): for i in iter(value):
if isinstance(i, (list, tuple)): if isListLike(i):
for j in flattenValue(i): for j in flattenValue(i):
yield j yield j
else: else:
yield i yield i
def isListLike(value):
"""
Returns True if the given value is a list-like instance
"""
return isinstance(value, (list, tuple, set, BigArray))
def getSortedInjectionTests(): def getSortedInjectionTests():
""" """
Returns prioritized test list by eventually detected DBMS from error Returns prioritized test list by eventually detected DBMS from error
@ -2784,7 +2791,7 @@ def isNoneValue(value):
if isinstance(value, basestring): if isinstance(value, basestring):
return value in ("None", "") return value in ("None", "")
elif isinstance(value, (list, tuple)): elif isListLike(value):
return all(isNoneValue(_) for _ in value) return all(isNoneValue(_) for _ in value)
elif isinstance(value, dict): elif isinstance(value, dict):
return not any(value) return not any(value)
@ -3127,7 +3134,7 @@ def applyFunctionRecursively(value, function):
Applies function recursively through list-like structures Applies function recursively through list-like structures
""" """
if isinstance(value, (list, tuple, set, BigArray)): if isListLike(value):
retVal = [applyFunctionRecursively(_, function) for _ in value] retVal = [applyFunctionRecursively(_, function) for _ in value]
else: else:
retVal = function(value) retVal = function(value)

View File

@ -17,6 +17,7 @@ from lib.core.common import Backend
from lib.core.common import dataToDumpFile from lib.core.common import dataToDumpFile
from lib.core.common import dataToStdout from lib.core.common import dataToStdout
from lib.core.common import getUnicode from lib.core.common import getUnicode
from lib.core.common import isListLike
from lib.core.common import normalizeUnicode from lib.core.common import normalizeUnicode
from lib.core.common import openFile from lib.core.common import openFile
from lib.core.common import prioritySortColumns from lib.core.common import prioritySortColumns
@ -72,7 +73,7 @@ class Dump:
return self._outputFile return self._outputFile
def string(self, header, data, sort=True): def string(self, header, data, sort=True):
if isinstance(data, (list, tuple, set)): if isListLike(data):
self.lister(header, data, sort) self.lister(header, data, sort)
elif data: elif data:
data = self._formatString(getUnicode(data)) data = self._formatString(getUnicode(data))
@ -102,7 +103,7 @@ class Dump:
for element in elements: for element in elements:
if isinstance(element, basestring): if isinstance(element, basestring):
self._write("[*] %s" % element) self._write("[*] %s" % element)
elif isinstance(element, (list, tuple, set)): elif isListLike(element):
self._write("[*] " + ", ".join(getUnicode(e) for e in element)) self._write("[*] " + ", ".join(getUnicode(e) for e in element))
if elements: if elements:
@ -173,7 +174,7 @@ class Dump:
for tables in dbTables.values(): for tables in dbTables.values():
for table in tables: for table in tables:
if isinstance(table, (list, tuple, set)): if table and isListLike(table):
table = table[0] table = table[0]
maxlength = max(maxlength, len(normalizeUnicode(table) or str(table))) maxlength = max(maxlength, len(normalizeUnicode(table) or str(table)))
@ -193,7 +194,7 @@ class Dump:
self._write("+%s+" % lines) self._write("+%s+" % lines)
for table in tables: for table in tables:
if isinstance(table, (list, tuple, set)): if table and isListLike(table):
table = table[0] table = table[0]
blank = " " * (maxlength - len(normalizeUnicode(table) or str(table))) blank = " " * (maxlength - len(normalizeUnicode(table) or str(table)))

View File

@ -35,6 +35,7 @@ from lib.core.common import findPageForms
from lib.core.common import getConsoleWidth from lib.core.common import getConsoleWidth
from lib.core.common import getFileItems from lib.core.common import getFileItems
from lib.core.common import getFileType from lib.core.common import getFileType
from lib.core.common import isListLike
from lib.core.common import normalizePath from lib.core.common import normalizePath
from lib.core.common import ntToPosixSlashes from lib.core.common import ntToPosixSlashes
from lib.core.common import openFile from lib.core.common import openFile
@ -1630,7 +1631,7 @@ def __saveCmdline():
optionData.sort() optionData.sort()
for option, value, datatype in optionData: for option, value, datatype in optionData:
if isinstance(datatype, (list, tuple, set)): if datatype and isListLike(datatype):
datatype = datatype[0] datatype = datatype[0]
if value is None: if value is None:

View File

@ -17,6 +17,7 @@ from lib.core.common import getCurrentThreadData
from lib.core.common import getUnicode from lib.core.common import getUnicode
from lib.core.common import hashDBRetrieve from lib.core.common import hashDBRetrieve
from lib.core.common import hashDBWrite from lib.core.common import hashDBWrite
from lib.core.common import isListLike
from lib.core.data import conf from lib.core.data import conf
from lib.core.data import kb from lib.core.data import kb
from lib.core.data import logger from lib.core.data import logger
@ -61,7 +62,7 @@ def direct(query, content=True):
if not output: if not output:
return output return output
elif content: elif content:
if output and isinstance(output, (list, tuple)): if output and isListLike(output):
if len(output[0]) == 1: if len(output[0]) == 1:
if len(output) > 1: if len(output) > 1:
output = map(lambda _: _[0], output) output = map(lambda _: _[0], output)

View File

@ -23,6 +23,7 @@ from lib.core.common import hashDBRetrieve
from lib.core.common import hashDBWrite from lib.core.common import hashDBWrite
from lib.core.common import incrementCounter from lib.core.common import incrementCounter
from lib.core.common import initTechnique from lib.core.common import initTechnique
from lib.core.common import isListLike
from lib.core.common import isNumPosStrValue from lib.core.common import isNumPosStrValue
from lib.core.common import listToStrValue from lib.core.common import listToStrValue
from lib.core.common import readInput from lib.core.common import readInput
@ -367,7 +368,7 @@ def errorUse(expression, expected=None, dump=False):
if not outputs and not abortedFlag: if not outputs and not abortedFlag:
outputs = __errorFields(expression, expressionFields, expressionFieldsList) outputs = __errorFields(expression, expressionFields, expressionFieldsList)
if outputs and isinstance(outputs, list) and len(outputs) == 1 and isinstance(outputs[0], basestring): if outputs and isListLike(outputs) and len(outputs) == 1 and isinstance(outputs[0], basestring):
outputs = outputs[0] outputs = outputs[0]
duration = calculateDeltaSeconds(start) duration = calculateDeltaSeconds(start)

View File

@ -20,6 +20,7 @@ from lib.core.common import filterPairValues
from lib.core.common import getLimitRange from lib.core.common import getLimitRange
from lib.core.common import getUnicode from lib.core.common import getUnicode
from lib.core.common import isInferenceAvailable from lib.core.common import isInferenceAvailable
from lib.core.common import isListLike
from lib.core.common import isNoneValue from lib.core.common import isNoneValue
from lib.core.common import isNumPosStrValue from lib.core.common import isNumPosStrValue
from lib.core.common import isTechniqueAvailable from lib.core.common import isTechniqueAvailable
@ -879,7 +880,7 @@ class Enumeration:
if not isNoneValue(value): if not isNoneValue(value):
value = filter(None, arrayizeValue(value)) value = filter(None, arrayizeValue(value))
if len(value) > 0 and not isinstance(value[0], (list, tuple)): if len(value) > 0 and not isListLike(value[0]):
value = map(lambda x: (dbs[0], x), value) value = map(lambda x: (dbs[0], x), value)
for db, table in filterPairValues(value): for db, table in filterPairValues(value):
@ -1620,7 +1621,7 @@ class Enumeration:
entries = [] entries = []
elif isinstance(entries, basestring): elif isinstance(entries, basestring):
entries = [entries] entries = [entries]
elif not isinstance(entries, (list, tuple)): elif not isListLike(entries):
entries = [] entries = []
entriesCount = len(entries) entriesCount = len(entries)
@ -1630,7 +1631,7 @@ class Enumeration:
colLen = len(column) colLen = len(column)
if column not in kb.data.dumpedTable: if column not in kb.data.dumpedTable:
kb.data.dumpedTable[column] = {"length": colLen, "values": []} kb.data.dumpedTable[column] = {"length": colLen, "values": BigArray()}
for entry in entries: for entry in entries:
if entry is None or len(entry) == 0: if entry is None or len(entry) == 0:

View File

@ -15,6 +15,7 @@ from lib.core.agent import agent
from lib.core.common import dataToOutFile from lib.core.common import dataToOutFile
from lib.core.common import Backend from lib.core.common import Backend
from lib.core.common import isNumPosStrValue from lib.core.common import isNumPosStrValue
from lib.core.common import isListLike
from lib.core.common import isTechniqueAvailable from lib.core.common import isTechniqueAvailable
from lib.core.common import randomStr from lib.core.common import randomStr
from lib.core.common import readInput from lib.core.common import readInput
@ -213,11 +214,11 @@ class Filesystem:
self.cleanup(onlyFileTbl=True) self.cleanup(onlyFileTbl=True)
return return
elif isinstance(fileContent, (list, tuple, set)): elif isListLike(fileContent):
newFileContent = "" newFileContent = ""
for chunk in fileContent: for chunk in fileContent:
if isinstance(chunk, (list, tuple, set)): if isListLike(chunk):
if len(chunk) > 0: if len(chunk) > 0:
chunk = chunk[0] chunk = chunk[0]
else: else: