def create_config_json(): # read the default config.json from the simulator source directory default_config_path = os.path.join(backend.get_simulator_path(), 'bin/config.json') with open(default_config_path) as f: default_config = json.load(f) # check its settings check_config_json = os.path.join(backend.get_simulator_path(), 'bin/check_config_json.py') popen = subprocess.Popen( [sys.executable, check_config_json, '-s', '-c', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) _ = popen.communicate(json.dumps(default_config)) if popen.returncode != 0: raise ValueError('invalid default config.json') # create a new config.json config = dict(CONFIG_JSON_TEMPLATE) config['settings'] = default_config['settings'] config_dir = os.path.dirname(backend.SIM_CONFIG_PATH) if os.path.exists(config_dir) is True: _delete_config_json() else: os.mkdir(config_dir) with open(backend.SIM_CONFIG_PATH, 'w') as f: json.dump(config, f, indent=4)
def default_config(): # read the default 6tisch.json from the simulator source directory default_config_path = os.path.join(backend.get_simulator_path(), 'bin/6tisch.json') with open(default_config_path) as f: default_config = json.load(f) return default_config
def put_default_config(config_str): try: config = json.loads(config_str) except ValueError: return {'config': None, 'message': 'No JSON object could be decoded'} new_config = backend.utils.CONFIG_JSON_TEMPLATE.copy() if 'settings' not in config: return {'config': None, 'message': '"settings" is missing'} else: new_config['settings'] = config['settings'] check_config_json = os.path.join(backend.get_simulator_path(), 'bin/check_config_json.py') # check the given config popen = subprocess.Popen( [sys.executable, check_config_json, '-s', '-c', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8') _, stderrdata = popen.communicate(json.dumps(new_config)) if popen.returncode == 0: # if the check succeeds, update the default config.json new_config['settings'] = config['settings'] # write the new config to the default config.json file with open(backend.SIM_CONFIG_PATH, 'w') as f: json.dump(new_config, f) ret = {'config': get_default_config(), 'message': 'success'} else: # return error message ret = {'config': None, 'message': stderrdata} return ret
def get_default_config(): if os.path.exists(backend.SIM_CONFIG_PATH) is False: # someone has deleted our config.json file... recreate one backend.utils.create_config_json() with open(backend.SIM_CONFIG_PATH, 'r') as f: config = json.load(f) # convert a path of the trace file to the abosolute one original_config_path = os.path.join(backend.get_simulator_path(), 'bin') for settings_type in ['combination', 'regular']: if 'conn_trace' not in config['settings'][settings_type]: continue if isinstance(config['settings'][settings_type]['conn_trace'], list): config['settings'][settings_type]['conn_trace'] = list( map(lambda trace_file: os.path.abspath( os.path.join(original_config_path, trace_file)) if os.path.isabs(trace_file) is False else trace_file)) elif (config['settings'][settings_type]['conn_trace'] and (os.path.isabs( config['settings'][settings_type]['conn_trace']) is False)): config['settings'][settings_type]['conn_trace'] = os.path.abspath( os.path.join(original_config_path, config['settings'][settings_type]['conn_trace'])) else: # it's an absolute path or None; it doesn't need the coversion pass return config
def get_available_connectivities(): conn_py_path = os.path.join(backend.get_simulator_path(), 'SimEngine/Connectivity.py') ret_val = set() with open(conn_py_path, 'r') as f: ret_val = set( re.findall(r'ConnectivityMatrix\w+', f.read(), re.MULTILINE)) # remove "ConnectivityMatrixBase" that is the base (super) class # for concrete scheduling function implementations ret_val.remove('ConnectivityMatrixBase') # strip leading "Connectivity" and return return map(lambda elem: re.sub(r'ConnectivityMatrix(\w+)', r'\1', elem), ret_val)
def get_available_scheduling_functions(): sf_py_path = os.path.join(backend.get_simulator_path(), 'SimEngine/Mote/sf.py') ret_val = set() with open(sf_py_path, 'r') as f: ret_val = set( re.findall(r'SchedulingFunction\w+', f.read(), re.MULTILINE)) # remove "SchedulingFunctionBase" that is the base (super) class # for concrete scheduling function implementations ret_val.remove('SchedulingFunctionBase') # strip leading "SchedulingFunction" and return return map(lambda elem: re.sub(r'SchedulingFunction(\w+)', r'\1', elem), ret_val)