2021-10-09 20:37:54 +02:00
|
|
|
# standard imports
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
|
|
|
# local imports
|
2021-10-10 11:33:30 +02:00
|
|
|
from .base import (
|
|
|
|
Data,
|
|
|
|
data_dir,
|
|
|
|
)
|
2021-10-09 20:37:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Network(Data):
|
|
|
|
|
2021-10-09 21:45:07 +02:00
|
|
|
def __init__(self, path='.', targets=[]):
|
2021-10-09 20:37:54 +02:00
|
|
|
super(Network, self).__init__()
|
|
|
|
self.references = None
|
|
|
|
self.path = path
|
2021-10-09 21:45:07 +02:00
|
|
|
self.targets = targets
|
2021-10-09 20:37:54 +02:00
|
|
|
self.network_path = os.path.join(self.path, 'network.json')
|
|
|
|
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
super(Network, self).load()
|
|
|
|
|
|
|
|
f = open(self.network_path, 'r')
|
|
|
|
o = json.load(f)
|
|
|
|
f.close()
|
|
|
|
|
2021-10-10 14:49:22 +02:00
|
|
|
self.resources = o['resources']
|
2021-10-09 20:37:54 +02:00
|
|
|
|
|
|
|
self.inited = True
|
|
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
super(Network, self).load()
|
|
|
|
|
2021-10-10 11:33:30 +02:00
|
|
|
network_template_file_path = os.path.join(data_dir, 'network_template_v{}.json'.format(self.version()))
|
2021-10-09 21:45:07 +02:00
|
|
|
|
2021-10-10 11:33:30 +02:00
|
|
|
f = open(network_template_file_path)
|
|
|
|
o_part = json.load(f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
f = open(self.network_path, 'w')
|
|
|
|
o = {'resources': {}}
|
|
|
|
|
2021-10-09 21:45:07 +02:00
|
|
|
for v in self.targets:
|
2021-10-10 11:33:30 +02:00
|
|
|
o['resources'][v] = o_part
|
|
|
|
json.dump(o, f)
|
2021-10-09 20:37:54 +02:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
2021-10-09 21:04:11 +02:00
|
|
|
def reference(self, k):
|
|
|
|
v = self.references.get(k)
|
|
|
|
if v == None:
|
|
|
|
raise AttributeError('no defined reference for {}'.format(k))
|
|
|
|
return v
|
|
|
|
|
|
|
|
|
2021-10-09 20:37:54 +02:00
|
|
|
def __str__(self):
|
|
|
|
s = ''
|
2021-10-10 14:49:22 +02:00
|
|
|
for k in self.resources.keys():
|
|
|
|
for kk in self.resources[k].keys():
|
|
|
|
v = self.resources[k][kk]
|
|
|
|
if v == None:
|
|
|
|
v = ''
|
|
|
|
s += '{}.{} = {}\n'.format(k, kk, v)
|
2021-10-09 20:37:54 +02:00
|
|
|
|
|
|
|
return s
|