mirror of
https://github.com/nmap/nmap.git
synced 2025-12-07 21:21:31 +00:00
Simplify config reading/writing. Related #449
This commit is contained in:
@@ -253,14 +253,16 @@ class Profile(UmitConfigParser, object):
|
|||||||
def __init__(self, user_profile=None, *args):
|
def __init__(self, user_profile=None, *args):
|
||||||
UmitConfigParser.__init__(self, *args)
|
UmitConfigParser.__init__(self, *args)
|
||||||
|
|
||||||
|
try:
|
||||||
if not user_profile:
|
if not user_profile:
|
||||||
user_profile = Path.scan_profile
|
user_profile = Path.scan_profile
|
||||||
|
|
||||||
fconf = open(user_profile, 'r')
|
self.read(user_profile)
|
||||||
self.readfp(fconf, user_profile)
|
except Exception as e:
|
||||||
|
# No scan profiles found is not a reason to crash.
|
||||||
fconf.close()
|
self.add_profile("Profiles not found",
|
||||||
del(fconf)
|
command="nmap",
|
||||||
|
description="The {} file was not found".format(user_profile))
|
||||||
|
|
||||||
self.attributes = {}
|
self.attributes = {}
|
||||||
|
|
||||||
|
|||||||
@@ -126,10 +126,9 @@ from zenmapCore.UmitLogging import log
|
|||||||
|
|
||||||
|
|
||||||
class UmitConfigParser(ConfigParser):
|
class UmitConfigParser(ConfigParser):
|
||||||
filenames = None
|
|
||||||
fp = None
|
|
||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
|
self.filenames = None
|
||||||
ConfigParser.__init__(self, *args)
|
ConfigParser.__init__(self, *args)
|
||||||
|
|
||||||
def set(self, section, option, value):
|
def set(self, section, option, value):
|
||||||
@@ -142,31 +141,17 @@ class UmitConfigParser(ConfigParser):
|
|||||||
def read(self, filename):
|
def read(self, filename):
|
||||||
log.debug(">>> Trying to parse: %s" % filename)
|
log.debug(">>> Trying to parse: %s" % filename)
|
||||||
|
|
||||||
self.filenames = ConfigParser.read(self, filename)
|
if ConfigParser.read(self, filename):
|
||||||
return self.filenames
|
|
||||||
|
|
||||||
def readfp(self, fp, filename=None):
|
|
||||||
ConfigParser.readfp(self, fp, filename)
|
|
||||||
self.fp = fp
|
|
||||||
self.filenames = filename
|
self.filenames = filename
|
||||||
|
|
||||||
|
return self.filenames
|
||||||
|
|
||||||
def save_changes(self):
|
def save_changes(self):
|
||||||
if self.filenames:
|
if self.filenames:
|
||||||
filename = None
|
log.debug("saving to %s" % self.filenames)
|
||||||
if isinstance(self.filenames, basestring):
|
self.write(open(self.filenames, 'w'))
|
||||||
filename = self.filenames
|
|
||||||
elif isinstance(self.filenames, list):
|
|
||||||
if len(self.filenames) == 1:
|
|
||||||
filename = self.filenames[0]
|
|
||||||
else:
|
else:
|
||||||
raise ValueError("UmitConfigParser can't handle a list "
|
log.debug(">>> UmitConfigParser can't save changes: no filename")
|
||||||
"of filenames: %s" % self.filenames)
|
|
||||||
else:
|
|
||||||
raise ValueError("UmitConfigParser can't handle a filename of "
|
|
||||||
"type %s: %s" % (type(self.filenames), self.filenames))
|
|
||||||
self.write(open(filename, 'w'))
|
|
||||||
elif self.fp:
|
|
||||||
self.write(self.fp)
|
|
||||||
|
|
||||||
def write(self, fp):
|
def write(self, fp):
|
||||||
'''Write alphabetically sorted config files'''
|
'''Write alphabetically sorted config files'''
|
||||||
|
|||||||
Reference in New Issue
Block a user