Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def load_config(UserConfig=None):
parser = configparser.ConfigParser(default_section='default')
DefaultConfigs = sorted(glob.glob(sys.prefix + '/share/pybugz.d/*.conf'))
SystemConfigs = sorted(glob.glob('/etc/pybugz.d/*.conf'))
if UserConfig is not None:
UserConfig = os.path.expanduser(UserConfig)
else:
UserConfig = os.path.expanduser('~/.bugzrc')
try:
parser.read(DefaultConfigs + SystemConfigs + [UserConfig])
except configparser.DuplicateOptionError as error:
log_error(error)
sys.exit(1)
except configparser.DuplicateSectionError as error:
log_error(error)
sys.exit(1)
except configparser.MissingSectionHeaderError as error:
log_error(error)
sys.exit(1)
except configparser.ParsingError as error:
log_error(error)
sys.exit(1)
return parser
def __init__(self, section, configfile):
self._configfile = configfile
self._section = section
self._config = configparser.RawConfigParser()
try:
self._config.read(self._configfile)
self._config.has_section(
self._section) or self._set_default_config(self._section)
self.__config__ = self._read_config(self._section)
except configparser.DuplicateSectionError:
print(u'Doppelte Sektion in der Konfigurationsdatei.')
raise
except:
print(u'Ein unbekannter Fehler in der Konfigurationsdatei ist aufgetreten.')
raise
# write date the file
f = open( "%s/agents.ini" % Settings.getDirExec() , 'w')
self.configsFile.write(f)
f.close()
# notify all admin and tester
notif = ( 'agents-default', ( 'add', self.getDefaultAgents() ) )
ESI.instance().notifyByUserTypes(body = notif,
admin=True,
monitor=False,
tester=True)
# return OK
ret = self.context.CODE_OK
except ConfigParser.DuplicateSectionError:
self.error( "agent already exist %s" % str(aName) )
ret = self.context.CODE_ALLREADY_EXISTS
except Exception as e:
self.error( "unable to add default agent: %s" % str(e) )
ret = self.context.CODE_FAILED
return ret
if not isinstance(items, dict):
if isinstance(items, list):
items = ','.join(to_text(i) for i in items)
self._option_write(
config,
'DEFAULT',
section,
items
)
else:
# Attempt to add a section to the config file passing if
# an error is raised that is related to the section
# already existing.
try:
config.add_section(section)
except (ConfigParser.DuplicateSectionError, ValueError):
pass
for key, value in items.items():
try:
self._option_write(config, section, key, value)
except ConfigParser.NoSectionError as exp:
error_msg = str(exp)
error_msg += (
' Try being more explicit with your override'
'data. Sections are case sensitive.'
)
raise errors.AnsibleModuleError(error_msg)
else:
config_object.close()
config_dict_new = {}
config_defaults = config.defaults()
def assertSection(self, section):
""" Add section if not present.
"""
try:
self.add_section(section)
except configparser.DuplicateSectionError:
pass
def add_section( self, sectionname:str ) -> None:
# We do not care if the section already exists
try:
CONFIG.add_section( sectionname )
except configparser.DuplicateSectionError:
pass
def set_str(self, section_path, value):
try:
section, option = section_path
except ValueError:
raise KeyError(section_path)
try:
self._cfg.add_section(section)
except configparser.DuplicateSectionError:
pass
self._cfg.set(section, option, value)
self._dirty = True
def assertSection(self, section):
"""Add section, if not present"""
try:
self.add_section(section)
except configparser.DuplicateSectionError:
pass
def add_backend(self, backend_name, module_name, params):
"""
Add a backend to config.
:param backend_name: name of the backend in config
:param module_name: name of the Python submodule to run
:param params: params to pass to the module
:type params: :class:`dict`
"""
if not backend_name:
raise ValueError(u'Please give a name to the configured backend.')
config = self._read_config()
try:
config.add_section(backend_name)
except DuplicateSectionError:
raise BackendAlreadyExists(backend_name)
config.set(backend_name, '_module', module_name)
for key, value in params.items():
config.set(backend_name, key, value)
self._write_config(config)
def add_section(self, section):
"""Create a new section in the configuration.
Raise DuplicateSectionError if a section by the specified name
already exists. Raise ValueError if name is DEFAULT.
Args:
section (str or :class:`Section`): name or Section type
"""
if section in self.sections():
raise DuplicateSectionError(section)
if isinstance(section, str):
# create a new section
section = Section(section, container=self)
elif not isinstance(section, Section):
raise ValueError("Parameter must be a string or Section type!")
self._structure.append(section)