2 # Copyright (C) 2013 Hat, Inc.
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the
16 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 # Boston, MA 02111-1307, USA.
22 from .utils import to_underscores
25 class SectionsFile(object):
26 def __init__(self, sections):
27 self.sections = sections
30 class Section(object):
38 class Subsection(object):
39 def __init__(self, name):
44 def parse_sections_file(lines):
46 current_section = None
47 current_subsection = None
52 if not line or line.isspace():
55 if line == "<SECTION>":
56 current_section = Section()
57 sections.append(current_section)
58 current_subsection = Subsection(None)
59 current_section.subsections.append(current_subsection)
62 if line == "</SECTION>":
63 current_section = None
66 match = re.match(r"<FILE>(?P<contents>.*)</FILE>", line)
68 current_section.file = match.groupdict['contents']
71 match = re.match(r"<TITLE>(?P<contents>.*)</TITLE>", line)
73 current_section.title = match.groupdict['contents']
76 match = re.match(r"<INCLUDE>(?P<contents>.*)</INCLUDE>", line)
78 current_section.includes = match.groupdict['contents']
81 match = re.match(r"<SUBSECTION(?: (?P<name>.*))?>", line)
83 current_subsection = Subsection(match.groupdict.get('name', None))
84 current_section.subsections.append(current_subsection)
87 if line.startswith("<") and line.endswith(">"):
88 # Other directive to gtk-doc, not a symbol.
91 current_subsection.symbols.append(line)
93 return SectionsFile(sections)
96 def write_sections_file(f, sections_file):
97 for section in sections_file.sections:
98 f.write("\n<SECTION>\n")
99 if section.file is not None:
100 f.write("<FILE>%s</FILE>\n" % (section.file, ))
101 if section.title is not None:
102 f.write("<TITLE>%s</TITLE>\n" % (section.title, ))
103 if section.includes is not None:
104 f.write("<INCLUDE>%s</INCLUDE>\n" % (section.includes, ))
106 is_first_subsection = True
107 for subsection in section.subsections:
108 if subsection.name is not None:
109 f.write("<SUBSECTION %s>\n" % (subsection.name, ))
110 elif not is_first_subsection:
111 f.write("\n<SUBSECTION>\n")
113 is_first_subsection = False
115 for symbol in subsection.symbols:
116 f.write(symbol + "\n")
119 def generate_sections_file(transformer):
120 ns = transformer.namespace
124 def new_section(file_, title):
127 section.title = title
128 section.subsections.append(Subsection(None))
129 sections.append(section)
132 def append_symbol(section, sym):
133 section.subsections[0].symbols.append(sym)
135 general_section = new_section("main", "Main")
137 for node in ns.itervalues():
138 if isinstance(node, ast.Function):
139 append_symbol(general_section, node.symbol)
140 elif isinstance(node, (ast.Class, ast.Interface)):
141 gtype_name = node.gtype_name
142 file_name = to_underscores(gtype_name).replace('_', '-').lower()
143 section = new_section(file_name, gtype_name)
144 append_symbol(section, gtype_name)
145 append_symbol(section, node.glib_type_struct.target_giname.replace('.', ''))
147 for meth in node.methods:
148 append_symbol(section, meth.symbol)
149 for meth in node.static_methods:
150 append_symbol(section, meth.symbol)
152 return SectionsFile(sections)