Skip to content

Commit f6509db

Browse files
committed
minor refactoring
1 parent 32f52cd commit f6509db

1 file changed

Lines changed: 31 additions & 37 deletions

File tree

‎lib/core/common.py‎

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -751,15 +751,12 @@ def dataToOutFile(data):
751751
if not data:
752752
return "No data retrieved"
753753

754-
rFile = filePathToString(conf.rFile)
755-
rFilePath = "%s%s%s" % (conf.filePath, os.sep, rFile)
756-
rFileFP = codecs.open(rFilePath, "wb")
754+
retVal = "%s%s%s" % (conf.filePath, os.sep, filePathToString(conf.rFile))
757755

758-
rFileFP.write(data)
759-
rFileFP.flush()
760-
rFileFP.close()
756+
with codecs.open(retVal, "wb") as f:
757+
f.write(data)
761758

762-
return rFilePath
759+
return retVal
763760

764761
def strToHex(value):
765762
"""
@@ -825,13 +822,13 @@ def randomStr(length=4, lowercase=False, alphabet=None):
825822
"""
826823

827824
if alphabet:
828-
rndStr = "".join(random.choice(alphabet) for _ in xrange(0, length))
825+
retVal = "".join(random.choice(alphabet) for _ in xrange(0, length))
829826
elif lowercase:
830-
rndStr = "".join(random.choice(string.lowercase) for _ in xrange(0, length))
827+
retVal = "".join(random.choice(string.lowercase) for _ in xrange(0, length))
831828
else:
832-
rndStr = "".join(random.choice(string.letters) for _ in xrange(0, length))
829+
retVal = "".join(random.choice(string.letters) for _ in xrange(0, length))
833830

834-
return rndStr
831+
return retVal
835832

836833
def sanitizeStr(value):
837834
"""
@@ -899,17 +896,17 @@ def parsePasswordHash(password):
899896
return password
900897

901898
def cleanQuery(query):
902-
upperQuery = query
899+
retVal = query
903900

904901
for sqlStatements in SQL_STATEMENTS.values():
905902
for sqlStatement in sqlStatements:
906903
sqlStatementEsc = sqlStatement.replace("(", "\\(")
907904
queryMatch = re.search("(%s)" % sqlStatementEsc, query, re.I)
908905

909906
if queryMatch and "sys_exec" not in query:
910-
upperQuery = upperQuery.replace(queryMatch.group(1), sqlStatement.upper())
907+
retVal = retVal.replace(queryMatch.group(1), sqlStatement.upper())
911908

912-
return upperQuery
909+
return retVal
913910

914911
def setPaths():
915912
"""
@@ -1247,6 +1244,7 @@ def parseFilePaths(page):
12471244

12481245
def getLocalIP():
12491246
retVal = None
1247+
12501248
try:
12511249
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
12521250
s.connect((conf.hostname, conf.port))
@@ -1309,41 +1307,40 @@ def getCharset(charsetType=None):
13091307
return asciiTbl
13101308

13111309
def searchEnvPath(filename):
1312-
result = None
1310+
retVal = None
13131311
path = os.environ.get("PATH", "")
13141312
paths = path.split(";") if IS_WIN else path.split(":")
13151313

13161314
for _ in paths:
13171315
_ = _.replace(";", "")
1318-
result = os.path.exists(os.path.normpath(os.path.join(_, filename)))
1316+
retVal = os.path.exists(os.path.normpath(os.path.join(_, filename)))
13191317

1320-
if result:
1318+
if retVal:
13211319
break
13221320

1323-
return result
1321+
return retVal
13241322

13251323
def urlEncodeCookieValues(cookieStr):
13261324
if cookieStr:
1327-
result = ""
1325+
retVal = ""
13281326

13291327
for part in cookieStr.split(';'):
13301328
index = part.find('=') + 1
1331-
13321329
if index > 0:
13331330
name = part[:index - 1].strip()
13341331
value = urlencode(part[index:], convall=True)
1335-
result += "; %s=%s" % (name, value)
1332+
retVal += "; %s=%s" % (name, value)
13361333
elif part.strip().lower() != "secure":
1337-
result += "%s%s" % ("%3B", urlencode(part, convall=True))
1334+
retVal += "%s%s" % ("%3B", urlencode(part, convall=True))
13381335
else:
1339-
result += "; secure"
1336+
retVal += "; secure"
13401337

1341-
if result.startswith('; '):
1342-
result = result[2:]
1343-
elif result.startswith('%3B'):
1344-
result = result[3:]
1338+
if retVal.startswith('; '):
1339+
retVal = retVal[2:]
1340+
elif retVal.startswith('%3B'):
1341+
retVal = retVal[3:]
13451342

1346-
return result
1343+
return retVal
13471344
else:
13481345
return None
13491346

@@ -1352,8 +1349,6 @@ def directoryPath(filepath):
13521349
Returns directory path for a given filepath
13531350
"""
13541351

1355-
retVal = None
1356-
13571352
if isWindowsDriveLetterPath(filepath):
13581353
retVal = ntpath.dirname(filepath)
13591354
else:
@@ -1366,8 +1361,6 @@ def normalizePath(filepath):
13661361
Returns normalized string representation of a given filepath
13671362
"""
13681363

1369-
retVal = None
1370-
13711364
if isWindowsDriveLetterPath(filepath):
13721365
retVal = ntpath.normpath(filepath)
13731366
else:
@@ -1487,8 +1480,8 @@ def __del__():
14871480
def decloakToMkstemp(filepath, **kwargs):
14881481
handle, name = mkstemp(**kwargs)
14891482

1490-
fptr = os.fdopen(handle)
1491-
fptr.close() # close low level handle (causing problems latter)
1483+
_ = os.fdopen(handle)
1484+
_.close() # close low level handle (causing problems latter)
14921485

14931486
retVal = open(name, 'w+b')
14941487

@@ -1598,9 +1591,8 @@ def parseXmlFile(xmlFile, handler):
15981591
Parses XML file by a given handler
15991592
"""
16001593

1601-
stream = StringIO(readCachedFileContent(xmlFile))
1602-
parse(stream, handler)
1603-
stream.close()
1594+
with StringIO(readCachedFileContent(xmlFile)) as stream:
1595+
parse(stream, handler)
16041596

16051597
def getSPQLSnippet(dbms, name, **variables):
16061598
"""
@@ -2270,8 +2262,10 @@ def filterStringValue(value, regex, replacement=""):
22702262
"""
22712263

22722264
retVal = value
2265+
22732266
if value:
22742267
retVal = re.sub(regex.replace("[", "[^") if "[^" not in regex else regex.replace("[^", "["), replacement, value)
2268+
22752269
return retVal
22762270

22772271
def filterControlChars(value):

0 commit comments

Comments
 (0)