File: theme.py

package info (click to toggle)
teg 0.11.2%2Bdebian-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,340 kB
  • sloc: ansic: 17,600; sh: 8,546; python: 1,442; xml: 1,310; makefile: 357
file content (167 lines) | stat: -rw-r--r-- 6,090 bytes parent folder | download | duplicates (4)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
## $Id: theme.py,v 1.4 2003/09/09 18:02:13 riq Exp $
##
## Tenes Empanadas Graciela
##
## Copyright (C) 2000 Ricardo Quesada
##
##  Author: Ricardo Calixto Quesada <riq@coresecurity.com>
##
## 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; only version 2 of the License
##
## 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.



import os
import xml.dom.minidom
import pygame

# TEG imports
import country

class Theme(object):

    themeName = 'm3'
    themePrefix = ''

    def __init__(self, themeName = 'm3'):

        Theme.Theme = themeName
#        self._themePrefix = os.path.join( os.environ['HOME'], '.teg', 'themes', Theme.themeName )
        Theme.themePrefix = os.path.join( '..','..','client','themes', Theme.themeName )
        file_name =  os.path.join( Theme.themePrefix, 'teg_theme.xml' )

        self._themeDom = xml.dom.minidom.parse( file_name )

        self.defaultDict()

    def defaultDict( self ):
        self.themeDict = {}
        self.themeDict['continents'] = {}


    def parseTheme(self):
        
        teg_theme = self._themeDom.getElementsByTagName('teg_theme')[0]
        self.processElements(teg_theme, self.themeDict)


    def processElements(self, element, dictionary, parent = None):

        for node in element.childNodes:
            if node.nodeType == node.ELEMENT_NODE:
                try:
                    method = getattr(self, '_parse_%s' % node.nodeName )
                    method( node, dictionary, parent )
                except AttributeError, e:
                    print 'Element not handled: %s' % node.nodeName
                    print str(e)

    def loadImage( name ):
        """ Load image and return image object"""
        fullname = os.path.join( Theme.themePrefix, name)
        try:
            image = pygame.image.load(fullname)
            if image.get_alpha is None:
                image = image.convert()
            else:
                image = image.convert_alpha()
        except pygame.error, message:
            print 'Cannot load image:', fullname
            raise SystemExit, message
        return image
    loadImage = staticmethod( loadImage )

    ###
    ### XML handlers
    ###
    def _parse_continent( self, element, dictionary, parent ):
        c = country.Continent()
        name =  element.attributes.getNamedItem('name').nodeValue
#        print 'Parsing: %s' % name
        c['name'] = name
        c['offset'] = ( int( element.attributes.getNamedItem('pos_x').nodeValue ),
                         int( element.attributes.getNamedItem('pos_y').nodeValue ) )
        c['countries'] = {}

        dictionary['continents'][name] = c
        self.processElements( element,  dictionary['continents'][name]['countries'], c )


    def _parse_country( self, element, dictionary, parent ):
        name =  element.attributes.getNamedItem('name').nodeValue

        c = country.Country()
        c['name'] = name
        c['file'] = element.attributes.getNamedItem('file').nodeValue
        c['image'] = Theme.loadImage( c['file'] )
        c['continent'] = parent['name']
        c['offset'] = ( int( element.attributes.getNamedItem('pos_x').nodeValue ),
                                    int( element.attributes.getNamedItem('pos_y').nodeValue ) )
        try:
            c['army_offset'] = ( int( element.attributes.getNamedItem('army_x').nodeValue ),
                                            int( element.attributes.getNamedItem('army_y').nodeValue ) )
        except AttributeError, e:
            c['army_offset'] = (0,0)

        dictionary[name] = c


    def _parse_dices_pos( self, element, dictionary, parent ):
        pass


    def _parse_dices_ext_pos( self, element, dictionary, parent ):
        pass

    def _parse_armies_pos( self, element, dictionary, parent ):
        pass

    def _parse_dices_text( self, element, dictionary, parent ):
        pass

    def _parse_author( self, element, dictionary, parent ):
        pass
        
    def _parse_email( self, element, dictionary, parent ):
        pass

    def _parse_version( self, element, dictionary, parent ):
        pass

    def _parse_screenshot( self, element, dictionary, parent ):
        pass

    def _parse_screen( self, element, dictionary, parent ):
        dictionary['screen'] = {}
        dictionary['screen']['size'] = ( int( element.attributes.getNamedItem('size_x').nodeValue ),
                                            int( element.attributes.getNamedItem('size_y').nodeValue ) )
        

    def _parse_toolbar( self, element, dictionary, parent ):
        dictionary['toolbar'] = {}
        dictionary['toolbar']['offset'] = ( int( element.attributes.getNamedItem('offset_x').nodeValue ),
                                            int( element.attributes.getNamedItem('offset_y').nodeValue ) )
        dictionary['toolbar']['image'] = Theme.loadImage( element.attributes.getNamedItem('name').nodeValue )
        dictionary['toolbar']['text_color'] = element.attributes.getNamedItem('text_color').nodeValue

    def _parse_map( self, element, dictionary, parent ):
        dictionary['map'] = {}
        dictionary['map']['offset'] = ( int( element.attributes.getNamedItem('offset_x').nodeValue ),
                                            int( element.attributes.getNamedItem('offset_y').nodeValue ) )
        dictionary['map']['image'] = Theme.loadImage( element.attributes.getNamedItem('file').nodeValue )

    def _parse_cards( self, element, dictionary, parent ):
        pass

    def __del__(self):
        self._themeDom.unlink()