File: convert-course.py

package info (click to toggle)
ktouch 4:18.04.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,920 kB
  • sloc: xml: 41,792; cpp: 11,475; python: 403; sh: 4; makefile: 3
file content (140 lines) | stat: -rw-r--r-- 4,848 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#
#  Copyright 2012  Sebastian Gottfried <sebastiangottfried@web.de>
#
#  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 this program.  If not, see <http://www.gnu.org/licenses/>.
#

import argparse
import os
import uuid
from lxml import etree

class Course(object):
    def __init__(self, id, title, description, keyboard_layout_name, lessons=None):
        self.id = id
        self.title = title
        self.description = description
        self.keyboard_layout_name = keyboard_layout_name
        self.lessons = lessons or []
    def xml_tree(self):
        root = etree.Element('course')
        id = etree.SubElement(root, "id")
        id.text = self.id
        title = etree.SubElement(root, "title")
        title.text = self.title
        description = etree.SubElement(root, "description")
        description.text = self.description
        keyboard_layout_name = etree.SubElement(root, "keyboardLayout")
        keyboard_layout_name.text = self.keyboard_layout_name
        lessons = etree.SubElement(root, "lessons")
        for lesson in self.lessons:
            lesson.build_xml(lessons)
        return root
    def __repr__(self):
        return "<Course {!r}, {!r}, {!r}, {!r}>".format(
            self.title,
            self.description,
            self.keyboard_layout_name,
            self.lessons
        )

class Lesson(object):
    def __init__(self, id, title, new_characters, text):
        self.id = id
        self.title = title
        self.new_characters = new_characters
        self.text = text
    def build_xml(self, parent_node):
        node = etree.SubElement(parent_node, "lesson")
        id = etree.SubElement(node, "id")
        id.text = self.id
        title = etree.SubElement(node, "title")
        title.text = self.title
        new_characters = etree.SubElement(node, "newCharacters")
        new_characters.text = self.new_characters
        text = etree.SubElement(node, "text")
        text.text = self.text
    def __repr__(self):
        return "<Lesson {!r}, {!r}, {!r}>".format(
            self.title,
            self.new_characters,
            self.text
        )

def read(f, layout):
    tree = etree.parse(f)
    root = tree.getroot()
    id = make_id()
    title, = root.xpath("//KTouchLecture/Title[1]/text()")
    description, = root.xpath("//KTouchLecture/Comment[1]/text()")
    lesson_nodes = root.xpath("//KTouchLecture/Levels/Level")
    lessons = [parse_lesson(node) for node in lesson_nodes]
    return Course(id, title, description, layout, lessons)

def parse_lesson(lesson_node):
    id = make_id()
    title, = lesson_node.xpath("./NewCharsLabel[1]/text()") or ('',)
    if title == '':
        index = lesson_node.getparent().index(lesson_node)
        title = u"Lesson {}".format(index + 1)
    new_characters, = lesson_node.xpath("./NewCharacters[1]/text()") or ('',)
    input_lines = lesson_node.xpath("./Line/text()")
    lines = []
    for line in input_lines:
        line = unicode(line)
        while len(line) > 59:
            try:
                split_pos = line.rindex(" ", 0, 60)
            except ValueError:
                break
            lines.append(line[:split_pos])
            line = line[split_pos + 1:]
        lines.append(line)
    text = u"\n".join(lines)
    return Lesson(id, title, new_characters, text)

def parse_chars(char_nodes):
    chars = []
    for char_node in char_nodes:
        value = char_node.text
        position = POSITION_MAP[char_node.attrib['Position']]
        modifier = MODIFIER_MAP[position]
        char = Char(value, position, modifier)
        chars.append(char)
        if value.lower() != value:
            hiddenChar = Char(value.lower(), 'hidden')
            char.modifier = 'shift'
            chars.append(hiddenChar)
    return chars


def make_id():
    return "{{{}}}".format(uuid.uuid4())

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='convert ktouch course files'
    )
    parser.add_argument('-l', '--layout',
        type=str,
        metavar="LAYOUT",
        default=""
    )
    parser.add_argument('input_file',
        type=argparse.FileType('r'),
        metavar="INPUTFILE",
    )
    args = parser.parse_args()
    course = read(args.input_file, args.layout)
    print etree.tostring(course.xml_tree())