Changeset 147 for trunk/ticketvalidation

Show
Ignore:
Timestamp:
27/01/10 11:41:03 (7 months ago)
Author:
mbooth
Message:

Implement boolean and/or operators for rule condition expressions.

Location:
trunk/ticketvalidation/ticketvalidation
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/ticketvalidation/ticketvalidation/admin.py

    r144 r147  
    1010from trac.web.chrome import add_script 
    1111 
     12__all__ = ['TicketValidationAdminPanel'] 
     13 
    1214 
    1315class TicketValidationAdminPanel(Component): 
    14     """Provides an admin panel for ticket validation config.""" 
     16    """Provides an admin panel for configuring ticket validation rules.""" 
    1517 
    1618    implements(IAdminPanelProvider) 
  • trunk/ticketvalidation/ticketvalidation/main.py

    r146 r147  
    1717from trac.web.chrome import ITemplateProvider 
    1818 
    19 __all__ = ['TicketValidationPlugin'] 
     19__all__ = ['TicketValidationRules'] 
    2020 
    2121 
    2222class BoolOperand(object): 
     23    ticket = None 
    2324    def __init__(self, t): 
    2425        self.args = t[0] 
     
    2627        return "(" + " ".join(map(str,self.args)) + ")" 
    2728 
    28  
    2929class BoolEquality(BoolOperand): 
    3030    def __nonzero__(self): 
    3131        # TODO - implement this 
    32         return True 
    33  
     32        return False 
    3433 
    3534class BoolAnd(BoolOperand): 
    3635    def __nonzero__(self): 
    37         # TODO - implement this 
     36        for a in self.args[0::2]: 
     37            v = bool(a) 
     38            if not v: 
     39                return False 
    3840        return True 
    39  
    4041 
    4142class BoolOr(BoolOperand): 
    4243    def __nonzero__(self): 
    43         # TODO - implement this 
    44         return True 
     44        for a in self.args[0::2]: 
     45            v = bool(a) 
     46            if v: 
     47                return True 
     48        return False 
    4549 
    4650 
    47 class TicketValidationPlugin(Component): 
     51class TicketValidationRules(Component): 
    4852    """Main component of the ticket validation plug-in. Fetches the validation 
    49     rules from the config and applies them when the user submits the ticket. If 
    50     any fields are found to violate the rules, the submission is disallowed and 
    51     the user is chastised.""" 
     53    rules from the config, parses them and applies them appropriately.""" 
    5254 
    5355    implements(ITemplateProvider, ITicketManipulator) 
     
    5961         
    6062        # grammar definition for parsing rule conditions 
    61         self._grammar = operatorPrecedence(Word(alphanums + '_') | quotedString, 
     63        self._grammar = operatorPrecedence(Word(alphanums + '_') | quotedString.setParseAction(removeQuotes), 
    6264                                           [(oneOf('== !='), 2, opAssoc.LEFT, BoolEquality), 
    6365                                            (oneOf('and &&'), 2, opAssoc.LEFT, BoolAnd), 
     
    7375                    'condition': config.get(name), 
    7476                    'required': config.get('%s.required' % name).split(), 
     77                    'hidden': config.get('%s.hidden' % name).split(), 
    7578                    } 
    7679            rules.append(rule) 
     
    113116        """This API is called by Trac when the user tries to submit a ticket. 
    114117        This is where the magic happens for required fields.""" 
    115         rules = self.get_rules() 
    116118        problems = [] 
    117         for r in rules: 
     119        BoolOperand.ticket = ticket 
     120        for r in self.get_rules(): 
    118121            result = self._grammar.parseString(r['condition'])[0] 
    119122            self.log.debug('required field rule "%s": %s is %s' % (r['name'], str(result), bool(result)))