68 lines
1.4 KiB
Python
68 lines
1.4 KiB
Python
# standard imports
|
|
import os
|
|
import json
|
|
|
|
# local imports
|
|
from .base import (
|
|
Data,
|
|
data_dir,
|
|
)
|
|
|
|
|
|
class Network(Data):
|
|
|
|
def __init__(self, path='.', targets=[]):
|
|
super(Network, self).__init__()
|
|
self.references = None
|
|
self.path = path
|
|
self.targets = targets
|
|
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()
|
|
|
|
self.references = o['references']
|
|
|
|
self.inited = True
|
|
|
|
|
|
def start(self):
|
|
super(Network, self).load()
|
|
|
|
network_template_file_path = os.path.join(data_dir, 'network_template_v{}.json'.format(self.version()))
|
|
|
|
f = open(network_template_file_path)
|
|
o_part = json.load(f)
|
|
f.close()
|
|
|
|
f = open(self.network_path, 'w')
|
|
o = {'resources': {}}
|
|
|
|
for v in self.targets:
|
|
o['resources'][v] = o_part
|
|
json.dump(o, f)
|
|
f.close()
|
|
|
|
|
|
def reference(self, k):
|
|
v = self.references.get(k)
|
|
if v == None:
|
|
raise AttributeError('no defined reference for {}'.format(k))
|
|
return v
|
|
|
|
|
|
def __str__(self):
|
|
s = ''
|
|
for k in self.references.keys():
|
|
v = self.references[k]
|
|
if v == None:
|
|
v = ''
|
|
s += '{}: {}\n'.format(k, v)
|
|
|
|
return s
|