def normalize_config(config): """Take a 'loose' config and return a new config that conforms to the DSConfig format. Current transforms: - server instances (e.g. 'TangoTest/1') are split into a server level and an instance level (e.g. 'TangoTest' -> '1'). This is to convert "v1" format files to the "v2" format. - "devices" toplevel; allows to change *existing* devices by just adding them directly to a "devices" key in the config, instead of having to list out the server, instance and class (since this information can be gotten from the DB.) """ old_config = expand_config(config) new_config = SetterDict() if "servers" in old_config: new_config.servers = old_config["servers"] if "classes" in old_config: new_config.classes = old_config["classes"] if "devices" in old_config: db = PyTango.Database() for device, props in old_config["devices"].items(): try: info = db.get_device_info(device) except PyTango.DevFailed as e: sys.exit("Can't reconfigure device %s: %s" % (device, str(e[0].desc))) srv, inst = info.ds_full_name.split("/") new_config.servers[srv][inst][info.class_name][device] = props return new_config.to_dict()
def get_db_data(db, patterns=None, class_properties=False, **options): # dump TANGO database into JSON. Optionally filter which things to include # (currently only "positive" filters are possible; you can say which # servers/classes/devices to include, but you can't exclude selectively) # By default, dserver devices aren't included! dbproxy = PyTango.DeviceProxy(db.dev_name()) data = SetterDict() if not patterns: # the user did not specify a pattern, so we will dump *everything* servers = get_servers_with_filters( dbproxy, **options) data.servers.update(servers) if class_properties: classes = get_classes_properties(dbproxy) data.classes.update(classes) else: # go through all patterns and fill in the data for pattern in patterns: prefix, pattern = pattern.split(":") kwargs = {prefix: pattern} kwargs.update(options) servers = get_servers_with_filters(dbproxy, **kwargs) data.servers.update(servers) if class_properties: classes = get_classes_properties(dbproxy, server=pattern) data.classes.update(classes) return data.to_dict()
def get_dict_from_db(db, data, narrow=False, skip_protected=True): """Takes a data dict, checks if any if the definitions are already in the DB and returns a dict describing them. By default it includes all devices for each server+class, use the 'narrow' flag to limit to the devices present in the input data. """ # This is where we'll collect all the relevant data dbdict = SetterDict() moved_devices = defaultdict(list) # Devices that are already defined somewhere else for server, inst, clss, device in get_devices_from_dict( data.get("servers", {})): try: devinfo = db.get_device_info(device) srvname = "%s/%s" % (server, inst) if devinfo.ds_full_name.lower() != srvname.lower(): moved_devices[devinfo.ds_full_name].append((clss, device)) except PyTango.DevFailed: pass # Servers for srvr, insts in data.get("servers", {}).items(): for inst, classes in insts.items(): for clss, devs in classes.items(): devs = CaselessDictionary(devs) if narrow: devices = devs.keys() else: srv_full_name = "%s/%s" % (srvr, inst) devices = db.get_device_name(srv_full_name, clss) for device in devices: new_props = devs.get(device, {}) db_props = get_device(db, device, new_props, skip_protected) dbdict.servers[srvr][inst][clss][device] = db_props # Classes for class_name, cls in data.get("classes", {}).items(): props = cls.get("properties", {}).keys() for prop, value in db.get_class_property(class_name, props).items(): if value: value = [str(v) for v in value] dbdict.classes[class_name].properties[prop] = value attr_props = cls.get("attribute_properties") if attr_props: dbprops = db.get_class_attribute_property(class_name, attr_props.keys()) for attr, props in dbprops.items(): props = dict((prop, [str(v) for v in values]) for prop, values in props.items()) dbdict.classes[class_name].attribute_properties[attr] = props return dbdict.to_dict(), moved_devices