Edgewall Software
Modify

Opened 16 years ago

Closed 15 years ago

Last modified 15 years ago

#7432 closed enhancement (fixed)

Add new configuration option types

Reported by: Emmanuel Blot Owned by: Remy Blank
Priority: lowest Milestone: 0.12
Component: general Version: 0.11-stable
Severity: minor Keywords:
Cc: Branch:
Release Notes:
API Changes:
Internal Changes:

Description

Although the following option types are not required by Trac core itself, they might be interesting for various plugins and could be part of Trac core, rather than being implemented in several plugin:

  • Choice among a list of option
  • Float option

The following add-on could be part of trunk/trac/config.py

class FloatOption(Option):
    """Descriptor for float configuration options."""
    
    def accessor(self, section, name, default=''):
        """Return the value of the specified option as float.
        
        If the specified option can not be converted to a float, a
        `ConfigurationError` exception is raised.
        
        Valid default input is a string or a float. Returns an float.
        """
        value = section.get(name, default)
        if not value:
            return 0.0
        try:
            return float(value)
        except ValueError:
            raise ConfigurationError('expected real number, got %s' % \
                                     repr(value))

class ChoiceOption(Option):
    """Descriptor for choice configuration options."""

    def __init__(self, section, name, default=None, choices='', doc=''):
        Option.__init__(self, section, name, default, doc)
        self.choices = filter(None, [c.strip() for c in choices.split(',')])

    def accessor(self, section, name, default):
        value = section.get(name, default)
        if value not in self.choices:
            raise ConfigurationError('expected a choice among "%s", got %s' % \
                                     (', '.join(self.choices), repr(value)))
        return value

Attachments (0)

Change History (7)

comment:1 by Remy Blank, 16 years ago

Milestone: 2.0

Sounds useful, but I would tend to wait for at least one use case before integrating this.

in reply to:  1 ; comment:2 by Emmanuel Blot, 16 years ago

Replying to rblank:

Sounds useful, but I would tend to wait for at least one use case before integrating this.

One use case in Trac, or one use case in a plugin ?

in reply to:  2 comment:3 by Remy Blank, 16 years ago

Replying to eblot:

One use case in Trac, or one use case in a plugin ?

I meant one use case in a plugin, but since you're asking, I suppose you have one already ;-)

Feel free to re-schedule to an earlier milestone in this case.

comment:4 by Remy Blank, 15 years ago

Milestone: 2.00.12
Owner: set to Remy Blank

FloatOption would be useful for specifying [trac] auto_preview_timeout, as mentioned in comment:4:ticket:9144.

comment:5 by Remy Blank, 15 years ago

FloatOption was added in [9364].

comment:6 by Remy Blank, 15 years ago

Resolution: fixed
Status: newclosed

And ChoiceOption in [9365].

comment:7 by Martin Scharrer <martin@…>, 15 years ago

In addition to ChoiceOption an option type for multiple non-exclusive options would be useful, e.g. called 'NonexclChoiceOption' (please find a better name :-) ).

In other words: ChoiceOption is like a HTML radio button list where only one element can be selected. The new option NonexclChoiceOption type would be like a HTML check box list where multiple elements can be enabled/disabled.

Graphical configuration tools should render this options as the above HTML form lists.

The Option should return a list of enabled elements/options, so that a plugin could be coded like:

class MyPlugin (component):
   implements ( something )
   
   enabled_features = NonexclChoiceOption('myplugin', 'features', ['feature_a','feature_b','feature_c'], doc='...')
   enabled_realms   = NonexclChoiceOption('myplugin', 'realms', ['wiki','ticket'], doc='Work with wikis and/or tickets')

   def somemethod(self):
      if 'wiki' in self.enabled_realms:
          wiki_code()

      if 'ticket' in self.enabled_realms:
          ticket_code()

      if 'feature_a' in self.enabled_features:
          feature_a_code()   enabled_features = NonexclChoiceOption('myplugin', 'features', default=['A','B','C'], doc='...')


While this can be also done with the ListOption it wouldn't be clear to the user which elements are valid in the list, which is probably the reason ChoiceOption was created.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Remy Blank.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Remy Blank to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.