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 += " "
if checkBatch and conf.batch:
if isinstance(default, (list, tuple, set)):
if isListLike(default):
options = ",".join(getUnicode(opt, UNICODE_ENCODING) for opt in default)
elif default:
options = getUnicode(default, UNICODE_ENCODING)
@ -1888,7 +1888,7 @@ def getUnicode(value, encoding=None, system=False, noneToNull=False):
if noneToNull and value is None:
return NULL
if isinstance(value, (list, tuple)):
if isListLike(value):
value = list(getUnicode(_, encoding, system, noneToNull) for _ in 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
"""
if not isinstance(value, (list, tuple)):
if not isListLike(value):
value = [value]
return value
@ -2435,7 +2435,7 @@ def unArrayizeValue(value):
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
return value
@ -2446,12 +2446,19 @@ def flattenValue(value):
"""
for i in iter(value):
if isinstance(i, (list, tuple)):
if isListLike(i):
for j in flattenValue(i):
yield j
else:
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():
"""
Returns prioritized test list by eventually detected DBMS from error
@ -2784,7 +2791,7 @@ def isNoneValue(value):
if isinstance(value, basestring):
return value in ("None", "")
elif isinstance(value, (list, tuple)):
elif isListLike(value):
return all(isNoneValue(_) for _ in value)
elif isinstance(value, dict):
return not any(value)
@ -3127,7 +3134,7 @@ def applyFunctionRecursively(value, function):
Applies function recursively through list-like structures
"""
if isinstance(value, (list, tuple, set, BigArray)):
if isListLike(value):
retVal = [applyFunctionRecursively(_, function) for _ in value]
else:
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 dataToStdout
from lib.core.common import getUnicode
from lib.core.common import isListLike
from lib.core.common import normalizeUnicode
from lib.core.common import openFile
from lib.core.common import prioritySortColumns
@ -72,7 +73,7 @@ class Dump:
return self._outputFile
def string(self, header, data, sort=True):
if isinstance(data, (list, tuple, set)):
if isListLike(data):
self.lister(header, data, sort)
elif data:
data = self._formatString(getUnicode(data))
@ -102,7 +103,7 @@ class Dump:
for element in elements:
if isinstance(element, basestring):
self._write("[*] %s" % element)
elif isinstance(element, (list, tuple, set)):
elif isListLike(element):
self._write("[*] " + ", ".join(getUnicode(e) for e in element))
if elements:
@ -173,7 +174,7 @@ class Dump:
for tables in dbTables.values():
for table in tables:
if isinstance(table, (list, tuple, set)):
if table and isListLike(table):
table = table[0]
maxlength = max(maxlength, len(normalizeUnicode(table) or str(table)))
@ -193,7 +194,7 @@ class Dump:
self._write("+%s+" % lines)
for table in tables:
if isinstance(table, (list, tuple, set)):
if table and isListLike(table):
table = table[0]
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 getFileItems
from lib.core.common import getFileType
from lib.core.common import isListLike
from lib.core.common import normalizePath
from lib.core.common import ntToPosixSlashes
from lib.core.common import openFile
@ -1630,7 +1631,7 @@ def __saveCmdline():
optionData.sort()
for option, value, datatype in optionData:
if isinstance(datatype, (list, tuple, set)):
if datatype and isListLike(datatype):
datatype = datatype[0]
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 hashDBRetrieve
from lib.core.common import hashDBWrite
from lib.core.common import isListLike
from lib.core.data import conf
from lib.core.data import kb
from lib.core.data import logger
@ -61,7 +62,7 @@ def direct(query, content=True):
if not output:
return output
elif content:
if output and isinstance(output, (list, tuple)):
if output and isListLike(output):
if len(output[0]) == 1:
if len(output) > 1:
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 incrementCounter
from lib.core.common import initTechnique
from lib.core.common import isListLike
from lib.core.common import isNumPosStrValue
from lib.core.common import listToStrValue
from lib.core.common import readInput
@ -367,7 +368,7 @@ def errorUse(expression, expected=None, dump=False):
if not outputs and not abortedFlag:
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]
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 getUnicode
from lib.core.common import isInferenceAvailable
from lib.core.common import isListLike
from lib.core.common import isNoneValue
from lib.core.common import isNumPosStrValue
from lib.core.common import isTechniqueAvailable
@ -879,7 +880,7 @@ class Enumeration:
if not isNoneValue(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)
for db, table in filterPairValues(value):
@ -1620,7 +1621,7 @@ class Enumeration:
entries = []
elif isinstance(entries, basestring):
entries = [entries]
elif not isinstance(entries, (list, tuple)):
elif not isListLike(entries):
entries = []
entriesCount = len(entries)
@ -1630,7 +1631,7 @@ class Enumeration:
colLen = len(column)
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:
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 Backend
from lib.core.common import isNumPosStrValue
from lib.core.common import isListLike
from lib.core.common import isTechniqueAvailable
from lib.core.common import randomStr
from lib.core.common import readInput
@ -213,11 +214,11 @@ class Filesystem:
self.cleanup(onlyFileTbl=True)
return
elif isinstance(fileContent, (list, tuple, set)):
elif isListLike(fileContent):
newFileContent = ""
for chunk in fileContent:
if isinstance(chunk, (list, tuple, set)):
if isListLike(chunk):
if len(chunk) > 0:
chunk = chunk[0]
else: