Use 'with open() as' to automaticly close() files

This commit is contained in:
cclauss
2016-04-26 15:46:14 +02:00
parent e6ac43b299
commit 5e08804cec
+23 -38
View File
@@ -174,12 +174,8 @@ def promptForExclusions():
if not settings["auto"]: if not settings["auto"]:
print ("OK, we'll only exclude domains in the whitelist.") print ("OK, we'll only exclude domains in the whitelist.")
def promptForMoreCustomExclusions(): def promptForMoreCustomExclusions(question="Do you have more domains you want to enter?"):
response = query_yes_no("Do you have more domains you want to enter?") return query_yes_no(question) == "yes"
if response == "yes":
return True
else:
return False
def promptForMove(finalFile): def promptForMove(finalFile):
@@ -212,7 +208,7 @@ def gatherCustomExclusions():
domainFromUser = myInput("Enter the domain you want to exclude (e.g. facebook.com): ") domainFromUser = myInput("Enter the domain you want to exclude (e.g. facebook.com): ")
if isValidDomainFormat(domainFromUser): if isValidDomainFormat(domainFromUser):
excludeDomain(domainFromUser) excludeDomain(domainFromUser)
if promptForMoreCustomExclusions() is False: if not promptForMoreCustomExclusions():
return return
def excludeDomain(domain): def excludeDomain(domain):
@@ -231,11 +227,7 @@ def updateAllSources():
allsources = list(set(settings["sources"]) | set(settings["extensionsources"])) allsources = list(set(settings["sources"]) | set(settings["extensionsources"]))
for source in allsources: for source in allsources:
if os.path.isdir(source): if os.path.isdir(source):
updateURLs = getUpdateURLsFromFile(source) for updateURL in getUpdateURLsFromFile(source):
if not len(updateURLs):
continue
for updateURL in updateURLs:
print ("Updating source " + os.path.basename(source) + " from " + updateURL) print ("Updating source " + os.path.basename(source) + " from " + updateURL)
# Cross-python call # Cross-python call
updatedFile = getFileByUrl(updateURL) updatedFile = getFileByUrl(updateURL)
@@ -266,28 +258,27 @@ def getUpdateURLsFromFile(source):
def getUpdateURLFromFile(source): def getUpdateURLFromFile(source):
pathToUpdateFile = os.path.join(settings["datapath"], source, settings["updateurlfilename"]) pathToUpdateFile = os.path.join(settings["datapath"], source, settings["updateurlfilename"])
if os.path.exists(pathToUpdateFile): if os.path.exists(pathToUpdateFile):
updateFile = open(pathToUpdateFile, "r") with open(pathToUpdateFile, "r") as updateFile:
retURL = updateFile.readline().strip() return updateFile.readline().strip()
updateFile.close() printFailure("Warning: Can't find the update file for source " + source + "\n" +
else: "Make sure that there's a file at " + pathToUpdateFile)
retURL = None return None
printFailure("Warning: Can't find the update file for source " + source + "\n" +
"Make sure that there's a file at " + pathToUpdateFile)
return retURL
# End Update Logic # End Update Logic
# File Logic # File Logic
def createInitialFile(): def createInitialFile():
mergeFile = tempfile.NamedTemporaryFile() mergeFile = tempfile.NamedTemporaryFile()
for source in settings["sources"]: for source in settings["sources"]:
curFile = open(os.path.join(settings["datapath"], source, settings["datafilenames"]), "r") filename = os.path.join(settings["datapath"], source, settings["datafilenames"])
#Done in a cross-python way with open(curFile, "r"):
writeData(mergeFile, curFile.read()) #Done in a cross-python way
writeData(mergeFile, curFile.read())
for source in settings["extensions"]: for source in settings["extensions"]:
curFile = open(os.path.join(settings["extensionspath"], source, settings["datafilenames"]), "r") filename = os.path.join(settings["extensionspath"], source, settings["datafilenames"])
#Done in a cross-python way with open(filename, "r") as curFile:
writeData(mergeFile, curFile.read()) #Done in a cross-python way
writeData(mergeFile, curFile.read())
return mergeFile return mergeFile
@@ -304,10 +295,8 @@ def removeDupsAndExcl(mergeFile):
os.makedirs(settings["outputpath"]) os.makedirs(settings["outputpath"])
# Another mode is required to read and write the file in Python 3 # Another mode is required to read and write the file in Python 3
if Python3: finalFile = open(os.path.join(settings["outputpath"], "hosts"),
finalFile = open(os.path.join(settings["outputpath"], "hosts"), "w+b") "w+b" if Python3 else "w+")
else:
finalFile = open(os.path.join(settings["outputpath"], "hosts"), "w+")
mergeFile.seek(0) # reset file pointer mergeFile.seek(0) # reset file pointer
hostnames = set() hostnames = set()
@@ -333,9 +322,7 @@ def removeDupsAndExcl(mergeFile):
continue continue
strippedRule = stripRule(line) #strip comments strippedRule = stripRule(line) #strip comments
if len(strippedRule) == 0: if not strippedRule or matchesExclusions(strippedRule):
continue
if matchesExclusions(strippedRule):
continue continue
hostname, normalizedRule = normalizeRule(strippedRule) # normalize rule hostname, normalizedRule = normalizeRule(strippedRule) # normalize rule
for exclude in exclusions: for exclude in exclusions:
@@ -358,7 +345,7 @@ def normalizeRule(rule):
if result: if result:
hostname, suffix = result.group(2,3) hostname, suffix = result.group(2,3)
hostname = hostname.lower().strip() # explicitly lowercase and trim the hostname hostname = hostname.lower().strip() # explicitly lowercase and trim the hostname
if suffix is not "": if suffix:
# add suffix as comment only, not as a separate host # add suffix as comment only, not as a separate host
return hostname, "%s %s #%s\n" % (settings["targetip"], hostname, suffix) return hostname, "%s %s #%s\n" % (settings["targetip"], hostname, suffix)
else: else:
@@ -419,10 +406,8 @@ def updateReadmeData():
if settings["extensions"]: if settings["extensions"]:
extensionsKey = "-".join(settings["extensions"]) extensionsKey = "-".join(settings["extensions"])
generationData = {} generationData = {"location": os.path.join(settings["outputsubfolder"], "")
generationData["location"] = os.path.join(settings["outputsubfolder"], "") "entries": settings["numberofrules"]}
generationData["entries"] = settings["numberofrules"]
settings["readmedata"][extensionsKey] = generationData settings["readmedata"][extensionsKey] = generationData
with open(settings["readmedatafilename"], "w") as f: with open(settings["readmedatafilename"], "w") as f:
json.dump(settings["readmedata"], f) json.dump(settings["readmedata"], f)