Example #1
0
def edit_config_file(verbose=False):
    """
    Prompt the user for which file to edit,
    then allow them to set options in that file
    """
    suggestions = []
    instanceConfig = config_file.InstanceConfigFile()
    if instanceConfig.can_write():
        suggestions.append(
            prompt.Suggestion(instanceConfig.filename(),
                              'I',
                              desc='Instance',
                              default=True))
    userConfig = config_file.UserConfigFile()
    if userConfig.can_write():
        suggestions.append(
            prompt.Suggestion(userConfig.filename(),
                              'U',
                              desc='User',
                              default=True))
    systemConfig = config_file.SystemConfigFile()
    if systemConfig.can_write():
        suggestions.append(
            prompt.Suggestion(systemConfig.filename(),
                              'S',
                              desc='System',
                              default=True))

    def filenameValidator(filename):
        """
        Returns True if this is a valid file to edit
        """
        if os.path.isfile(filename):
            if not os.access(filename, os.W_OK):
                raise config_option.BadValue(
                    'You do not have write permission')
            else:
                return filename

        if os.path.isdir(filename):
            raise config_option.BadValue('This is a directory')
        dirname = os.path.dirname(os.path.realpath(filename))
        if not os.path.isdir(dirname):
            raise config_option.BadValue('Path not found: %s' % dirname)
        elif not os.access(dirname, os.W_OK):
            raise config_option.BadValue('You do not have write permission')
        return filename

    filename = prompt.get_input(
        message='Which file do you want to edit?',
        suggestions=suggestions,
        validator=filenameValidator,
        is_path=True,
    )

    print 'Editing file at %s ...' % os.path.realpath(filename)
    print

    is_standard_location = False

    if filename == instanceConfig.filename():
        is_standard_location = True
        instanceConfig = None
    if filename == userConfig.filename():
        is_standard_location = True
        userConfig = None
    if filename == systemConfig.filename():
        is_standard_location = True
        systemConfig = None

    configFile = config_file.ConfigFile(filename)

    min_visibility = config_option.Visibility.DEFAULT
    if verbose:
        min_visibility = config_option.Visibility.HIDDEN

    # Loop through the visible options
    for option in [
            o for o in current_config.option_list
            if o.visibility() >= min_visibility
    ]:
        previous_value = configFile.get(option.config_file_key())
        suggestions = [
            prompt.Suggestion(None,
                              'U',
                              desc='unset',
                              default=(previous_value is None))
        ]
        if previous_value is not None:
            suggestions.append(
                prompt.Suggestion(previous_value,
                                  '',
                                  desc='Previous',
                                  default=True))
        if instanceConfig is not None:
            instance_value = instanceConfig.get(option.config_file_key())
            if instance_value is not None:
                suggestions.append(
                    prompt.Suggestion(instance_value,
                                      'I',
                                      desc='Instance',
                                      default=is_standard_location))
        if userConfig is not None:
            user_value = userConfig.get(option.config_file_key())
            if user_value is not None:
                suggestions.append(
                    prompt.Suggestion(user_value,
                                      'U',
                                      desc='User',
                                      default=is_standard_location))
        if systemConfig is not None:
            system_value = systemConfig.get(option.config_file_key())
            if system_value is not None:
                suggestions.append(
                    prompt.Suggestion(system_value,
                                      'S',
                                      desc='System',
                                      default=is_standard_location))
        suggestions += option.suggestions()
        if option.optional():
            suggestions.append(
                prompt.Suggestion('', 'N', desc='none', default=True))

        prompt.print_section_header(option.prompt_title())
        value = prompt.get_input(
            message=option.prompt_message(),
            validator=option.validate,
            suggestions=suggestions,
            is_path=option.is_path(),
        )
        print
        configFile.set(option.config_file_key(), value)

    configFile.save()
    print 'New config saved at %s' % configFile.filename()
    print
    print configFile
Example #2
0
def load_option(
    option,
    mode,
    newConfig,
    instanceConfig=None,
    userConfig=None,
    systemConfig=None,
):
    """
    Called from load_config() [below]

    Arguments:
    option -- an Option instance
    mode -- see docstring for load_config()
    newConfig -- an instance of ConfigFile
    instanceConfig -- the current InstanceConfigFile
    userConfig -- the current UserConfigFile
    systemConfig -- the current SystemConfigFile
    """
    if 'DIGITS_MODE_TEST' in os.environ and option.has_test_value():
        option.set(option.test_value())
        return

    suggestions = []
    instance_value = instanceConfig.get(option.config_file_key())
    if instance_value is not None:
        suggestions.append(
            prompt.Suggestion(instance_value,
                              '',
                              desc='Previous',
                              default=True))
    user_value = userConfig.get(option.config_file_key())
    if user_value is not None:
        suggestions.append(
            prompt.Suggestion(user_value, 'U', desc='User', default=True))
    system_value = systemConfig.get(option.config_file_key())
    if system_value is not None:
        suggestions.append(
            prompt.Suggestion(system_value, 'S', desc='System', default=True))
    suggestions += option.suggestions()
    if option.optional():
        suggestions.append(
            prompt.Suggestion('', 'N', desc='none', default=True))

    # Try to use the default values for options less than
    #   or equal to (LTE) this value
    try_defaults_lte = config_option.Visibility.DEFAULT

    if mode == 'verbose':
        try_defaults_lte = config_option.Visibility.NEVER
    elif mode == 'normal':
        try_defaults_lte = config_option.Visibility.HIDDEN
    elif mode == 'quiet':
        pass
    elif mode == 'force':
        pass
    else:
        raise config_option.BadValue('Unknown mode "%s"' % mode)

    valid = False
    if option.visibility() <= try_defaults_lte:
        # check for a valid default value
        for s in [s for s in suggestions if s.default]:
            try:
                option.set(s.value)
                valid = True
                break
            except config_option.BadValue as e:
                print 'Default value for %s "%s" invalid:' % (
                    option.config_file_key(), s.value)
                print '\t%s' % e
    if not valid:
        if mode == 'force':
            raise RuntimeError(
                'No valid default value found for configuration option "%s"' %
                option.config_file_key())
        else:
            # prompt user for value
            prompt.print_section_header(option.prompt_title())
            value = prompt.get_input(
                message=option.prompt_message(),
                validator=option.validate,
                suggestions=suggestions,
                is_path=option.is_path(),
            )
            print
            option.set(value)
            newConfig.set(option.config_file_key(), option._config_file_value)
Example #3
0
def edit_config_file(verbose=False):
    """
    Prompt the user for which file to edit,
    then allow them to set options in that file
    """
    suggestions = []
    instanceConfig = config_file.InstanceConfigFile()
    if instanceConfig.can_write():
        suggestions.append(prompt.Suggestion(
            instanceConfig.filename(), 'I',
            desc = 'Instance', default=True))
    userConfig = config_file.UserConfigFile()
    if userConfig.can_write():
        suggestions.append(prompt.Suggestion(
            userConfig.filename(), 'U',
            desc = 'User', default=True))
    systemConfig = config_file.SystemConfigFile()
    if systemConfig.can_write():
        suggestions.append(prompt.Suggestion(
            systemConfig.filename(), 'S',
            desc = 'System', default=True))

    def filenameValidator(filename):
        """
        Returns True if this is a valid file to edit
        """
        if os.path.isfile(filename):
            if not os.access(filename, os.W_OK):
                raise config_option.BadValue('You do not have write permission')
            else:
                return filename

        if os.path.isdir(filename):
            raise config_option.BadValue('This is a directory')
        dirname = os.path.dirname(os.path.realpath(filename))
        if not os.path.isdir(dirname):
            raise config_option.BadValue('Path not found: %s' % dirname)
        elif not os.access(dirname, os.W_OK):
            raise config_option.BadValue('You do not have write permission')
        return filename

    filename = prompt.get_input(
            message     = 'Which file do you want to edit?',
            suggestions = suggestions,
            validator   = filenameValidator,
            is_path     = True,
            )

    print 'Editing file at %s ...' % os.path.realpath(filename)
    print

    is_standard_location = False

    if filename == instanceConfig.filename():
        is_standard_location = True
        instanceConfig = None
    if filename == userConfig.filename():
        is_standard_location = True
        userConfig = None
    if filename == systemConfig.filename():
        is_standard_location = True
        systemConfig = None

    configFile = config_file.ConfigFile(filename)

    min_visibility = config_option.Visibility.DEFAULT
    if verbose:
        min_visibility = config_option.Visibility.HIDDEN

    # Loop through the visible options
    for option in [o for o in current_config.option_list
            if o.visibility() >= min_visibility]:
        previous_value = configFile.get(option.config_file_key())
        suggestions = [prompt.Suggestion(None, 'U',
            desc='unset', default=(previous_value is None))]
        if previous_value is not None:
            suggestions.append(prompt.Suggestion(previous_value, '',
                desc = 'Previous', default = True))
        if instanceConfig is not None:
            instance_value = instanceConfig.get(option.config_file_key())
            if instance_value is not None:
                suggestions.append(prompt.Suggestion(instance_value, 'I',
                    desc = 'Instance', default = is_standard_location))
        if userConfig is not None:
            user_value = userConfig.get(option.config_file_key())
            if user_value is not None:
                suggestions.append(prompt.Suggestion(user_value, 'U',
                    desc = 'User', default = is_standard_location))
        if systemConfig is not None:
            system_value = systemConfig.get(option.config_file_key())
            if system_value is not None:
                suggestions.append(prompt.Suggestion(system_value, 'S',
                    desc = 'System', default = is_standard_location))
        suggestions += option.suggestions()
        if option.optional():
            suggestions.append(prompt.Suggestion('', 'N',
                desc = 'none', default = True))

        prompt.print_section_header(option.prompt_title())
        value = prompt.get_input(
                message     = option.prompt_message(),
                validator   = option.validate,
                suggestions = suggestions,
                is_path     = option.is_path(),
                )
        print
        configFile.set(option.config_file_key(), value)

    configFile.save()
    print 'New config saved at %s' % configFile.filename()
    print
    print configFile
Example #4
0
def load_option(option, mode, newConfig,
        instanceConfig  =None,
        userConfig      = None,
        systemConfig    = None,
        ):
    """
    Called from load_config() [below]

    Arguments:
    option -- an Option instance
    mode -- see docstring for load_config()
    newConfig -- an instance of ConfigFile
    instanceConfig -- the current InstanceConfigFile
    userConfig -- the current UserConfigFile
    systemConfig -- the current SystemConfigFile
    """
    if 'DIGITS_MODE_TEST' in os.environ and option.has_test_value():
        option.set(option.test_value())
        return

    suggestions = []
    instance_value = instanceConfig.get(option.config_file_key())
    if instance_value is not None:
        suggestions.append(prompt.Suggestion(instance_value, '',
            desc = 'Previous', default = True))
    user_value = userConfig.get(option.config_file_key())
    if user_value is not None:
        suggestions.append(prompt.Suggestion(user_value, 'U',
            desc = 'User', default = True))
    system_value = systemConfig.get(option.config_file_key())
    if system_value is not None:
        suggestions.append(prompt.Suggestion(system_value, 'S',
            desc = 'System', default = True))
    suggestions += option.suggestions()
    if option.optional():
        suggestions.append(prompt.Suggestion('', 'N',
            desc = 'none', default = True))

    # Try to use the default values for options less than
    #   or equal to (LTE) this value
    try_defaults_lte = config_option.Visibility.DEFAULT

    if mode == 'verbose':
        try_defaults_lte = config_option.Visibility.NEVER
    elif mode == 'normal':
        try_defaults_lte = config_option.Visibility.HIDDEN
    elif mode == 'quiet':
        pass
    elif mode == 'force':
        pass
    else:
        raise config_option.BadValue('Unknown mode "%s"' % mode)

    valid = False
    if option.visibility() <= try_defaults_lte:
        # check for a valid default value
        for s in [s for s in suggestions if s.default]:
            try:
                option.set(s.value)
                valid = True
                break
            except config_option.BadValue as e:
                print 'Default value for %s "%s" invalid:' % (option.config_file_key(), s.value)
                print '\t%s' % e
    if not valid:
        if mode == 'force':
            raise RuntimeError('No valid default value found for configuration option "%s"' % option.config_file_key())
        else:
            # prompt user for value
            prompt.print_section_header(option.prompt_title())
            value = prompt.get_input(
                    message     = option.prompt_message(),
                    validator   = option.validate,
                    suggestions = suggestions,
                    is_path     = option.is_path(),
                    )
            print
            option.set(value)
            newConfig.set(option.config_file_key(), option._config_file_value)