Tweak the generator

master
LEdoian 3 years ago
parent c926f36c8b
commit 314170744e

@ -4,7 +4,7 @@ import ipaddress
import jinja2 import jinja2
import os import os
import sys import sys
import pathlib from pathlib import Path
class Bridge: class Bridge:
def __init__(self, name, num): def __init__(self, name, num):
@ -20,7 +20,7 @@ class Computer:
def add_to_bridge(self, bridge): def add_to_bridge(self, bridge):
self.bridges.append(bridge) self.bridges.append(bridge)
bridge.computers.append(computer) bridge.computers.append(self)
def gen_files(self): def gen_files(self):
files = [ files = [
@ -34,7 +34,8 @@ class Computer:
environment = jinja2.Environment(loader=loader, trim_blocks=True) environment = jinja2.Environment(loader=loader, trim_blocks=True)
for f in files: for f in files:
template = environment.get_template(f) template = environment.get_template(f)
template.stream(context).dump(self.name + '/' + f) (Path('output') / self.name).mkdir(parents=True,exist_ok=True)
template.stream(context).dump('output/' + self.name + '/' + f)
# Computers: # Computers:
comps = {} comps = {}
@ -44,12 +45,12 @@ for i, n in enumerate('ABCDEFGHIX'):
# Bridges # Bridges
bridges = {} bridges = {}
for i, n in [(x, 'net_'+str(x)) for x in range(1, 8)]: for i, n in [(x, 'net_'+str(x)) for x in range(1, 8)]:
bridges[n] = Bridge(n, i) bridges[i] = Bridge(n, i)
def dump_bridges(fn): def dump_bridges(fn):
with open(fn, 'w+') as f: with open(fn, 'w+') as f:
for br in bridges: for br in bridges:
f.write(br + '\n') f.write(bridges[br].name + '\n')
# Connections # Connections
# dict[ net_num -> [comp_names] # dict[ net_num -> [comp_names]
@ -63,8 +64,15 @@ conns = {
7: 'X', 7: 'X',
} }
print(conns)
print(comps)
print(bridges)
for net, cs in conns.items(): for net, cs in conns.items():
for c in cs: for c in cs:
comps[c].add_to_bridge(bridges[net]) comps[c].add_to_bridge(bridges[net])
dump_bridges('./bridges') dump_bridges('output/bridges')
for c in comps.values():
c.gen_files()

Loading…
Cancel
Save