<commit_before>import inspect
import pkg_resources
import sys
from StringIO import StringIO

from genshi.template.text import (NewTextTemplate as TextTemplate,
                                  TemplateSyntaxError)
from trac.admin.api import IAdminCommandProvider, IAdminPanelProvider
from trac.core import *
from trac.config import *
from trac.notification import NotifyEmail
from trac.ticket.api import ITicketChangeListener, ITicketActionController, TicketSystem
from trac.util.text import CRLF
from trac.web.chrome import Chrome, ITemplateProvider, add_warning, add_notice

class TicketWorkflowNotifier(Component):
    implements(ITicketChangeListener, ITicketActionController, ITemplateProvider,
               IAdminCommandProvider, IAdminPanelProvider)

    def get_admin_panels(self, req):
        """Return a list of available admin panels.
        
        The items returned by this function must be tuples of the form
        `(category, category_label, page, page_label)`.
        """
        if req.perm.has_permission('TRAC_ADMIN'):
            yield ('ticket', 'Ticket System', 
                   "workflow_notification", "Workflow Notifications")

    def render_admin_panel(self, req, category, page, path_info):

        if path_info is None:
            return self.render_admin_panel_list(req)
        return self.render_admin_panel_detail(req, path_info)

    def render_admin_panel_detail(self, req, name):
        section = self.config['ticket-workflow-notifications']
        rule = {}
        rule['condition'] = section.get('%s.condition' % name, None)
        for key in 'body subject recipients'.split():
            val = section.get('%s.%s' % (name, key))
            if key == 'recipients':
                val = [i.strip() for i in val.split(",")]
            rule[key] = val
        rule['actions'] = [i.strip() for i in section.get(name).split(",")]
        
        data = {
            'rule': rule,
            'name': name,
            }
        return ('workflow_notification_rule_admin.html', data)

    def render_admin_panel_list(self, req):

        section = self.config['ticket-workflow-notifications']
        notifications = {}

        for name in section:
            if '.' in name:
                continue
            rule = {}
            rule['condition'] = section.get('%s.condition' % name, None)
            for key in 'body subject recipients'.split():
                val = section.get('%s.%s' % (name, key))
                if key == 'recipients':<commit_msg>implement TTW editing of existing rules<commit_after>import inspect
import pkg_resources
import sys
from StringIO import StringIO

from genshi.template.text import (NewTextTemplate as TextTemplate,
                                  TemplateSyntaxError)
from trac.admin.api import IAdminCommandProvider, IAdminPanelProvider
from trac.core import *
from trac.config import *
from trac.notification import NotifyEmail
from trac.ticket.api import ITicketChangeListener, ITicketActionController, TicketSystem
from trac.util.text import CRLF
from trac.web.chrome import Chrome, ITemplateProvider, add_warning, add_notice

class TicketWorkflowNotifier(Component):
    implements(ITicketChangeListener, ITicketActionController, ITemplateProvider,
               IAdminCommandProvider, IAdminPanelProvider)

    def get_admin_panels(self, req):
        """Return a list of available admin panels.
        
        The items returned by this function must be tuples of the form
        `(category, category_label, page, page_label)`.
        """
        if req.perm.has_permission('TRAC_ADMIN'):
            yield ('ticket', 'Ticket System', 
                   "workflow_notification", "Workflow Notifications")

    def render_admin_panel(self, req, category, page, path_info):

        if path_info is None:
            return self.render_admin_panel_list(req)
        return self.render_admin_panel_detail(req, path_info)

    def render_admin_panel_detail(self, req, name):
        section = self.config['ticket-workflow-notifications']
        rule = {}
        rule['condition'] = section.get('%s.condition' % name, None)
        for key in 'body subject recipients'.split():
            val = section.get('%s.%s' % (name, key))
            if key == 'recipients':
                val = [i.strip() for i in val.split(",")]
            rule[key] = val
        rule['actions'] = [i.strip() for i in section.get(name).split(",")]
        
        data = {
            'rule': rule,
            'name': name,
            }
        if req.method == "GET":
            return ('workflow_notification_rule_admin.html', data)
        
        for key in 'body subject condition recipients actions'.split():
            if key in ("actions", "recipients"):
                rule[key] = [i.strip() for i in req.args[key].split(",")]
            else:
                rule[key] = req.args[key] # @@TODO fail gracefully if keys are missing
            if key == "actions":
                self.config.set("ticket-workflow-notifications", name, ','.join(rule['actions']))
            else:
                if key == "recipients":
                    self.config.set("ticket-workflow-notifications", 
                                    "%s.recipients" % name, ','.join(rule[key]))
                else:
                    self.config.set("ticket-workflow-notifications", 
                                    "%s.%s" % (name, key), rule[key])
        errs = StringIO()
        try:
            self.validate(ostream=errs)
        except TemplateSyntaxError:
            errs.seek(0)
            add_warning(req, errs.read())
            self.config.parse_if_needed(force=True)
            return ('workflow_notification_rule_admin.html', data)
        else:
            self.config.save()
            add_notice(req, "Your changes have been saved")
            return req.redirect(req.href("admin/ticket/workflow_notification/%s" % name))

    def render_admin_panel_list(self, req):

        section = self.config['ticket-workflow-notifications']
        notifications = {}

        for name in section:
            if '.' in name:
                continue
            rule = {}
            rule['condition'] = section.get('%s.condition' % name, None)
            for key in 'body subject recipients'.split():
                val = section.get('%s.%s' % (name, key))
                if key == 'recipients':
{'fraction_non_alphanumeric': 0.10715956948993917, 'fraction_numerical': 0.00015598190609889253, 'mean_word_length': 2.2848360655737703, 'pattern_counts': {'":': 3, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3498318', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1697, 'n_words': 448}