CLIENTS and Resources are saved and loaded
This commit is contained in:
parent
2e82c6036c
commit
8f4ec0bab7
1
config.yaml
Normal file
1
config.yaml
Normal file
@ -0,0 +1 @@
|
||||
clients-data-file: /vagrant/clients.json
|
@ -8,6 +8,8 @@ import actions
|
||||
import signals
|
||||
import db
|
||||
|
||||
from x import utils
|
||||
|
||||
|
||||
class Resource(object):
|
||||
def __init__(self, name, metadata, args, base_dir):
|
||||
@ -63,6 +65,7 @@ def create(name, base_path, dest_path, args, connections={}):
|
||||
meta['id'] = name
|
||||
meta['version'] = '1.0.0'
|
||||
meta['actions'] = {}
|
||||
meta['input'] = args
|
||||
|
||||
if os.path.exists(actions_path):
|
||||
for f in os.listdir(actions_path):
|
||||
@ -77,3 +80,13 @@ def create(name, base_path, dest_path, args, connections={}):
|
||||
f.write(yaml.dump(meta))
|
||||
db.resource_add(name, resource)
|
||||
return resource
|
||||
|
||||
|
||||
def load(dest_path):
|
||||
meta_file = os.path.join(dest_path, 'meta.yaml')
|
||||
meta = utils.load_file(meta_file)
|
||||
name = meta['id']
|
||||
args = meta['input']
|
||||
|
||||
return Resource(name, meta, args, dest_path)
|
||||
|
||||
|
17
x/signals.py
17
x/signals.py
@ -3,14 +3,21 @@ from collections import defaultdict
|
||||
|
||||
import db
|
||||
|
||||
from x import utils
|
||||
|
||||
CLIENTS = defaultdict(lambda: defaultdict(list))
|
||||
|
||||
CLIENTS_CONFIG_KEY = 'clients-data-file'
|
||||
CLIENTS = utils.read_config_file(CLIENTS_CONFIG_KEY)
|
||||
|
||||
|
||||
def connect(emitter, reciver, mappings):
|
||||
for src, dst in mappings:
|
||||
CLIENTS.setdefault(emitter.name, {})
|
||||
CLIENTS[emitter.name].setdefault(src, [])
|
||||
CLIENTS[emitter.name][src].append((reciver.name, dst))
|
||||
|
||||
utils.save_to_config_file(CLIENTS_CONFIG_KEY, CLIENTS)
|
||||
|
||||
|
||||
def notify(source, key, value):
|
||||
if key in CLIENTS[source.name]:
|
||||
@ -35,4 +42,12 @@ def assign_connections(reciver, connections):
|
||||
connect(resource, reciver, r_mappings)
|
||||
|
||||
|
||||
def connection_graph():
|
||||
resource_dependencies = {}
|
||||
|
||||
for source, destinations in CLIENTS.items():
|
||||
resource_dependencies[source] = [
|
||||
destination[0] for destination in destinations
|
||||
]
|
||||
|
||||
return resource_dependencies
|
||||
|
42
x/utils.py
Normal file
42
x/utils.py
Normal file
@ -0,0 +1,42 @@
|
||||
import json
|
||||
import os
|
||||
import yaml
|
||||
|
||||
|
||||
def ext_encoder(fpath):
|
||||
ext = os.path.splitext(os.path.basename(fpath))[1].strip('.')
|
||||
if ext in ['json']:
|
||||
return json
|
||||
elif ext in ['yaml', 'yml']:
|
||||
return yaml
|
||||
|
||||
raise Exception('Unknown extension {}'.format(ext))
|
||||
|
||||
|
||||
def load_file(fpath):
|
||||
encoder = ext_encoder(fpath)
|
||||
|
||||
try:
|
||||
with open(fpath) as f:
|
||||
return encoder.load(f)
|
||||
except IOError:
|
||||
return {}
|
||||
|
||||
|
||||
def read_config():
|
||||
return load_file('/vagrant/config.yaml')
|
||||
|
||||
|
||||
def read_config_file(key):
|
||||
fpath = read_config()[key]
|
||||
|
||||
return load_file(fpath)
|
||||
|
||||
|
||||
def save_to_config_file(key, data):
|
||||
fpath = read_config()[key]
|
||||
|
||||
with open(fpath, 'w') as f:
|
||||
encoder = ext_encoder(fpath)
|
||||
encoder.dump(data, f)
|
||||
|
Loading…
Reference in New Issue
Block a user