Пример #1
0
 def suggestions(self):
     suggestions = []
     if 'TORCH_ROOT' in os.environ:
         d = os.environ['TORCH_ROOT']
         try:
             suggestions.append(
                 prompt.Suggestion(self.validate(d),
                                   'R',
                                   desc='TORCH_ROOT',
                                   default=True))
         except config_option.BadValue as e:
             print 'TORCH_ROOT "%s" is invalid:' % d
             print '\t%s' % e
     if 'TORCH_HOME' in os.environ:
         d = os.environ['TORCH_HOME']
         try:
             default = True
             if len(suggestions) > 0:
                 default = False
             suggestions.append(
                 prompt.Suggestion(self.validate(d),
                                   'H',
                                   desc='TORCH_HOME',
                                   default=default))
         except config_option.BadValue as e:
             print 'TORCH_HOME "%s" is invalid:' % d
             print '\t%s' % e
     suggestions.append(
         prompt.Suggestion('<PATHS>',
                           'P',
                           desc='PATH/TORCHPATH',
                           default=True))
     return suggestions
Пример #2
0
 def suggestions(self):
     suggestions = []
     if 'CAFFE_ROOT' in os.environ:
         d = os.environ['CAFFE_ROOT']
         try:
             suggestions.append(
                 prompt.Suggestion(self.validate(d),
                                   'R',
                                   desc='CAFFE_ROOT',
                                   default=True))
         except config_option.BadValue:
             pass
     if 'CAFFE_HOME' in os.environ:
         d = os.environ['CAFFE_HOME']
         try:
             default = True
             if len(suggestions) > 0:
                 default = False
             suggestions.append(
                 prompt.Suggestion(self.validate(d),
                                   'H',
                                   desc='CAFFE_HOME',
                                   default=default))
         except config_option.BadValue:
             pass
     suggestions.append(
         prompt.Suggestion('<PATHS>',
                           'P',
                           desc='PATH/PYTHONPATH',
                           default=True))
     return suggestions
Пример #3
0
 def suggestions(self):
     return [
         prompt.Suggestion('debug', 'D'),
         prompt.Suggestion('info', 'I', default=True),
         prompt.Suggestion('warning', 'W'),
         prompt.Suggestion('error', 'E'),
     ]
Пример #4
0
    def suggestions(self):
        suggested_dir = os.path.dirname(digits.__file__)

        if os.access(suggested_dir, os.W_OK):
            return [
                prompt.Suggestion(os.path.join(suggested_dir, 'digits.log'),
                                  'D',
                                  desc='default',
                                  default=True)
            ]
        else:
            return []
Пример #5
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
Пример #6
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)
Пример #7
0
 def suggestions(self):
     hostname = platform.node()
     return [prompt.Suggestion(hostname, 'H', desc='HOSTNAME')]
Пример #8
0
 def suggestions(self):
     d = os.path.join(os.path.dirname(digits.__file__), 'jobs')
     return [prompt.Suggestion(d, 'D', desc='default', default=True)]