def get_configuration(configuration=None): """ Parse a configuration file Parameters ---------- configuration : str or list, optional A configuration file or list of configuration files to parse, defaults to the deploy_default.conf file in the package and deploy.conf in the current working directory. Returns ------- configparser The parsed configuration """ if not configuration: # pragma: no cover configuration = [ # Config file that is part of the package # PACKAGE_DEFAULT_CONFIG, # Any deploy.conf files in the current directory 'deploy.conf' ] config = ConfigParser() # Set the config defaults try: config.read_string(config_defaults()) except AttributeError: config.readfp(io.BytesIO(config_defaults())) logger.debug('Working with default dict: %r', config_defaults()) config.read(configuration) return config
def process(self): nm_config = NetworkManagerConfig() try: # Use 'NM --print-config' to read the configurationo so # that the main configuration file and other files in # various directories get merged in the right way. r = run(['NetworkManager', '--print-config'], split=False)['stdout'] except (OSError, CalledProcessError) as e: self.log.warning( 'Error reading NetworkManager configuration: {}'.format(e)) return parser = ConfigParser() try: if hasattr(parser, 'read_string'): # Python 3 parser.read_string(r) else: # Python 2 from cStringIO import StringIO parser.readfp(StringIO(r)) except ParsingError as e: self.log.warning( 'Error parsing NetworkManager configuration: {}'.format(e)) return if parser.has_option('main', 'dhcp'): nm_config.dhcp = parser.get("main", "dhcp") self.produce(nm_config)
def get_configuration_dict(configuration=None, value_types=None): """ Parse the configuration files Parameters ---------- configuration : str or list, optional A configuration file or list of configuration files to parse, defaults to the deploy_default.conf file in the package and deploy.conf in the current working directory. value_types : dict, optional Dictionary containing classes to apply to specific items Returns ------- dict Configuration dictionary """ if not value_types: # pragma: no cover value_types = config_types() if configuration is None or configuration is '': # pragma: no cover configuration = [ # Config file that is part of the package # PACKAGE_DEFAULT_CONFIG, # Any deploy.conf files in the current directory 'deploy.conf' ] config = ConfigParser() # Set the config defaults try: config.read_string(config_defaults()) except AttributeError: config.readfp(io.BytesIO(config_defaults())) logger.debug('Working with default dict: %r', config_defaults()) config.read(configuration) result_dict = {} for section in config.sections(): result_dict[section] = {} for key, val in config.items(section): result_dict[section][key] = str_format_env(val) config_update(result_dict) if 'locations' not in result_dict.keys(): result_dict['locations'] = {} result_dict['locations']['package_scripts'] = package_scripts_directory() if not result_dict['global'].get('virtualenv_dir', None): result_dict['global']['virtualenv_dir'] = \ default_virtualenv_directory() cast_types(result_dict) return result_dict
def loadManifest(data): if six.PY2: parser = ConfigParser(None, multidict) parser.readfp(six.StringIO(data)) else: if isinstance(data, six.binary_type): data = data.decode() parser = ConfigParser(dict_type=multidict, strict=False) parser.read_string(data) return parser
def parse_nm_config(cfg): parser = ConfigParser() try: if hasattr(parser, 'read_string'): # Python 3 parser.read_string(cfg) else: # Python 2 from cStringIO import StringIO parser.readfp(StringIO(cfg)) return parser except (ParsingError, TypeError) as e: api.current_logger().warning( 'Error parsing NetworkManager configuration: {}'.format(e)) return None
def pretty_format_ini(argv=None): # type: (typing.Optional[typing.List[typing.Text]]) -> int parser = argparse.ArgumentParser() parser.add_argument( "--autofix", action="store_true", dest="autofix", help="Automatically fixes encountered not-pretty-formatted files", ) parser.add_argument("filenames", nargs="*", help="Filenames to fix") args = parser.parse_args(argv) status = 0 for ini_file in set(args.filenames): with open(ini_file) as input_file: string_content = "".join(input_file.readlines()) config_parser = ConfigParser() try: if PY3: # pragma: no cover # py3+ only config_parser.read_string(string_content) else: # pragma: no cover # py27 only config_parser.readfp(StringIO(str(string_content))) pretty_content = StringIO() config_parser.write(pretty_content) pretty_content_str = remove_trailing_whitespaces_and_set_new_line_ending( pretty_content.getvalue(), ) if string_content != pretty_content_str: print("File {} is not pretty-formatted".format(ini_file)) if args.autofix: print("Fixing file {}".format(ini_file)) with io.open(ini_file, "w", encoding="UTF-8") as output_file: output_file.write(text_type(pretty_content_str)) status = 1 except Error: print("Input File {} is not a valid INI file".format(ini_file)) return 1 return status
def pretty_format_ini(argv=None): parser = argparse.ArgumentParser() parser.add_argument( '--autofix', action='store_true', dest='autofix', help='Automatically fixes encountered not-pretty-formatted files', ) parser.add_argument('filenames', nargs='*', help='Filenames to fix') args = parser.parse_args(argv) status = 0 for ini_file in set(args.filenames): with open(ini_file) as f: string_content = ''.join(f.readlines()) config_parser = ConfigParser() try: if PY3: # pragma: no cover # py3+ only config_parser.read_string(string_content) else: # pragma: no cover # py27 only config_parser.readfp(StringIO(string_content)) pretty_content = StringIO() config_parser.write(pretty_content) pretty_content_str = remove_trailing_whitespaces_and_set_new_line_ending( pretty_content.getvalue(), ) if string_content != pretty_content_str: print('File {} is not pretty-formatted'.format(ini_file)) if args.autofix: print('Fixing file {}'.format(ini_file)) with io.open(ini_file, 'w', encoding='UTF-8') as f: f.write(text_type(pretty_content_str)) status = 1 except Error: print('Input File {} is not a valid INI file'.format(ini_file)) return 1 return status
def getPluginSettings(themeDirectory, plugins=None): """Given an IResourceDirectory for a theme, return the settings for the given list of plugins (or all plugins, if not given) provided as a list of (name, plugin) pairs. Returns a dict of dicts, with the outer dict having plugin names as keys and containing plugins settings (key/value pairs) as values. """ if plugins is None: plugins = getPlugins() # noinspection PyPep8Naming manifestContents = {} if themeDirectory.isFile(MANIFEST_FILENAME): parser = ConfigParser() fp = themeDirectory.openFile(MANIFEST_FILENAME) try: if six.PY2: parser.readfp(fp) else: parser.read_string(fp.read().decode()) for section in parser.sections(): manifestContents[section] = {} for name, value in parser.items(section): manifestContents[section][name] = value finally: try: fp.close() except AttributeError: pass pluginSettings = {} for name, plugin in plugins: pluginSettings[name] = manifestContents.get( "%s:%s" % (THEME_RESOURCE_NAME, name), {}) # noqa return pluginSettings
def getLayoutsFromManifest(fp, _format, directory_name): # support multiple sections with the same name in manifest.cfg if six.PY2: parser = ConfigParser(None, multidict) parser.readfp(fp) else: data = fp.read() if isinstance(data, six.binary_type): data = data.decode() parser = ConfigParser(dict_type=multidict, strict=False) parser.read_string(data) layouts = {} for section in parser.sections(): if not section.startswith(_format.resourceType) or \ ':variants' in section: continue # id is a combination of directory name + filename if parser.has_option(section, 'file'): filename = parser.get(section, 'file') else: filename = '' # this should not happen... _id = directory_name + '/' + filename if _id in layouts: # because TTW resources are created first, we consider layouts # with same id in a TTW to be taken before other resources continue data = { 'directory': directory_name } for key in _format.keys: if parser.has_option(section, key): data[key] = parser.get(section, key) else: data[key] = _format.defaults.get(key, None) layouts[_id] = data return layouts
def _parse_linfo(self, linfo_file): cp = ConfigParser() try: if six.PY2: cp.readfp(linfo_file) else: cp.read_string(linfo_file.read().decode()) except ParsingError as e: logging.exception('Exception reading linfo file: %s', e) return section = 'Activity' if cp.has_option(section, 'name'): self._name = cp.get(section, 'name') if cp.has_option(section, 'summary'): self._summary = cp.get(section, 'summary') if cp.has_option(section, 'tags'): tag_list = cp.get(section, 'tags').strip(';') self._tags = [tag.strip() for tag in tag_list.split(';')]
def _parse_info(self, info_file): cp = ConfigParser() if six.PY2: cp.readfp(info_file) else: cp.read_string(info_file.read().decode()) section = 'Activity' if cp.has_option(section, 'bundle_id'): self._bundle_id = cp.get(section, 'bundle_id') else: if cp.has_option(section, 'service_name'): self._bundle_id = cp.get(section, 'service_name') logging.error('ATTENTION: service_name property in the ' 'activity.info file is deprecated, should be ' 'changed to bundle_id') else: raise MalformedBundleException( 'Activity bundle %s does not specify a bundle_id' % self.get_path()) if ' ' in self._bundle_id: raise MalformedBundleException('Space in bundle_id') if cp.has_option(section, 'name'): self._name = cp.get(section, 'name') else: raise MalformedBundleException( 'Activity bundle %s does not specify a name' % self.get_path()) if cp.has_option(section, 'exec'): self.bundle_exec = cp.get(section, 'exec') else: if cp.has_option(section, 'class'): self.bundle_exec = 'sugar-activity ' + cp.get(section, 'class') logging.error('ATTENTION: class property in the ' 'activity.info file is deprecated, should be ' 'changed to exec') else: raise MalformedBundleException( 'Activity bundle %s must specify exec' % self.get_path()) if cp.has_option(section, 'mime_types'): mime_list = cp.get(section, 'mime_types').strip(';') self._mime_types = [mime.strip() for mime in mime_list.split(';')] if cp.has_option(section, 'show_launcher'): if cp.get(section, 'show_launcher') == 'no': self._show_launcher = False if cp.has_option(section, 'tags'): tag_list = cp.get(section, 'tags').strip(';') self._tags = [tag.strip() for tag in tag_list.split(';')] if cp.has_option(section, 'icon'): self._icon = cp.get(section, 'icon') else: logging.warning('Activity bundle %s does not specify an icon' % self.get_path()) if cp.has_option(section, 'activity_version'): version = cp.get(section, 'activity_version') try: NormalizedVersion(version) except InvalidVersionError: raise MalformedBundleException( 'Activity bundle %s has invalid version number %s' % (self.get_path(), version)) self._activity_version = version else: logging.warning( 'Activity bundle %s does not specify an activity_version, ' 'assuming %s' % (self.get_path(), self._activity_version)) if cp.has_option(section, 'summary'): self._summary = cp.get(section, 'summary') if cp.has_option(section, 'description'): self._description = cp.get(section, 'description') if cp.has_option(section, 'single_instance'): if cp.get(section, 'single_instance') == 'yes': self._single_instance = True if cp.has_option(section, 'max_participants'): max_participants = cp.get(section, 'max_participants') try: self._max_participants = int(max_participants) except ValueError: raise MalformedBundleException( 'Activity bundle %s has invalid max_participants %s' % (self.get_path(), max_participants)) if not cp.has_option(section, 'license'): logging.warning('Activity bundle %s does not specify a license' % self.get_path())
def run(self, task): host = task.vars["HOST"] # if homeDir is a relative path it will be relative to the baseDir of the host instance # which might be different from the current directory if host is an external instance confDir = abspath(host.context, host["homeDir"]) conf = host["conf"] name = task.vars["SELF"]["name"] confPath = os.path.join(confDir, "programs", name + ".conf") if six.PY3: parser = ConfigParser(inline_comment_prefixes=(";", "#"), strict=False) parser.read_string(conf) else: parser = ConfigParser() parser.readfp(six.StringIO(conf)) serverConfig = dict(parser.items("supervisorctl", vars=dict(here=confDir))) serverConfig.pop("here", None) server = getServerProxy(**serverConfig) error = None op = task.configSpec.operation modified = False try: if op == "start": server.supervisor.startProcess(name) modified = True elif op == "stop": server.supervisor.stopProcess(name) modified = True elif op == "delete": if os.path.exists(confPath): os.remove(confPath) modified = _reloadConfig(server, name) elif op == "configure": program = task.vars["SELF"]["program"] programDir = os.path.dirname(confPath) task.logger.debug("writing %s", confPath) if not os.path.isdir(programDir): os.makedirs(programDir) with open(confPath, "w") as conff: conf = "[program:%s]\n" % name conf += "\n".join("%s= %s" % (k, v) for k, v in program.items()) if "environment" not in program: conf += "\nenvironment= " conf += ",".join( '%s="%s"' % (k, v.replace("%", "%%")) for (k, v) in task.getEnvironment(True).items() ) conff.write(conf) modified = _reloadConfig(server, name) server.supervisor.addProcessGroup(name) except Fault as err: if ( not (op == "start" and err.faultCode == 60) # ok, 60 == ALREADY_STARTED and not (op == "stop" and err.faultCode == 70) # ok, 70 == NOT_RUNNING and not ( # ok, 90 == ALREADY_ADDED op == "configure" and err.faultCode == 90 ) ): error = "supervisor error: " + str(err) yield task.done(success=not error, modified=modified, result=error)