File: make_templates.py

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (42 lines) | stat: -rw-r--r-- 1,327 bytes parent folder | download | duplicates (2)
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
#! make_templates.py
"""Parse a C++ input and try to generate SWIG %template declarations so that this
    template <typename T1, typename T2>
    DoubleVect
    OnBitProjSimilarity(const T1& bv1,const T2& bv2);
becomes this
    %template(OnBitProjSimilarityEBV) OnBitProjSimilarity<ExplicitBitVect,ExplicitBitVect>;
"""

import re

template_match = re.compile(r"""template\s*\<(.+)\>\s*.*\s+(\w+)\s*\(.*""")


def make_entry(name, suffix, template_types):
  template_part = '<' + ','.join(template_types) + '>'
  return '%template(' + name + suffix + ') ' + name + template_part + ';'


def handle_template_line(template_line, suffix, template_type):
  # print template_line
  match = template_match.search(template_line)
  types = match.group(1)
  name = match.group(2)
  template_types = len(types.split(',')) * [template_type]
  print(make_entry(name, suffix, template_types))


def find_declarations(file_name, suffix, template_type):
  f = open(file_name)
  in_template = False
  template_line = ''
  for line in f:
    if in_template:
      template_line += ' ' + line.strip()
      if line.find(')') >= 0:
        handle_template_line(template_line, suffix, template_type)
        in_template = False
    elif line.strip().startswith('template'):
      in_template = True
      template_line = line.strip()
  f.close()