def parse_devices_file(devices_file): """ :param devices_file: -> Parse values of a device in the 'flash_modes' file given. -> Except an incorrect XML format and raise ERROR. """ devices = {} try: devices_tree = Et.parse(devices_file) devices_tags = devices_tree.findall(DEVICE_TAG_REF) for device_tag in devices_tags: device_name = device_tag.get(NAME_REF) variable_tag = device_tag.find(VARIABLE_REF) variable_name = variable_tag.get(NAME_REF) variable_value = variable_tag.get(VALUE_REF) device_object = db.Device(device_name, variable_name, variable_value) for board_tag in device_tag.find(BOARDS_REF): board_name = board_tag.get(NAME_REF) device_object.boards_regex.append(board_name) devices[device_name] = device_object except Et.ParseError as msg: LOGGER.error( "ERROR in syntax of XML file : '%s'. " "Original message : '%s'.", devices_file, msg) return devices
def load_devices(devices_file): """ :param devices_file: -> Parse the devices from the 'flash_modes.xml' files. -> Show the result of scan if DEBUG mode is on (main.py) """ devices = parse_devices_file(devices_file) default_object = db.Device("Default") devices[default_object.name] = default_object devices[DEFAULT_DEVICE.name] = DEFAULT_DEVICE result = "{} devices found." devices_nb = len(devices) info = result.format(devices_nb) if logging.DEBUG: devices_str = "Devices :\n" for device_object in devices.values(): devices_str += str(device_object) + "\n" LOGGER.debug(devices_str) return devices, info
DEVICE = "flash_modes" DEVICES_FILE = DEVICE + XML_EXT BOARD_REF = "board" BOARDS_REF = "boards" DEVICE_TAG_REF = MODE_REF DEVICE_STRUCTURE = [(DEVICE, [], [(MODE_REF, [NAME_REF], [ (VARIABLE_REF, [NAME_REF, VALUE_REF], [(BOARDS_REF, [], [(BOARD_REF, [NAME_REF], [])])]) ])])] # DEFAULT DEVICE : DEFAULT_DEVICE_NAME = " __Default__ " DEFAULT_DEVICE = db.Device(DEFAULT_DEVICE_NAME) # TOOLS TOOLS = "tools" # CONTROL_PANEL : CONTROL_PANEL = "control_panel" CONTROL_PANEL_FILE = CONTROL_PANEL + XML_EXT SECTION_REF = "section" PROGRAM_REF = "program" SESSION_REF = "session" COMMAND_REF = "command" ICON_REF = "icon" FLAG_REF = "flag" OPTION_REF = "arg"