~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to widelandslib/make_flow_diagram.py

  • Committer: kaputtnik
  • Date: 2019-06-14 18:40:56 UTC
  • mfrom: (532.1.31 widelands)
  • Revision ID: kaputtnik-20190614184056-l0ha8pm5zais9mxk
Adapted code for use with python 3.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
def add_building(g, b, limit_inputs=None, limit_outputs=None, limit_buildings=None, link_workers=True, limit_recruits=None):
69
69
    # Add the nice node
70
70
    workers = ''
71
 
    if isinstance(b, (ProductionSite,)):
 
71
    if isinstance(b, ProductionSite):
72
72
        workers = r"""<table border="0px" cellspacing="0">"""
73
73
        for worker in b.workers:
74
74
            wo = b.tribe.workers[worker]
86
86
                g.add_edge(Edge(b.name, wo.name, color='darkgreen'))
87
87
        workers += r"""</table>"""
88
88
 
89
 
    if isinstance(b, (MilitarySite,)):
 
89
    if isinstance(b, MilitarySite):
90
90
        workers = r"""<table border="0px" cellspacing="0">"""
91
91
        workers += (r"""<tr><td border="0px">Keeps %s Soldiers</td></tr>"""
92
92
                    r"""<tr><td border="0px">Conquers %s fields</td></tr>"""
94
94
        workers += r"""</table>"""
95
95
 
96
96
    costs = r"""<tr><td colspan="2"><table border="0px" cellspacing="0">"""
97
 
    for ware, count in b.buildcost.items():
 
97
    for ware, count in list(b.buildcost.items()):
98
98
        w = b.tribe.wares[ware]
99
99
        costs += ('<tr><td border="0px">%s x </td><td border="0px"><img src="%s"/></td><td border="0px">%s</td></tr>' %
100
100
                  (count, w.image, w.descname))
133
133
    else:
134
134
        g.add_node(n)
135
135
 
136
 
    if isinstance(b, (ProductionSite,)):
 
136
    if isinstance(b, ProductionSite):
137
137
        # for worker,c in b.workers:
138
138
        #     g.add_edge(Edge(worker, name, color="orange"))
139
139
 
181
181
def make_graph(tribe_name):
182
182
    global tdir
183
183
    tdir = mkdtemp(prefix='widelands-help')
184
 
 
185
184
    json_directory = path.normpath(settings.MEDIA_ROOT + '/map_object_info')
186
 
    tribeinfo_file = open(path.normpath(
187
 
        json_directory + '/tribe_' + tribe_name + '.json'), 'r')
188
 
    tribeinfo = json.load(tribeinfo_file)
 
185
    with open(path.normpath(
 
186
            json_directory + '/tribe_' + tribe_name + '.json'), 'r') as tribeinfo_file:
 
187
        tribeinfo = json.load(tribeinfo_file)
189
188
 
190
189
    t = Tribe(tribeinfo, json_directory)
191
190
 
192
191
    g = CleanedDot(concentrate='false', style='filled', bgcolor='white',
193
192
                   overlap='false', splines='true', rankdir='LR')
194
193
 
195
 
    for name, w in t.wares.items():
 
194
    for name, w in list(t.wares.items()):
196
195
        add_ware(g, w)
197
196
    #
198
197
    # for name,w in t.workers.items():
199
198
    #     add_worker(g, w)
200
199
 
201
 
    for name, b in t.buildings.items():
 
200
    for name, b in list(t.buildings.items()):
202
201
        add_building(g, b, link_workers=False)
203
202
 
204
203
    g.write_pdf(path.join(tdir, '%s.pdf' % tribe_name))
211
210
 
212
211
 
213
212
def make_building_graph(t, building_name):
214
 
    if isinstance(t, basestring):
 
213
    if isinstance(t, str):
215
214
        t = Tribe(t)
216
215
 
217
216
    b = t.buildings[building_name]
219
218
    g = CleanedDot(concentrate='false', bgcolor='transparent',
220
219
                   overlap='false', splines='true', rankdir='LR')
221
220
 
222
 
    if not isinstance(b, (ProductionSite,)):
 
221
    if not isinstance(b, ProductionSite):
223
222
        inputs, outputs = [], []
224
223
    else:
225
224
        # TODO: prepare for tribes having buildings with a ware as both input
254
253
 
255
254
 
256
255
def make_worker_graph(t, worker_name):
257
 
    if isinstance(t, basestring):
 
256
    if isinstance(t, str):
258
257
        t = Tribe(t)
259
258
 
260
259
    w = t.workers[worker_name]
262
261
    g = CleanedDot(concentrate='false', bgcolor='transparent',
263
262
                   overlap='false', splines='true', rankdir='LR')
264
263
 
265
 
    buildings = [bld for bld in t.buildings.values() if
 
264
    buildings = [bld for bld in list(t.buildings.values()) if
266
265
                 isinstance(bld, ProductionSite) and
267
266
                 (w.name in bld.workers or w.name in bld.recruits)]
268
267
 
275
274
    sg = Subgraph('%s_enhancements' % w.name,
276
275
                  ordering='out', rankdir='TB', rank='same')
277
276
    # find exactly one level of enhancement
278
 
    for other in t.workers.values():
 
277
    for other in list(t.workers.values()):
279
278
        if other.becomes == w.name:
280
279
            add_worker(sg, other)
281
280
            g.add_edge(Edge(other.name, w.name, color='blue'))
294
293
 
295
294
 
296
295
def make_ware_graph(t, ware_name):
297
 
    if isinstance(t, basestring):
 
296
    if isinstance(t, str):
298
297
        t = Tribe(t)
299
298
    w = t.wares[ware_name]
300
299
 
301
300
    g = CleanedDot(concentrate='false', bgcolor='transparent',
302
301
                   overlap='false', splines='true', rankdir='LR')
303
302
 
304
 
    buildings = [bld for bld in t.buildings.values() if isinstance(
305
 
        bld, (ProductionSite, )) and (w.name in bld.inputs or w.name in bld.outputs)]
 
303
    buildings = [bld for bld in list(t.buildings.values()) if isinstance(
 
304
        bld, ProductionSite) and (w.name in bld.inputs or w.name in bld.outputs)]
306
305
    [add_building(g, bld, limit_inputs=[w.name], limit_outputs=[w.name], limit_buildings=[
307
306
                  b.name for b in buildings], link_workers=False) for bld in buildings]
308
307
 
326
325
def make_all_subgraphs(t):
327
326
    global tdir
328
327
    tdir = mkdtemp(prefix='widelands-help')
329
 
    if isinstance(t, basestring):
 
328
    if isinstance(t, str):
330
329
        t = Tribe(t)
331
 
    print 'making all subgraphs for tribe', t.name, 'in', tdir
 
330
    print('making all subgraphs for tribe', t.name, 'in', tdir)
332
331
 
333
 
    print '  making wares'
 
332
    print('  making wares')
334
333
 
335
334
    for w in t.wares:
336
 
        print '    ' + w
 
335
        print('    ' + w)
337
336
        make_ware_graph(t, w)
338
337
        process_dotfile(path.join(tdir, 'help/%s/wares/%s/' % (t.name, w)))
339
338
 
340
 
    print '  making workers'
 
339
    print('  making workers')
341
340
 
342
341
    for w in t.workers:
343
 
        print '    ' + w
 
342
        print('    ' + w)
344
343
        make_worker_graph(t, w)
345
344
        process_dotfile(path.join(tdir, 'help/%s/workers/%s/' % (t.name, w)))
346
345
 
347
 
    print '  making buildings'
 
346
    print('  making buildings')
348
347
 
349
348
    for b in t.buildings:
350
 
        print '    ' + b
 
349
        print('    ' + b)
351
350
        make_building_graph(t, b)
352
351
        process_dotfile(path.join(tdir, 'help/%s/buildings/%s/' % (t.name, b)))
353
352
 
359
358
    if b.enhanced_building:
360
359
        add_building()
361
360
 
 
361
 
362
362
if __name__ == '__main__':
363
363
    make_all_subgraphs()