def process_raw_config(config_string, config_type="raw"): """Parses raw config string into python objects""" if config_type == "raw": return config_string elif config_type == "json": config = parse_json_config(config_string) if not isinstance(config, (list, dict)): raise ValueError("Configuration must be a list or object.") return config elif config_type == "csv": f = StringIO(config_string) return [x for x in DictReader(f)] raise ValueError("Unsupported configuration type.")
def process_driver_config(config_path, csv_name_map, csv_contents): print "Processing config:", config_path with open(config_path) as f: device_config = parse_json_config(f.read()) registry_config_file_name = device_config["registry_config"] #Sort out name collisions and add to map if needed if registry_config_file_name not in csv_name_map: print "Processing CSV:", registry_config_file_name base_name = registry_config_file_name.split('/')[-1] base_name = "registry_configs/" + base_name if base_name in csv_contents: count = 0 new_csv_name = base_name + str(count) while(new_csv_name in csv_contents): count += 1 new_csv_name = base_name + str(count) base_name = new_csv_name with open(registry_config_file_name) as f: csv_contents[base_name] = f.read() csv_name_map[registry_config_file_name] = base_name #Overwrite file name with config store reference. device_config["registry_config"] = "config://" + csv_name_map[registry_config_file_name] new_config_name = "devices" for topic_bit in ("campus", "building", "unit", "path"): topic_bit = device_config.pop(topic_bit, '') if topic_bit: new_config_name += "/" + topic_bit return new_config_name, device_config
def process_driver_config(config_path, csv_name_map, csv_contents): print "Processing config:", config_path with open(config_path) as f: device_config = parse_json_config(f.read()) registry_config_file_name = device_config["registry_config"] #Sort out name collisions and add to map if needed if registry_config_file_name not in csv_name_map: print "Processing CSV:", registry_config_file_name base_name = registry_config_file_name.split('/')[-1] base_name = "registry_configs/" + base_name if base_name in csv_contents: count = 0 new_csv_name = base_name + str(count) while (new_csv_name in csv_contents): count += 1 new_csv_name = base_name + str(count) base_name = new_csv_name with open(registry_config_file_name) as f: csv_contents[base_name] = f.read() csv_name_map[registry_config_file_name] = base_name #Overwrite file name with config store reference. device_config["registry_config"] = "config://" + csv_name_map[ registry_config_file_name] new_config_name = "devices" for topic_bit in ("campus", "building", "unit", "path"): topic_bit = device_config.pop(topic_bit, '') if topic_bit: new_config_name += "/" + topic_bit return new_config_name, device_config
def process_main_config(main_file, output_directory, keep=False): main_config = parse_json_config(main_file.read()) driver_list = main_config.pop("driver_config_list") driver_count = len(driver_list) csv_name_map = {} csv_contents = {} driver_configs = {} for config_path in driver_list: new_config_name, device_config = process_driver_config( config_path, csv_name_map, csv_contents) if new_config_name in driver_configs: print "WARNING DUPLICATE DEVICES:", new_config_name, "FOUND IN", config_path driver_configs[new_config_name] = device_config staggered_start = main_config.pop('staggered_start', None) if staggered_start is not None: main_config["driver_scrape_interval"] = staggered_start / float( driver_count) print "New Main config:" pprint(main_config) print if not os.path.exists(output_directory): os.makedirs(output_directory) os.chdir(output_directory) devices_path = "devices" registries_path = "registry_configs" if not keep: if os.path.exists(devices_path): shutil.rmtree(devices_path, ignore_errors=True) if os.path.exists(registries_path): shutil.rmtree(registries_path, ignore_errors=True) if not os.path.exists(devices_path): os.makedirs(devices_path) if not os.path.exists(registries_path): os.makedirs(registries_path) print "Writing 'config'" with open("config", "w") as f: f.write(jsonapi.dumps(main_config, indent=2)) for name, contents in csv_contents.iteritems(): print "Writing", name with open(name, "w") as f: f.write(contents) unique_paths = set() for name, config in driver_configs.iteritems(): print "Writing", name dir_name = os.path.dirname(name) if dir_name not in unique_paths and not os.path.exists(dir_name): os.makedirs(dir_name) unique_paths.add(dir_name) with open(name, "w") as f: f.write(jsonapi.dumps(config, indent=2))
def process_main_config(main_file, output_directory, keep=False): main_config = parse_json_config(main_file.read()) driver_list = main_config.pop("driver_config_list") driver_count = len(driver_list) csv_name_map = {} csv_contents = {} driver_configs = {} for config_path in driver_list: new_config_name, device_config = process_driver_config(config_path, csv_name_map, csv_contents) if new_config_name in driver_configs: print "WARNING DUPLICATE DEVICES:", new_config_name, "FOUND IN", config_path driver_configs[new_config_name] = device_config staggered_start = main_config.pop('staggered_start', None) if staggered_start is not None: main_config["driver_scrape_interval"] = staggered_start / float(driver_count) print "New Main config:" pprint(main_config) print if not os.path.exists(output_directory): os.makedirs(output_directory) os.chdir(output_directory) devices_path = "devices" registries_path = "registry_configs" if not keep: if os.path.exists(devices_path): shutil.rmtree(devices_path, ignore_errors=True) if os.path.exists(registries_path): shutil.rmtree(registries_path, ignore_errors=True) if not os.path.exists(devices_path): os.makedirs(devices_path) if not os.path.exists(registries_path): os.makedirs(registries_path) print "Writing 'config'" with open("config", "w") as f: f.write(jsonapi.dumps(main_config, indent=2)) for name, contents in csv_contents.iteritems(): print "Writing", name with open(name, "w") as f: f.write(contents) unique_paths = set() for name, config in driver_configs.iteritems(): print "Writing", name dir_name = os.path.dirname(name) if dir_name not in unique_paths and not os.path.exists(dir_name): os.makedirs(dir_name) unique_paths.add(dir_name) with open(name, "w") as f: f.write(jsonapi.dumps(config, indent=2))