Рефакторинг: работа с файлами

develop
parent e4fa7c25b2
commit dd56b3dab0

@ -1211,11 +1211,11 @@ class PartitionDistributive(Distributive):
if not format in self.format_map:
raise DistributiveError(
_("The specified format of '%s' is not supported") % format)
if dev in map(lambda y: y.split(" ")[0],
filter(lambda x: x.startswith("/"),
open("/proc/swaps"))):
raise DistributiveError(
_("Failed to format %s: this partition is used as swap") % dev)
with open("/proc/swaps") as f:
if dev in map(lambda y: y.split(" ")[0],
filter(lambda x: x.startswith("/"),f)):
raise DistributiveError(
_("Failed to format %s: this partition is used as swap") % dev)
self._checkMount(dev)
if not os.access(dev, os.W_OK):
raise DistributiveError(_("Failed to format the partition") +
@ -1289,12 +1289,13 @@ class PartitionDistributive(Distributive):
def formatSwapPartition(self, dev):
"""Format swap partition"""
if dev in map(lambda y: y.split(" ")[0],
filter(lambda x: x.startswith("/"),
open("/proc/swaps"))):
raise DistributiveError(
_("Failed to execute 'mkswap %s': the swap partition is used "
"by the current system") % dev)
with open("/proc/swaps") as f:
if dev in map(lambda y: y.split(" ")[0],
filter(lambda x: x.startswith("/"),f)):
raise DistributiveError(
_("Failed to execute 'mkswap %s': "
"the swap partition is used "
"by the current system") % dev)
if isMount(dev):
raise DistributiveError(
_("Failed to format %s: this partition is mounted") % dev)

@ -547,7 +547,8 @@ class Install(MethodsInterface):
new_nvidia_mask = self.clVars.Get('os_nvidia_mask')
if new_nvidia_mask == current_nvidia_mask:
return True
open(nvidia_mask_file, 'w').write(new_nvidia_mask)
with open(nvidia_mask_file, 'w') as f:
f.write(new_nvidia_mask)
return True
def changeScheduler(self, scheduler):

@ -34,10 +34,10 @@ class _shareData(object):
def getDataInFile(self, fileName='', lenData=7):
"""Get data list from file"""
return map(lambda x: x[:lenData],
filter(lambda x: len(x) >= lenData,
map(lambda x: x.rstrip().split(":"),
open(fileName))))
with open(fileName) as f:
return map(lambda x: x[:lenData],
filter(lambda x: len(x) >= lenData,
map(lambda x: x.rstrip().split(":"), f)))
class migrateGroups(_shareData):

@ -480,9 +480,9 @@ class VariableClMigrateUserPwd(UserHelper, Variable):
migrateusers = self.Get("cl_migrate_user")
if migrateusers:
lenData = 9
shadowData = filter(lambda x: len(x) == lenData,
map(lambda x: x.rstrip().split(":"),
open(fileName)))
with open(fileName) as f:
shadowData = filter(lambda x: len(x) == lenData,
map(lambda x: x.rstrip().split(":"), f))
shadowData = filter(lambda x: x[0] in migrateusers, shadowData)
shadowData = map(lambda x: (x[0], x[1]), shadowData)
shadowUsers = map(lambda x: x[0], shadowData)

Loading…
Cancel
Save