Imported Upstream version 1.37.1
[platform/upstream/gobject-introspection.git] / giscanner / sectionparser.py
1 # -*- Mode: Python -*-
2 # Copyright (C) 2013 Hat, Inc.
3 #
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.
8 #
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.
13 #
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.
18 #
19
20 import re
21
22
23 class SectionsFile(object):
24     def __init__(self, sections):
25         self.sections = sections
26
27
28 class Section(object):
29     def __init__(self):
30         self.file = None
31         self.title = None
32         self.includes = None
33         self.subsections = []
34
35
36 class Subsection(object):
37     def __init__(self, name):
38         self.name = name
39         self.symbols = []
40
41
42 def parse_sections_file(lines):
43     sections = []
44     current_section = None
45     current_subsection = None
46
47     for line in lines:
48         line = line.rstrip()
49
50         if not line or line.isspace():
51             continue
52
53         if line == "<SECTION>":
54             current_section = Section()
55             sections.append(current_section)
56             current_subsection = Subsection(None)
57             current_section.subsections.append(current_subsection)
58             continue
59
60         if line == "</SECTION>":
61             current_section = None
62             continue
63
64         match = re.match(r"<FILE>(?P<contents>.*)</FILE>", line)
65         if match:
66             current_section.file = match.groupdict['contents']
67             continue
68
69         match = re.match(r"<TITLE>(?P<contents>.*)</TITLE>", line)
70         if match:
71             current_section.title = match.groupdict['contents']
72             continue
73
74         match = re.match(r"<INCLUDE>(?P<contents>.*)</INCLUDE>", line)
75         if match:
76             current_section.includes = match.groupdict['contents']
77             continue
78
79         match = re.match(r"<SUBSECTION(?: (?P<name>.*))?>", line)
80         if match:
81             current_subsection = Subsection(match.groupdict.get('name', None))
82             current_section.subsections.append(current_subsection)
83             continue
84
85         if line.startswith("<") and line.endswith(">"):
86             # Other directive to gtk-doc, not a symbol.
87             continue
88
89         current_subsection.symbols.append(line)
90
91     return SectionsFile(sections)