Ejemplo n.º 1
0
 def save(self):
     """
     Save the config object in config file
     """
     if self.config_file == None:
         raise InvalidOperation('save')
     logger.debug('Saving config in config file')
     with open(self.config_file, 'w') as configfile:
         self.config_parser.write(configfile)
     self.config_file = None
Ejemplo n.º 2
0
    def list(self):
        """
        List config sections

        :return: list of projects (sections in config)
        :rtype: list
        """
        if self.config_file == None:
            raise InvalidOperation('list')
        return self.config_parser.sections()
Ejemplo n.º 3
0
    def has_project_enabled(self, project):
        """
        Check if a project is enabled

        :param project: project name
        :return: True if project is enabled, False if not
        :rtype: boolean
        """
        if self.config_file == None:
            raise InvalidOperation('has_project_enabled')
        return True if project in self.list_enabled_projects() else False
Ejemplo n.º 4
0
    def has_project(self, project):
        """
        Check if a project (a section in config) is present

        :param project: section name
        :return: True if section exists, False if not
        :rtype: boolean
        """
        if self.config_file == None:
            raise InvalidOperation('has_project')
        return self.config_parser.has_section(project)
Ejemplo n.º 5
0
    def remove_project(self, project):
        """
        Remove a project (config section in config object)

        :param project: section name
        :return: True if section is removed, False if not exist
        :rtype: boolean
        """
        if self.config_file == None:
            raise InvalidOperation('remove')
        logger.debug('Removing {}'.format(project))
        return self.config_parser.remove_section(project)
Ejemplo n.º 6
0
    def list_enabled_projects(self):
        """
        Get the list of enabled projects

        :return: list of enabled projects
        :rtype: list
        """
        if self.config_file == None:
            raise InvalidOperation('list_enabled_projects')
        try:
            return self.config_parser.get('DEFAULT', 'sync_projects').split()
        except NoOptionError:
            return []
Ejemplo n.º 7
0
    def enable_project(self, project):
        """
        Enable a project adding it to sync_projects

        :param project: project name
        """
        if self.config_file == None:
            raise InvalidOperation('enable_project')
        logger.debug('Enabling project {}'.format(project))
        enabled_projects = self.list_enabled_projects()
        enabled_projects.append(project)
        enabled_projects.sort()
        self.config_parser.set('DEFAULT', 'sync_projects',
                               ' '.join(enabled_projects))
Ejemplo n.º 8
0
    def readboolean(self, section, option, fallback=False):
        """
        Returns a boolean config option value from config file

        :param section: section where the option is stored
        :param option: option name
        :param fallback: (optional) fallback value
        :return: a config option value
        :rtype: boolean
        """
        if self.config_file == None:
            raise InvalidOperation('readboolean')
        return self.config_parser.getboolean(section,
                                             option,
                                             fallback=fallback)
Ejemplo n.º 9
0
    def read(self, section, option, fallback=None):
        """
        Returns a config option value from config file

        :param section: section where the option is stored
        :param option: option name
        :param fallback: (optional) fallback value
        :return: a config option value
        :rtype: string
        """
        if self.config_file == None:
            raise InvalidOperation('read')
        if fallback is None:
            return self.config_parser.get(section, option)
        else:
            return self.config_parser.get(section, option, fallback=fallback)
Ejemplo n.º 10
0
    def write(self, section, option, value):
        """
        Write a config option value in config object

        :param section: section where the option is stored
        :param option: option name
        :param value: option value
        """
        if self.config_file == None:
            raise InvalidOperation('write')
        if section != 'DEFAULT' and not self.config_parser.has_section(
                section):
            logger.debug('Adding new section {}'.format(section))
            self.config_parser.add_section(section)
        logger.debug('Adding {}.{} with value {}'.format(
            section, option, value))
        self.config_parser.set(section, option, value)
Ejemplo n.º 11
0
    def remove(self, section, option):
        """
        Remove a config option in config object

        :param section: section where the option is stored
        :param option: option name
        :return: True if option is removed, False if not exist
        :rtype: boolean
        """
        if self.config_file == None:
            raise InvalidOperation('remove')
        logger.debug('Removing {}.{}'.format(section, option))
        option_removed = self.config_parser.remove_option(section, option)
        if section != 'DEFAULT' and option_removed:
            if self.config_parser.items(section) == self.config_parser.items(
                    'DEFAULT'):
                logger.debug('Removing empty section {}'.format(section))
                self.config_parser.remove_section(section)
        return option_removed
Ejemplo n.º 12
0
    def disable_project(self, project):
        """
        Disable a project removing it from sync_projects

        :param project: project name
        :return: True if project is disabled, False if not
        :rtype: boolean
        """
        if self.config_file == None:
            raise InvalidOperation('disable_project')
        logger.debug('Disabling project {}'.format(project))
        enabled_projects = self.list_enabled_projects()
        try:
            enabled_projects.remove(project)
            self.config_parser.set('DEFAULT', 'sync_projects',
                                   ' '.join(enabled_projects))
            return True
        except ValueError:
            logger.debug(
                'Nothing to do, {} is not in enabled projects'.format(project))
            return False