1# Copyright Bruno da Silva de Oliveira 2003. Use, modification and
2# distribution is subject to the Boost Software License, Version 1.0.
3# (See accompanying file LICENSE_1_0.txt or copy at
4# http://www.boost.org/LICENSE_1_0.txt)
5
6from Exporter import Exporter
7from settings import *
8import utils
9
10#==============================================================================
11# EnumExporter
12#==============================================================================
13class EnumExporter(Exporter):
14    'Exports enumerators'
15
16    def __init__(self, info):
17        Exporter.__init__(self, info)
18
19
20    def SetDeclarations(self, declarations):
21        Exporter.SetDeclarations(self, declarations)
22        if self.declarations:
23            self.enum = self.GetDeclaration(self.info.name)
24        else:
25            self.enum = None
26
27    def Export(self, codeunit, exported_names):
28        if self.info.exclude:
29            return
30        indent = self.INDENT
31        in_indent = self.INDENT*2
32        rename = self.info.rename or self.enum.name
33        full_name = self.enum.FullName()
34        unnamed_enum = False
35        if rename.startswith('$_') or rename.startswith('._'):
36            unnamed_enum = True
37        code = ''
38        if not unnamed_enum:
39            code += indent + namespaces.python
40            code += 'enum_< %s >("%s")\n' % (full_name, rename)
41        for name in self.enum.values:
42            rename = self.info[name].rename or name
43            value_fullname = self.enum.ValueFullName(name)
44            if not unnamed_enum:
45                code += in_indent + '.value("%s", %s)\n' % (rename, value_fullname)
46            else:
47                code += indent + namespaces.python
48                code += 'scope().attr("%s") = (int)%s;\n' % (rename, value_fullname )
49        if self.info.export_values and not unnamed_enum:
50            code += in_indent + '.export_values()\n'
51        if not unnamed_enum:
52            code += indent + ';\n'
53        code += '\n'
54        codeunit.Write('module', code)
55        exported_names[self.enum.FullName()] = 1
56
57    def Name(self):
58        return self.info.name
59