Beispiel #1
0
class Log(glados.Module):
    def __init__(self, server_instance, full_name):
        super(Log, self).__init__(server_instance, full_name)

        self.log_path = os.path.join(self.local_data_dir, 'log')
        if not os.path.exists(self.log_path):
            os.makedirs(self.log_path)

        self.date = datetime.now().strftime('%Y-%m-%d')
        self.log_file = LZMAFile(
            os.path.join(self.log_path, 'chanlog-{}.txt.xz'.format(self.date)),
            'a')

    def __open_new_log_if_necessary(self):
        date = datetime.now().strftime('%Y-%m-%d')
        if not self.date == date:
            self.log_file.close()
            self.date = date
            self.log_file = LZMAFile(
                os.path.join(self.log_path,
                             'chanlog-{}.txt.xz'.format(self.date)), 'a')

    @glados.Permissions.spamalot
    @glados.Module.rule('^.*$', ignorecommands=False)
    async def on_message(self, message, match):
        server_name = message.server.name if message.server else ''
        server_id = message.server.id if message.server else ''
        self.__open_new_log_if_necessary()
        info = u'[{0}] {1}({2}): #{3}: {4}({5}): {6}\n'.format(
            datetime.now().strftime('%Y-%m-%d %H:%M:%S'), server_name,
            server_id, message.channel.name, message.author.name,
            message.author.id, message.clean_content)

        self.log_file.write(info.encode('utf-8'))
        self.log_file.flush()
        return ()
Beispiel #2
0
class LZMAPlugin:
    """
    Compresses received data using `lzma <https://en.wikipedia.org/wiki/Lempel–Ziv–Markov_chain_algorithm>`_.

    Accepted ``feed_options`` parameters:

    - `lzma_format`
    - `lzma_check`
    - `lzma_preset`
    - `lzma_filters`

    .. note::
        ``lzma_filters`` cannot be used in pypy version 7.3.1 and older.

    See :py:class:`lzma.LZMAFile` for more info about parameters.
    """
    def __init__(self, file: BinaryIO, feed_options: Dict[str, Any]) -> None:
        self.file = file
        self.feed_options = feed_options

        format = self.feed_options.get("lzma_format")
        check = self.feed_options.get("lzma_check", -1)
        preset = self.feed_options.get("lzma_preset")
        filters = self.feed_options.get("lzma_filters")
        self.lzmafile = LZMAFile(filename=self.file,
                                 mode="wb",
                                 format=format,
                                 check=check,
                                 preset=preset,
                                 filters=filters)

    def write(self, data: bytes) -> int:
        return self.lzmafile.write(data)

    def close(self) -> None:
        self.lzmafile.close()
        self.file.close()
Beispiel #3
0
        log_data = LZMAFile(os.path.join(log_dir, log_file_name), 'r').read().decode('utf-8')
        new_log_file = LZMAFile(os.path.join(log_dir, log_file_name), 'w')
        for line in log_data.split('\n'):
            if not line:
                continue
            m = Message(line)

            if int(m.author_id) == 0:
                for id, member in info[server_id]["members"].items():
                    if m.author == member["name"]:
                        m.author_id = id
                        break
                else:
                    failed_members.add(m.author)

            log_msg = u'[{0}] {1}({2}): {3}: {4}({5}): {6}\n'.format(
                m.stamp_str,
                info[server_id]["name"],
                server_id,
                m.channel,
                m.author,
                m.author_id,
                m.message)
            new_log_file.write(log_msg.encode('utf-8'))


print("The following members failed to match any IDs in the dumpservers.json.xz file. This means they were no longer part of the server when the server data was dumped.")
for name in failed_members:
    print(name)