File: main.py

package info (click to toggle)
naev 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 386,084 kB
  • sloc: ansic: 93,149; xml: 87,292; python: 2,347; sh: 904; makefile: 654; lisp: 162; awk: 4
file content (265 lines) | stat: -rwxr-xr-x 10,134 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python3
# -*- encoding: utf8 -*-
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=80:
# License: X/MIT
# author: Ludovic Bellière AKA. xrogaan

from collections import defaultdict
import os
import glob
import shutil
from datetime import datetime
from optparse import OptionParser

from jinja2 import Environment, FileSystemLoader
from lxml import etree
import yaml

__version__ = "1.0"

class yamlLabelReader:
    def __init__(self, stream):
        self.ydata = yaml.safe_load(stream)
        keys = set(self.ydata['shipstats'])
        labeled = set(self.ydata['statslabel'])
        for label in keys - labeled:
            print("Warning: missing label for shipstats key:", label)
        for label in labeled - keys:
            print("Notice: statslabel is orphan", label)

    def getShipStatsLabels(self, label):
        """
        Custom filter for the template enigne. Translate the xml values in
        pretty form.
        usage: {{ ship.stats|getStatsLabel }}
        """
        labels = self.ydata['shipstats']
        return labels.get(label, label+"(NOTFOUND)")

    def getStatsLabelsLabel(self, label):
        """
        Custom filter for the template engine. Explain the components.
        usage: {{ stat|getStatsLabelsLabel }}
        """
        labelsLabel = self.ydata['statslabel']
        return labelsLabel.get(label, label+"(NOTFOUND)")

class harvester:
    __xmlData=None
    __tagsBlacklist = ['sound', 'GUI']
    __tagsSortBase = ['base_type', 'class', 'price']
    __tagsSlots = ['utility', 'structure', 'weapon']

    # For updates, see ship.c:820
    __classGroup = {
        'heavy': ['carrier', 'cruiser', 'mothership'],
        'medium': ['cruise ship', 'freighter', 'destroyer', 'corvette',
                        'heavy drone', 'armoured transport']
        }

    def __init__(self, xmlPath):
        if self.__xmlData is None:
            self.__xmlData = glob.glob(os.path.join(xmlPath, "ships/*.xml"))

        self.ships = defaultdict(dict)
        self.shipSortBy = defaultdict(dict)
        for ship in self.__xmlData:
            ship = etree.parse(ship).getroot()
            shipName = ship.get('name')
            shipClass = ship.find('class').text

            # We only want to list player-available ships.
            if ship.find('mission') is not None:
                continue

            for details in ship.iterchildren():
                if details.tag in self.__tagsBlacklist:
                    continue

                if details.tag in self.__tagsSortBase:
                    if details.text not in self.shipSortBy[details.tag]:
                        self.shipSortBy[details.tag][details.text] = []

                # seems empty, ignoring. <- perhapse not a good idea
                if not details.text:
                    continue

                # my, my ... You're quite empty. Let's go for the children.
                if '\n  ' in details.text:
                    compiled = defaultdict(list)
                    for subDetails in details.iterchildren():
                        # we're talking about slots
                        if  details.tag == "slots":
                            if subDetails.text:
                                size=subDetails.text
                            elif shipClass.lower() in self.__classGroup['heavy']:
                                size='Heavy'
                            elif shipClass.lower() in self.__classGroup['medium']:
                                size='Medium'
                            else:
                                size='Light'

                            compiled[subDetails.tag].append(size)
                        else:
                            compiled[subDetails.tag] = subDetails.text
                    self.ships[shipName][details.tag] = compiled
                    del(compiled)
                else:
                    self.ships[shipName][details.tag] = details.text

            self.ships[shipName]['name'] = shipName

            if 'movement' not in self.ships[shipName]:
                self.ships[shipName]['movement'] = {'speed': 0, 'thrust': 0, 'turn': 0}

            for i in self.__tagsSortBase:
                self.store_by(i, self.ships[shipName][i], self.ships[shipName])

    def store_by(self, item, shipDetails, shipData):
        self.shipSortBy[item][shipDetails].append(shipData)

    def get_by(self, item, name=None):
        if item == 'name':
            return self.ships[name]
        return self.shipSortBy[item]

    def __iter__(self):
        return iter(self.ships.items())

class fashion:
    __xmlData = None
    __tagsBlackList = ['gfx_end','gfx','sound']
    __typeBlackList = ['map', 'gui', 'license']

    # TODO: ammo type have specific arguments (missiles have duration)
    # TODO: retrofit this code, we needs several ways to retrieve data and they
    # are all different. So, I'll discard the initial parsing and do a on
    # request walk only. groupBySlot will list only elements who's got a slot
    # subelement. getByType(name) will return a list of the specific type. Each
    # of them are unique. Here is a list: map, gui, license, ammo, modification.
    def __init__(self, xmlPath):
        if self.__xmlData is None:
            self.__xmlData = {}
            __xmlData = glob.glob(os.path.join(xmlPath, 'outfits/*/*.xml'))
            for item in __xmlData:
                basename = os.path.basename(item)
                self.__xmlData[basename] = etree.parse(item)

        self.slots = defaultdict(list)

    def _parseOutfit(self, outfitObj):
        outfitName = outfitObj.get('name')
        outfitGeneral, outfitSpecific = {}, {}

        for general in outfitObj.find('general').iterchildren():
            outfitGeneral[general.tag] = general.text

        for specific in outfitObj.find('specific').iterchildren():
            if specific.tag in self.__tagsBlackList:
                continue
            if len(specific.attrib) < 1:
                outfitSpecific[specific.tag] =  specific.text
            else:
                outfitSpecific[specific.tag] =  {
                            'attribs': specific.attrib,
                            'text': specific.text
                            }

        return {
                'name': outfitName,
                'general': outfitGeneral,
                'specific': outfitSpecific
                }

    def groupBySlots(self):
        for item in self.__xmlData.values():
            mySlot = item.findtext('general/slot')
            if mySlot is None:
                mySlot = "NA"
            self.slots[mySlot].append(self._parseOutfit(item.getroot()))

        return self.slots

    def iterSlot(self, mySlotName):
        for slotName, outfit in self.slots:
            if slotName == mySlotName:
                yield outfit['name']



if __name__ == "__main__":


    usage="Usage: %prog OUTPUTPATH"
    parser = OptionParser(usage=usage, version="%prog "+__version__,
                          description="Nice looking generator for naev ships")
#    parser.add_option("-o", "--output-path", dest="output", metavar="PATH",
#                      help="Path in whitch files goes.")
    parser.add_option("-t", "--template-path", dest="templates",
                      default='./templates', metavar="PATH",
                      help="""Uses template in that PATH
                              instead of the default one""")

    (cfg, arguments) = parser.parse_args()

    if len(arguments) != 1:
        parser.error("A wise man would know where to store the generated files.")
    currentPath = os.path.abspath(os.path.curdir)
    storagePath = os.path.abspath(os.path.normpath(arguments[0]))
    tplPath = os.path.abspath(os.path.normpath(cfg.templates))
    naevPath = os.path.abspath(os.path.normpath("../../dat/"))

    date = str( datetime.utcnow().strftime("%c UTC") )

    myLoader = FileSystemLoader(cfg.templates if cfg.templates else tplPath)
    labels = yamlLabelReader(open('labels.yml', 'r'))
    env = Environment(loader=myLoader)
    env.filters['getStatsLabel'] = labels.getShipStatsLabels
    env.filters['getStatsLabelsLabel'] = labels.getStatsLabelsLabel

    # creating ships html
    myTpl = env.get_template('ships_index.html')
    yaarh = harvester(naevPath)
    shipIStore = os.path.normpath(storagePath + '/ships/')
    if not os.path.exists(storagePath):
        os.mkdir(storagePath, 0o755)
    if not os.path.exists(shipIStore):
        os.mkdir(shipIStore, 0o755)
    myTpl.stream(shipList=yaarh.get_by('class'), date=date).dump(shipIStore+'/index.html')
    del(myTpl)

    for (shipName, shipData) in yaarh:
        myTpl = env.get_template('ship.html')
        myPath = os.path.abspath(os.path.normpath("%s/ships/%s.html" % (storagePath,shipName)))
        myTpl.stream(shipName=shipName, shipData=shipData, date=date).dump(myPath)

    # fancy outfits
    myTpl = env.get_template('outfits_index.html')
    panty = fashion(naevPath)
    outfitsStore = os.path.normpath(storagePath + '/outfits/')
    if not os.path.exists(outfitsStore):
        os.mkdir(outfitsStore, 0o755)
    myTpl.stream(outfits=panty.groupBySlots(), date=date).dump(outfitsStore+'/index.html')

    for (slotName, outfitsList) in panty.slots.items():
        for outfitDetails in outfitsList:
            myTpl = env.get_template('outfit.html')
            myStorage = os.path.normpath("%s/outfits/%s.html") % (
                    storagePath,
                    outfitDetails['name']
                )
            myTpl.stream(slotName=slotName, outfitData=outfitDetails,
                    date=date).dump(myStorage)

    for f in glob.glob(currentPath+'/*.css'):
        bname = os.path.basename(f)
        print('Copying css file:', bname, 'in', storagePath)
        shutil.copy(f, storagePath)

    mediapath = storagePath + '/ships/media'
    if not os.path.exists(mediapath):
        os.mkdir(mediapath)
    print('Copying image files...')
    for f in glob.glob(currentPath+'/../../dat/gfx/ship/*/*_comm.png'):
        bname = os.path.basename(f)
        shutil.copy(f, mediapath)