aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/inputpanel/hwr_test_data/build_unipen_data.py
blob: eb4231c3d48f015712204e6a5842efda9cefe8fb (plain)
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
#!/usr/bin/env python

#############################################################################
##
## Copyright (C) 2017 The Qt Company Ltd.
## Contact: https://www.qt.io/licensing/
##
## This file is part of the Qt Virtual Keyboard module of the Qt Toolkit.
##
## $QT_BEGIN_LICENSE:GPL$
## Commercial License Usage
## Licensees holding valid commercial Qt licenses may use this file in
## accordance with the commercial license agreement provided with the
## Software or, alternatively, in accordance with the terms contained in
## a written agreement between you and The Qt Company. For licensing terms
## and conditions see https://www.qt.io/terms-conditions. For further
## information use the contact form at https://www.qt.io/contact-us.
##
## GNU General Public License Usage
## Alternatively, this file may be used under the terms of the GNU
## General Public License version 3 or (at your option) any later version
## approved by the KDE Free Qt Foundation. The licenses are as published by
## the Free Software Foundation and appearing in the file LICENSE.GPL3
## included in the packaging of this file. Please review the following
## information to ensure the GNU General Public License requirements will
## be met: https://www.gnu.org/licenses/gpl-3.0.html.
##
## $QT_END_LICENSE$
##
#############################################################################

import os
import sys
import json
import datetime
import getopt
import re

unipen_file_pattern = re.compile(r'(^[0-9]{2,9}).*\.txt')

def print_header():
    print """/****************************************************************************
**
** Copyright (C) %s The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Virtual Keyboard module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/""" % datetime.datetime.now().year

def scan_unipen_files(path):
    file_list = []
    for root, dirs, files in os.walk(path):
        for name in files:
            if unipen_file_pattern.match(name):
                file_list.append(os.path.join(root, name))
    return file_list

def help():
    sys.exit("""Command line tool for converting Unipen files to JavaScript.

Usage: %s [dir1] .. [dirX]

Scans given directories recursively for Unipen text files and converts
them into JavaScript format. The result is printed to the stdout.

The Unipen file name must start with the Unicode character encoded in decimal
format, and end with .txt extension.""" % sys.argv[0])

def main():
    if len(sys.argv) < 2:
        help()

    # Collect arguments
    opts, args = getopt.getopt(sys.argv[1:], '', [])

    # Build a list of Unipen files in directories
    file_list = []
    for arg in args:
        if not os.path.exists(arg):
            sys.exit("Error: no such directory " + arg)
        file_list.extend(scan_unipen_files(arg))

    # Scan the Unipen files and build a data structure
    unipen_map = {}
    for file_name in file_list:
        lines = tuple(open(file_name, 'r'))
        unipen_data = {}
        pen_data = []
        for line in lines:
            line = line.strip()
            if not line:
                continue
            if line.startswith('.'):
                parts = line.split(' ')
                if len(parts) > 2:
                    unipen_data[parts[0]] = parts[1:]
                elif len(parts) == 2:
                    unipen_data[parts[0]] = int(parts[1]) if parts[1].isdigit() else parts[1]
                elif len(parts) == 1:
                    if parts[0] == '.PEN_DOWN':
                        if not unipen_data.has_key('.PEN'):
                            unipen_data['.PEN'] = []
                        pen_data = []
                    elif parts[0] == '.PEN_UP':
                        unipen_data['.PEN'].append(pen_data)
            else:
                pen_data.append([int(coord) for coord in line.split(' ')])
        file_name_match = unipen_file_pattern.match(os.path.basename(file_name))
        unipen_map["0x%04x" % int(file_name_match.groups()[0])] = unipen_data

    # Convert to json format and print to stdout
    print_header()
    print
    print 'var unipenData = ' + json.dumps(unipen_map, indent=4, separators=(',', ': '), sort_keys=True)

if __name__ == '__main__':
    main()