<commit_before>import xmlrpclib
from datetime import datetime, timedelta
from matrixbot import utils

class TracPlugin:
    def __init__(self, bot, settings):
        self.logger = utils.get_logger()
        self.bot = bot
        self.settings = settings
        self.logger.info("TracPlugin loaded (%(name)s)" % settings)
        self.timestamp = datetime.utcnow()
        self.server = xmlrpclib.ServerProxy(
            '%(url_protocol)s://%(url_auth_user)s:%(url_auth_password)s@%(url_domain)s%(url_path)s/login/xmlrpc' % self.settings
        )

    def pretty_ticket(self, ticket):
        ticket[3]["ticket_id"] = ticket[0]
        url = '%(url_protocol)s://%(url_domain)s%(url_path)s' % self.settings
        ticket[3]["ticket_url"] = "%s/ticket/%s" % (url, ticket[0])
        res = """%(summary)s:
    * URL: %(ticket_url)s
    * [severity: %(severity)s] [owner: %(owner)s] [reporter: %(reporter)s] [status: %(status)s]""" % ticket[3]
        return res

    def async(self, handler):
        self.logger.debug("TracPlugin async")
        server = self.server

        d = self.timestamp
        self.timestamp = datetime.utcnow()
        res = []
        for t in server.ticket.getRecentChanges(d):
            ticket = server.ticket.get(t)
            changes = server.ticket.changeLog(t)
            if len(changes) == 0 and 'new' in self.settings['status']:  # No changes implies New ticket
                res.append(ticket)
            for c in changes:
                if (
                    c[0] > d and c[2] == 'status'
                    and c[4] in self.settings['status']
                ):
                    res.append(ticket)

        if len(res) == 0:
            return

        res = map(
            self.pretty_ticket,
            res
        )
        message = "\n".join(res)
        for room_id in self.settings["rooms"]:
            room_id = self.bot.get_real_room_id(room_id)
            handler(room_id, message)

    def command(self, sender, room_id, body, handler):
        self.logger.debug("TracPlugin command")
        plugin_name = self.settings["name"]

        # TODO: This should be a decorator
        if self.bot.only_local_domain and not self.bot.is_local_user_id(sender):
            self.logger.warning(
                "TracPlugin %s plugin is not allowed for external sender (%s)" % (plugin_name, sender)
            )
            return

        sender = sender.replace('@','')
        sender = sender.split(':')[0]
        command_list = body.split()[1:]
        if len(command_list) > 0 and command_list[0] == plugin_name: 
            if command_list[1] == "create": <commit_msg>Trac: Send notices instead of normal messages<commit_after>import xmlrpclib
from datetime import datetime, timedelta
from matrixbot import utils

class TracPlugin:
    def __init__(self, bot, settings):
        self.logger = utils.get_logger()
        self.bot = bot
        self.settings = settings
        self.logger.info("TracPlugin loaded (%(name)s)" % settings)
        self.timestamp = datetime.utcnow()
        self.server = xmlrpclib.ServerProxy(
            '%(url_protocol)s://%(url_auth_user)s:%(url_auth_password)s@%(url_domain)s%(url_path)s/login/xmlrpc' % self.settings
        )

    def pretty_ticket(self, ticket):
        ticket[3]["ticket_id"] = ticket[0]
        url = '%(url_protocol)s://%(url_domain)s%(url_path)s' % self.settings
        ticket[3]["ticket_url"] = "%s/ticket/%s" % (url, ticket[0])
        res = """%(summary)s:
    * URL: %(ticket_url)s
    * [severity: %(severity)s] [owner: %(owner)s] [reporter: %(reporter)s] [status: %(status)s]""" % ticket[3]
        return res

    def async(self, handler):
        self.logger.debug("TracPlugin async")
        server = self.server

        d = self.timestamp
        self.timestamp = datetime.utcnow()
        res = []
        for t in server.ticket.getRecentChanges(d):
            ticket = server.ticket.get(t)
            changes = server.ticket.changeLog(t)
            if len(changes) == 0 and 'new' in self.settings['status']:  # No changes implies New ticket
                res.append(ticket)
            for c in changes:
                if (
                    c[0] > d and c[2] == 'status'
                    and c[4] in self.settings['status']
                ):
                    res.append(ticket)

        if len(res) == 0:
            return

        res = map(
            self.pretty_ticket,
            res
        )
        message = "\n".join(res)
        for room_id in self.settings["rooms"]:
            room_id = self.bot.get_real_room_id(room_id)
            self.bot.send_notice(room_id, message)

    def command(self, sender, room_id, body, handler):
        self.logger.debug("TracPlugin command")
        plugin_name = self.settings["name"]

        # TODO: This should be a decorator
        if self.bot.only_local_domain and not self.bot.is_local_user_id(sender):
            self.logger.warning(
                "TracPlugin %s plugin is not allowed for external sender (%s)" % (plugin_name, sender)
            )
            return

        sender = sender.replace('@','')
        sender = sender.split(':')[0]
        command_list = body.split()[1:]
        if len(command_list) > 0 and command_list[0] == plugin_name: 
            if command_list[1] == "create": 
{'fraction_non_alphanumeric': 0.13196921344096113, 'fraction_numerical': 0.005631687629059508, 'mean_word_length': 2.117612638970158, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4300706', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1609, 'n_words': 358}