On 21 March 2012 14:36, David Edmundson <[email protected]> wrote:

> Nicely done. I'm up for going down this route.
>
> TODO List:
>
> As this is a bodge, we should probably talk to Tp upstream.
> Check it all loads in the log viewer (I'm thinking it might not (if
> your account names don't match))
> We need prevention for "double imports". (i.e running your script twice)
> We need a UI. I'm thinking maybe a first run wizard on the log viewer.
>


Hi David,

I added a sanitised version wrote by Luca (einar).

My test was simple... I run the script on the xml log file of kopete and
got a folder for the contact with whom I spoke...
copied it manually in the right account (so I did not have to worry about
different schemes for naming accounts)
started ktp view logger and the messages were there...

Personally I will like to see two more things added... I will have a look
with Luca tonight to them
1. being able to convert multiple files in one go... it is simple to do..
2. import group chats... I know how tp-logger stores them... I just need an
example from kopete...

Personally once these are done I will write a small how to and that is
all... Automatic imports, bullet proof, may take too much time and I do not
know if it is really worthy of all that effort.

for convenience we can ship it with one of our components... if upstream
does not want to take it.

Alin
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#  Copyright 2012 Luca Beltrame <[email protected]>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with . If not, see <http://www.gnu.org/licenses/>.

from __future__ import with_statement

import sys
import errno
import os
import itertools

from dateutil.parser import parse
from lxml import etree

def mkdir_p(path):
    try:
        os.makedirs(path)
    except OSError as exc: # Python >2.5
        if exc.errno == errno.EEXIST:
            pass
        else: raise

def get_day(element):

    date = element.get("time")
    day = date.split()[0]

    return day


def convert_log(tree):

    all_messages = tree.iterfind("msg")

    date = tree.find("head/date")
    month = date.get("month")
    year = date.get("year")
    base_name = year+month

    other = tree.xpath('//head/contact[not(@type = "myself")]')[0]
    other = other.get("contactId")

    mkdir_p(other)

    # Group by day
    # The key is the actual day

    grouper = itertools.groupby(all_messages,
                                key=get_day)

    for groupname, messages in grouper:

        new_log = etree.Element("log")

        for message in messages:

            day, time = message.get("time").split()

            # Get a Python representation of the time

            new_time = parse(" ".join((day, month, year, time)))
            new_time = new_time.isoformat()

            day = day.zfill(2)

            nickname = message.get("nick")
            from_ = message.get("from")
            inbound = message.get("in")
            message_text = message.text
            issuer = "True" if inbound == "0" else "False"

            new_message = etree.SubElement(new_log, "message")
            new_message.text = message_text

            attributes = new_message.attrib
            attributes["time"] = new_time
            attributes["id"] = from_
            attributes["name"] = nickname
            attributes["token"] = ""
            attributes["issuer"] = issuer
            attributes["type"] = "normal"
            attributes["message-token"] = ""

        new_log.addprevious(
            etree.PI('xml-stylesheet',
                     'type="text/xsl" href="log-store-xml.xsl"'))

        file_name = base_name + day + ".log"
        out_file = os.path.join(other, file_name)

        with open(out_file, "w") as handle:
            log_data = etree.ElementTree(new_log)
            log_data.write(handle, pretty_print=True,xml_declaration=True,
                           encoding='utf-8')

def main():

    input_file = sys.argv[1]
    document = etree.parse(input_file)
    root = document.getroot()

    convert_log(root)


if __name__ == "__main__":
    main()
_______________________________________________
KDE-Telepathy mailing list
[email protected]
https://mail.kde.org/mailman/listinfo/kde-telepathy

Reply via email to