File: pyqrc.py

package info (click to toggle)
klayout 0.28.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 279,296 kB
  • sloc: cpp: 2,009,432; ruby: 43,292; xml: 25,855; python: 11,686; sh: 1,611; tcl: 212; perl: 170; makefile: 112; ansic: 42
file content (154 lines) | stat: -rwxr-xr-x 4,002 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154

# A lean substitute for QRC which is employed in the non-Qt case

import xml.etree.ElementTree as et
import argparse
import zlib
import os


# A class providing the generator

class RCFile(object):

  def __init__(self, alias, path):
    self.path = path
    self.alias = alias

  def write(self, file, index):

    f = open(self.path, "rb")
    raw_data = f.read()
    f.close()

    data = zlib.compress(raw_data)
    compressed = "true"

    cb = "{"
    bc = "}"
    cls = "Resource" + str(index)
    file.write(f"\n// Resource file {self.path} as {self.alias}\n")
    file.write( "namespace {\n")
    file.write( "\n")
    file.write(f"  struct {cls}\n")
    file.write( "  {\n")
    file.write(f"    {cls}() {cb}\n")
    file.write(f"      static bool compressed = {compressed};\n")
    file.write(f"      static const char *name = \"{self.alias}\";\n")
    file.write( "      static const unsigned char data[] = {")
    n = 0
    for b in data:
      if n == 0:
        file.write("\n        ")
      file.write("0x%02x," % b)
      n += 1
      if n == 16:
        n = 0
    file.write( "\n")
    file.write( "      };\n")
    file.write( "      m_id = tl::register_resource(name, compressed, data, sizeof(data) / sizeof(data[0]));\n")
    file.write( "    }\n")
    file.write(f"    ~{cls}() {cb}\n")
    file.write( "      tl::unregister_resource(m_id);\n")
    file.write( "    }\n")
    file.write( "    tl::resource_id_type m_id;\n")
    file.write(f"  {bc} resource_instance{index};\n")
    file.write( "\n")
    file.write( "}\n")

    
class RCGenerator(object):

  def __init__(self):
    self.files = []

  def append(self, path, alias):
    self.files.append(RCFile(path, alias))

  def dump_files(self):
    for f in self.files:
      print(f.path)

  def write(self, file):
    file.write(f"""
/*

  KLayout Layout Viewer
  Copyright (C) 2006-2023 Matthias Koefferlein

  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, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

/**
*  DO NOT EDIT THIS FILE. 
*  This file has been created automatically
*/

#include "tlResources.h"
""")
    i = 1
    for f in self.files:
      f.write(file, i)
      i += 1
  

# The main code

generator = RCGenerator()

# argument parsing

parser = argparse.ArgumentParser(description='Lean QRC parser')
parser.add_argument('input', type=str, nargs='+',
                    help='The QRC input file')
parser.add_argument('--output', '-o', type=str, nargs='?',
                    help='The C++ output file')
parser.add_argument('--path', '-p', type=str, nargs='?',
                    help='Path to the input files (default is current directory)')

args = parser.parse_args()

# read the input file

for input in args.input:

  root_node = et.parse(input).getroot()

  for qresource in root_node.findall('qresource'):
    prefix = qresource.get('prefix')
    for file in qresource.findall('file'):
      alias = file.get('alias')
      path = file.text
      if alias is None:
        alias = path
      if prefix is not None:
        alias = prefix + "/" + alias
      if args.path is not None:
        path = os.path.join(args.path, path)
      else:
        path = os.path.join(os.path.dirname(input), path)
      generator.append(alias, path)

# produce the output file

if args.output is not None:
  f = open(args.output, "w")
  generator.write(f)
  f.close()
else:
  generator.dump_files()